Android Kaki

Build beautiful, usable products using required Components for Android.

Obtain platform-specific implementations with Koin in KMM | by Mirzamehdi Karimov | Might 2023


Mirzamehdi Karimov

ProAndroidDev

Throughout the migration of Quotes challenge to KMM (Kotlin Cellular Multiplatform) I’ve to implement totally different courses for every platform and I’m utilizing Koin for dependency injection.

Certainly one of such courses is Community handlerwhich has a single technique known as hasNetworkConnection() is testing community connectivity and I must implement this class on each Android and iOS platforms. How to do that in cross-platform improvement we declare one thing like hope after which present the really implementation for every platform ( https://kotlinlang.org/docs/multiplatform-connect-to-apis.html).

I’ve two unique concepts for doing this, and I will take a look at every one.

The naive method – NetworkHandler class like hope class:
The thought is to declare the NetworkHandler class as a tentative class, then present the precise implementation for every platform. One thing like this:

//commonMain module
anticipate class NetworkHandler(){
enjoyable hasNetworkConnection():Boolean
}
------------------------------------


//iosMain or androidMain module
precise class NetworkHandler precise constructor() {
precise enjoyable hasNetworkConnection():Boolean {
//implementation particular to every module
}
}

After I seemed on the implementation of the NetworkHandler class, I seen that it is dependent upon the Android utility context. And in iOS surroundings, I do not even know what dependencies it should want, so this technique does not work. Due to this fact, I needed to think about an alternate.

Predominant method – Create NetworkHandler and supply implementations in every platform:

//commonMain module
interface NetworkHandler {
enjoyable hasNetworkConnection():Boolean
}
--------------------------------------


//androidMain module
class AndroidNetworkHandler(context:Context):NetworkHandler{
override enjoyable hasNetworkConnection(): Boolean {
//implementation in android world
}
}


----------------------------------------


//iosMain module
class IosNetworkHandler():NetworkHandler{
override enjoyable hasNetworkConnection(): Boolean {
// I nonetheless do not understand how is implemenation in ios
// so I simply return true right here :D
return true
}
}

To this point so good, however nonetheless one downside. I am utilizing Koin as DI and within the core module I want to supply the precise implementation of the NetworkHandler class. Earlier than migrating to cross-platform, it was as follows:

val coreModule = module {
single { NetworkHandler(androidContext()) }
}

Now in my cross-platform core module I want to inform koin someway “Howdy Koin, right here is my precise implementation of NetworkHandler for every platform” and that is the place I discovered out the highly effective means of Koin use “embrace” And “DSL builder” characteristic. The thought is to separate the Koin module into two elements: the generic module and the platform-specific module. The platform-specific module is asserted as an anticipated variable and the The precise implementation is offered in every platform.By utilizing the “embrace” characteristic, we mix these modules right into a single module.

//commonMain module
non-public val commonCoreModule = module {
//Add frequent core module implementations right here
}
//https://github.com/InsertKoinIO/koin/points/1341
// writing get() is essential
val coreModule : Module get() = module {
consists of(commonCoreModule + platformCoreModule)
}


inside anticipate val platformCoreModule:Module


-------------------------------


//androidMain module
inside precise val platformCoreModule: Module = module {
singleOf(::AndroidNetworkHandler) bind NetworkHandler::class
}


------------------------------------


//iosMain module
inside precise val platformCoreModule: Module = module {
singleOf(::IosNetworkHandler) bind NetworkHandler::class
}

By following this method, I used to be capable of efficiently provision totally different implementations for every platform utilizing Koin. This enables me to customise the conduct of the NetworkHandler class primarily based on the precise necessities of every platform.

John Wick: Chapter 4 (FREE) FULLMOVIE The Super Mario Bros Movie avatar 2 Where To Watch Creed 3 Free At Home Knock at the Cabin (2023) FullMovie Where To Watch Ant-Man 3 and the Wasp: Quantumania Cocaine Bear 2023 (FullMovie) Scream 6 Full Movie
Updated: May 29, 2023 — 12:57 pm

Leave a Reply

Your email address will not be published. Required fields are marked *

androidkaki.com © 2023 Android kaki