Use object to provide implementations

pull/1323/head
lihenggui 2 years ago
parent 460060389e
commit 2f412db367

@ -18,7 +18,7 @@ package com.google.samples.apps.nowinandroid.util
import android.util.Log
import androidx.profileinstaller.ProfileVerifier
import com.google.samples.apps.nowinandroid.core.network.di.ApplicationScope
import com.google.samples.apps.nowinandroid.core.di.ApplicationScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.guava.await
import kotlinx.coroutines.launch

@ -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.
@ -22,6 +22,7 @@ plugins {
android {
namespace = "com.google.samples.apps.nowinandroid.core.common"
}
kotlin {
sourceSets {
commonMain.dependencies {

@ -0,0 +1,28 @@
/*
* 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 kotlinx.coroutines.Dispatchers
import me.tatarka.inject.annotations.Provides
actual object DispatchersComponent {
@Provides
actual fun providesIODispatcher(): IODispatcher = Dispatchers.IO
@Provides
actual fun providesDefaultDispatcher(): DefaultDispatcher = Dispatchers.Default
}

@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.network.di
package com.google.samples.apps.nowinandroid.core.di
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob

@ -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,20 +14,18 @@
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.network.di
package com.google.samples.apps.nowinandroid.core.di
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import me.tatarka.inject.annotations.Component
import me.tatarka.inject.annotations.Provides
typealias DefaultDispatcher = CoroutineDispatcher
typealias IODispatcher = CoroutineDispatcher
@Component
abstract class DispatchersComponent {
expect object DispatchersComponent {
@Provides
fun providesIODispatcher(): IODispatcher = Dispatchers.Unconfined
fun providesIODispatcher(): IODispatcher
@Provides
fun providesDefaultDispatcher(): DefaultDispatcher = Dispatchers.Default
fun providesDefaultDispatcher(): DefaultDispatcher
}

@ -0,0 +1,30 @@
/*
* 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 kotlinx.coroutines.Dispatchers
import me.tatarka.inject.annotations.Provides
actual object DispatchersComponent {
// TODO Provides an actual IODispatcher
@Provides
actual fun providesIODispatcher(): IODispatcher = Dispatchers.Default
@Provides
actual fun providesDefaultDispatcher(): DefaultDispatcher = Dispatchers.Default
}

@ -0,0 +1,28 @@
/*
* 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 kotlinx.coroutines.Dispatchers
import me.tatarka.inject.annotations.Provides
actual object DispatchersComponent {
@Provides
actual fun providesIODispatcher(): IODispatcher = Dispatchers.IO
@Provides
actual fun providesDefaultDispatcher(): DefaultDispatcher = Dispatchers.Default
}

@ -0,0 +1,30 @@
/*
* 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 kotlinx.coroutines.Dispatchers
import me.tatarka.inject.annotations.Provides
actual object DispatchersComponent {
// TODO Provides an actual IODispatcher
@Provides
actual fun providesIODispatcher(): IODispatcher = Dispatchers.Default
@Provides
actual fun providesDefaultDispatcher(): DefaultDispatcher = Dispatchers.Default
}

@ -34,14 +34,15 @@ actual class DriverModule(private val context: Context) {
schema: SqlSchema<QueryResult.AsyncValue<Unit>>,
): SqlDriver {
val synchronousSchema = schema.synchronous()
return AndroidSqliteDriver(schema = synchronousSchema,
return AndroidSqliteDriver(
schema = synchronousSchema,
context = context,
name = "nia-database.db",
callback = object : AndroidSqliteDriver.Callback(synchronousSchema) {
override fun onOpen(db: SupportSQLiteDatabase) {
db.setForeignKeyConstraintsEnabled(true)
}
}
},
)
}
}

@ -26,7 +26,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import kotlinx.datetime.Instant
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals

@ -21,7 +21,7 @@ import androidx.datastore.core.DataStoreFactory
import com.google.samples.apps.nowinandroid.core.datastore.UserPreferences
import com.google.samples.apps.nowinandroid.core.datastore.UserPreferencesSerializer
import com.google.samples.apps.nowinandroid.core.datastore.di.DataStoreModule
import com.google.samples.apps.nowinandroid.core.network.di.ApplicationScope
import com.google.samples.apps.nowinandroid.core.di.ApplicationScope
import dagger.Module
import dagger.Provides
import dagger.hilt.components.SingletonComponent

@ -23,9 +23,9 @@ import androidx.datastore.dataStoreFile
import com.google.samples.apps.nowinandroid.core.datastore.IntToStringIdsMigration
import com.google.samples.apps.nowinandroid.core.datastore.UserPreferences
import com.google.samples.apps.nowinandroid.core.datastore.UserPreferencesSerializer
import com.google.samples.apps.nowinandroid.core.di.ApplicationScope
import com.google.samples.apps.nowinandroid.core.network.Dispatcher
import com.google.samples.apps.nowinandroid.core.network.NiaDispatchers.IO
import com.google.samples.apps.nowinandroid.core.network.di.ApplicationScope
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn

@ -16,10 +16,10 @@
package com.google.samples.apps.nowinandroid.core.testing.di
import com.google.samples.apps.nowinandroid.core.di.DispatchersComponent
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.DispatchersComponent
import dagger.Module
import dagger.Provides
import dagger.hilt.components.SingletonComponent

Loading…
Cancel
Save