@@ -430,7 +430,7 @@ class RefreshController @Inject constructor(
430
430
val upToDateFolder = getUpToDateFolder(folder.id)
431
431
val isConversationMode = localSettings.threadMode == ThreadMode .CONVERSATION
432
432
433
- return @write createThreads (scope, upToDateFolder, messages, isConversationMode).also {
433
+ return @write handleAddedMessages (scope, upToDateFolder, messages, isConversationMode).also {
434
434
435
435
// TODO: This count will be false for INBOX & SNOOZED when the snooze feature will be implemented
436
436
val messagesCount = MessageController .getMessagesCountByFolderId(upToDateFolder.id, realm = this )
@@ -503,7 +503,7 @@ class RefreshController @Inject constructor(
503
503
// endregion
504
504
505
505
// region Create Threads
506
- private fun MutableRealm.createThreads (
506
+ private fun MutableRealm.handleAddedMessages (
507
507
scope : CoroutineScope ,
508
508
folder : Folder ,
509
509
remoteMessages : List <Message >,
@@ -521,7 +521,7 @@ class RefreshController @Inject constructor(
521
521
addedMessagesUids.add(remoteMessage.shortUid)
522
522
523
523
val newThread = if (isConversationMode) {
524
- createNewThread (scope, remoteMessage, impactedThreadsManaged)
524
+ handleAddedMessage (scope, remoteMessage, impactedThreadsManaged)
525
525
} else {
526
526
remoteMessage.toThread()
527
527
}
@@ -542,7 +542,20 @@ class RefreshController @Inject constructor(
542
542
return impactedThreadsUnmanaged
543
543
}
544
544
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 (
546
559
scope : CoroutineScope ,
547
560
remoteMessage : Message ,
548
561
impactedThreadsManaged : MutableSet <Thread >,
@@ -559,50 +572,19 @@ class RefreshController @Inject constructor(
559
572
// Create Thread in this Folder
560
573
val thread = createNewThreadIfRequired(scope, remoteMessage, existingThreads, existingMessages)
561
574
// Update Threads in other Folders
562
- updateOtherExistingThreads (scope, remoteMessage, existingThreads, existingMessages, impactedThreadsManaged)
575
+ updateExistingThreads (scope, remoteMessage, existingThreads, existingMessages, impactedThreadsManaged)
563
576
564
577
// Now that all other existing Threads are updated, we need to remove the duplicated Threads.
565
578
if (isThereDuplicatedThreads) removeDuplicatedThreads(remoteMessage.messageIds, impactedThreadsManaged)
566
579
567
580
return thread
568
581
}
569
582
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
-
583
583
private fun MutableRealm.isThereDuplicatedThreads (messageIds : RealmSet <String >, threadsCount : Int ): Boolean {
584
584
val foldersCount = ThreadController .getExistingThreadsFoldersCount(messageIds, realm = this )
585
585
return foldersCount != threadsCount.toLong()
586
586
}
587
587
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
-
606
588
private fun TypedRealm.createNewThreadIfRequired (
607
589
scope : CoroutineScope ,
608
590
newMessage : Message ,
@@ -621,7 +603,7 @@ class RefreshController @Inject constructor(
621
603
return newThread
622
604
}
623
605
624
- private fun MutableRealm.updateOtherExistingThreads (
606
+ private fun MutableRealm.updateExistingThreads (
625
607
scope : CoroutineScope ,
626
608
remoteMessage : Message ,
627
609
existingThreads : RealmResults <Thread >,
@@ -651,6 +633,24 @@ class RefreshController @Inject constructor(
651
633
}
652
634
}
653
635
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
+
654
654
private fun MutableRealm.putNewThreadInRealm (newThread : Thread ): Thread {
655
655
return ThreadController .upsertThread(newThread, realm = this )
656
656
}
0 commit comments