Hi Good morning, How do I avoid multiple fast clic...
# compose
a
Hi Good morning, How do I avoid multiple fast clicks on a button without creating a custom button to handle the delay? What is the best solution?
t
I think you can use
debounce
or
sample
in Flow for grouping events, i wish it is helpful for your problem.
❤️ 2
s
If it's about navigation use what MR3Y suggests ^^ If not you will most likely need to do this sort of logic in your ViewModel or whatever you use.
☝️ 1
❤️ 1
1
a
But why do we need to use
dropUnlessResumed
only with navigation? What about situations where I need to click to send an event, and I need to handle that from the UI or display an element? @MR3Y @Stylianos Gakis
m
I think it is totally fine (as long as it is something UI related). Navigation just happens to be the perfect & common use case for this API. Also: you don't have to tag/mention anyone who is already part of this thread. he will be notified of your message anyway.
1
👍 1
Oh, wait. I think I'm wrong here. If your UI remains in resumed state, then you'll end up sending these duplicate events which you want to avoid
❤️ 1
s
For navigation in particular it's just a perfect fit because as soon as you start a navigation animation, the library ensures that your current destination is not in the "Resumed" state anymore. So it's simply a very convenient API to use when you want to guard against duplicate navigations. There's nothing else special about it.
☝️ 2
❤️ 1
For any other case, it really depends on what your requirements are tbh, there's no clear cut answer. But again, you probably want to do that sort of logic in your VM if it's about business logic. If it's showing/hiding UI and so on, then perhaps it fits doing it there too, but again, I'd say it depends. If you have a very concrete description of what you want to do we may be able to help more.
a
In this case, how can I ensure that clicking a button multiple times doesn't cause the browser to open multiple instances in a stack? I already have the following function:
fun openUrl(context: Context, url: String) {}
and just use this in Button
Copy code
onPrivacyPolicyClicked = {
  openUrl(context, PRIVACY_POLICY_URL)
}
s
Wrap that lambda with a dropUnlessResumed, does it do what you want it to?
🙌 1
a
I'm asking because I saw
dropUnlessResumed
used with navigation in an example.
Is it fine to use it in this case? I'm asking because there might be another solution that better fits this situation.
s
It is an example, but it does what it says, it drops the event unless the lifecycle is Resumed. If that is something which fits other use cases, I'd say go for it 😊
1
❤️ 1