Use Koin for di in the common module

pull/2064/head
lihenggui 1 year ago
parent 50eec57a68
commit 0e9d23318a

@ -15,7 +15,7 @@
*/
plugins {
alias(libs.plugins.nowinandroid.kmp.library)
alias(libs.plugins.nowinandroid.kotlin.inject)
alias(libs.plugins.nowinandroid.di.koin)
alias(libs.plugins.nowinandroid.android.library.jacoco)
}

@ -1,44 +0,0 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.di
import com.google.samples.apps.nowinandroid.core.di.NiaDispatchers.DEFAULT
import com.google.samples.apps.nowinandroid.core.di.NiaDispatchers.IO
import com.google.samples.apps.nowinandroid.core.di.NiaDispatchers.MAIN
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import me.tatarka.inject.annotations.Component
import me.tatarka.inject.annotations.Provides
@Component
abstract class AndroidDispatchersComponent : DispatchersComponent() {
@Provides
override fun providesIODispatcher():
@Dispatcher(IO)
CoroutineDispatcher = Dispatchers.IO
@Provides
override fun providesDefaultDispatcher():
@Dispatcher(DEFAULT)
CoroutineDispatcher =
Dispatchers.Default
@Provides
override fun providesMainDispatcher():
@Dispatcher(MAIN)
CoroutineDispatcher = Dispatchers.Main
}

@ -18,9 +18,13 @@ package com.google.samples.apps.nowinandroid.core.di
import android.content.Context
import coil3.PlatformContext
import me.tatarka.inject.annotations.Inject
import org.koin.core.annotation.Single
@Inject
class AndroidPlatformContextProvider(private val context: Context) : PlatformContextProvider {
override val platformContext: PlatformContext get() = context
}
@Single
actual class PlatformContextProvider(
val context: Context,
) {
actual fun getPlatformContext(): PlatformContext {
return context
}
}

