diff --git a/app/src/main/java/com/hippo/ehviewer/FavouriteStatusRouter.kt b/app/src/main/java/com/hippo/ehviewer/FavouriteStatusRouter.kt index d15cd4d50..a05382a5c 100644 --- a/app/src/main/java/com/hippo/ehviewer/FavouriteStatusRouter.kt +++ b/app/src/main/java/com/hippo/ehviewer/FavouriteStatusRouter.kt @@ -19,11 +19,8 @@ import com.hippo.ehviewer.client.data.GalleryInfo import com.hippo.ehviewer.yorozuya.IntIdGenerator import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.channels.ProducerScope -import kotlinx.coroutines.channels.awaitClose -import kotlinx.coroutines.flow.SharingStarted -import kotlinx.coroutines.flow.callbackFlow -import kotlinx.coroutines.flow.shareIn +import kotlinx.coroutines.flow.MutableSharedFlow +import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.flow.transform import kotlinx.coroutines.launch @@ -48,17 +45,12 @@ object FavouriteStatusRouter { info.favoriteSlot = slot } } - collector.trySend(gid to slot) + _globalFlow.tryEmit(gid to slot) } private val listenerScope = CoroutineScope(Dispatchers.IO) - private lateinit var collector: ProducerScope> - - val globalFlow = callbackFlow> { - collector = this - awaitClose { error("Unreachable!!!") } - }.shareIn(listenerScope, SharingStarted.Eagerly).apply { + private val _globalFlow = MutableSharedFlow>(extraBufferCapacity = 1).apply { listenerScope.launch { collect { (gid, slot) -> EhDB.modifyHistoryInfoFavslotNonRefresh(gid, slot) @@ -66,5 +58,7 @@ object FavouriteStatusRouter { } } + val globalFlow = _globalFlow.asSharedFlow() + fun stateFlow(targetGid: Long) = globalFlow.transform { (gid, slot) -> if (targetGid == gid) emit(slot) } }