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

henrikhorbovyi

06/08/2020, 2:52 PM
Koin + Compose What do you think about this approach?
Copy code
Injectable {
    val myDependency: MyDependecy by inject() 
    HomeScreen(myDependency)     
}
z

Zach Klippenstein (he/him) [MOD]

06/08/2020, 4:10 PM
What does
Injectable
do?
h

henrikhorbovyi

06/08/2020, 4:11 PM
Just work around to allow the use o
KoinComponent.inject()
Because I can't use
inject()
inside a function, so
Injectable
gives me an
InjectableComponent
in its scope which is a KoinComponent
p

pavi2410

06/08/2020, 4:35 PM
You can use
get()
inside a function
h

henrikhorbovyi

06/08/2020, 4:44 PM
But
get
just return a KoinApplication
Actually get has no <T>
But with the
Injectable
composable it works, cause I can call the extension
fun <T> KoinComponent.get()
h

henrikhorbovyi

06/08/2020, 4:54 PM
exactly what I'm using, but you can't call
KoinComponent.get
from inside a scope which is not a KoinComponent
Try it: 1 - Create a file 2 - Create the following composable
Copy code
@Composable
fun MyScreen() {
   // call your KoinComponent.get() here
}
And event if I could I would use
inject
extension which is lazy
p

pavi2410

06/08/2020, 5:00 PM
I am not sure why you can't call
get()
when you can acesss
inject()
as both are extension functions of
KoinComponent
.
And, if a lazy property is called immediately, how is it different than calling
get()
directly?
h

henrikhorbovyi

06/08/2020, 5:09 PM
Oh! I got your point. You are just asking me to do
Copy code
Injectable {
    val repository = get<MyRepository>()
    HomeScreen(repository)
}
instead of:
Copy code
Injectable {
    val repository by inject<MyRepository>()
    HomeScreen(repository)
}
is it?
p

pavi2410

06/08/2020, 5:24 PM
Yeah
I also got your point 😁
h

henrikhorbovyi

06/08/2020, 5:26 PM
😄