Search code examples
iosswiftparsingvoting

Youtube style voting poll. what is the logic?


Hi I'm trying to make my voting poll similar to Youtube. Right now, when the user clicks on the 'like' button, it will go up and if the same user clicks on it, it will go down. my question is what is the best approach in making the dislike button so that "when the user likes the button and then clicks on the dislike button, the counters get calculated correctly and vice versa?" Thanks!

**Additional info: I have a dislike button and a dislikelabel as well.

cell!.shouldEnableLikeButton(false)

let liked: Bool = cell!.likeButton.selected
cell?.setLikeStatus(liked)

let originalButtonTitle = cell?.likeLabel!.text

var likeCount: Int = originalButtonTitle!.toInt()! 

if liked {
    likeCount += 1
} else {
    likeCount -= 1
}

cell!.likeLabel.text = "\(likeCount)"

Solution

  • i am assuming you have a backend that accepts a number for modifying the count.

    Store the likeCount for the "poll" locally on the phone. Keep each poll as a unique id and keep a map in the phone db of what the user already voted for that poll. It can have three states:

    1. Yes
    2. No
    3. NotYetVoted

    Now, if the status locally changes from Yes to No. app reports a -2 to the backend. If the status changes No to Yes, app reports a +2. If the status changes from NotYetVoted to Yes, app reports a +1, NotYetVoted to No, app reports a -1. In all other cases, app does not report anything to the backend.

    I hope this helps.