Skip to content

Commit

Permalink
Merge pull request #99 from DroidKaigi/takahirom/refactor-naming/2024…
Browse files Browse the repository at this point in the history
…-07-07

Fix naming of safeCollectAsState to safeCollectAsRetainedState
  • Loading branch information
takahirom authored Jul 7, 2024
2 parents ca180ba + dbcdde3 commit 451b3ce
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -56,7 +57,7 @@ fun SafeLaunchedEffect(key: Any?, block: suspend CoroutineScope.() -> Unit) {
}

@Composable
fun <T : R, R> Flow<T>.safeCollectAsState(
fun <T : R, R> Flow<T>.safeCollectAsRetainedState(
initial: R,
context: CoroutineContext = EmptyCoroutineContext,
): State<R> {
Expand All @@ -65,10 +66,10 @@ fun <T : R, R> Flow<T>.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) {
Expand All @@ -77,9 +78,10 @@ fun <T : R, R> Flow<T>.safeCollectAsState(
}
}

@SuppressLint("StateFlowValueCalledInComposition")
@Composable
fun <T : R, R> StateFlow<T>.safeCollectAsState(
fun <T : R, R> StateFlow<T>.safeCollectAsRetainedState(
context: CoroutineContext = EmptyCoroutineContext,
): State<R> {
return safeCollectAsState(value, context)
return safeCollectAsRetainedState(value, context)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,7 +19,7 @@ public class DefaultContributorsRepository(

@Composable
override fun contributors(): PersistentList<Contributor> {
val contributors by contributorsStateFlow.safeCollectAsState()
val contributors by contributorsStateFlow.safeCollectAsRetainedState()
SafeLaunchedEffect(Unit) {
if (contributors.isEmpty()) {
refresh()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,7 +19,7 @@ public class DefaultEventMapRepository(

@Composable
override fun eventMapEvents(): PersistentList<EventMapEvent> {
val eventMap by eventMapStateFlow.safeCollectAsState()
val eventMap by eventMapStateFlow.safeCollectAsRetainedState()
SafeLaunchedEffect(Unit) {
if (eventMap.isEmpty()) {
refresh()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,7 +18,7 @@ fun mainScreenPresenter(
): MainScreenUiState = providePresenterDefaults { userMessageStateHolder ->
val isAchievementsEnabled: Boolean by achievementRepository
.getAchievementEnabledStream()
.safeCollectAsState(
.safeCollectAsRetainedState(
initial = false,
)
MainScreenUiState(
Expand Down

0 comments on commit 451b3ce

Please sign in to comment.