-
Notifications
You must be signed in to change notification settings - Fork 0
koin: Variable injection in compose
Devrath edited this page Mar 7, 2024
·
1 revision
@Composable
fun VariableInjectionComposable(navController: NavHostController) {
val myService = koinInject<HelloService>()
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
val cxt = LocalContext.current
AppButton(text = "Variable injection Demo", onClick = {
Toast.makeText(cxt,myService.doSomething(),Toast.LENGTH_LONG).show()
})
}
}
interface HelloService {
fun doSomething(): String
}
class HelloServiceImpl : HelloService {
override fun doSomething(): String {
return "Hello Service, Koin!"
}
}
val singletonService = module {
singleOf<HelloService>(::HelloServiceImpl)
}
class KotlinApplication : Application() {
override fun onCreate() {
super.onCreate()
startKoin {
androidContext(this@KotlinApplication)
modules(singletonService)
}
}
}