Hello, I created <an issue> to add inject for Scre...
# koin-contributors
a
Hello, I created an issue to add inject for Screen in order to work with Android Auto without doing workarounds. What do you think ?
I'd like also to add a carContext() as done for androidContext()
@arnaud.giuliani ? I'd like to not embark my workarounds in production if possible 😞 I'd do the PR
a
Hi @atlantis210 any problem using
KoinComponent
? is there any missing API ?
a
Hi, the thing is I had to override screens :
Copy code
abstract class MyScreen(carContext: CarContext): Screen(carContext), KoinComponent
whereas Screen has lifecycle and add a function for settings the carContext as you did for the Android Context
Copy code
fun KoinApplication.carContext(carContext: CarContext): KoinApplication {
    if (KoinApplication.logger.isAt(<http://Level.INFO|Level.INFO>)) {
        <http://KoinApplication.logger.info|KoinApplication.logger.info>("[init] declare CarContext")
    }

    koin.rootScope.beanRegistry.findDefinition(clazz = CarContext::class) ?:
        koin.rootScope.beanRegistry.saveDefinition(DefinitionFactory.createSingle { carContext })

    return this
}
and also
Copy code
fun Scope.carContext(): CarContext = try {
    get()
} catch (e: Exception) {
    throw MissingAndroidContextException("$e")
}
So if this could be inside a library koin-android-auto or something like that, I think it would be easier for other people to continue using koin when on Android Auto
Do you find this relevant or not ?
a
could you set it up in the Koin start sequence, like
androidContext()
DSL keyword
a
Unfortunately no. The thing is, the
CarContext
has to be set only when connected to Android Auto. And when unplugged the
Context
has to be back to the standard Android Context.
a
does this car context survive along your application?
a
It seems it has the same lifecycle as the Android Auto application. The documentation is not clear about it
What do you think ?
a
perhaps this is something you can declare dynamically with
koin.declare()
API
it will add the instance to your Koin instance
the thing to know is: can you consider it as a singleton or not
?
a
I'm testing it but I'll still have to override the
Screen
component to implements KoinComponent with it whereas it has a lifecycle
I'll still have to do
koin.rootScope.beanRegistry.findDefinition(clazz = CarContext::class) ?: koin.declare(carContext)
Otherwise there'll be a
DefinitionOverrideException
So I agree, I can remove
Scope.carContext()
but I like to use it the same way you did for
androidContext()
. But I'll still have to use`koin.declare()` or
saveDefinition()
and override the
Screen
component
a
I’ll still have to do 
koin.rootScope.beanRegistry.findDefinition(clazz = CarContext::class) ?: koin.declare(carContext)
check the
declare()
function from Koin. You can declare things on the fly
and specify
override=true
For the KoinComponent side, if you don”t need ViewModel it’s still Ok. But let see what dependencies we would require to bring it as “official”
else you ’ll have to do your own extensions
a
You'll have to add this
Copy code
implementation '<http://androidx.car.app:app:1.0.0-beta01|androidx.car.app:app:1.0.0-beta01>'
And yes, there is no ViewModel in Android Auto since there is no layouts nor views
a
apart if you need Android Scope API, then KoinComponent can suits you for now
a
"check the 
declare()
  function from Koin. You can declare things on the fly and specify `override=true`". I could do that but that's not ideal
But I understand your point of view 🙂
It'll do with my workarounds 😉
a
cool 👍
a
anyway, that's a great library you did
👍 1
And I'll close my PR issue
Tell me if you'll think of implementing this for Android Auto 😄 I'll be happy to do it 😉
a
if there is a need, it will be more a koin-android-auto for isolating those API 🙂
a
Yep I agree