Timeline rework: continue branching things.

This commit is contained in:
ganfra 2021-09-08 18:00:54 +02:00
parent 57e5eca784
commit 94a6950394
7 changed files with 41 additions and 28 deletions

View File

@ -115,9 +115,7 @@ interface Timeline {
*/
fun onNewTimelineEvents(eventIds: List<String>)
fun onStateUpdated() {
//NOOP
}
fun onStateUpdated() = Unit
}
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 New Vector Ltd
* Copyright (c) 2021 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -41,17 +41,17 @@ import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicReference
class DefaultTimeline internal constructor(private val roomId: String,
private val initialEventId: String?,
private val realmConfiguration: RealmConfiguration,
private val loadRoomMembersTask: LoadRoomMembersTask,
private val readReceiptHandler: ReadReceiptHandler,
paginationTask: PaginationTask,
getEventTask: GetContextOfEventTask,
fetchTokenAndPaginateTask: FetchTokenAndPaginateTask,
timelineEventMapper: TimelineEventMapper,
timelineInput: TimelineInput,
eventDecryptor: TimelineEventDecryptor) : Timeline {
internal class DefaultTimeline internal constructor(private val roomId: String,
private val initialEventId: String?,
private val realmConfiguration: RealmConfiguration,
private val loadRoomMembersTask: LoadRoomMembersTask,
private val readReceiptHandler: ReadReceiptHandler,
paginationTask: PaginationTask,
getEventTask: GetContextOfEventTask,
fetchTokenAndPaginateTask: FetchTokenAndPaginateTask,
timelineEventMapper: TimelineEventMapper,
timelineInput: TimelineInput,
eventDecryptor: TimelineEventDecryptor) : Timeline {
companion object {
val BACKGROUND_HANDLER = createBackgroundHandler("SimpleTimeline_Thread")
@ -77,8 +77,10 @@ class DefaultTimeline internal constructor(private val roomId: String,
timelineEventMapper = timelineEventMapper,
realm = backgroundRealm,
getContextOfEventTask = getEventTask,
onEventsUpdated = this::postSnapshot
onEventsUpdated = this::postSnapshot,
onNewTimelineEvents = this::onNewTimelineEvents
)
private var strategy: LoadTimelineStrategy = buildStrategy(LoadTimelineStrategy.Mode.Default)
override val isLive: Boolean
@ -231,6 +233,14 @@ class DefaultTimeline internal constructor(private val roomId: String,
}
}
private fun onNewTimelineEvents(eventIds: List<String>) {
timelineScope.launch(Dispatchers.Main) {
listeners.forEach {
tryOrNull { it.onNewTimelineEvents(eventIds) }
}
}
}
private suspend fun updateState(direction: Timeline.Direction, update: (Timeline.PaginationState) -> Timeline.PaginationState) {
val stateReference = when (direction) {
Timeline.Direction.FORWARDS -> forwardState

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 New Vector Ltd
* Copyright (c) 2021 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 New Vector Ltd
* Copyright (c) 2021 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -24,7 +24,6 @@ import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.session.room.send.SendState
import org.matrix.android.sdk.api.session.room.timeline.Timeline
import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
import org.matrix.android.sdk.api.session.room.timeline.TimelineSettings
import org.matrix.android.sdk.internal.database.mapper.TimelineEventMapper
import org.matrix.android.sdk.internal.database.model.ChunkEntity
import org.matrix.android.sdk.internal.database.model.ChunkEntityFields
@ -59,7 +58,8 @@ internal class LoadTimelineStrategy(
val getContextOfEventTask: GetContextOfEventTask,
val timelineInput: TimelineInput,
val timelineEventMapper: TimelineEventMapper,
val onEventsUpdated: () -> Unit
val onEventsUpdated: () -> Unit,
val onNewTimelineEvents: (List<String>) -> Unit
)
private var chunkEntity: RealmResults<ChunkEntity>? = null
@ -86,6 +86,7 @@ internal class LoadTimelineStrategy(
return
}
if (uiEchoManager.onLocalEchoCreated(timelineEvent)) {
dependencies.onNewTimelineEvents(listOf(timelineEvent.eventId))
dependencies.onEventsUpdated()
}
}
@ -98,9 +99,16 @@ internal class LoadTimelineStrategy(
dependencies.onEventsUpdated()
}
}
override fun onNewTimelineEvents(roomId: String, eventIds: List<String>) {
super.onNewTimelineEvents(roomId, eventIds)
if (mode == Mode.Default && roomId == this@LoadTimelineStrategy.roomId) {
dependencies.onNewTimelineEvents(eventIds)
}
}
}
private val uiEchoManager = UIEchoManager(TimelineSettings(10), uiEchoManagerListener)
private val uiEchoManager = UIEchoManager(uiEchoManagerListener)
private val sendingEventsDataSource: SendingEventsDataSource = RealmSendingEventsDataSource(
roomId = roomId,
realm = dependencies.realm,
@ -118,7 +126,7 @@ internal class LoadTimelineStrategy(
it.addChangeListener(chunkEntityListener)
timelineChunk = it.createTimelineChunk()
}
if(mode is Mode.Default){
if (mode is Mode.Default) {
loadMore(10, Timeline.Direction.BACKWARDS)
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 New Vector Ltd
* Copyright (c) 2021 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 New Vector Ltd
* Copyright (c) 2021 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -28,10 +28,7 @@ import org.matrix.android.sdk.api.session.room.timeline.TimelineSettings
import timber.log.Timber
import java.util.Collections
internal class UIEchoManager(
private val settings: TimelineSettings,
private val listener: Listener
) {
internal class UIEchoManager(private val listener: Listener) {
interface Listener {
fun rebuildEvent(eventId: String, builder: (TimelineEvent) -> TimelineEvent?): Boolean