https://kotlinlang.org logo
#koin-dev
Title
# koin-dev
a

atlantis210

03/18/2021, 10:12 AM
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

arnaud.giuliani

03/23/2021, 9:11 AM
Hi @atlantis210 any problem using
KoinComponent
? is there any missing API ?
a

atlantis210

03/23/2021, 9:20 AM
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

arnaud.giuliani

03/23/2021, 10:17 AM
could you set it up in the Koin start sequence, like
androidContext()
DSL keyword
a

atlantis210

03/23/2021, 10:29 AM
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

arnaud.giuliani

03/23/2021, 10:45 AM
does this car context survive along your application?
a

atlantis210

03/23/2021, 10:50 AM
It seems it has the same lifecycle as the Android Auto application. The documentation is not clear about it
What do you think ?
a

arnaud.giuliani

03/23/2021, 3:42 PM
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

atlantis210

03/23/2021, 3:51 PM
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

arnaud.giuliani

03/23/2021, 4:14 PM
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

atlantis210

03/23/2021, 4:16 PM
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

arnaud.giuliani

03/23/2021, 4:22 PM
apart if you need Android Scope API, then KoinComponent can suits you for now
a

atlantis210

03/23/2021, 4:24 PM
"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

arnaud.giuliani

03/23/2021, 4:27 PM
cool 👍
a

atlantis210

03/23/2021, 4:27 PM
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

arnaud.giuliani

03/23/2021, 5:58 PM
if there is a need, it will be more a koin-android-auto for isolating those API 🙂
a

atlantis210

03/23/2021, 5:58 PM
Yep I agree
4 Views