Skip to content

Commit

Permalink
✨ Paste project preview UI supports responsive (#374)
Browse files Browse the repository at this point in the history
  • Loading branch information
guiyanakuang authored Feb 24, 2024
1 parent 2ca3872 commit 34cf44f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ interface ClipDao {
fun getClipData(appInstanceId: String? = null,
limit: Int): RealmResults<ClipData>

fun getClipData(appInstanceId: String?,
limit: Int,
createTime: RealmInstant,
excludeClipId: List<Int>): RealmResults<ClipData>
fun getClipDataGreaterThan(appInstanceId: String? = null,
createTime: RealmInstant): RealmResults<ClipData>

fun getClipDataLessThan(appInstanceId: String? = null,
limit: Int,
createTime: RealmInstant): RealmResults<ClipData>
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,73 @@ import androidx.compose.ui.unit.dp
import com.clipevery.LocalKoinApplication
import com.clipevery.dao.clip.ClipDao
import com.clipevery.dao.clip.ClipData
import io.realm.kotlin.notifications.ResultsChange
import io.realm.kotlin.notifications.UpdatedResults
import io.realm.kotlin.query.RealmResults
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

val clipDataComparator = compareByDescending<ClipData> { it.createTime }
.thenBy{ it.appInstanceId }
.thenBy { it.clipId }

@Composable
fun ClipPreviewsView() {
val current = LocalKoinApplication.current
val clipDao = current.koin.get<ClipDao>()

val clipDataList: List<ClipData> = clipDao.getClipData(limit = 20)
val clipDataList: RealmResults<ClipData> = clipDao.getClipData(limit = 20)

val listState = rememberLazyListState()
var isScrolling by remember { mutableStateOf(false) }
var scrollJob: Job? by remember { mutableStateOf(null) }
val coroutineScope = rememberCoroutineScope()
val rememberClipDataList = remember { mutableStateListOf<ClipData>().apply {
addAll(clipDataList)
addAll(clipDataList.sortedWith(clipDataComparator))
} }

LaunchedEffect(Unit) {
val clipDatasFlow = clipDao.getClipData(limit = 20).asFlow()
clipDatasFlow.collect { changes: ResultsChange<ClipData> ->
when (changes) {
is UpdatedResults -> {
changes.insertions
changes.insertionRanges
val firstClipData: ClipData? = rememberClipDataList.firstOrNull()

firstClipData?.let {
val newClipDataList = clipDao.getClipDataGreaterThan(createTime = it.createTime)
for (clipData in newClipDataList.reversed()) {
if (clipDataComparator.compare(clipData, firstClipData) < 0) {
rememberClipDataList.add(0, clipData)
}
}
} ?: run {
val newClipDataList = clipDao.getClipData(limit = 20)
rememberClipDataList.addAll(newClipDataList.sortedWith(clipDataComparator))
}
}
else -> {
// types other than UpdatedResults are not changes -- ignore them
}
}
}
}

LaunchedEffect(listState) {
snapshotFlow { listState.layoutInfo.visibleItemsInfo }
.collect { visibleItems ->
if (visibleItems.isNotEmpty() && visibleItems.last().index == rememberClipDataList.size - 1) {
// todo loadMoreItems()
val lastClipData: ClipData? = rememberClipDataList.lastOrNull()
lastClipData?.let {
val newClipDataList = clipDao.getClipDataLessThan(createTime = it.createTime, limit = 20)
for (clipData in newClipDataList) {
if (clipDataComparator.compare(clipData, lastClipData) > 0) {
rememberClipDataList.add(clipData)
}
}
}
}
isScrolling = true
scrollJob?.cancel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,26 @@ class ClipRealm(private val realm: Realm): ClipDao {
return query.sort("createTime", Sort.DESCENDING).limit(limit).find()
}

override fun getClipData(
override fun getClipDataGreaterThan(
appInstanceId: String?,
createTime: RealmInstant
): RealmResults<ClipData> {
var query = appInstanceId?.let {
realm.query(ClipData::class).query("appInstanceId == $0", appInstanceId)
} ?: realm.query(ClipData::class)
query = query.query("createTime >= $0", createTime)
return query.sort("createTime", Sort.DESCENDING).find()
}

override fun getClipDataLessThan(
appInstanceId: String?,
limit: Int,
createTime: RealmInstant,
excludeClipId: List<Int>
createTime: RealmInstant
): RealmResults<ClipData> {
var query = appInstanceId?.let {
realm.query(ClipData::class).query("appInstanceId == $0", appInstanceId)
} ?: realm.query(ClipData::class)
query = query.query("clipId not in $0", excludeClipId)
query = query.query("createTime <= $0", createTime)
return query.sort("createTime", Sort.DESCENDING).limit(limit).find()
}
}

0 comments on commit 34cf44f

Please sign in to comment.