Skip to content

Commit

Permalink
💄 Enable device list support for responsive UI (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
guiyanakuang authored Feb 5, 2024
1 parent 5d66b28 commit 1e6aabe
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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<SyncRuntimeInfo>
fun getAllSyncRuntimeInfos(): RealmResults<SyncRuntimeInfo>

fun getSyncRuntimeInfo(appInstanceId: String): SyncRuntimeInfo?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PageViewContext>) {
Expand All @@ -52,11 +52,30 @@ fun DevicesView(currentPageViewContext: MutableState<PageViewContext>) {
fun DevicesListView(currentPageViewContext: MutableState<PageViewContext>) {
val current = LocalKoinApplication.current
val syncRuntimeInfoDao = current.koin.get<SyncRuntimeInfoDao>()
val syncRuntimeInfosFlow = syncRuntimeInfoDao.getAllSyncRuntimeInfos().asFlow()
var rememberSyncRuntimeInfos by remember { mutableStateOf(emptyList<SyncRuntimeInfo>()) }
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<SyncRuntimeInfo> ->
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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SyncRuntimeInfo> {
override fun getAllSyncRuntimeInfos(): RealmResults<SyncRuntimeInfo> {
return realm.query(SyncRuntimeInfo::class).sort("createTime", Sort.DESCENDING).find()
}

Expand Down

0 comments on commit 1e6aabe

Please sign in to comment.