Home > database >  Like and Dislike button functionality
Like and Dislike button functionality

Time:08-10

I'm trying to implement the logic of the Like and Dislike button. I have a property named like with the type of Integer from the server. The like value could be either 1, 0 or -1.

1 = like

0 = nothing

-1 = dislike

The default value is 0, so when you click Like button once, the like value = 1, you click it the second time, the like value = 0.

It is similar to dislike, when Dislike button is clicked, the like value = -1, you click it the second time, the like value = 0.

Currently, I have this function for updating the value

The Boolean like and dislike properties are for The thumbs icon (they can't be true at the same time), the talkLikeValue is the value I want to send back to the server.

var talkLikeValue: Int? = 0
var like: Bool = false
var dislike: Bool = false

func updateLikeAndDislike() {
    if like == true {
        talkLikeValue = 1
        dislike = false
    } else if dislike == true {
        talkLikeValue = -1
        like = false
    }
}

and my button.

//// Dislike button
Button {
        Task {
            updateLikeAndDislike()
            dislike.toggle()
            await homeVM.likeTalks(like: homeVM.talkLikeValue)
         }
       } label: {
          SystemImageView(
            name: talk.like == -1 ? "hand.thumbsdown.fill" : "hand.thumbsdown",
            width: 32,
            height: 32,
            color: .blue)
       }
//// like button
Button {
        Task {
            updateLikeAndDislike()
            like.toggle()
            await homeVM.likeTalks(like: homeVM.talkLikeValue)
         }
       } label: {
          SystemImageView(
            name: talk.like == 1 ? "hand.thumbsup.fill" : "hand.thumbsup",
            width: 32,
            height: 32,
            color: .blue)
       }

My code is not working, it sends the correct value to the server 70% of the time and the animation is completely not working. Any thought?

CodePudding user response:

Seems kind of overly complicated for what should be a simple toggle function. Maybe try something like this:

var talkLikeValue = 0

func toggleLikeValue(tappedValue: Int) {
  // Check if value already has the value for the tapped button
  if talkLikeValue == tappedValue {
    // If it is already tapped, then the button is already tapped
    talkLikeValue = 0
  } else {
    // Else just set the value to the tappedValue
    talkLikeValue = tappedValue
  }
}

And update your button

Button {
        Task {
            toggleLikeValue(-1)
            await homeVM.likeTalks(like: homeVM.talkLikeValue)
         }
       } label: {
          SystemImageView(
            name: talk.talkLikeValue == -1 ? "hand.thumbsdown.fill" : "hand.thumbsdown",
            width: 32,
            height: 32,
            color: .blue)
       }
//// like button
Button {
        Task {
            toggleLikeValue(1)
            await homeVM.likeTalks(like: homeVM.talkLikeValue)
         }
       } label: {
          SystemImageView(
            name: talk.talkLikeValue == 1 ? "hand.thumbsup.fill" : "hand.thumbsup",
            width: 32,
            height: 32,
            color: .blue)
       }
  • Related