Make :core:common as a multiplatform library

pull/1323/head
lihenggui 2 years ago
parent 29c3038e70
commit c5521ce89b

@ -14,16 +14,23 @@
* limitations under the License.
*/
plugins {
alias(libs.plugins.nowinandroid.android.library)
alias(libs.plugins.nowinandroid.kmp.library)
alias(libs.plugins.nowinandroid.kotlin.inject)
alias(libs.plugins.nowinandroid.android.library.jacoco)
alias(libs.plugins.nowinandroid.android.hilt)
}
android {
namespace = "com.google.samples.apps.nowinandroid.core.common"
}
dependencies {
testImplementation(libs.kotlinx.coroutines.test)
testImplementation(libs.turbine)
}
kotlin {
sourceSets {
commonMain.dependencies {
implementation(libs.kotlinx.coroutines.core)
}
commonTest.dependencies {
implementation(libs.kotlin.test)
implementation(libs.turbine)
implementation(libs.kotlinx.coroutines.test)
}
}
}

@ -1,5 +1,5 @@
/*
* Copyright 2022 The Android Open Source Project
* 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.
@ -14,16 +14,19 @@
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.network
package com.google.samples.apps.nowinandroid.core.network.di
import javax.inject.Qualifier
import kotlin.annotation.AnnotationRetention.RUNTIME
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import me.tatarka.inject.annotations.Component
import me.tatarka.inject.annotations.Provides
@Qualifier
@Retention(RUNTIME)
annotation class Dispatcher(val niaDispatcher: NiaDispatchers)
typealias ApplicationScope = CoroutineScope
enum class NiaDispatchers {
Default,
IO,
@Component
abstract class CoroutineScopeComponent {
@Provides
fun providesCoroutineScope(
dispatcher: DefaultDispatcher,
): ApplicationScope = CoroutineScope(SupervisorJob() + dispatcher)
}

@ -16,24 +16,18 @@
package com.google.samples.apps.nowinandroid.core.network.di
import com.google.samples.apps.nowinandroid.core.network.Dispatcher
import com.google.samples.apps.nowinandroid.core.network.NiaDispatchers.Default
import com.google.samples.apps.nowinandroid.core.network.NiaDispatchers.IO
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import me.tatarka.inject.annotations.Component
import me.tatarka.inject.annotations.Provides
@Module
@InstallIn(SingletonComponent::class)
object DispatchersModule {
typealias DefaultDispatcher = CoroutineDispatcher
typealias IODispatcher = CoroutineDispatcher
@Component
abstract class DispatchersComponent {
@Provides
@Dispatcher(IO)
fun providesIODispatcher(): CoroutineDispatcher = Dispatchers.IO
fun providesIODispatcher(): IODispatcher = Dispatchers.Unconfined
@Provides
@Dispatcher(Default)
fun providesDefaultDispatcher(): CoroutineDispatcher = Dispatchers.Default
fun providesDefaultDispatcher(): DefaultDispatcher = Dispatchers.Default
}

@ -19,7 +19,7 @@ package com.google.samples.apps.nowinandroid.core.result
import app.cash.turbine.test
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.test.runTest
import org.junit.Test
import kotlin.test.Test
import kotlin.test.assertEquals
class ResultKtTest {
@ -38,7 +38,7 @@ class ResultKtTest {
when (val errorResult = awaitItem()) {
is Result.Error -> assertEquals(
"Test Done",
errorResult.exception?.message,
errorResult.exception.message,
)
Result.Loading,
is Result.Success,

@ -1,44 +0,0 @@
/*
* Copyright 2023 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.network.di
import com.google.samples.apps.nowinandroid.core.network.Dispatcher
import com.google.samples.apps.nowinandroid.core.network.NiaDispatchers.Default
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import javax.inject.Qualifier
import javax.inject.Singleton
@Retention(AnnotationRetention.RUNTIME)
@Qualifier
annotation class ApplicationScope
@Module
@InstallIn(SingletonComponent::class)
internal object CoroutineScopesModule {
@Provides
@Singleton
@ApplicationScope
fun providesCoroutineScope(
@Dispatcher(Default) dispatcher: CoroutineDispatcher,
): CoroutineScope = CoroutineScope(SupervisorJob() + dispatcher)
}

@ -46,9 +46,6 @@ kotlin {
nativeMain.dependencies {
implementation(libs.sqldelight.native.driver)
}
nativeTest.dependencies {
implementation(libs.sqldelight.sqlite.driver)
}
jvmMain.dependencies {
implementation(libs.sqldelight.sqlite.driver)
}

@ -19,7 +19,7 @@ package com.google.samples.apps.nowinandroid.core.testing.di
import com.google.samples.apps.nowinandroid.core.network.Dispatcher
import com.google.samples.apps.nowinandroid.core.network.NiaDispatchers.Default
import com.google.samples.apps.nowinandroid.core.network.NiaDispatchers.IO
import com.google.samples.apps.nowinandroid.core.network.di.DispatchersModule
import com.google.samples.apps.nowinandroid.core.network.di.DispatchersComponent
import dagger.Module
import dagger.Provides
import dagger.hilt.components.SingletonComponent
@ -30,7 +30,7 @@ import kotlinx.coroutines.test.TestDispatcher
@Module
@TestInstallIn(
components = [SingletonComponent::class],
replaces = [DispatchersModule::class],
replaces = [DispatchersComponent::class],
)
internal object TestDispatchersModule {
@Provides

Loading…
Cancel
Save