Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix blink favorite icon #945

Closed
wants to merge 14 commits into from
Closed
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
}
}
kyanro marked this conversation as resolved.
Show resolved Hide resolved

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
kyanro marked this conversation as resolved.
Show resolved Hide resolved

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
Loading