diff --git a/composeApp/src/commonMain/kotlin/com/nuvio/app/features/collection/CollectionRepository.kt b/composeApp/src/commonMain/kotlin/com/nuvio/app/features/collection/CollectionRepository.kt index 9d57d011..0e9553ae 100644 --- a/composeApp/src/commonMain/kotlin/com/nuvio/app/features/collection/CollectionRepository.kt +++ b/composeApp/src/commonMain/kotlin/com/nuvio/app/features/collection/CollectionRepository.kt @@ -4,7 +4,10 @@ import co.touchlab.kermit.Logger import com.nuvio.app.features.addons.AddonRepository import com.nuvio.app.features.addons.ManagedAddon import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.MutableSharedFlow +import kotlinx.coroutines.flow.SharedFlow import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.runBlocking import kotlinx.serialization.decodeFromString @@ -33,6 +36,8 @@ object CollectionRepository { private val _collections = MutableStateFlow>(emptyList()) val collections: StateFlow> = _collections.asStateFlow() + private val _localChangeEvents = MutableSharedFlow(extraBufferCapacity = 1) + internal val localChangeEvents: SharedFlow = _localChangeEvents.asSharedFlow() private var rawCollectionsJson: JsonElement = JsonArray(emptyList()) private var hasLoaded = false @@ -244,16 +249,19 @@ object CollectionRepository { internal fun applyFromRemote(collections: List, rawJson: JsonElement) { rawCollectionsJson = rawJson _collections.value = collections - persist() + persist(sync = false) } private fun ensureLoaded() { if (!hasLoaded) initialize() } - private fun persist() { + private fun persist(sync: Boolean = true) { runCatching { CollectionStorage.savePayload(mergedCollectionsJson().toString()) + if (sync) { + _localChangeEvents.tryEmit(Unit) + } }.onFailure { e -> log.e(e) { "Failed to persist collections" } } diff --git a/composeApp/src/commonMain/kotlin/com/nuvio/app/features/collection/CollectionSyncService.kt b/composeApp/src/commonMain/kotlin/com/nuvio/app/features/collection/CollectionSyncService.kt index 1ec14547..de0931ec 100644 --- a/composeApp/src/commonMain/kotlin/com/nuvio/app/features/collection/CollectionSyncService.kt +++ b/composeApp/src/commonMain/kotlin/com/nuvio/app/features/collection/CollectionSyncService.kt @@ -15,8 +15,6 @@ import kotlinx.coroutines.Job import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.delay import kotlinx.coroutines.flow.debounce -import kotlinx.coroutines.flow.distinctUntilChanged -import kotlinx.coroutines.flow.drop import kotlinx.coroutines.launch import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonArray @@ -125,9 +123,7 @@ object CollectionSyncService { @OptIn(FlowPreview::class) private fun observeLocalChangesAndPush() { observeJob = scope.launch { - CollectionRepository.collections - .drop(1) - .distinctUntilChanged() + CollectionRepository.localChangeEvents .debounce(PUSH_DEBOUNCE_MS) .collect { if (isSyncingFromRemote) return@collect