Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do you keep the button state after termination? #38

Open
MaeseppTarvo opened this issue Oct 9, 2016 · 0 comments
Open

How do you keep the button state after termination? #38

MaeseppTarvo opened this issue Oct 9, 2016 · 0 comments

Comments

@MaeseppTarvo
Copy link

MaeseppTarvo commented Oct 9, 2016

Hey. I do not seem to find a way to keep the button state if user has changed view or terminated the app? I think it is even harder if buttons are in tableview? For example if you use it with Firebase with this code:

func likeSystem(sender: DOFavoriteButton, cellForRowAt indexPath: IndexPath){
        if sender.isSelected {
        let cell = tableView.dequeueReusableCell(withIdentifier: "snusProductsCell", for: indexPath) as! SnusProductTableViewCell
        self.databaseRef.child("Snuses").child(self.products[indexPath.row].snusProductTitle).runTransactionBlock({
            (currentData:FIRMutableData!) -> FIRTransactionResult in
            if var post = currentData.value as? [String : AnyObject], let uid = FIRAuth.auth()?.currentUser?.uid {
                var stars : Dictionary<String, Bool>
                stars = post["likes"] as? [String : Bool] ?? [:]
                var starCount = post["hasLiked"] as? Int ?? 0
                if let _ = stars[uid] {
                    // Unstar the post and remove self from stars
                    starCount -= 1
                    stars.removeValue(forKey: uid)

                } else {
                    // Star the post and add self to stars
                    starCount += 1

                    stars[uid] = true
                    sender.deselect()
                }
                post["raters"] = starCount as AnyObject?
                post["hasLiked"] = stars as AnyObject?

                // Set value and report transaction success
                currentData.value = post

                return FIRTransactionResult.success(withValue: currentData)
            }
            return FIRTransactionResult.success(withValue: currentData)
        }) { (error, committed, snapshot) in
            if let error = error {
                print(error.localizedDescription)
            }
        }
        }else{
            sender.select()
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant