Guilherme Delgado
07/20/2023, 11:15 AMGuilherme Delgado
07/20/2023, 11:17 AMclass MyApp : Application() {
override fun onCreate() {
super.onCreate()
buildConfigApiUrl = BuildConfig.API_URL
DependencyInjection.initKoin(BuildConfig.DEBUG) {
androidLogger()
androidContext(this@MyApp)
modules(appModule, networkModule, viewModelsModule)
//this needs to happen after the modules initialization, but the modules need this information also ^^'
chuckerInterceptor = get(named("chuckerInterceptor"))
sharedPrefs = get(named("encryptedPrefs"))
}
}
}
single(named("encryptedPrefs")) {
EncryptedSharedPreferences.create(...)
}
single(named("chuckerInterceptor")) {
ChuckerInterceptor....
}
androidMain (shared module)
lateinit var buildConfigApiUrl: String
lateinit var sharedPrefs: SharedPreferences
lateinit var chuckerInterceptor: Interceptor
actual class PlatformDependencies actual constructor() {
actual val baseUrl: String = buildConfigApiUrl
actual fun getSettings(): Settings = SharedPreferencesSettings(sharedPrefs)
actual fun getHttpEngine(): HttpClientEngine = OkHttp.create { OkHttpConfig().also { addInterceptor(chuckerInterceptor) } }
}
commonMain (shared module)
fun initKoin(enableNetworkLogs: Boolean = false, appDeclaration: KoinAppDeclaration = {}) {
startKoin {
appDeclaration()
modules(commonModule(enableNetworkLogs), platformModule())
}
}
private fun commonModule(enableNetworkLogs: Boolean) = module {
single { PlatformDependencies().getSettings() }
single { LocalStorage(get()) }
....
}
Guilherme Delgado
07/20/2023, 12:51 PMGuilherme Delgado
07/20/2023, 12:52 PMGuilherme Delgado
07/20/2023, 12:52 PMoverride fun onCreate() {
super.onCreate()
buildConfigApiUrl = BuildConfig.API_URL
sharedPrefs = EncryptedSharedPreferences.create(...)
chuckerInterceptor = ChuckerInterceptor
.Builder(...)
DependencyInjection.initKoin(BuildConfig.DEBUG) {
androidLogger()
androidContext(this@KalendarApp)
modules(appModule, networkModule, viewModelsModule)
}
}
Guilherme Delgado
07/20/2023, 12:53 PMarnaud.giuliani
07/28/2023, 1:04 PM