Skip to content

Commit

Permalink
fix blink favorite icon
Browse files Browse the repository at this point in the history
  • Loading branch information
kyanro committed Sep 3, 2024
1 parent c883317 commit d55402c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -90,6 +91,9 @@ public class DefaultSessionsRepository(
@Composable
public override fun timetable(): Timetable {
var first by remember { mutableStateOf(true) }
var favoriteSessions: PersistentSet<TimetableItemId> by remember {
mutableStateOf(userDataStore.getFavoriteSessionMemoryCacheOrNull ?: persistentSetOf())
}
SafeLaunchedEffect(first) {
if (first) {
Logger.d("DefaultSessionsRepository onStart getTimetableStream()")
Expand All @@ -99,6 +103,12 @@ public class DefaultSessionsRepository(
}
}

SafeLaunchedEffect(Unit) {
userDataStore.getFavoriteSessionStream().collect {
favoriteSessions = it
}
}

val timetable by remember {
sessionCacheDataStore.getTimetableStream().catch { e ->
Logger.d(
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ public class UserDataStore(private val dataStore: DataStore<Preferences>) {
private val mutableIdToken = MutableStateFlow<String?>(null)
public val idToken: StateFlow<String?> = mutableIdToken

private var favoriteSessionMemoryCache: PersistentSet<TimetableItemId>? = null
public val getFavoriteSessionMemoryCacheOrNull: PersistentSet<TimetableItemId>?
get() = favoriteSessionMemoryCache

public fun getFavoriteSessionStream(): Flow<PersistentSet<TimetableItemId>> {
return dataStore.data
.catch { exception ->
if (exception is IOException) {
favoriteSessionMemoryCache = null
emit(emptyPreferences())
} else {
throw exception
Expand All @@ -33,7 +38,9 @@ public class UserDataStore(private val dataStore: DataStore<Preferences>) {
.map { preferences: Preferences ->
(preferences[KEY_FAVORITE_SESSION_IDS]?.split(",") ?: listOf())
.map { TimetableItemId(it) }
.toPersistentSet()
.toPersistentSet().also {
favoriteSessionMemoryCache = it
}
}
}

Expand Down

0 comments on commit d55402c

Please sign in to comment.