evgenyneu / Cosmos

A star rating control for iOS/tvOS written in Swift
MIT License
2.18k stars 366 forks source link

How to prevent didTouchCosmos from issuing a rating #182

Closed lsamaria closed 3 years ago

lsamaria commented 3 years ago

Please consider submitting the following information (if relevant):

I have a food app where users can post food. I want to prevent them from rating their own posts. The thing is I do need the .didTouchCosmos delegate method to get called so that I can show them an alert "you can't rate your own post". If I don't initialize the delegate method then when they touch it nothing will happen which isn't good ux. I need it touchable, I just need the rating to be ignored.

I tried this but the stars still show the rating once the user touches .didTouchCosmos. However I set .didFinishTouchingCosmos to nil and that never gets called because I don't need it.

cosmosView.didTouchCosmos = { [weak self](rating) in

    if let postUID = self?.post.uid, let currentUID = Auth.auth().currentUser?.uid {

        if postUID == currentUID {

            // 1. show alert "you can't rate your own post"

            // 2. set didFinishTouchingCosmos to nil
            self?.cosmosView.didFinishTouchingCosmos = nil
            return
        }

        // ...
    }
}

// this code never runs because I set this to nil above
cosmosView.didFinishTouchingCosmos = { [weak self](rating) in

    if let postUID = self?.post.uid, let currentUID = Auth.auth().currentUser?.uid {

        if postUID == currentUID {

            return
        }
    }
}

Just to be clear. If it's the same user who posted the food, the only thing that should happen is an alert will appear every time they touch it, but whatever star/rating that they selected will be ignored.