From 6b56a4f906b808d08987377189fb4caad5b598e9 Mon Sep 17 00:00:00 2001
From: Taras Smakula <tarassmakula@gmail.com>
Date: Tue, 12 Mar 2024 18:34:56 +0200
Subject: [PATCH] Add room id to update snapshot

---
 .../timeline/builder/BaseTimelineBuilder.kt       | 15 +++++++++++----
 .../timeline/builder/MultiTimelineBuilder.kt      |  6 ++++--
 .../timeline/builder/SingleTimelineBuilder.kt     |  6 ++++--
 .../data_source/BaseTimelineDataSource.kt         | 15 +++++++++------
 4 files changed, 28 insertions(+), 14 deletions(-)

diff --git a/core/src/main/java/org/futo/circles/core/feature/timeline/builder/BaseTimelineBuilder.kt b/core/src/main/java/org/futo/circles/core/feature/timeline/builder/BaseTimelineBuilder.kt
index c6098fee0..46f12ca07 100644
--- a/core/src/main/java/org/futo/circles/core/feature/timeline/builder/BaseTimelineBuilder.kt
+++ b/core/src/main/java/org/futo/circles/core/feature/timeline/builder/BaseTimelineBuilder.kt
@@ -17,13 +17,20 @@ abstract class BaseTimelineBuilder {
     private val supportedTimelineEvens: List<String> =
         listOf(EventType.MESSAGE, EventType.POLL_START.stable, EventType.POLL_START.unstable)
 
-    abstract suspend fun List<TimelineEvent>.processSnapshot(isThread: Boolean): List<Post>
-
-    suspend fun build(snapshot: List<TimelineEvent>, isThread: Boolean): List<Post> =
+    abstract suspend fun List<TimelineEvent>.processSnapshot(
+        roomId: String,
+        isThread: Boolean
+    ): List<Post>
+
+    suspend fun build(
+        roomId: String,
+        snapshot: List<TimelineEvent>,
+        isThread: Boolean
+    ): List<Post> =
         withContext(Dispatchers.IO) {
             snapshot
                 .filterTimelineEvents(isThread)
-                .processSnapshot(isThread)
+                .processSnapshot(roomId, isThread)
         }
 
     protected fun sortList(list: List<Post>, isThread: Boolean) =
diff --git a/core/src/main/java/org/futo/circles/core/feature/timeline/builder/MultiTimelineBuilder.kt b/core/src/main/java/org/futo/circles/core/feature/timeline/builder/MultiTimelineBuilder.kt
index 92b6f9170..8ef9b1f01 100644
--- a/core/src/main/java/org/futo/circles/core/feature/timeline/builder/MultiTimelineBuilder.kt
+++ b/core/src/main/java/org/futo/circles/core/feature/timeline/builder/MultiTimelineBuilder.kt
@@ -16,8 +16,10 @@ class MultiTimelineBuilder @Inject constructor() : BaseTimelineBuilder() {
     private var currentSnapshotMap: MutableMap<String, List<Post>> = mutableMapOf()
     private var readReceiptMap: MutableMap<String, List<Long>> = mutableMapOf()
 
-    override suspend fun List<TimelineEvent>.processSnapshot(isThread: Boolean): List<Post> {
-        val roomId = firstOrNull()?.roomId ?: return getCurrentTimelinesPostsList()
+    override suspend fun List<TimelineEvent>.processSnapshot(
+        roomId: String,
+        isThread: Boolean
+    ): List<Post> {
         val room = MatrixSessionProvider.currentSession?.getRoom(roomId)
             ?: return getCurrentTimelinesPostsList()
         val roomName = room.roomSummary()?.nameOrId()
diff --git a/core/src/main/java/org/futo/circles/core/feature/timeline/builder/SingleTimelineBuilder.kt b/core/src/main/java/org/futo/circles/core/feature/timeline/builder/SingleTimelineBuilder.kt
index ff7360689..b1d0e9ba3 100644
--- a/core/src/main/java/org/futo/circles/core/feature/timeline/builder/SingleTimelineBuilder.kt
+++ b/core/src/main/java/org/futo/circles/core/feature/timeline/builder/SingleTimelineBuilder.kt
@@ -14,8 +14,10 @@ class SingleTimelineBuilder @Inject constructor() : BaseTimelineBuilder() {
 
     private var currentSnapshotList: List<Post> = listOf()
 
-    override suspend fun List<TimelineEvent>.processSnapshot(isThread: Boolean): List<Post> {
-        val roomId = firstOrNull()?.roomId ?: return currentSnapshotList
+    override suspend fun List<TimelineEvent>.processSnapshot(
+        roomId: String,
+        isThread: Boolean
+    ): List<Post> {
         val room =
             MatrixSessionProvider.currentSession?.getRoom(roomId) ?: return currentSnapshotList
         val receipts = getReadReceipts(room)
diff --git a/core/src/main/java/org/futo/circles/core/feature/timeline/data_source/BaseTimelineDataSource.kt b/core/src/main/java/org/futo/circles/core/feature/timeline/data_source/BaseTimelineDataSource.kt
index e36237e28..f2e906de7 100644
--- a/core/src/main/java/org/futo/circles/core/feature/timeline/data_source/BaseTimelineDataSource.kt
+++ b/core/src/main/java/org/futo/circles/core/feature/timeline/data_source/BaseTimelineDataSource.kt
@@ -5,7 +5,6 @@ import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.channels.awaitClose
 import kotlinx.coroutines.flow.Flow
 import kotlinx.coroutines.flow.callbackFlow
-import kotlinx.coroutines.flow.debounce
 import kotlinx.coroutines.flow.distinctUntilChanged
 import kotlinx.coroutines.flow.flowOn
 import kotlinx.coroutines.flow.mapLatest
@@ -47,8 +46,12 @@ abstract class BaseTimelineDataSource(
 
     fun getTimelineEventFlow(): Flow<List<Post>> = callbackFlow {
         val listener = object : Timeline.Listener {
-            override fun onTimelineUpdated(snapshot: List<TimelineEvent>) {
-                if (snapshot.isNotEmpty()) trySend(snapshot)
+            override fun onTimelineUpdated(
+                roomId: String,
+                timelineId: String,
+                snapshot: List<TimelineEvent>
+            ) {
+                if (snapshot.isNotEmpty()) trySend(roomId to snapshot)
             }
 
             override fun onTimelineFailure(timelineId: String, throwable: Throwable) {
@@ -58,9 +61,9 @@ abstract class BaseTimelineDataSource(
         startTimeline(listener)
         awaitClose()
     }.flowOn(Dispatchers.IO)
-        .mapLatest {
-            val items = timelineBuilder.build(it, isThread)
-            if (it.isNotEmpty() && items.size <= LOAD_MORE_THRESHOLD) loadMore()
+        .mapLatest { (roomId, snapshot) ->
+            val items = timelineBuilder.build(roomId, snapshot, isThread)
+            if (snapshot.isNotEmpty() && items.size <= LOAD_MORE_THRESHOLD) loadMore()
             items
         }
         .distinctUntilChanged()
-- 
GitLab