swiftxcodeswift5gesturekoloda

How to hit Like and dislike API on left and right drag of cards using KolodaView Swift


I am using kolodaview for card swift like view. Everything is working fine but i stuck at one place. When user slide card left or right, API hiting for like and dislike. For this left and right koloda given delegate method:

 func koloda(_ koloda: KolodaView, draggedCardWithPercentage finishPercentage: CGFloat, in direction: SwipeResultDirection){
 if direction == .left {

 if finishPercentage == 100.0 {
    apifordislike()
  }
 } else if direction == .right{
    apiforlike()
 }
}

Its working but issue is that, while swapping finishPercentage == 100.0 and hitting API, but user slowly slide the card API hitting more than one, 100.0 is repeated. I want only one hit.


Solution

  • Why not use

    func koloda(_ koloda: KolodaView, didSwipeCardAt index: Int, in direction: SwipeResultDirection) {
    

    instead?

    Otherwise, keep info about status - e.g.

    ...

    var alreadyLiked: Bool = false
    var alreadyDisliked: Bool = false
    

    ...

    func koloda(_ koloda: KolodaView, draggedCardWithPercentage finishPercentage: CGFloat, in direction: SwipeResultDirection){
     if direction == .left {
    
     if finishPercentage == 100.0 {
        if !alreadyDisliked {
            alreadyDisliked = true
            apifordislike()
         }
      }
     } else if direction == .right{
        if !alreadyLiked {
            alreadyLiked = true
            apiforlike()
        }
     }
    }
    

    in your case, you might keep two variables in case someone changes it's mind to change to dislike. Also, you might want to restart flags when action is changed from liked to disliked and vice versa.