@ -16,12 +16,17 @@
package com.google.samples.apps.nowinandroid.core.di
import android.app.Application
import me.tatarka.inject.annotations.Component
import me.tatarka.inject.annotations.Provides
import org.koin.core.annotation.ComponentScan
import org.koin.core.annotation.Module
import org.koin.ksp.generated.module
@Component
@ApplicationScope
abstract class AndroidApplicationComponent(
@get:Provides val application: Application,
fun commonModule() = listOf(
CommonModule().module,
coroutineDispatcherModule,
coroutineScopeModule,
imageLoaderModule,
)
@Module
@ComponentScan
class CommonModule

@ -16,11 +16,13 @@
package com.google.samples.apps.nowinandroid.core.di
import android.app.Application
import coil3.PlatformContext
import me.tatarka.inject.annotations.Provides
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import org.koin.core.qualifier.named
import org.koin.dsl.module
interface PlatformComponent {
@Provides
fun providePlatformContext(application: Application): PlatformContext = application
val coroutineDispatcherModule = module {
single(named("IoDispatcher")) { Dispatchers.IO }
single(named("DefaultDispatcher")) { Dispatchers.Default }
single(named("MainDispatcher")) { Dispatchers.Main }
}

@ -16,13 +16,12 @@
package com.google.samples.apps.nowinandroid.core.di
import com.google.samples.apps.nowinandroid.core.di.NiaDispatchers.DEFAULT
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import me.tatarka.inject.annotations.Component
import me.tatarka.inject.annotations.Provides
import me.tatarka.inject.annotations.Scope
import org.koin.core.qualifier.named
import org.koin.dsl.module
/**
* The application-level scope. There will only be one instance of anything annotated with this.
@ -30,14 +29,9 @@ import me.tatarka.inject.annotations.Scope
@Scope
annotation class ApplicationScope
@Component
abstract class CoroutineScopeComponent(
@Component val dispatchersComponent: DispatchersComponent,
) {
@Dispatcher(DEFAULT)
abstract val dispatcher: CoroutineDispatcher
@Provides
fun providesCoroutineScope(): CoroutineScope =
val coroutineScopeModule = module {
single {
val dispatcher: CoroutineDispatcher = get(named("DefaultDispatcher"))
CoroutineScope(SupervisorJob() + dispatcher)
}
}
}

@ -1,41 +0,0 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.di
import com.google.samples.apps.nowinandroid.core.di.NiaDispatchers.DEFAULT
import com.google.samples.apps.nowinandroid.core.di.NiaDispatchers.IO
import com.google.samples.apps.nowinandroid.core.di.NiaDispatchers.MAIN
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.IO
import me.tatarka.inject.annotations.Provides
abstract class DispatchersComponent {
@Provides
abstract fun providesIODispatcher():
@Dispatcher(IO)
CoroutineDispatcher
@Provides
abstract fun providesDefaultDispatcher():
@Dispatcher(DEFAULT)
CoroutineDispatcher
@Provides
abstract fun providesMainDispatcher():
@Dispatcher(MAIN)
CoroutineDispatcher
}

@ -21,27 +21,16 @@ import coil3.PlatformContext
import coil3.memory.MemoryCache
import coil3.request.crossfade
import coil3.util.DebugLogger
import me.tatarka.inject.annotations.Component
import me.tatarka.inject.annotations.Provides
import org.koin.dsl.module
@Component
abstract class ImageLoaderComponent {
/**
* Since we're displaying SVGs in the app, Coil needs an ImageLoader which supports this
* format. During Coil's initialization it will call `applicationContext.newImageLoader()` to
* obtain an ImageLoader.
*
* @see <a href="https://github.com/coil-kt/coil/blob/main/coil-singleton/src/main/java/coil/Coil.kt">Coil</a>
*/
@Provides
fun provideImageLoader(
context: PlatformContext,
): ImageLoader {
return ImageLoader.Builder(context)
val imageLoaderModule = module {
single<ImageLoader> {
val platformContext: PlatformContext = get()
ImageLoader.Builder(platformContext)
.memoryCache {
MemoryCache.Builder()
// Set the max size to 25% of the app's available memory.
.maxSizePercent(context, percent = 0.25)
.maxSizePercent(platformContext, percent = 0.25)
.build()
}
// Show a short crossfade when loading images asynchronously.

@ -18,6 +18,6 @@ package com.google.samples.apps.nowinandroid.core.di
import coil3.PlatformContext
interface PlatformContextProvider {
val platformContext: PlatformContext
expect class PlatformContextProvider {
fun getPlatformContext(): PlatformContext
}

@ -1,43 +0,0 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.di
import com.google.samples.apps.nowinandroid.core.di.NiaDispatchers.DEFAULT
import com.google.samples.apps.nowinandroid.core.di.NiaDispatchers.IO
import com.google.samples.apps.nowinandroid.core.di.NiaDispatchers.MAIN
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import me.tatarka.inject.annotations.Component
import me.tatarka.inject.annotations.Provides
@Component
abstract class JvmDispatchersComponent : DispatchersComponent() {
@Provides
override fun providesIODispatcher():
@Dispatcher(IO)
CoroutineDispatcher = Dispatchers.IO
@Provides
override fun providesDefaultDispatcher():
@Dispatcher(DEFAULT)
CoroutineDispatcher = Dispatchers.Default
@Provides
override fun providesMainDispatcher():
@Dispatcher(MAIN)
CoroutineDispatcher = Dispatchers.Main
}

@ -17,8 +17,9 @@
package com.google.samples.apps.nowinandroid.core.di
import coil3.PlatformContext
import org.koin.core.annotation.Single
class NativePlatformContextProvider : PlatformContextProvider {
override val platformContext: PlatformContext
get() = PlatformContext.INSTANCE
@Single
actual class PlatformContextProvider {
actual fun getPlatformContext(): PlatformContext = PlatformContext.INSTANCE
}

@ -1,45 +0,0 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.di
import com.google.samples.apps.nowinandroid.core.di.NiaDispatchers.DEFAULT
import com.google.samples.apps.nowinandroid.core.di.NiaDispatchers.IO
import com.google.samples.apps.nowinandroid.core.di.NiaDispatchers.MAIN
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import me.tatarka.inject.annotations.Component
import me.tatarka.inject.annotations.Provides
@Component
abstract class NativeDispatchersComponent : DispatchersComponent() {
@Provides
override fun providesIODispatcher():
@Dispatcher(IO)
CoroutineDispatcher = Dispatchers.IO
@Provides
override fun providesDefaultDispatcher():
@Dispatcher(DEFAULT)
CoroutineDispatcher =
Dispatchers.Default
@Provides
override fun providesMainDispatcher():
@Dispatcher(MAIN)
CoroutineDispatcher = Dispatchers.Main
}

@ -17,8 +17,9 @@
package com.google.samples.apps.nowinandroid.core.di
import coil3.PlatformContext
import org.koin.core.annotation.Single
class JvmPlatformContextProvider : PlatformContextProvider {
override val platformContext: PlatformContext
get() = PlatformContext.INSTANCE
@Single
actual class PlatformContextProvider {
actual fun getPlatformContext(): PlatformContext = PlatformContext.INSTANCE
}

@ -2,7 +2,7 @@
accompanist = "0.36.0"
androidDesugarJdkLibs = "2.1.2"
# AGP and tools should be updated together
androidGradlePlugin = "8.6.1"
androidGradlePlugin = "8.7.0"
androidTools = "31.7.0"
androidxActivity = "1.9.2"
androidxAppCompat = "1.7.0"
@ -146,7 +146,7 @@ hilt-ext-work = { group = "androidx.hilt", name = "hilt-work", version.ref = "hi
javax-inject = { module = "javax.inject:javax.inject", version = "1" }
koin-annotations-bom = { group = "io.insert-koin", name = "koin-annotations-bom", version.ref = "koin-annotations" }
koin-annotations = { group = "io.insert-koin", name = "koin-annotations" }
koin-ksp-compiler = { group = "io.insert-koin", name = "koin-ksp-compiler" }
koin-ksp-compiler = { group = "io.insert-koin", name = "koin-ksp-compiler", version.ref = "koin-annotations" }
koin-bom = { group = "io.insert-koin", name = "koin-bom", version.ref = "koin" }
koin-android = { group = "io.insert-koin", name = "koin-android"}
koin-core = { group = "io.insert-koin", name = "koin-core"}

Loading…
Cancel
Save