From d55402c7982ae691bc49e753234410bf6d254715 Mon Sep 17 00:00:00 2001 From: kyanro Date: Tue, 3 Sep 2024 23:35:11 +0900 Subject: [PATCH 1/8] fix blink favorite icon --- .../data/sessions/DefaultSessionsRepository.kt | 14 ++++++++++---- .../confsched/data/user/UserDataStore.kt | 9 ++++++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/sessions/DefaultSessionsRepository.kt b/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/sessions/DefaultSessionsRepository.kt index 1e5ba29e4..7077f117b 100644 --- a/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/sessions/DefaultSessionsRepository.kt +++ b/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/sessions/DefaultSessionsRepository.kt @@ -17,6 +17,7 @@ import io.github.droidkaigi.confsched.model.SessionsRepository import io.github.droidkaigi.confsched.model.Timetable import io.github.droidkaigi.confsched.model.TimetableItem import io.github.droidkaigi.confsched.model.TimetableItemId +import kotlinx.collections.immutable.PersistentSet import kotlinx.collections.immutable.persistentSetOf import kotlinx.coroutines.ensureActive import kotlinx.coroutines.flow.Flow @@ -90,6 +91,9 @@ public class DefaultSessionsRepository( @Composable public override fun timetable(): Timetable { var first by remember { mutableStateOf(true) } + var favoriteSessions: PersistentSet by remember { + mutableStateOf(userDataStore.getFavoriteSessionMemoryCacheOrNull ?: persistentSetOf()) + } SafeLaunchedEffect(first) { if (first) { Logger.d("DefaultSessionsRepository onStart getTimetableStream()") @@ -99,6 +103,12 @@ public class DefaultSessionsRepository( } } + SafeLaunchedEffect(Unit) { + userDataStore.getFavoriteSessionStream().collect { + favoriteSessions = it + } + } + val timetable by remember { sessionCacheDataStore.getTimetableStream().catch { e -> Logger.d( @@ -109,10 +119,6 @@ public class DefaultSessionsRepository( emitAll(sessionCacheDataStore.getTimetableStream()) } }.safeCollectAsRetainedState(Timetable()) - val favoriteSessions by remember { - userDataStore.getFavoriteSessionStream() - }.safeCollectAsRetainedState(persistentSetOf()) - Logger.d { "DefaultSessionsRepository timetable() count=${timetable.timetableItems.size}" } return timetable.copy(bookmarks = favoriteSessions) } diff --git a/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/user/UserDataStore.kt b/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/user/UserDataStore.kt index c2d5c84ef..49a2ad967 100644 --- a/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/user/UserDataStore.kt +++ b/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/user/UserDataStore.kt @@ -21,10 +21,15 @@ public class UserDataStore(private val dataStore: DataStore) { private val mutableIdToken = MutableStateFlow(null) public val idToken: StateFlow = mutableIdToken + private var favoriteSessionMemoryCache: PersistentSet? = null + public val getFavoriteSessionMemoryCacheOrNull: PersistentSet? + get() = favoriteSessionMemoryCache + public fun getFavoriteSessionStream(): Flow> { return dataStore.data .catch { exception -> if (exception is IOException) { + favoriteSessionMemoryCache = null emit(emptyPreferences()) } else { throw exception @@ -33,7 +38,9 @@ public class UserDataStore(private val dataStore: DataStore) { .map { preferences: Preferences -> (preferences[KEY_FAVORITE_SESSION_IDS]?.split(",") ?: listOf()) .map { TimetableItemId(it) } - .toPersistentSet() + .toPersistentSet().also { + favoriteSessionMemoryCache = it + } } } From 95b4905f2d939f89196d8e498e65a24af9f6a8e9 Mon Sep 17 00:00:00 2001 From: kyanro Date: Thu, 5 Sep 2024 23:41:58 +0900 Subject: [PATCH 2/8] Revert "fix blink favorite icon" This reverts commit d55402c7982ae691bc49e753234410bf6d254715. --- .../data/sessions/DefaultSessionsRepository.kt | 14 ++++---------- .../confsched/data/user/UserDataStore.kt | 9 +-------- 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/sessions/DefaultSessionsRepository.kt b/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/sessions/DefaultSessionsRepository.kt index 7077f117b..1e5ba29e4 100644 --- a/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/sessions/DefaultSessionsRepository.kt +++ b/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/sessions/DefaultSessionsRepository.kt @@ -17,7 +17,6 @@ import io.github.droidkaigi.confsched.model.SessionsRepository import io.github.droidkaigi.confsched.model.Timetable import io.github.droidkaigi.confsched.model.TimetableItem import io.github.droidkaigi.confsched.model.TimetableItemId -import kotlinx.collections.immutable.PersistentSet import kotlinx.collections.immutable.persistentSetOf import kotlinx.coroutines.ensureActive import kotlinx.coroutines.flow.Flow @@ -91,9 +90,6 @@ public class DefaultSessionsRepository( @Composable public override fun timetable(): Timetable { var first by remember { mutableStateOf(true) } - var favoriteSessions: PersistentSet by remember { - mutableStateOf(userDataStore.getFavoriteSessionMemoryCacheOrNull ?: persistentSetOf()) - } SafeLaunchedEffect(first) { if (first) { Logger.d("DefaultSessionsRepository onStart getTimetableStream()") @@ -103,12 +99,6 @@ public class DefaultSessionsRepository( } } - SafeLaunchedEffect(Unit) { - userDataStore.getFavoriteSessionStream().collect { - favoriteSessions = it - } - } - val timetable by remember { sessionCacheDataStore.getTimetableStream().catch { e -> Logger.d( @@ -119,6 +109,10 @@ public class DefaultSessionsRepository( emitAll(sessionCacheDataStore.getTimetableStream()) } }.safeCollectAsRetainedState(Timetable()) + val favoriteSessions by remember { + userDataStore.getFavoriteSessionStream() + }.safeCollectAsRetainedState(persistentSetOf()) + Logger.d { "DefaultSessionsRepository timetable() count=${timetable.timetableItems.size}" } return timetable.copy(bookmarks = favoriteSessions) } diff --git a/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/user/UserDataStore.kt b/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/user/UserDataStore.kt index 49a2ad967..c2d5c84ef 100644 --- a/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/user/UserDataStore.kt +++ b/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/user/UserDataStore.kt @@ -21,15 +21,10 @@ public class UserDataStore(private val dataStore: DataStore) { private val mutableIdToken = MutableStateFlow(null) public val idToken: StateFlow = mutableIdToken - private var favoriteSessionMemoryCache: PersistentSet? = null - public val getFavoriteSessionMemoryCacheOrNull: PersistentSet? - get() = favoriteSessionMemoryCache - public fun getFavoriteSessionStream(): Flow> { return dataStore.data .catch { exception -> if (exception is IOException) { - favoriteSessionMemoryCache = null emit(emptyPreferences()) } else { throw exception @@ -38,9 +33,7 @@ public class UserDataStore(private val dataStore: DataStore) { .map { preferences: Preferences -> (preferences[KEY_FAVORITE_SESSION_IDS]?.split(",") ?: listOf()) .map { TimetableItemId(it) } - .toPersistentSet().also { - favoriteSessionMemoryCache = it - } + .toPersistentSet() } } From 246716d5320a52df67c19a3eed3086e70f2edd1b Mon Sep 17 00:00:00 2001 From: kyanro Date: Fri, 6 Sep 2024 00:04:57 +0900 Subject: [PATCH 3/8] change getFavoriteSessionStream return type to StateFlow --- .../confsched/data/user/UserDataStore.kt | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/user/UserDataStore.kt b/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/user/UserDataStore.kt index c2d5c84ef..e6cd6ba44 100644 --- a/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/user/UserDataStore.kt +++ b/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/user/UserDataStore.kt @@ -8,20 +8,27 @@ import androidx.datastore.preferences.core.emptyPreferences import androidx.datastore.preferences.core.stringPreferencesKey import io.github.droidkaigi.confsched.model.TimetableItemId import kotlinx.collections.immutable.PersistentSet +import kotlinx.collections.immutable.persistentSetOf import kotlinx.collections.immutable.toPersistentSet +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Job import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.stateIn public class UserDataStore(private val dataStore: DataStore) { private val mutableIdToken = MutableStateFlow(null) public val idToken: StateFlow = mutableIdToken - public fun getFavoriteSessionStream(): Flow> { + private val singletonCoroutineScope: CoroutineScope = CoroutineScope(Job()) + + public fun getFavoriteSessionStream(): StateFlow> { return dataStore.data .catch { exception -> if (exception is IOException) { @@ -34,7 +41,11 @@ public class UserDataStore(private val dataStore: DataStore) { (preferences[KEY_FAVORITE_SESSION_IDS]?.split(",") ?: listOf()) .map { TimetableItemId(it) } .toPersistentSet() - } + }.stateIn( + scope = singletonCoroutineScope, + started = SharingStarted.WhileSubscribed(5_000), + initialValue = persistentSetOf() + ) } public suspend fun toggleFavorite(id: TimetableItemId) { From 980a7383900239dac79eace32e9ca8d475b6fbd7 Mon Sep 17 00:00:00 2001 From: kyanro Date: Fri, 6 Sep 2024 00:11:13 +0900 Subject: [PATCH 4/8] change favoriteSessions remember cycle --- .../compose/ComposeEffectErrorHandler.kt | 24 +++++++++++++++++++ .../sessions/DefaultSessionsRepository.kt | 4 ++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/core/common/src/commonMain/kotlin/io/github/droidkaigi/confsched/compose/ComposeEffectErrorHandler.kt b/core/common/src/commonMain/kotlin/io/github/droidkaigi/confsched/compose/ComposeEffectErrorHandler.kt index fe91110d5..3c8828a82 100644 --- a/core/common/src/commonMain/kotlin/io/github/droidkaigi/confsched/compose/ComposeEffectErrorHandler.kt +++ b/core/common/src/commonMain/kotlin/io/github/droidkaigi/confsched/compose/ComposeEffectErrorHandler.kt @@ -5,7 +5,9 @@ import androidx.compose.runtime.InternalComposeApi import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.ProvidedValue import androidx.compose.runtime.State +import androidx.compose.runtime.collectAsState import androidx.compose.runtime.currentComposer +import androidx.compose.runtime.produceState import androidx.compose.runtime.staticCompositionLocalOf import io.github.takahirom.rin.produceRetainedState import kotlinx.coroutines.CancellationException @@ -57,6 +59,28 @@ fun SafeLaunchedEffect(key: Any?, block: suspend CoroutineScope.() -> Unit) { } } +@Composable +fun StateFlow.safeCollectAsState( + context: CoroutineContext = EmptyCoroutineContext, +): State { + val composeEffectErrorHandler = LocalComposeEffectErrorHandler.current + return produceState(value, this, context) { + try { + if (context == EmptyCoroutineContext) { + collect { value = it } + } else { + withContext(context) { + collect { value = it } + } + } + } catch (e: CancellationException) { + throw e + } catch (e: Throwable) { + composeEffectErrorHandler.emit(e) + } + } +} + @Composable fun Flow.safeCollectAsRetainedState( initial: R, diff --git a/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/sessions/DefaultSessionsRepository.kt b/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/sessions/DefaultSessionsRepository.kt index 1e5ba29e4..de08a9630 100644 --- a/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/sessions/DefaultSessionsRepository.kt +++ b/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/sessions/DefaultSessionsRepository.kt @@ -10,6 +10,7 @@ import androidx.compose.runtime.setValue import co.touchlab.kermit.Logger import io.github.droidkaigi.confsched.compose.SafeLaunchedEffect import io.github.droidkaigi.confsched.compose.safeCollectAsRetainedState +import io.github.droidkaigi.confsched.compose.safeCollectAsState import io.github.droidkaigi.confsched.data.sessions.response.SessionsAllResponse import io.github.droidkaigi.confsched.data.user.UserDataStore import io.github.droidkaigi.confsched.model.DroidKaigi2024Day @@ -17,7 +18,6 @@ import io.github.droidkaigi.confsched.model.SessionsRepository import io.github.droidkaigi.confsched.model.Timetable import io.github.droidkaigi.confsched.model.TimetableItem import io.github.droidkaigi.confsched.model.TimetableItemId -import kotlinx.collections.immutable.persistentSetOf import kotlinx.coroutines.ensureActive import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.catch @@ -111,7 +111,7 @@ public class DefaultSessionsRepository( }.safeCollectAsRetainedState(Timetable()) val favoriteSessions by remember { userDataStore.getFavoriteSessionStream() - }.safeCollectAsRetainedState(persistentSetOf()) + }.safeCollectAsState() Logger.d { "DefaultSessionsRepository timetable() count=${timetable.timetableItems.size}" } return timetable.copy(bookmarks = favoriteSessions) From 3686dee2522a5c7d715ea94192f7bdb3033497d0 Mon Sep 17 00:00:00 2001 From: kyanro Date: Fri, 6 Sep 2024 00:14:55 +0900 Subject: [PATCH 5/8] change getFavoriteSessionStream from a method to a val To return the same instance of StateFlow --- .../confsched/data/sessions/DefaultSessionsRepository.kt | 4 ++-- .../github/droidkaigi/confsched/data/user/UserDataStore.kt | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/sessions/DefaultSessionsRepository.kt b/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/sessions/DefaultSessionsRepository.kt index de08a9630..cf4236e48 100644 --- a/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/sessions/DefaultSessionsRepository.kt +++ b/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/sessions/DefaultSessionsRepository.kt @@ -44,7 +44,7 @@ public class DefaultSessionsRepository( refreshSessionData() emitAll(sessionCacheDataStore.getTimetableStream()) }, - userDataStore.getFavoriteSessionStream(), + userDataStore.getFavoriteSessionStream, ) { timetable, favorites -> timetable.copy(bookmarks = favorites) } @@ -110,7 +110,7 @@ public class DefaultSessionsRepository( } }.safeCollectAsRetainedState(Timetable()) val favoriteSessions by remember { - userDataStore.getFavoriteSessionStream() + userDataStore.getFavoriteSessionStream }.safeCollectAsState() Logger.d { "DefaultSessionsRepository timetable() count=${timetable.timetableItems.size}" } diff --git a/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/user/UserDataStore.kt b/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/user/UserDataStore.kt index e6cd6ba44..7e3c81281 100644 --- a/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/user/UserDataStore.kt +++ b/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/user/UserDataStore.kt @@ -28,8 +28,8 @@ public class UserDataStore(private val dataStore: DataStore) { private val singletonCoroutineScope: CoroutineScope = CoroutineScope(Job()) - public fun getFavoriteSessionStream(): StateFlow> { - return dataStore.data + public val getFavoriteSessionStream: StateFlow> = + dataStore.data .catch { exception -> if (exception is IOException) { emit(emptyPreferences()) @@ -46,10 +46,9 @@ public class UserDataStore(private val dataStore: DataStore) { started = SharingStarted.WhileSubscribed(5_000), initialValue = persistentSetOf() ) - } public suspend fun toggleFavorite(id: TimetableItemId) { - val updatedFavorites = getFavoriteSessionStream().first().toMutableSet() + val updatedFavorites = getFavoriteSessionStream.first().toMutableSet() if (updatedFavorites.contains(id)) { updatedFavorites.remove(id) From 0cff4dc8bc940056fc6f9ad834ccf29be09a95e2 Mon Sep 17 00:00:00 2001 From: kyanro Date: Fri, 6 Sep 2024 21:54:51 +0900 Subject: [PATCH 6/8] apply detekt --auto-correct --- .../droidkaigi/confsched/compose/ComposeEffectErrorHandler.kt | 1 - .../io/github/droidkaigi/confsched/data/user/UserDataStore.kt | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/core/common/src/commonMain/kotlin/io/github/droidkaigi/confsched/compose/ComposeEffectErrorHandler.kt b/core/common/src/commonMain/kotlin/io/github/droidkaigi/confsched/compose/ComposeEffectErrorHandler.kt index 3c8828a82..c1b5a0a29 100644 --- a/core/common/src/commonMain/kotlin/io/github/droidkaigi/confsched/compose/ComposeEffectErrorHandler.kt +++ b/core/common/src/commonMain/kotlin/io/github/droidkaigi/confsched/compose/ComposeEffectErrorHandler.kt @@ -5,7 +5,6 @@ import androidx.compose.runtime.InternalComposeApi import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.ProvidedValue import androidx.compose.runtime.State -import androidx.compose.runtime.collectAsState import androidx.compose.runtime.currentComposer import androidx.compose.runtime.produceState import androidx.compose.runtime.staticCompositionLocalOf diff --git a/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/user/UserDataStore.kt b/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/user/UserDataStore.kt index 7e3c81281..d2c5e2dbd 100644 --- a/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/user/UserDataStore.kt +++ b/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/user/UserDataStore.kt @@ -44,7 +44,7 @@ public class UserDataStore(private val dataStore: DataStore) { }.stateIn( scope = singletonCoroutineScope, started = SharingStarted.WhileSubscribed(5_000), - initialValue = persistentSetOf() + initialValue = persistentSetOf(), ) public suspend fun toggleFavorite(id: TimetableItemId) { From 05b654b611edd9694c8dbdec482d429558d5e634 Mon Sep 17 00:00:00 2001 From: kyanro Date: Sat, 7 Sep 2024 03:40:54 +0900 Subject: [PATCH 7/8] move coroutineScope field to constructor parameter --- .../confsched/data/user/UserDataStoreModule.kt | 7 ++++++- .../droidkaigi/confsched/data/user/UserDataStore.kt | 10 +++++----- .../io/github/droidkaigi/confsched/data/DataModule.kt | 6 +++++- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/core/data/src/androidMain/kotlin/io/github/droidkaigi/confsched/data/user/UserDataStoreModule.kt b/core/data/src/androidMain/kotlin/io/github/droidkaigi/confsched/data/user/UserDataStoreModule.kt index 60a6d7cf5..3e493a62f 100644 --- a/core/data/src/androidMain/kotlin/io/github/droidkaigi/confsched/data/user/UserDataStoreModule.kt +++ b/core/data/src/androidMain/kotlin/io/github/droidkaigi/confsched/data/user/UserDataStoreModule.kt @@ -6,6 +6,8 @@ import dagger.Module import dagger.Provides import dagger.hilt.InstallIn import dagger.hilt.components.SingletonComponent +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Job import javax.inject.Singleton @InstallIn(SingletonComponent::class) @@ -18,6 +20,9 @@ public class UserDataStoreModule { @UserDataStoreQualifier dataStore: DataStore, ): UserDataStore { - return UserDataStore(dataStore) + return UserDataStore( + dataStore = dataStore, + coroutineScope = CoroutineScope(Job()), + ) } } diff --git a/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/user/UserDataStore.kt b/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/user/UserDataStore.kt index d2c5e2dbd..36085452a 100644 --- a/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/user/UserDataStore.kt +++ b/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/user/UserDataStore.kt @@ -11,7 +11,6 @@ import kotlinx.collections.immutable.PersistentSet import kotlinx.collections.immutable.persistentSetOf import kotlinx.collections.immutable.toPersistentSet import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Job import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.SharingStarted @@ -21,13 +20,14 @@ import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.stateIn -public class UserDataStore(private val dataStore: DataStore) { +public class UserDataStore( + private val dataStore: DataStore, + coroutineScope: CoroutineScope, +) { private val mutableIdToken = MutableStateFlow(null) public val idToken: StateFlow = mutableIdToken - private val singletonCoroutineScope: CoroutineScope = CoroutineScope(Job()) - public val getFavoriteSessionStream: StateFlow> = dataStore.data .catch { exception -> @@ -42,7 +42,7 @@ public class UserDataStore(private val dataStore: DataStore) { .map { TimetableItemId(it) } .toPersistentSet() }.stateIn( - scope = singletonCoroutineScope, + scope = coroutineScope, started = SharingStarted.WhileSubscribed(5_000), initialValue = persistentSetOf(), ) diff --git a/core/data/src/iosMain/kotlin/io/github/droidkaigi/confsched/data/DataModule.kt b/core/data/src/iosMain/kotlin/io/github/droidkaigi/confsched/data/DataModule.kt index eb86ed48b..f891beee5 100644 --- a/core/data/src/iosMain/kotlin/io/github/droidkaigi/confsched/data/DataModule.kt +++ b/core/data/src/iosMain/kotlin/io/github/droidkaigi/confsched/data/DataModule.kt @@ -46,6 +46,7 @@ import io.ktor.client.plugins.logging.Logging import kotlinx.cinterop.ExperimentalForeignApi import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job import kotlinx.coroutines.SupervisorJob import org.koin.core.module.Module import org.koin.core.module.dsl.singleOf @@ -109,7 +110,10 @@ public val dataModule: Module = module { requireNotNull(documentDirectory).path + "/confsched2024.preferences_pb" }, ) - UserDataStore(dataStore) + UserDataStore( + dataStore = dataStore, + coroutineScope = CoroutineScope(Job()), + ) } single { val dataStore = createDataStore( From 8c44363577a9452513cbba2b0bc4022ff357d453 Mon Sep 17 00:00:00 2001 From: kyanro Date: Sat, 7 Sep 2024 03:41:28 +0900 Subject: [PATCH 8/8] Add TestUserDataStoreModule --- .../testing/data/TestUserDataStoreModule.kt | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 core/testing/src/main/java/io/github/droidkaigi/confsched/testing/data/TestUserDataStoreModule.kt diff --git a/core/testing/src/main/java/io/github/droidkaigi/confsched/testing/data/TestUserDataStoreModule.kt b/core/testing/src/main/java/io/github/droidkaigi/confsched/testing/data/TestUserDataStoreModule.kt new file mode 100644 index 000000000..8e32d6bbe --- /dev/null +++ b/core/testing/src/main/java/io/github/droidkaigi/confsched/testing/data/TestUserDataStoreModule.kt @@ -0,0 +1,33 @@ +package io.github.droidkaigi.confsched.testing.data + +import androidx.datastore.core.DataStore +import androidx.datastore.preferences.core.Preferences +import dagger.Module +import dagger.Provides +import dagger.hilt.components.SingletonComponent +import dagger.hilt.testing.TestInstallIn +import io.github.droidkaigi.confsched.data.user.UserDataStore +import io.github.droidkaigi.confsched.data.user.UserDataStoreModule +import io.github.droidkaigi.confsched.data.user.UserDataStoreQualifier +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Job +import kotlinx.coroutines.test.TestDispatcher +import javax.inject.Singleton + +@Module +@TestInstallIn(components = [SingletonComponent::class], replaces = [UserDataStoreModule::class]) +class TestUserDataStoreModule { + + @Provides + @Singleton + public fun provideUserDataStore( + @UserDataStoreQualifier + dataStore: DataStore, + testDispatcher: TestDispatcher, + ): UserDataStore { + return UserDataStore( + dataStore = dataStore, + coroutineScope = CoroutineScope(testDispatcher + Job()), + ) + } +}