Timeline: refactor a bit
This commit is contained in:
parent
fa48c29486
commit
8c66924be9
3 changed files with 35 additions and 31 deletions
|
|
@ -108,13 +108,13 @@ class RustMatrixRoom(
|
|||
timelineLimit = null
|
||||
)
|
||||
roomListItem.subscribe(settings)
|
||||
innerRoom.timelineDiffFlow { initialList ->
|
||||
timeline.postItems(initialList)
|
||||
}.onEach {
|
||||
syncUpdateFlow.value = systemClock.epochMillis()
|
||||
timeline.postDiff(it)
|
||||
}.launchIn(roomCoroutineScope)
|
||||
roomCoroutineScope.launch {
|
||||
roomCoroutineScope.launch(coroutineDispatchers.computation) {
|
||||
innerRoom.timelineDiffFlow { initialList ->
|
||||
timeline.postItems(initialList)
|
||||
}.onEach {
|
||||
syncUpdateFlow.value = systemClock.epochMillis()
|
||||
timeline.postDiff(it)
|
||||
}.launchIn(this)
|
||||
fetchMembers()
|
||||
}
|
||||
isInit.value = true
|
||||
|
|
@ -122,7 +122,7 @@ class RustMatrixRoom(
|
|||
}
|
||||
|
||||
override fun close() {
|
||||
if(isInit.value) {
|
||||
if (isInit.value) {
|
||||
isInit.value = false
|
||||
roomCoroutineScope.cancel()
|
||||
roomListItem.unsubscribe()
|
||||
|
|
@ -360,7 +360,6 @@ class RustMatrixRoom(
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
private suspend fun fetchMembers() = withContext(coroutineDispatchers.io) {
|
||||
runCatching {
|
||||
innerRoom.fetchMembers()
|
||||
|
|
|
|||
|
|
@ -19,11 +19,9 @@ package io.element.android.libraries.matrix.impl.timeline
|
|||
import io.element.android.libraries.matrix.api.timeline.MatrixTimeline
|
||||
import io.element.android.libraries.matrix.api.timeline.MatrixTimelineItem
|
||||
import io.element.android.libraries.matrix.api.timeline.item.virtual.VirtualTimelineItem
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import org.matrix.rustcomponents.sdk.TimelineChange
|
||||
import org.matrix.rustcomponents.sdk.TimelineDiff
|
||||
import org.matrix.rustcomponents.sdk.TimelineItem
|
||||
|
|
@ -31,20 +29,23 @@ import org.matrix.rustcomponents.sdk.TimelineItem
|
|||
internal class MatrixTimelineDiffProcessor(
|
||||
private val paginationState: MutableStateFlow<MatrixTimeline.PaginationState>,
|
||||
private val timelineItems: MutableStateFlow<List<MatrixTimelineItem>>,
|
||||
private val coroutineScope: CoroutineScope,
|
||||
private val diffDispatcher: CoroutineDispatcher,
|
||||
private val timelineItemFactory: MatrixTimelineItemMapper,
|
||||
) {
|
||||
|
||||
fun postDiff(diff: TimelineDiff) {
|
||||
coroutineScope.launch {
|
||||
updateTimelineItems {
|
||||
applyDiff(diff)
|
||||
}
|
||||
when (val firstItem = timelineItems.value.firstOrNull()) {
|
||||
is MatrixTimelineItem.Virtual -> updateBackPaginationState(firstItem.virtual)
|
||||
else -> updateBackPaginationState(null)
|
||||
}
|
||||
private val mutex = Mutex()
|
||||
|
||||
suspend fun postItems(items: List<TimelineItem>) {
|
||||
updateTimelineItems {
|
||||
val mappedItems = items.map { it.asMatrixTimelineItem() }
|
||||
addAll(mappedItems)
|
||||
updateBackPaginationState()
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun postDiff(diff: TimelineDiff) {
|
||||
updateTimelineItems {
|
||||
applyDiff(diff)
|
||||
updateBackPaginationState()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -68,7 +69,7 @@ internal class MatrixTimelineDiffProcessor(
|
|||
}
|
||||
|
||||
private suspend fun updateTimelineItems(block: MutableList<MatrixTimelineItem>.() -> Unit) =
|
||||
withContext(diffDispatcher) {
|
||||
mutex.withLock {
|
||||
val mutableTimelineItems = timelineItems.value.toMutableList()
|
||||
block(mutableTimelineItems)
|
||||
timelineItems.value = mutableTimelineItems
|
||||
|
|
@ -119,8 +120,14 @@ internal class MatrixTimelineDiffProcessor(
|
|||
}
|
||||
}
|
||||
|
||||
private fun List<MatrixTimelineItem>.updateBackPaginationState() {
|
||||
when (val firstItem = firstOrNull()) {
|
||||
is MatrixTimelineItem.Virtual -> updateBackPaginationState(firstItem.virtual)
|
||||
else -> updateBackPaginationState(null)
|
||||
}
|
||||
}
|
||||
|
||||
private fun TimelineItem.asMatrixTimelineItem(): MatrixTimelineItem {
|
||||
return timelineItemFactory.map(this)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -66,8 +66,6 @@ class RustMatrixTimeline(
|
|||
private val timelineDiffProcessor = MatrixTimelineDiffProcessor(
|
||||
paginationState = paginationState,
|
||||
timelineItems = timelineItems,
|
||||
coroutineScope = roomCoroutineScope,
|
||||
diffDispatcher = coroutineDispatchers.diffUpdateDispatcher,
|
||||
timelineItemFactory = timelineItemFactory,
|
||||
)
|
||||
|
||||
|
|
@ -80,11 +78,11 @@ class RustMatrixTimeline(
|
|||
return timelineItems.sample(50)
|
||||
}
|
||||
|
||||
internal fun postItems(items: List<TimelineItem>) {
|
||||
timelineItems.value = items.map(timelineItemFactory::map)
|
||||
internal suspend fun postItems(items: List<TimelineItem>) {
|
||||
timelineDiffProcessor.postItems(items)
|
||||
}
|
||||
|
||||
internal fun postDiff(timelineDiff: TimelineDiff) {
|
||||
internal suspend fun postDiff(timelineDiff: TimelineDiff) {
|
||||
timelineDiffProcessor.postDiff(timelineDiff)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue