Quickly branch pagination

This commit is contained in:
ganfra 2022-11-07 18:41:28 +01:00
parent 8f3233e450
commit 9dfd471907
6 changed files with 91 additions and 47 deletions

View file

@ -0,0 +1,2 @@
package io.element.android.x.core.data.flow

View file

@ -3,10 +3,13 @@ package io.element.android.x.matrix.room
import io.element.android.x.core.data.CoroutineDispatchers
import io.element.android.x.matrix.core.RoomId
import io.element.android.x.matrix.timeline.MatrixTimeline
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.withContext
import org.matrix.rustcomponents.sdk.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart
import org.matrix.rustcomponents.sdk.Room
import org.matrix.rustcomponents.sdk.SlidingSyncRoom
import org.matrix.rustcomponents.sdk.UpdateSummary
class MatrixRoom(
private val slidingSyncUpdateFlow: Flow<UpdateSummary>,
@ -15,7 +18,6 @@ class MatrixRoom(
private val coroutineDispatchers: CoroutineDispatchers,
) {
private val paginationOutcome = MutableStateFlow(PaginationOutcome(true))
fun syncUpdateFlow(): Flow<Unit> {
return slidingSyncUpdateFlow
.filter {
@ -26,11 +28,7 @@ class MatrixRoom(
}
fun timeline(): MatrixTimeline {
return MatrixTimeline(this)
}
internal fun timelineDiff(): Flow<TimelineDiff> {
return room.timelineDiff()
return MatrixTimeline(this, room, coroutineDispatchers)
}
val roomId = RoomId(room.id())
@ -55,18 +53,5 @@ class MatrixRoom(
return room.avatarUrl()
}
fun addTimelineListener(timelineListener: TimelineListener) {
room.addTimelineListener(timelineListener)
}
suspend fun paginateBackwards(count: Int): Result<Unit> = withContext(coroutineDispatchers.io) {
if (!paginationOutcome.value.moreMessages) {
return@withContext Result.failure(IllegalStateException("no more message"))
}
runCatching {
paginationOutcome.value = room.paginateBackwards(count.toUShort())
}
}
}

View file

@ -1,17 +1,20 @@
package io.element.android.x.matrix.timeline
import io.element.android.x.core.data.CoroutineDispatchers
import io.element.android.x.matrix.core.EventId
import io.element.android.x.matrix.room.MatrixRoom
import io.element.android.x.matrix.room.timelineDiff
import kotlinx.coroutines.flow.*
import org.matrix.rustcomponents.sdk.TimelineChange
import org.matrix.rustcomponents.sdk.TimelineDiff
import kotlinx.coroutines.withContext
import org.matrix.rustcomponents.sdk.*
import timber.log.Timber
import java.util.*
class MatrixTimeline(
private val room: MatrixRoom,
private val matrixRoom: MatrixRoom,
private val room: Room,
private val coroutineDispatchers: CoroutineDispatchers,
) {
interface Callback {
fun onUpdatedTimelineItem(eventId: EventId)
fun onStartedBackPaginating()
@ -20,6 +23,7 @@ class MatrixTimeline(
var callback: Callback? = null
private val paginationOutcome = MutableStateFlow(PaginationOutcome(true))
private val timelineItems: MutableStateFlow<List<MatrixTimelineItem>> =
MutableStateFlow(emptyList())
@ -30,6 +34,12 @@ class MatrixTimeline(
}
}
val hasMoreToLoad: Boolean
get() {
return paginationOutcome.value.moreMessages
}
private fun diffFlow(): Flow<Unit> {
return room.timelineDiff()
.onEach { timelineDiff ->
@ -78,28 +88,31 @@ class MatrixTimeline(
}
}
suspend fun paginateBackwards(count: Int): Result<Unit> = withContext(coroutineDispatchers.io) {
if (!paginationOutcome.value.moreMessages) {
return@withContext Result.failure(IllegalStateException("no more message"))
}
runCatching {
paginationOutcome.value = room.paginateBackwards(count.toUShort())
}
}
private fun updateTimelineItems(block: MutableList<MatrixTimelineItem>.() -> Unit) {
val mutableTimelineItems = timelineItems.value.toMutableList()
block(mutableTimelineItems)
timelineItems.value = mutableTimelineItems
}
suspend fun processItemAppearance(itemId: String) {
fun addListener(timelineListener: TimelineListener) {
room.addTimelineListener(timelineListener)
}
suspend fun processItemDisappearance(itemId: String) {
}
suspend fun paginateBackwards(count: Int): Result<Unit> {
return room.paginateBackwards(count)
fun dispose(){
room.removeTimeline()
}
suspend fun sendMessage(message: String): Result<Unit> {
return Result.success(Unit)
}
}