adrielcafe / voyager

🛸 A pragmatic navigation library for Jetpack Compose
https://voyager.adriel.cafe
MIT License
2.27k stars 109 forks source link

Koin-Integration: getting scoped ScreenModel #406

Open andretietz opened 2 weeks ago

andretietz commented 2 weeks ago

I am struggeling with following problem:

Having a KMP/compose multiplatform Project (mainly Android&Desktop) and need to scope(koin) my ScreenModel for Android-Lifecycle Reasons (in particular I am injecting a class that requires an activity to call and Android-API). In order to inject the screenmodel now, I have 2 different implementations of the screen, for Android & Desktop (no need for the scope on desktop).

Implementation on desktop:

class SignInScreen : Screen {
    @Composable
    override fun Content() {
        val viewmodel = getScreenModel<SignInViewModel>()
        AppTheme {
            Surface {
                ShowSignInScreen(
                    viewmodel = viewmodel
                )
            }
        }
    }
}

Implementation for Android:

class AndroidSignInScreen(private val scope: Scope) : Screen {
    @Composable
    override fun Content() {
        val viewmodel by scope.inject<SignInViewModel>()
        AppTheme {
            Surface {
                ShowSignInScreen(
                    viewmodel = viewmodel
                )
            }
        }
    }
}

My questions:

  1. Is there a way I can avoid the Android specific implementation? (prefered)
  2. Is there a way I can use getScreenModel on a scope? I read about navigation scoped screenmodels can this help somehow?

In ideal case the only difference between the platform implementations is the DI setup (sadly, I don't get rid of the scope on android => ideas are welcome, even though they don't belong here)