okmr-d / DOFavoriteButton

Cute Animated Button written in Swift.
MIT License
3.61k stars 297 forks source link

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

Open MaeseppTarvo opened 8 years ago

MaeseppTarvo commented 8 years ago

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()
        }
    }