Loading...

Kotlin Multiplatform by Tutorials by By Kevin Moore & By Saeed Taheri & By Carlos Mota

Kotlin Multiplatform by Tutorials by By Kevin Moore & By Saeed Taheri & By Carlos Mota

Author:By Kevin Moore & By Saeed Taheri & By Carlos Mota
Language: eng
Format: epub
Publisher: Ray Wenderlich


Android

Still in the shared module, open KoinAndroid.kt from androidMain and add this block of code:

actual val platformModule = module { single<Settings> { AndroidSettings(get()) } }

Make sure to import the Settings from com.russhwolf.settings.Settings package.

AndroidSettings is the Android implementation of Settings.

You’re using Koin’s single keyword, so it provides this dependency as a singleton.

AndroidSettings needs an instance of SharedPreferences in its constructor. You’re asking Koin to fetch that dependency at runtime. Don’t worry! To prevent a crash, you’ll provide that dependency soon.

Open OrganizeApp.kt in androidApp module. As you may remember, the initKoin function had a parameter named appModule. Now it’s time to use it.

Pass this block of code to the appModule parameter:

module { //1 single<Context> { [email protected] } //2 single<SharedPreferences> { get<Context>().getSharedPreferences( "OrganizeApp", Context.MODE_PRIVATE ) } }

Here’s what’s going on in the code above:

Setting up SharedPreferences requires an instance of Android Context. You’re declaring to Koin that it can use the application instance as the singleton context.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.