diff --git a/app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/RefreshController.kt b/app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/RefreshController.kt index 4b1b4c5389..316f275c37 100644 --- a/app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/RefreshController.kt +++ b/app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/RefreshController.kt @@ -596,7 +596,7 @@ class RefreshController @Inject constructor( // Add Sentry log and leave if the Message already exists if (existingMessage != null && !existingMessage.isOrphan()) { SentryLog.i( - TAG, + "Realm", "Already existing message in folder ${folder.displayForSentry()} | threadMode = ${localSettings.threadMode}", ) return true @@ -817,8 +817,4 @@ class RefreshController @Inject constructor( val onStart: (() -> Unit), val onStop: (() -> Unit), ) - - companion object { - private val TAG = RefreshController::class.java.simpleName - } } diff --git a/app/src/main/java/com/infomaniak/mail/ui/main/thread/ThreadFragment.kt b/app/src/main/java/com/infomaniak/mail/ui/main/thread/ThreadFragment.kt index 90fd1f89f2..0004b68e17 100644 --- a/app/src/main/java/com/infomaniak/mail/ui/main/thread/ThreadFragment.kt +++ b/app/src/main/java/com/infomaniak/mail/ui/main/thread/ThreadFragment.kt @@ -147,6 +147,7 @@ class ThreadFragment : Fragment() { observeLightThemeToggle() observeThreadLive() observeMessagesLive() + observeBatchedMessages() observeFailedMessages() observeQuickActionBarClicks() observeSubjectUpdateTriggers() @@ -428,13 +429,22 @@ class ThreadFragment : Fragment() { return@observe } - threadAdapter.submitList(items) + if (threadState.hasSuperCollapsedBlockBeenClicked) { + displayBatchedMessages(items) + } else { + threadAdapter.submitList(items) + } + if (messagesToFetch.isNotEmpty()) fetchMessagesHeavyData(messagesToFetch) fetchCalendarEvents(items) } } + private fun observeBatchedMessages() { + threadViewModel.batchedMessages.observe(viewLifecycleOwner, threadAdapter::submitList) + } + private fun observeFailedMessages() { threadViewModel.failedMessagesUids.observe(viewLifecycleOwner, threadAdapter::updateFailedMessages) } diff --git a/app/src/main/java/com/infomaniak/mail/ui/main/thread/ThreadViewModel.kt b/app/src/main/java/com/infomaniak/mail/ui/main/thread/ThreadViewModel.kt index b971f9550b..97a82361bf 100644 --- a/app/src/main/java/com/infomaniak/mail/ui/main/thread/ThreadViewModel.kt +++ b/app/src/main/java/com/infomaniak/mail/ui/main/thread/ThreadViewModel.kt @@ -74,6 +74,7 @@ class ThreadViewModel @Inject constructor( val threadLive = MutableLiveData() val messagesLive = MutableLiveData>() + val batchedMessages = SingleLiveEvent>() val quickActionBarClicks = SingleLiveEvent() @@ -246,6 +247,25 @@ class ThreadViewModel @Inject constructor( return@withContext message } + fun displayBatchedMessages(items: List) = viewModelScope.launch(ioCoroutineContext) { + + tailrec suspend fun sendBatchesRecursively(input: List, output: MutableList, batchSize: Int = 1) { + + val batch = input.take(batchSize) + output.addAll(batch) + + // We need to post a different list each time, because the `submitList` function in AsyncListDiffer + // won't trigger if we send the same list object (https://stackoverflow.com/questions/49726385). + batchedMessages.postValue(ArrayList(output)) + + if (batch.size < batchSize) return + delay(DELAY_BETWEEN_EACH_BATCHED_MESSAGES) + sendBatchesRecursively(input.subList(batchSize, input.size), output) + } + + sendBatchesRecursively(input = items, output = mutableListOf(), batchSize = 2) + } + fun openThread(threadUid: String) = liveData(ioCoroutineContext) { val thread = threadController.getThread(threadUid) ?: run { @@ -451,5 +471,6 @@ class ThreadViewModel @Inject constructor( companion object { private const val SUPER_COLLAPSED_BLOCK_MINIMUM_MESSAGES_LIMIT = 5 private const val SUPER_COLLAPSED_BLOCK_FIRST_INDEX_LIMIT = 3 + private const val DELAY_BETWEEN_EACH_BATCHED_MESSAGES = 50L } }