Open Nek-12 opened 6 months ago
it would be perfect, sometimes I don't want to provide anything explicitly, but still use it, for example, in the plugin with dependency that provided somewhere at the top.
In brief, my case is the following:
I want to handle authorization exception and navigate users to login page, when UnauthorizedException
is happened. I use Koin for the DI and it would perfectly match my need. I can handle exceptions within plugin system and another integration with Koin will help a lot.
Thanks for the feedback, unfortunately koin did not make it to 3.0, but yeah, I'm thinking of exactly the same use-case. It's going to be worked on next after I add some more docs.
But your use-case is already possible with existing tools.
You could grab the setup from docs or the sample app and create a plugin:
fun <S: MVIState, I: MVIIntent, A: MVIAction> authOnErrorPlugin(
onSignInNeeded: suspend () -> Unit
) = plugin<S, I, A> {
onException { e ->
if (e !is UnauthorizedException) return@onException e
onSignInNeeded()
null
}
}
In onSignInNeeded
, you can send an event to a container that will handle navigation events, something like AppEventContainer
, call your navigator callback, or just request another user (it depends on your stack). I personally like the approach with a separate container for events like this.
An even better approach would be to observe the current user object in whileSubscribed
and redirect if the user is null
for example.
Your way seems reasonable, but I just don't like singletons (and at the same time, I don't want to provide to every screen component this handler). For now I think about some way around Decompose to provide such handler from the top (but a bit busy with university at the moment).
Description (required)