Hello I'm looking for some guidance while migratin...
# koin
k
Hello I'm looking for some guidance while migrating koin v2 to v3. Also moving to kotlin 1.8.10 and want to integrate new features with compose
Copy code
"org.koin:koin-core:2.0.1"
"org.koin:koin-android:2.0.1"
"org.koin:koin-test:2.0.1"
"org.koin:koin-androidx-viewmodel:2.0.1"
"org.koin:koin-androidx-ext:2.0.1"
Will be replaced by
Copy code
val koinVersion = "3.0.1"
"io.insert-koin:koin-core-jvm:$koinVersion"
"io.insert-koin:koin-test:$koinVersion"
"io.insert-koin:koin-android:$koinVersion"
And following cases to rework Properties -
properties(map<String, String>)
This has changed from accepting
Map<String, Any>
to
Map<String, String>
but saving property still takes
Any
and there is
getProperty<T>(): T
. So is this just leftovers API's and it's discouraged to save other types than
String
or there this method has been renamed and I can't find it? I can solve it with extension function but I want to know if there is something existing.
Copy code
@OptIn(KoinInternalApi::class)
fun KoinApplication.anyProperties(values: Map<String, Any>): KoinApplication {
    koin.propertyRegistry.saveProperties(values)
    return this
}
SharedViewModels How do I get sharedVm from Activity now? Used to be
val viewModel by lazy { getSharedViewModel<AnViewModel>(from = { requireActivity() }) }
Should it be now
private val viewModel by activityViewModels<AnViewModel>()
? I saw issue https://github.com/InsertKoinIO/koin/issues/785 that
from
is renamed to
owner
but this is not available for fragment. BeenRegistry -
getDefinition()
Got a singleton class in which I insatiate a
viewModel
Copy code
val koinApplication = GlobalContext.get()
anViewModel = koinApplication.koin.getByClassName(AnManagerViewModel::class)

private inline fun <reified T> Koin.getByClassName(className : KClass<*>) : T? {
        val definition = this.rootScope.beanRegistry.getDefinition(className)
        if(definition != null){
            return this.get<T>(definition.qualifier)
        }
        return null
    }
How can I achieve it in koin v3? Can I get the definition by
getScope(ScopeDefinition.ROOT_SCOPE_ID)._scopeDefinition.definitions.find { it.qualifier?.value == className.qualifiedName }
? Thanks for help!
a
Hey will help you about that 👍 let me get some time to check all your topics
k
thanks!
a
I would even encourage to go with latest 3.3.2 or 3.4.3 ... as you will have latest fixes & API updates. There were some API renamign to be conform to core Android API
this will solve consistency in properties API
`sharedViewModel`has been named to
activityViewModel
To retrieve an instance with a given KClass, use:
Copy code
fun <T> get(
        clazz: KClass<*>,
        qualifier: Qualifier? = null,
        parameters: ParametersDefinition? = null
    )
koin.get(yourClass)
getScope(ScopeDefinition.ROOT_SCOPE_ID)._scopeDefinition.definitions.find { it.qualifier?.value == className.qualifiedName }
seems that you try to resolve a definition, with qualifier used in className
all from root
Really hard use of internals here 😕