@@ -239,7 +239,8 @@ class RefreshController @Inject constructor(
239239 upToDateFolder.oldMessagesUidsToFetch.isNotEmpty() &&
240240 maxPagesToFetch > 0
241241 ) {
242- totalNewThreads + = fetchOnePage(scope, upToDateFolder, Direction .IN_THE_PAST ).count()
242+ val impactedThreads = fetchOnePage(scope, upToDateFolder, Direction .IN_THE_PAST )
243+ totalNewThreads + = impactedThreads.count { it.folderId == upToDateFolder.id }
243244 upToDateFolder = getUpToDateFolder(folderId)
244245 maxPagesToFetch--
245246 }
@@ -328,12 +329,12 @@ class RefreshController @Inject constructor(
328329 var inboxUnreadCount: Int? = null
329330 FolderController .updateFolder(folder.id, realm = this ) { mutableRealm, it ->
330331
331- val allThreads = ThreadController .getThreadsByFolderId(it.id, realm = mutableRealm)
332- it.threads.replaceContent(list = allThreads )
332+ val allCurrentFolderThreads = ThreadController .getThreadsByFolderId(it.id, realm = mutableRealm)
333+ it.threads.replaceContent(list = allCurrentFolderThreads )
333334
334- val isConversationMode = localSettings.threadMode == ThreadMode .CONVERSATION
335335 inboxUnreadCount = updateFoldersUnreadCount(
336- foldersIds = (if (isConversationMode) allThreads.mapTo(mutableSetOf ()) { it.folderId } else emptySet()) + folder.id,
336+ // `impactedThreads` may not contain `folder.id` in special folders cases (i.e. snooze)
337+ foldersIds = impactedThreads.mapTo(mutableSetOf (folder.id)) { it.folderId },
337338 realm = mutableRealm,
338339 )
339340
@@ -418,31 +419,25 @@ class RefreshController @Inject constructor(
418419
419420 if (uids.isEmpty()) return emptySet()
420421
421- val impactedThreads = mutableSetOf<Thread >()
422-
423422 val apiResponse = delayApiCallManager.getMessagesByUids(scope, mailbox.uuid, folder.id, uids, okHttpClient)
424423 if (! apiResponse.isSuccess()) apiResponse.throwErrorAsException()
425424 scope.ensureActive()
426425
427- apiResponse.data?.messages?.let { messages ->
426+ return apiResponse.data?.messages?.let { messages ->
427+
428+ return @let write {
428429
429- write {
430430 val upToDateFolder = getUpToDateFolder(folder.id)
431431 val isConversationMode = localSettings.threadMode == ThreadMode .CONVERSATION
432- val allImpactedThreads = createThreads(scope, upToDateFolder, messages, isConversationMode)
433432
434- // TODO: This count will be false for INBOX & SNOOZED when the snooze feature will be implemented
435- val messagesCount = MessageController .getMessagesCountByFolderId(upToDateFolder.id, realm = this )
436- SentryLog .d(
437- " Realm" ,
438- " Saved Messages: ${upToDateFolder.displayForSentry()} | ($messagesCount )" ,
439- )
433+ return @write createThreads(scope, upToDateFolder, messages, isConversationMode).also {
440434
441- impactedThreads + = allImpactedThreads.filter { it.folderId == upToDateFolder.id }
435+ // TODO: This count will be false for INBOX & SNOOZED when the snooze feature will be implemented
436+ val messagesCount = MessageController .getMessagesCountByFolderId(upToDateFolder.id, realm = this )
437+ SentryLog .d(" Realm" , " Saved Messages: ${upToDateFolder.displayForSentry()} | ($messagesCount )" )
438+ }
442439 }
443- }
444-
445- return impactedThreads
440+ } ? : emptySet()
446441 }
447442 // endregion
448443
0 commit comments