https://kotlinlang.org logo
#compose
Title
# compose
p

pawegio

01/22/2021, 8:49 AM
I’m used to extracting navigation logic from android layer (hiding the impl details behind the interface), but I decided to give compose navigation a try. That involves tight coupling of navigation with the ui, triggering navigation from callbacks inside composable functions. I’m wondering if effects such as
onDispose()
are safe to perform such action or it’s rather considered as an anti-pattern/might lead to unexpected state (is there a risk of recomposition causing not intended navigation?). See example code below (where
isInProgress
is a part of collected state
false
by default):
Copy code
if (isInProgress) {
    CircularProgressIndicator()
    onDispose {
        navigateToDone()
    }
}
j

jim

01/22/2021, 10:40 AM
😬
Technically safe? eeh, probably. But the data flow of that code just really doesn't sit well with me. It feels too much like a combination of not-single-source-of-truth and children-with-side-effects.
I think I would consider moving the navigateToDone() into whatever is setting
isInProgress
, although it's a little hard to say for sure, depending on how tightly that code is coupled with the UI vs. general data layer. I get what you're trying to do there though, presumably you're trying to have a button or whatever that goes into some sort of spinning state and only subsequently pushes a new route into navigation whereby back button works. Not sure what the best way to model this would be. cc @Adam Powell for ideas.
1
👍 1
p

pawegio

01/22/2021, 11:47 AM
Thanks for feedback. Indeed, data flow is what worries me with this approach. Apart from navigation commands data goes through SharedFlow up and StateFlows down. However, all the routing is defined in top-level composables. I’m afraid of loosing a sync between ui and logic.
Thus, probably the best way to handle it is to inject navigator that holds current NavController to the logic.
a

Adam Powell

01/22/2021, 3:53 PM
+1 to what Jim said: trace back to the code that sets
isInProgress
from
true
to
false
when the async operation is done and you've found the spot to dispatch your navigation request.
👌 1
2 Views