From dbcdde34f8c22b8e5d1d2ab0f5aa8693880fd69c Mon Sep 17 00:00:00 2001 From: takahirom Date: Sun, 7 Jul 2024 13:22:00 +0900 Subject: [PATCH] Fix naming of safeCollectAsState to safeCollectAsRetainedState --- .../confsched/compose/ComposeEffectErrorHandler.kt | 12 +++++++----- .../contributors/DefaultContributorsRepository.kt | 4 ++-- .../data/eventmap/DefaultEventMapRepository.kt | 4 ++-- .../data/sessions/DefaultSessionsRepository.kt | 6 +++--- .../droidkaigi/confsched/main/MainScreenPresenter.kt | 4 ++-- 5 files changed, 16 insertions(+), 14 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 8a24aed32..e4dfe2a55 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 @@ -1,5 +1,6 @@ package io.github.droidkaigi.confsched.compose +import android.annotation.SuppressLint import androidx.compose.runtime.Composable import androidx.compose.runtime.InternalComposeApi import androidx.compose.runtime.LaunchedEffect @@ -56,7 +57,7 @@ fun SafeLaunchedEffect(key: Any?, block: suspend CoroutineScope.() -> Unit) { } @Composable -fun Flow.safeCollectAsState( +fun Flow.safeCollectAsRetainedState( initial: R, context: CoroutineContext = EmptyCoroutineContext, ): State { @@ -65,10 +66,10 @@ fun Flow.safeCollectAsState( return produceRetainedState(initial, this, context) { try { if (context == EmptyCoroutineContext) { - collect { value = it as R } + collect { value = it } } else { withContext(context) { - collect { value = it as R } + collect { value = it } } } } catch (e: Throwable) { @@ -77,9 +78,10 @@ fun Flow.safeCollectAsState( } } +@SuppressLint("StateFlowValueCalledInComposition") @Composable -fun StateFlow.safeCollectAsState( +fun StateFlow.safeCollectAsRetainedState( context: CoroutineContext = EmptyCoroutineContext, ): State { - return safeCollectAsState(value, context) + return safeCollectAsRetainedState(value, context) } diff --git a/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/contributors/DefaultContributorsRepository.kt b/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/contributors/DefaultContributorsRepository.kt index 86801a4ec..e6e377f2f 100644 --- a/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/contributors/DefaultContributorsRepository.kt +++ b/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/contributors/DefaultContributorsRepository.kt @@ -3,7 +3,7 @@ package io.github.droidkaigi.confsched.data.contributors import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import io.github.droidkaigi.confsched.compose.SafeLaunchedEffect -import io.github.droidkaigi.confsched.compose.safeCollectAsState +import io.github.droidkaigi.confsched.compose.safeCollectAsRetainedState import io.github.droidkaigi.confsched.model.Contributor import io.github.droidkaigi.confsched.model.ContributorsRepository import kotlinx.collections.immutable.PersistentList @@ -19,7 +19,7 @@ public class DefaultContributorsRepository( @Composable override fun contributors(): PersistentList { - val contributors by contributorsStateFlow.safeCollectAsState() + val contributors by contributorsStateFlow.safeCollectAsRetainedState() SafeLaunchedEffect(Unit) { if (contributors.isEmpty()) { refresh() diff --git a/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/eventmap/DefaultEventMapRepository.kt b/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/eventmap/DefaultEventMapRepository.kt index f83847fe5..a993b907e 100644 --- a/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/eventmap/DefaultEventMapRepository.kt +++ b/core/data/src/commonMain/kotlin/io/github/droidkaigi/confsched/data/eventmap/DefaultEventMapRepository.kt @@ -3,7 +3,7 @@ package io.github.droidkaigi.confsched.data.eventmap import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import io.github.droidkaigi.confsched.compose.SafeLaunchedEffect -import io.github.droidkaigi.confsched.compose.safeCollectAsState +import io.github.droidkaigi.confsched.compose.safeCollectAsRetainedState import io.github.droidkaigi.confsched.model.EventMapEvent import io.github.droidkaigi.confsched.model.EventMapRepository import kotlinx.collections.immutable.PersistentList @@ -19,7 +19,7 @@ public class DefaultEventMapRepository( @Composable override fun eventMapEvents(): PersistentList { - val eventMap by eventMapStateFlow.safeCollectAsState() + val eventMap by eventMapStateFlow.safeCollectAsRetainedState() SafeLaunchedEffect(Unit) { if (eventMap.isEmpty()) { refresh() 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 18e2e1350..da00a92c9 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 @@ -8,7 +8,7 @@ import androidx.compose.runtime.rememberUpdatedState import androidx.compose.runtime.setValue import co.touchlab.kermit.Logger import io.github.droidkaigi.confsched.compose.SafeLaunchedEffect -import io.github.droidkaigi.confsched.compose.safeCollectAsState +import io.github.droidkaigi.confsched.compose.safeCollectAsRetainedState import io.github.droidkaigi.confsched.data.user.UserDataStore import io.github.droidkaigi.confsched.model.SessionsRepository import io.github.droidkaigi.confsched.model.Timetable @@ -82,10 +82,10 @@ public class DefaultSessionsRepository( sessionCacheDataStore.save(sessionsApi.sessionsAllResponse()) emitAll(sessionCacheDataStore.getTimetableStream()) } - }.safeCollectAsState(Timetable()) + }.safeCollectAsRetainedState(Timetable()) val favoriteSessions by remember { userDataStore.getFavoriteSessionStream() - }.safeCollectAsState(persistentSetOf()) + }.safeCollectAsRetainedState(persistentSetOf()) Logger.d { "DefaultSessionsRepository timetable() count=${timetable.timetableItems.size}" } return timetable.copy(bookmarks = favoriteSessions) diff --git a/feature/main/src/commonMain/kotlin/io/github/droidkaigi/confsched/main/MainScreenPresenter.kt b/feature/main/src/commonMain/kotlin/io/github/droidkaigi/confsched/main/MainScreenPresenter.kt index 145085092..6f7fe5d83 100644 --- a/feature/main/src/commonMain/kotlin/io/github/droidkaigi/confsched/main/MainScreenPresenter.kt +++ b/feature/main/src/commonMain/kotlin/io/github/droidkaigi/confsched/main/MainScreenPresenter.kt @@ -2,7 +2,7 @@ package io.github.droidkaigi.confsched.main import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue -import io.github.droidkaigi.confsched.compose.safeCollectAsState +import io.github.droidkaigi.confsched.compose.safeCollectAsRetainedState import io.github.droidkaigi.confsched.model.AchievementRepository import io.github.droidkaigi.confsched.model.localAchievementRepository import io.github.droidkaigi.confsched.ui.providePresenterDefaults @@ -18,7 +18,7 @@ fun mainScreenPresenter( ): MainScreenUiState = providePresenterDefaults { userMessageStateHolder -> val isAchievementsEnabled: Boolean by achievementRepository .getAchievementEnabledStream() - .safeCollectAsState( + .safeCollectAsRetainedState( initial = false, ) MainScreenUiState(