diff --git a/composeApp/src/commonMain/kotlin/com/clipevery/dao/sync/SyncRuntimeInfoDao.kt b/composeApp/src/commonMain/kotlin/com/clipevery/dao/sync/SyncRuntimeInfoDao.kt index 3d4f81196..fed46ae8f 100644 --- a/composeApp/src/commonMain/kotlin/com/clipevery/dao/sync/SyncRuntimeInfoDao.kt +++ b/composeApp/src/commonMain/kotlin/com/clipevery/dao/sync/SyncRuntimeInfoDao.kt @@ -1,9 +1,10 @@ package com.clipevery.dao.sync import com.clipevery.dto.sync.SyncInfo +import io.realm.kotlin.query.RealmResults interface SyncRuntimeInfoDao { - fun getAllSyncRuntimeInfos(): List + fun getAllSyncRuntimeInfos(): RealmResults fun getSyncRuntimeInfo(appInstanceId: String): SyncRuntimeInfo? diff --git a/composeApp/src/commonMain/kotlin/com/clipevery/ui/devices/DevicesView.kt b/composeApp/src/commonMain/kotlin/com/clipevery/ui/devices/DevicesView.kt index 7319baf26..fed70a182 100644 --- a/composeApp/src/commonMain/kotlin/com/clipevery/ui/devices/DevicesView.kt +++ b/composeApp/src/commonMain/kotlin/com/clipevery/ui/devices/DevicesView.kt @@ -37,8 +37,8 @@ import com.clipevery.net.CheckAction import com.clipevery.net.DeviceRefresher import com.clipevery.ui.PageViewContext import com.clipevery.ui.base.ClipIconButton -import kotlinx.coroutines.flow.asFlow -import kotlinx.coroutines.flow.toList +import io.realm.kotlin.notifications.ResultsChange +import io.realm.kotlin.notifications.UpdatedResults @Composable fun DevicesView(currentPageViewContext: MutableState) { @@ -52,11 +52,30 @@ fun DevicesView(currentPageViewContext: MutableState) { fun DevicesListView(currentPageViewContext: MutableState) { val current = LocalKoinApplication.current val syncRuntimeInfoDao = current.koin.get() - val syncRuntimeInfosFlow = syncRuntimeInfoDao.getAllSyncRuntimeInfos().asFlow() - var rememberSyncRuntimeInfos by remember { mutableStateOf(emptyList()) } + val syncRuntimeInfos = syncRuntimeInfoDao.getAllSyncRuntimeInfos() + var rememberSyncRuntimeInfos by remember { mutableStateOf(syncRuntimeInfos) } - LaunchedEffect(syncRuntimeInfosFlow) { - rememberSyncRuntimeInfos = syncRuntimeInfosFlow.toList() + LaunchedEffect(Unit) { + val syncRuntimeInfosFlow = syncRuntimeInfos.asFlow() + syncRuntimeInfosFlow.collect { changes: ResultsChange -> + when (changes) { + is UpdatedResults -> { + changes.insertions // indexes of inserted objects + changes.insertionRanges // ranges of inserted objects + changes.changes // indexes of modified objects + changes.changeRanges // ranges of modified objects + changes.deletions // indexes of deleted objects + changes.deletionRanges // ranges of deleted objects + changes.list // the full collection of objects + + rememberSyncRuntimeInfos = syncRuntimeInfoDao.getAllSyncRuntimeInfos() + } + else -> { + // types other than UpdatedResults are not changes -- ignore them + } + } + + } } Column { for ((index, syncRuntimeInfo) in rememberSyncRuntimeInfos.withIndex()) { diff --git a/composeApp/src/desktopMain/kotlin/com/clipevery/dao/sync/SyncRuntimeInfoRealm.kt b/composeApp/src/desktopMain/kotlin/com/clipevery/dao/sync/SyncRuntimeInfoRealm.kt index 5e4b0df13..6dfa0c4df 100644 --- a/composeApp/src/desktopMain/kotlin/com/clipevery/dao/sync/SyncRuntimeInfoRealm.kt +++ b/composeApp/src/desktopMain/kotlin/com/clipevery/dao/sync/SyncRuntimeInfoRealm.kt @@ -3,12 +3,13 @@ package com.clipevery.dao.sync import com.clipevery.dto.sync.SyncInfo import io.realm.kotlin.Realm import io.realm.kotlin.ext.toRealmList +import io.realm.kotlin.query.RealmResults import io.realm.kotlin.query.Sort import io.realm.kotlin.types.RealmInstant class SyncRuntimeInfoRealm(private val realm: Realm): SyncRuntimeInfoDao { - override fun getAllSyncRuntimeInfos(): List { + override fun getAllSyncRuntimeInfos(): RealmResults { return realm.query(SyncRuntimeInfo::class).sort("createTime", Sort.DESCENDING).find() }