From dfabc02bf6e4359f8be33e6b32555ae20f81ac1a Mon Sep 17 00:00:00 2001 From: ganfra Date: Mon, 24 Apr 2023 10:43:37 +0200 Subject: [PATCH] Timeline : Add isInit to avoid calling rust methods when the timeline is not ready. --- .../libraries/matrix/impl/timeline/RustMatrixTimeline.kt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RustMatrixTimeline.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RustMatrixTimeline.kt index 6d19e817a4..3222cda704 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RustMatrixTimeline.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RustMatrixTimeline.kt @@ -42,6 +42,7 @@ import org.matrix.rustcomponents.sdk.SlidingSyncRoom import org.matrix.rustcomponents.sdk.TimelineItem import org.matrix.rustcomponents.sdk.TimelineListener import timber.log.Timber +import java.util.concurrent.atomic.AtomicBoolean class RustMatrixTimeline( private val matrixRoom: MatrixRoom, @@ -51,6 +52,8 @@ class RustMatrixTimeline( private val coroutineDispatchers: CoroutineDispatchers, ) : MatrixTimeline { + private val isInit = AtomicBoolean(false) + private val timelineItems: MutableStateFlow> = MutableStateFlow(emptyList()) @@ -95,6 +98,7 @@ class RustMatrixTimeline( withContext(coroutineDispatchers.diffUpdateDispatcher) { this@RustMatrixTimeline.timelineItems.value = matrixTimelineItems } + isInit.set(true) } .onFailure { Timber.e("Failed adding timeline listener on room with identifier: ${matrixRoom.roomId})") @@ -105,6 +109,7 @@ class RustMatrixTimeline( override fun dispose() { Timber.v("Dispose timeline for room ${matrixRoom.roomId}") listenerTokens.dispose() + isInit.set(false) } /** @@ -125,6 +130,9 @@ class RustMatrixTimeline( override suspend fun paginateBackwards(requestSize: Int, untilNumberOfItems: Int): Result = withContext(coroutineDispatchers.io) { runCatching { Timber.v("Start back paginating for room ${matrixRoom.roomId} ") + if (!isInit.get()) { + throw IllegalStateException("Timeline is not init yet") + } val paginationOptions = PaginationOptions.UntilNumItems( eventLimit = requestSize.toUShort(), items = untilNumberOfItems.toUShort()