Skip to content

Commit aa70106

Browse files
committed
Add Application-wide CoroutineScope in the DI graph
Following the work of #607.
1 parent 2c18740 commit aa70106

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2023 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.samples.apps.nowinandroid.core.network.di
18+
19+
import com.google.samples.apps.nowinandroid.core.network.Dispatcher
20+
import com.google.samples.apps.nowinandroid.core.network.NiaDispatchers.IO
21+
import dagger.Module
22+
import dagger.Provides
23+
import dagger.hilt.InstallIn
24+
import dagger.hilt.components.SingletonComponent
25+
import kotlinx.coroutines.CoroutineDispatcher
26+
import kotlinx.coroutines.CoroutineScope
27+
import kotlinx.coroutines.SupervisorJob
28+
import javax.inject.Singleton
29+
30+
@Module
31+
@InstallIn(SingletonComponent::class)
32+
object CoroutineScopesModule {
33+
@Provides
34+
@Singleton
35+
@Dispatcher(IO)
36+
fun providesIOCoroutineScope(
37+
@Dispatcher(IO) ioDispatcher: CoroutineDispatcher,
38+
): CoroutineScope = CoroutineScope(SupervisorJob() + ioDispatcher)
39+
}

core/datastore-test/src/main/java/com/google/samples/apps/nowinandroid/core/datastore/test/TestDataStoreModule.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ import dagger.Module
2727
import dagger.Provides
2828
import dagger.hilt.components.SingletonComponent
2929
import dagger.hilt.testing.TestInstallIn
30-
import kotlinx.coroutines.CoroutineDispatcher
3130
import kotlinx.coroutines.CoroutineScope
32-
import kotlinx.coroutines.SupervisorJob
3331
import org.junit.rules.TemporaryFolder
3432
import javax.inject.Singleton
3533

@@ -43,13 +41,12 @@ object TestDataStoreModule {
4341
@Provides
4442
@Singleton
4543
fun providesUserPreferencesDataStore(
46-
@Dispatcher(IO) ioDispatcher: CoroutineDispatcher,
44+
@Dispatcher(IO) ioScope: CoroutineScope,
4745
userPreferencesSerializer: UserPreferencesSerializer,
4846
tmpFolder: TemporaryFolder,
4947
): DataStore<UserPreferences> =
5048
tmpFolder.testUserPreferencesDataStore(
51-
// TODO: Provide an application-wide CoroutineScope in the DI graph
52-
coroutineScope = CoroutineScope(SupervisorJob() + ioDispatcher),
49+
coroutineScope = ioScope,
5350
userPreferencesSerializer = userPreferencesSerializer,
5451
)
5552
}

core/datastore/src/main/java/com/google/samples/apps/nowinandroid/core/datastore/di/DataStoreModule.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ import dagger.Provides
3030
import dagger.hilt.InstallIn
3131
import dagger.hilt.android.qualifiers.ApplicationContext
3232
import dagger.hilt.components.SingletonComponent
33-
import kotlinx.coroutines.CoroutineDispatcher
3433
import kotlinx.coroutines.CoroutineScope
35-
import kotlinx.coroutines.SupervisorJob
3634
import javax.inject.Singleton
3735

3836
@Module
@@ -43,12 +41,12 @@ object DataStoreModule {
4341
@Singleton
4442
fun providesUserPreferencesDataStore(
4543
@ApplicationContext context: Context,
46-
@Dispatcher(IO) ioDispatcher: CoroutineDispatcher,
44+
@Dispatcher(IO) ioScope: CoroutineScope,
4745
userPreferencesSerializer: UserPreferencesSerializer,
4846
): DataStore<UserPreferences> =
4947
DataStoreFactory.create(
5048
serializer = userPreferencesSerializer,
51-
scope = CoroutineScope(ioDispatcher + SupervisorJob()),
49+
scope = ioScope,
5250
migrations = listOf(
5351
IntToStringIdsMigration,
5452
),

0 commit comments

Comments
 (0)