Skip to content

Commit 2e8cdc1

Browse files
refactor: Sort code order in RefreshController
1 parent def565d commit 2e8cdc1

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/RefreshController.kt

+37-37
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ class RefreshController @Inject constructor(
430430
val upToDateFolder = getUpToDateFolder(folder.id)
431431
val isConversationMode = localSettings.threadMode == ThreadMode.CONVERSATION
432432

433-
return@write createThreads(scope, upToDateFolder, messages, isConversationMode).also {
433+
return@write handleAddedMessages(scope, upToDateFolder, messages, isConversationMode).also {
434434

435435
// TODO: This count will be false for INBOX & SNOOZED when the snooze feature will be implemented
436436
val messagesCount = MessageController.getMessagesCountByFolderId(upToDateFolder.id, realm = this)
@@ -503,7 +503,7 @@ class RefreshController @Inject constructor(
503503
//endregion
504504

505505
//region Create Threads
506-
private fun MutableRealm.createThreads(
506+
private fun MutableRealm.handleAddedMessages(
507507
scope: CoroutineScope,
508508
folder: Folder,
509509
remoteMessages: List<Message>,
@@ -521,7 +521,7 @@ class RefreshController @Inject constructor(
521521
addedMessagesUids.add(remoteMessage.shortUid)
522522

523523
val newThread = if (isConversationMode) {
524-
createNewThread(scope, remoteMessage, impactedThreadsManaged)
524+
handleAddedMessage(scope, remoteMessage, impactedThreadsManaged)
525525
} else {
526526
remoteMessage.toThread()
527527
}
@@ -542,7 +542,20 @@ class RefreshController @Inject constructor(
542542
return impactedThreadsUnmanaged
543543
}
544544

545-
private fun MutableRealm.createNewThread(
545+
private fun initMessageLocalValues(remoteMessage: Message, folder: Folder) {
546+
remoteMessage.initLocalValues(
547+
MessageInitialState(
548+
date = remoteMessage.date,
549+
isFullyDownloaded = false,
550+
isTrashed = folder.role == FolderRole.TRASH,
551+
isFromSearch = false,
552+
draftLocalUuid = null,
553+
),
554+
latestCalendarEventResponse = null,
555+
)
556+
}
557+
558+
private fun MutableRealm.handleAddedMessage(
546559
scope: CoroutineScope,
547560
remoteMessage: Message,
548561
impactedThreadsManaged: MutableSet<Thread>,
@@ -559,50 +572,19 @@ class RefreshController @Inject constructor(
559572
// Create Thread in this Folder
560573
val thread = createNewThreadIfRequired(scope, remoteMessage, existingThreads, existingMessages)
561574
// Update Threads in other Folders
562-
updateOtherExistingThreads(scope, remoteMessage, existingThreads, existingMessages, impactedThreadsManaged)
575+
updateExistingThreads(scope, remoteMessage, existingThreads, existingMessages, impactedThreadsManaged)
563576

564577
// Now that all other existing Threads are updated, we need to remove the duplicated Threads.
565578
if (isThereDuplicatedThreads) removeDuplicatedThreads(remoteMessage.messageIds, impactedThreadsManaged)
566579

567580
return thread
568581
}
569582

570-
private fun initMessageLocalValues(remoteMessage: Message, folder: Folder) {
571-
remoteMessage.initLocalValues(
572-
MessageInitialState(
573-
date = remoteMessage.date,
574-
isFullyDownloaded = false,
575-
isTrashed = folder.role == FolderRole.TRASH,
576-
isFromSearch = false,
577-
draftLocalUuid = null,
578-
),
579-
latestCalendarEventResponse = null,
580-
)
581-
}
582-
583583
private fun MutableRealm.isThereDuplicatedThreads(messageIds: RealmSet<String>, threadsCount: Int): Boolean {
584584
val foldersCount = ThreadController.getExistingThreadsFoldersCount(messageIds, realm = this)
585585
return foldersCount != threadsCount.toLong()
586586
}
587587

588-
private fun MutableRealm.removeDuplicatedThreads(messageIds: RealmSet<String>, impactedThreadsManaged: MutableSet<Thread>) {
589-
590-
// Create a map with all duplicated Threads of the same Thread in a list.
591-
val map = mutableMapOf<String, MutableList<Thread>>()
592-
ThreadController.getThreadsByMessageIds(messageIds, realm = this).forEach {
593-
map.getOrPut(it.folderId) { mutableListOf() }.add(it)
594-
}
595-
596-
map.values.forEach { threads ->
597-
threads.forEachIndexed { index, thread ->
598-
if (index > 0) { // We want to keep only 1 duplicated Thread, so we skip the 1st one. (He's the chosen one!)
599-
impactedThreadsManaged.remove(thread)
600-
delete(thread) // Delete the other Threads. Sorry bro, you won't be missed.
601-
}
602-
}
603-
}
604-
}
605-
606588
private fun TypedRealm.createNewThreadIfRequired(
607589
scope: CoroutineScope,
608590
newMessage: Message,
@@ -621,7 +603,7 @@ class RefreshController @Inject constructor(
621603
return newThread
622604
}
623605

624-
private fun MutableRealm.updateOtherExistingThreads(
606+
private fun MutableRealm.updateExistingThreads(
625607
scope: CoroutineScope,
626608
remoteMessage: Message,
627609
existingThreads: RealmResults<Thread>,
@@ -651,6 +633,24 @@ class RefreshController @Inject constructor(
651633
}
652634
}
653635

636+
private fun MutableRealm.removeDuplicatedThreads(messageIds: RealmSet<String>, impactedThreadsManaged: MutableSet<Thread>) {
637+
638+
// Create a map with all duplicated Threads of the same Thread in a list.
639+
val map = mutableMapOf<String, MutableList<Thread>>()
640+
ThreadController.getThreadsByMessageIds(messageIds, realm = this).forEach {
641+
map.getOrPut(it.folderId) { mutableListOf() }.add(it)
642+
}
643+
644+
map.values.forEach { threads ->
645+
threads.forEachIndexed { index, thread ->
646+
if (index > 0) { // We want to keep only 1 duplicated Thread, so we skip the 1st one. (He's the chosen one!)
647+
impactedThreadsManaged.remove(thread)
648+
delete(thread) // Delete the other Threads. Sorry bro, you won't be missed.
649+
}
650+
}
651+
}
652+
}
653+
654654
private fun MutableRealm.putNewThreadInRealm(newThread: Thread): Thread {
655655
return ThreadController.upsertThread(newThread, realm = this)
656656
}

0 commit comments

Comments
 (0)