I have an animation that occurs after a swipe on an item of an ngFor list of custom components. Each item starts with a state ‘novote’. After being swiped on, it should then be in a state either ‘upvote’ or ‘downvote’, depending on the direction swiped. The problem is that when you swipe on an object, the state changes and the animation occurs, but if you swipe on it for a second time, the state is again ‘novote,’ so the animation occurs again. If you swipe on it a third time, it is the correct state, so the animation does not occur.
My list html:
<ion-list *ngFor="let song of songList | async" [class.selected]="song">
<song-item (swipeleft)="vote(song, true)" (swiperight)="vote(song, false)" [song]="song" class="bottom-border"></song-item>
</ion-list>
song-item.html:
<ion-item (swipeleft)="toggleVoteAnim(-1)" (swiperight)="toggleVoteAnim(1)" [@myvote]="voteState">
<div> {{song.title}}<br/>
- <small>{{song.artist}}</small></div>
<div item-right>{{song.upVotes}}</div>
<div item-right>UP</div>
<div item-right>|</div>
<div item-right>DOWN</div>
<div item-right>{{song.downVotes}}</div>
</ion-item>
song-item.ts:
import { Component, Input, OnInit } from '@angular/core';
import { trigger, state, style, transition, animate, keyframes } from '@angular/animations';
import { Song } from "../../interfaces/song";
/**
* Generated class for the SongItemComponent component.
*
* See https://angular.io/api/core/Component for more info on Angular
* Components.
*/
@Component({
selector: 'song-item',
templateUrl: 'song-item.html',
animations: [
trigger('myvote', [
state('novote', style({
backgroundColor: '#191414'
})),
state('upvote', style({
backgroundColor: '#191414'
})),
state('downvote', style({
backgroundColor: '#191414'
})),
transition('* => upvote',
animate('.25s', keyframes([
style({backgroundColor: '#191414', offset: 0}),
style({backgroundColor: '#1db954', offset: 0.25}),
style({backgroundColor: '#191414', offset: 1})
]))
),
transition('* => downvote',
animate('.25s', keyframes([
style({backgroundColor: '#191414', offset: 0}),
style({backgroundColor: '#f53d3d', offset: 0.25}),
style({backgroundColor: '#191414', offset: 1})
]))
)
])
]
})
export class SongItemComponent implements OnInit {
@Input() song: Song;
voteState = 'novote';
constructor() {}
ngOnInit() {}
/**
* Toggles the vote animation
* @param dir - direction of the swipe. -1 is left (upvote), 1 is right (downvote)
*/
toggleVoteAnim(dir: number) {
console.log('Current vote state: ' + this.voteState);
if (dir === -1) {
console.log('direction is: ' + dir);
if (this.voteState === 'novote') {
this.voteState = (this.voteState === 'novote') ? 'upvote' : 'novote';
console.log(this.song.title + " was novote, now it is: " + this.voteState);
}
if (this.voteState === 'downvote') {
this.voteState = (this.voteState === 'downvote') ? 'upvote' : 'downvote';
console.log(this.song.title + " was downvote, now it is: " + this.voteState);
}
} else if (dir === 1) {
console.log('direction is: ' + dir);
if (this.voteState === 'novote') {
this.voteState = (this.voteState === 'novote') ? 'downvote' : 'novote';
console.log(this.song.title + " was novote, now it is: " + this.voteState);
}
if (this.voteState === 'upvote') {
this.voteState = (this.voteState === 'upvote') ? 'downvote' : 'upvote';
console.log(this.song.title + " was upvote, now it is: " + this.voteState);
}
}
console.log(this.song.title + " final votestate: " + this.voteState);
}
}
On the first left swipe, it prints:
Current vote state: novote
direction is: -1
song was novote, now it is: upvote
song final votestate: upvote
So far so good.
On the second left swipe:
Current vote state: novote
direction is: -1
song was novote, now it is: upvote
song final votestate: upvote
Not good. The voteState at the start should have been ‘upvote’ not ‘novote’, as per the final votestate from the previous swipe.
On the third left swipe:
Current vote state: upvote
direction is: -1
song final votestate: upvote
This is what should have happened with the second swipe. This is awfully confusing as it was working perfectly just a couple days ago, and I don’t think any changes were made to anything involved here…