Timeline: clean up

This commit is contained in:
ganfra 2021-12-09 12:33:56 +01:00
parent faebf95e1c
commit bf287d1827
14 changed files with 29 additions and 34 deletions

View file

@ -43,7 +43,6 @@ class TimelineSimpleBackPaginationTest : InstrumentedTest {
@Test
fun timeline_backPaginate_shouldReachEndOfTimeline() {
val numberOfMessagesToSent = 200
val cryptoTestData = cryptoTestHelper.doE2ETestWithAliceAndBobInARoom(false)
@ -69,12 +68,12 @@ class TimelineSimpleBackPaginationTest : InstrumentedTest {
bobTimeline.start()
commonTestHelper.waitWithLatch(timeout = TestConstants.timeOutMillis * 10) {
val listener = object : Timeline.Listener {
override fun onStateUpdated(direction: Timeline.Direction, state: Timeline.PaginationState) {
if (direction == Timeline.Direction.FORWARDS)
if (direction == Timeline.Direction.FORWARDS) {
return
}
if (state.hasMoreToLoad && !state.loading) {
bobTimeline.paginate(Timeline.Direction.BACKWARDS, 30)
} else if (!state.hasMoreToLoad) {

View file

@ -44,7 +44,7 @@ internal class DatabaseCleaner @Inject constructor(@SessionDatabase private val
awaitTransaction(realmConfiguration) { realm ->
val allRooms = realm.where(RoomEntity::class.java).findAll()
Timber.v("There are ${allRooms.size} rooms in this session")
//cleanUp(realm, MAX_NUMBER_OF_EVENTS_IN_DB / 2L)
// cleanUp(realm, MAX_NUMBER_OF_EVENTS_IN_DB / 2L)
}
}
}

View file

@ -414,5 +414,4 @@ internal class RealmSessionStoreMigration @Inject constructor(
}
}
}
}

View file

@ -110,7 +110,7 @@ internal fun ChunkEntity.addTimelineEvent(roomId: String,
true
}
}
//numberOfTimelineEvents++
// numberOfTimelineEvents++
timelineEvents.add(timelineEventEntity)
}
@ -207,11 +207,11 @@ internal fun ChunkEntity.isMoreRecentThan(chunkToCheck: ChunkEntity): Boolean {
if (this.isLastForward) return true
if (chunkToCheck.isLastForward) return false
// Check if the chunk to check is linked to this one
if(chunkToCheck.doesNextChunksVerifyCondition { it == this }){
if (chunkToCheck.doesNextChunksVerifyCondition { it == this }) {
return true
}
// Otherwise check if this chunk is linked to last forward
if(this.doesNextChunksVerifyCondition { it.isLastForward }){
if (this.doesNextChunksVerifyCondition { it.isLastForward }) {
return true
}
// We don't know, so we assume it's false

View file

@ -31,7 +31,7 @@ internal open class ChunkEntity(@Index var prevToken: String? = null,
var nextChunk: ChunkEntity? = null,
var stateEvents: RealmList<EventEntity> = RealmList(),
var timelineEvents: RealmList<TimelineEventEntity> = RealmList(),
//var numberOfTimelineEvents: Long = 0,
// var numberOfTimelineEvents: Long = 0,
// Only one chunk will have isLastForward == true
@Index var isLastForward: Boolean = false,
@Index var isLastBackward: Boolean = false

View file

@ -18,7 +18,6 @@ package org.matrix.android.sdk.internal.database.query
import io.realm.Realm
import io.realm.RealmConfiguration
import org.matrix.android.sdk.api.session.events.model.LocalEcho
import org.matrix.android.sdk.internal.database.helper.doesNextChunksVerifyCondition
import org.matrix.android.sdk.internal.database.helper.isMoreRecentThan
import org.matrix.android.sdk.internal.database.model.ChunkEntity
import org.matrix.android.sdk.internal.database.model.ReadMarkerEntity

View file

@ -34,7 +34,6 @@ import org.matrix.android.sdk.internal.database.query.isEventRead
import org.matrix.android.sdk.internal.database.query.where
import org.matrix.android.sdk.internal.di.SessionDatabase
import org.matrix.android.sdk.internal.di.UserId
import org.matrix.android.sdk.internal.task.TaskExecutor
internal class DefaultReadService @AssistedInject constructor(
@Assisted private val roomId: String,

View file

@ -19,7 +19,6 @@ package org.matrix.android.sdk.internal.session.room.timeline
import io.realm.Realm
import io.realm.RealmConfiguration
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.android.asCoroutineDispatcher
import kotlinx.coroutines.cancelChildren
@ -31,6 +30,7 @@ import kotlinx.coroutines.flow.sample
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.internal.closeQuietly
import org.matrix.android.sdk.api.MatrixCoroutineDispatchers
import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.session.room.timeline.Timeline
import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
@ -53,6 +53,7 @@ internal class DefaultTimeline internal constructor(private val roomId: String,
private val loadRoomMembersTask: LoadRoomMembersTask,
private val readReceiptHandler: ReadReceiptHandler,
private val settings: TimelineSettings,
private val coroutineDispatchers: MatrixCoroutineDispatchers,
paginationTask: PaginationTask,
getEventTask: GetContextOfEventTask,
fetchTokenAndPaginateTask: FetchTokenAndPaginateTask,
@ -102,7 +103,7 @@ internal class DefaultTimeline internal constructor(private val roomId: String,
listeners.add(listener)
timelineScope.launch {
val snapshot = strategy.buildSnapshot()
withContext(Dispatchers.Main) {
withContext(coroutineDispatchers.main) {
tryOrNull { listener.onTimelineUpdated(snapshot) }
}
}
@ -280,7 +281,7 @@ internal class DefaultTimeline internal constructor(private val roomId: String,
private suspend fun postSnapshot() {
val snapshot = strategy.buildSnapshot()
Timber.v("Post snapshot of ${snapshot.size} events")
withContext(Dispatchers.Main) {
withContext(coroutineDispatchers.main) {
listeners.forEach {
tryOrNull { it.onTimelineUpdated(snapshot) }
}
@ -288,7 +289,7 @@ internal class DefaultTimeline internal constructor(private val roomId: String,
}
private fun onNewTimelineEvents(eventIds: List<String>) {
timelineScope.launch(Dispatchers.Main) {
timelineScope.launch(coroutineDispatchers.main) {
listeners.forEach {
tryOrNull { it.onNewTimelineEvents(eventIds) }
}
@ -309,7 +310,7 @@ internal class DefaultTimeline internal constructor(private val roomId: String,
}
private fun postPaginationState(direction: Timeline.Direction, state: Timeline.PaginationState) {
timelineScope.launch(Dispatchers.Main) {
timelineScope.launch(coroutineDispatchers.main) {
Timber.v("Post $direction pagination state: $state ")
listeners.forEach {
tryOrNull { it.onStateUpdated(direction, state) }

View file

@ -23,6 +23,7 @@ import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import io.realm.Sort
import io.realm.kotlin.where
import org.matrix.android.sdk.api.MatrixCoroutineDispatchers
import org.matrix.android.sdk.api.session.events.model.isImageMessage
import org.matrix.android.sdk.api.session.events.model.isVideoMessage
import org.matrix.android.sdk.api.session.room.timeline.Timeline
@ -54,7 +55,8 @@ internal class DefaultTimelineService @AssistedInject constructor(
private val timelineEventMapper: TimelineEventMapper,
private val loadRoomMembersTask: LoadRoomMembersTask,
private val threadsAwarenessHandler: ThreadsAwarenessHandler,
private val readReceiptHandler: ReadReceiptHandler
private val readReceiptHandler: ReadReceiptHandler,
private val coroutineDispatchers: MatrixCoroutineDispatchers
) : TimelineService {
@AssistedFactory
@ -68,6 +70,7 @@ internal class DefaultTimelineService @AssistedInject constructor(
initialEventId = eventId,
settings = settings,
realmConfiguration = monarchy.realmConfiguration,
coroutineDispatchers = coroutineDispatchers,
paginationTask = paginationTask,
timelineEventMapper = timelineEventMapper,
timelineInput = timelineInput,

View file

@ -230,5 +230,3 @@ internal class LoadTimelineStrategy(
}
}
}

View file

@ -85,11 +85,12 @@ internal class TimelineChunk(private val chunkEntity: ChunkEntity,
}
}
private val timelineEventCollectionListener = OrderedRealmCollectionChangeListener { results: RealmResults<TimelineEventEntity>, changeSet: OrderedCollectionChangeSet ->
Timber.v("on timeline events chunk update")
val frozenResults = results.freeze()
handleDatabaseChangeSet(frozenResults, changeSet)
}
private val timelineEventsChangeListener =
OrderedRealmCollectionChangeListener { results: RealmResults<TimelineEventEntity>, changeSet: OrderedCollectionChangeSet ->
Timber.v("on timeline events chunk update")
val frozenResults = results.freeze()
handleDatabaseChangeSet(frozenResults, changeSet)
}
private var timelineEventEntities: RealmResults<TimelineEventEntity> = chunkEntity.sortedTimelineEvents()
private val builtEvents: MutableList<TimelineEvent> = Collections.synchronizedList(ArrayList())
@ -99,7 +100,7 @@ internal class TimelineChunk(private val chunkEntity: ChunkEntity,
private var prevChunk: TimelineChunk? = null
init {
timelineEventEntities.addChangeListener(timelineEventCollectionListener)
timelineEventEntities.addChangeListener(timelineEventsChangeListener)
chunkEntity.addChangeListener(chunkObjectListener)
}
@ -261,7 +262,7 @@ internal class TimelineChunk(private val chunkEntity: ChunkEntity,
prevChunk = null
prevChunkLatch?.cancel()
chunkEntity.removeChangeListener(chunkObjectListener)
timelineEventEntities.removeChangeListener(timelineEventCollectionListener)
timelineEventEntities.removeChangeListener(timelineEventsChangeListener)
}
/**
@ -311,8 +312,8 @@ internal class TimelineChunk(private val chunkEntity: ChunkEntity,
val timelineEvent = buildTimelineEvent(this)
val transactionId = timelineEvent.root.unsignedData?.transactionId
uiEchoManager?.onSyncedEvent(transactionId)
if (timelineEvent.isEncrypted()
&& timelineEvent.root.mxDecryptionResult == null) {
if (timelineEvent.isEncrypted() &&
timelineEvent.root.mxDecryptionResult == null) {
timelineEvent.root.eventId?.also { eventDecryptor.requestDecryption(TimelineEventDecryptor.DecryptionRequest(timelineEvent.root, timelineId)) }
}
return timelineEvent

View file

@ -133,7 +133,7 @@ internal class TokenChunkEventPersistor @Inject constructor(@SessionDatabase pri
if (event.eventId == null || event.senderId == null) {
return@forEach
}
//We check for the timeline event with this id
// We check for the timeline event with this id
val eventId = event.eventId
val existingTimelineEvent = TimelineEventEntity.where(realm, roomId, eventId).findFirst()
// If it exists, we want to stop here, just link the prevChunk

View file

@ -24,7 +24,6 @@ import org.matrix.android.sdk.api.session.room.model.ReactionAggregatedSummary
import org.matrix.android.sdk.api.session.room.model.relation.ReactionContent
import org.matrix.android.sdk.api.session.room.send.SendState
import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
import org.matrix.android.sdk.api.session.room.timeline.TimelineSettings
import timber.log.Timber
import java.util.Collections

View file

@ -17,9 +17,6 @@
package im.vector.app.core.epoxy
import androidx.annotation.CallSuper
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
import com.airbnb.epoxy.EpoxyModelWithHolder
import com.airbnb.epoxy.VisibilityState
import kotlinx.coroutines.CoroutineScope
@ -30,7 +27,7 @@ import kotlinx.coroutines.cancelChildren
/**
* EpoxyModelWithHolder which can listen to visibility state change
*/
abstract class VectorEpoxyModel<H : VectorEpoxyHolder> : EpoxyModelWithHolder<H>(){
abstract class VectorEpoxyModel<H : VectorEpoxyHolder> : EpoxyModelWithHolder<H>() {
protected val coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Main)