Modo docs Help

Modo and Dependency Injection (DI)

You can safely inject Screen into your DI container, but it must be removed from DI as soon as the Screen is removed from the hierarchy. Built-in screen effects can be used for this purpose.

Your DI scope can be identified by screenKey. Here is a sample of how you can use it with Toothpick:

@Composable internal fun Screen.rememberScreenScope( parentScope: Scope = LocalToothpickScope.current, moduleProvider: () -> Module = { module { } }, ): Scope { val scope = remember { getOrInitScope(screenKey, parentScope, moduleProvider) } OnScreenRemoved("scope") { Toothpick.closeScope(scope.name) } return scope } private fun getOrInitScope( screenKey: ScreenKey, parentScope: Scope, moduleProvider: () -> Module = { module { } }, ): Scope { val scopeName = screenKey.scopeName() return if (Toothpick.isScopeOpen(scopeName)) { Toothpick.openScope(scopeName) } else { val scope = Toothpick.openScopes(parentScope.name, scopeName) scope.installModules(moduleProvider.invoke()) } }
Last modified: 12 June 2024