@@ -47,6 +47,7 @@ import io.realm.kotlin.query.RealmResults
47
47
import io.sentry.Sentry
48
48
import io.sentry.SentryLevel
49
49
import kotlinx.coroutines.*
50
+ import kotlinx.coroutines.flow.Flow
50
51
import kotlinx.coroutines.flow.map
51
52
import javax.inject.Inject
52
53
import kotlin.collections.set
@@ -128,25 +129,43 @@ class ThreadViewModel @Inject constructor(
128
129
}
129
130
}
130
131
131
- fun reassignMessagesLive (threadUid : String , messageUid : String? = null, withSuperCollapsedBlock : Boolean = true) {
132
+ fun reassignMessagesLive (threadUid : String ) {
133
+ reassignMessages {
134
+ messageController.getSortedAndNotDeletedMessagesAsync(threadUid)?.map { mapRealmMessagesResult(it.list, threadUid) }
135
+ }
136
+ }
137
+
138
+ fun reassignMessagesLiveWithoutSuperCollapsedBlock (messageUid : String ) {
139
+ reassignMessages {
140
+ messageController.getMessagesAsync(messageUid).map { mapRealmMessagesResult(it.list) }
141
+ }
142
+ }
143
+
144
+ private fun reassignMessages (messagesFlow : (() -> Flow <Pair <ThreadAdapterItems , MessagesWithoutHeavyData >>? )) {
132
145
messagesLiveJob?.cancel()
133
146
messagesLiveJob = viewModelScope.launch(ioCoroutineContext) {
147
+ messagesFlow()?.collect(messagesLive::postValue)
148
+ }
149
+ }
134
150
135
- val flow = if (messageUid == null ) {
136
- messageController.getSortedAndNotDeletedMessagesAsync(threadUid)
137
- } else {
138
- messageController.getMessagesAsync(messageUid)
139
- }
151
+ private suspend fun mapRealmMessagesResult (messages : RealmResults <Message >): Pair <ThreadAdapterItems , MessagesWithoutHeavyData > {
140
152
141
- flow?.map { mapRealmMessagesResult(it.list, threadUid, withSuperCollapsedBlock) }
142
- ?.collect(messagesLive::postValue)
153
+ val items = mutableListOf<Any >()
154
+ val messagesToFetch = mutableListOf<Message >()
155
+
156
+ messages.forEach { message ->
157
+ splitBody(message).let {
158
+ items + = it
159
+ if (! it.isFullyDownloaded()) messagesToFetch + = it
160
+ }
143
161
}
162
+
163
+ return items to messagesToFetch
144
164
}
145
165
146
166
private suspend fun mapRealmMessagesResult (
147
167
messages : RealmResults <Message >,
148
168
threadUid : String ,
149
- withSuperCollapsedBlock : Boolean ,
150
169
): Pair <ThreadAdapterItems , MessagesWithoutHeavyData > {
151
170
152
171
superCollapsedBlock = superCollapsedBlock ? : SuperCollapsedBlock ()
@@ -155,8 +174,7 @@ class ThreadViewModel @Inject constructor(
155
174
val messagesToFetch = mutableListOf<Message >()
156
175
val thread = messages.firstOrNull()?.threads?.firstOrNull { it.uid == threadUid } ? : return items to messagesToFetch
157
176
val firstIndexAfterBlock = computeFirstIndexAfterBlock(thread, messages)
158
- superCollapsedBlock!! .shouldBeDisplayed =
159
- shouldBlockBeDisplayed(messages.count(), firstIndexAfterBlock, withSuperCollapsedBlock)
177
+ superCollapsedBlock!! .shouldBeDisplayed = shouldBlockBeDisplayed(messages.count(), firstIndexAfterBlock)
160
178
161
179
suspend fun addMessage (message : Message ) {
162
180
splitBody(message).let {
@@ -241,9 +259,8 @@ class ThreadViewModel @Inject constructor(
241
259
* - If there's any unread Message in between, it will be displayed (hence, all following Messages will be displayed too).
242
260
* After all these Messages are displayed, if there's at least 2 remaining Messages, they're gonna be collapsed in the Block.
243
261
*/
244
- private fun shouldBlockBeDisplayed (messagesCount : Int , firstIndexAfterBlock : Int , withSuperCollapsedBlock : Boolean ): Boolean {
245
- return withSuperCollapsedBlock && // When we want to print a mail, we need the full list of Messages
246
- superCollapsedBlock?.shouldBeDisplayed == true && // If the Block was hidden for any reason, we mustn't ever display it again
262
+ private fun shouldBlockBeDisplayed (messagesCount : Int , firstIndexAfterBlock : Int ): Boolean {
263
+ return superCollapsedBlock?.shouldBeDisplayed == true && // If the Block was hidden for any reason, we mustn't ever display it again
247
264
! hasSuperCollapsedBlockBeenClicked && // Block hasn't been expanded by the user
248
265
messagesCount >= SUPER_COLLAPSED_BLOCK_MINIMUM_MESSAGES_LIMIT && // At least 5 Messages in the Thread
249
266
firstIndexAfterBlock >= SUPER_COLLAPSED_BLOCK_FIRST_INDEX_LIMIT // At least 2 Messages in the Block
0 commit comments