diff --git a/MailCore/Cache/MailboxManager/MailboxManager+Message.swift b/MailCore/Cache/MailboxManager/MailboxManager+Message.swift index 5d032c5e77..d8bdf1193b 100644 --- a/MailCore/Cache/MailboxManager/MailboxManager+Message.swift +++ b/MailCore/Cache/MailboxManager/MailboxManager+Message.swift @@ -332,7 +332,7 @@ public extension MailboxManager { await backgroundRealm.execute { [self] realm in if let folder = folder.fresh(using: realm) { - createMultiMessagesThreads(messageByUids: messageByUidsResult, folder: folder, using: realm) + createThreads(messageByUids: messageByUidsResult, folder: folder, using: realm) } SentryDebug.sendMissingMessagesSentry( sentUids: uniqueUids, @@ -343,7 +343,7 @@ public extension MailboxManager { } } - private func createMultiMessagesThreads(messageByUids: MessageByUidsResult, folder: Folder, using realm: Realm) { + private func createThreads(messageByUids: MessageByUidsResult, folder: Folder, using realm: Realm) { var threadsToUpdate = Set() try? realm.safeWrite { for message in messageByUids.messages { @@ -360,28 +360,12 @@ public extension MailboxManager { } message.inTrash = folder.role == .trash message.computeReference() - let existingThreads = Array(realm.objects(Thread.self) - .where { $0.messageIds.containsAny(in: message.linkedUids) }) - - if let newThread = createNewThreadIfRequired( - for: message, - folder: folder, - existingThreads: existingThreads - ) { - threadsToUpdate.insert(newThread) - } - - var allExistingMessages = Set(existingThreads.flatMap(\.messages)) - allExistingMessages.insert(message) - - for thread in existingThreads { - for existingMessage in allExistingMessages { - if !thread.messages.map(\.uid).contains(existingMessage.uid) { - thread.addMessageIfNeeded(newMessage: message.fresh(using: realm) ?? message) - } - } - threadsToUpdate.insert(thread) + let isThreadMode = UserDefaults.shared.threadMode == .conversation + if isThreadMode { + createConversationThread(message: message, folder: folder, threadsToUpdate: &threadsToUpdate, using: realm) + } else { + createSingleMessageThread(message: message, folder: folder) // threadsToUpdate ? } if let message = realm.objects(Message.self).first(where: { $0.uid == message.uid }) { @@ -392,6 +376,42 @@ public extension MailboxManager { } } + private func createConversationThread( + message: Message, + folder: Folder, + threadsToUpdate: inout Set, + using realm: Realm + ) { + let existingThreads = Array(realm.objects(Thread.self) + .where { $0.messageIds.containsAny(in: message.linkedUids) /* && $0.isConversationThread == true */ }) + + if let newThread = createNewThreadIfRequired( + for: message, + folder: folder, + existingThreads: existingThreads + ) { + threadsToUpdate.insert(newThread) + } + + var allExistingMessages = Set(existingThreads.flatMap(\.messages)) + allExistingMessages.insert(message) + + for thread in existingThreads { + for existingMessage in allExistingMessages { + if !thread.messages.map(\.uid).contains(existingMessage.uid) { + thread.addMessageIfNeeded(newMessage: message.fresh(using: realm) ?? message) + } + } + + threadsToUpdate.insert(thread) + } + } + + private func createSingleMessageThread(message: Message, folder: Folder) { + let thread = message.toThread().detached() + folder.threads.insert(thread) + } + private func createNewThreadIfRequired(for message: Message, folder: Folder, existingThreads: [Thread]) -> Thread? { guard !existingThreads.contains(where: { $0.folder == folder }) else { return nil } diff --git a/MailCore/Cache/MailboxManager/MailboxManager.swift b/MailCore/Cache/MailboxManager/MailboxManager.swift index 484f40aba9..f8c3650da0 100644 --- a/MailCore/Cache/MailboxManager/MailboxManager.swift +++ b/MailCore/Cache/MailboxManager/MailboxManager.swift @@ -184,6 +184,27 @@ public final class MailboxManager: ObservableObject, MailboxManageable { let realm = getRealm() return realm.objects(Folder.self).contains { $0.unreadCount > 0 } } + + public func cleanRealm() { + Task { + await backgroundRealm.execute { realm in + + let folders = realm.objects(Folder.self) + let threads = realm.objects(Thread.self) + let messages = realm.objects(Message.self) + + try? realm.safeWrite { + realm.delete(threads) + realm.delete(messages) + for folder in folders { + folder.cursor = nil + folder.resetHistoryInfo() + folder.computeUnreadCount() + } + } + } + } + } } // MARK: - Equatable conformance diff --git a/MailResources/Localizable/de.lproj/Localizable.strings b/MailResources/Localizable/de.lproj/Localizable.strings index 961a117e44..703e8987d6 100644 --- a/MailResources/Localizable/de.lproj/Localizable.strings +++ b/MailResources/Localizable/de.lproj/Localizable.strings @@ -5,7 +5,7 @@ * Locale: de, German * Tagged: ios * Exported by: Ambroise Decouttere - * Exported at: Wed, 09 Aug 2023 18:11:40 +0200 + * Exported at: Thu, 10 Aug 2023 14:44:55 +0200 */ /* loco:6256793050618f7416758a32 */ @@ -1223,12 +1223,12 @@ /* loco:64c21f7b8b1932323c047792 */ "newAccountStorageDrive" = "15 GB kDrive-Speicher"; +/* loco:64ca16484dbf65eb9b0ea862 */ +"pleaseLogInFirst" = "Bitte melden Sie sich zuerst in der ikMail-App an."; + /* loco:64cca81829e8aeaec602e1c2 */ "settingsThreadModeTitle" = "Modus der Nachrichtenanzeige"; -/* loco:64ccb58ceef1939fc6061fa2 */ -"contentDescriptionButtonDeleteSearch" = "Suche löschen"; - /* loco:64d209e0b3606abecd06b622 */ "errorNewFolderInvalidCharacter" = "Der Ordnername enthält ungültige Zeichen"; diff --git a/MailResources/Localizable/en.lproj/Localizable.strings b/MailResources/Localizable/en.lproj/Localizable.strings index 148f5ef88c..78ec61e98f 100644 --- a/MailResources/Localizable/en.lproj/Localizable.strings +++ b/MailResources/Localizable/en.lproj/Localizable.strings @@ -5,7 +5,7 @@ * Locale: en, English * Tagged: ios * Exported by: Ambroise Decouttere - * Exported at: Wed, 09 Aug 2023 18:11:40 +0200 + * Exported at: Thu, 10 Aug 2023 14:44:54 +0200 */ /* loco:6256793050618f7416758a32 */ @@ -1223,12 +1223,12 @@ /* loco:64c21f7b8b1932323c047792 */ "newAccountStorageDrive" = "15 GB of kDrive storage"; +/* loco:64ca16484dbf65eb9b0ea862 */ +"pleaseLogInFirst" = "Please login in the ikMail app first."; + /* loco:64cca81829e8aeaec602e1c2 */ "settingsThreadModeTitle" = "Message display mode"; -/* loco:64ccb58ceef1939fc6061fa2 */ -"contentDescriptionButtonDeleteSearch" = "Clear search"; - /* loco:64d209e0b3606abecd06b622 */ "errorNewFolderInvalidCharacter" = "The folder name contains invalid characters"; diff --git a/MailResources/Localizable/es.lproj/Localizable.strings b/MailResources/Localizable/es.lproj/Localizable.strings index e66656f45e..6b8f96f5c7 100644 --- a/MailResources/Localizable/es.lproj/Localizable.strings +++ b/MailResources/Localizable/es.lproj/Localizable.strings @@ -5,7 +5,7 @@ * Locale: es, Spanish * Tagged: ios * Exported by: Ambroise Decouttere - * Exported at: Wed, 09 Aug 2023 18:11:40 +0200 + * Exported at: Thu, 10 Aug 2023 14:44:55 +0200 */ /* loco:6256793050618f7416758a32 */ @@ -1223,12 +1223,12 @@ /* loco:64c21f7b8b1932323c047792 */ "newAccountStorageDrive" = "15 GB de almacenamiento kDrive"; +/* loco:64ca16484dbf65eb9b0ea862 */ +"pleaseLogInFirst" = "Inicia sesión primero en la aplicación ikMail."; + /* loco:64cca81829e8aeaec602e1c2 */ "settingsThreadModeTitle" = "Modo de visualización de mensajes"; -/* loco:64ccb58ceef1939fc6061fa2 */ -"contentDescriptionButtonDeleteSearch" = "Borrar búsqueda"; - /* loco:64d209e0b3606abecd06b622 */ "errorNewFolderInvalidCharacter" = "El nombre de la carpeta contiene caracteres no válidos"; diff --git a/MailResources/Localizable/fr.lproj/Localizable.strings b/MailResources/Localizable/fr.lproj/Localizable.strings index 187e2bba5d..02bfbd63a6 100644 --- a/MailResources/Localizable/fr.lproj/Localizable.strings +++ b/MailResources/Localizable/fr.lproj/Localizable.strings @@ -5,7 +5,7 @@ * Locale: fr, French * Tagged: ios * Exported by: Ambroise Decouttere - * Exported at: Wed, 09 Aug 2023 18:11:40 +0200 + * Exported at: Thu, 10 Aug 2023 14:44:55 +0200 */ /* loco:6256793050618f7416758a32 */ @@ -1223,12 +1223,12 @@ /* loco:64c21f7b8b1932323c047792 */ "newAccountStorageDrive" = "15 Go de stockage kDrive"; +/* loco:64ca16484dbf65eb9b0ea862 */ +"pleaseLogInFirst" = "Veuillez d'abord vous connecter à l'application ikMail."; + /* loco:64cca81829e8aeaec602e1c2 */ "settingsThreadModeTitle" = "Mode d’affichage des messages"; -/* loco:64ccb58ceef1939fc6061fa2 */ -"contentDescriptionButtonDeleteSearch" = "Effacer la recherche"; - /* loco:64d209e0b3606abecd06b622 */ "errorNewFolderInvalidCharacter" = "Le nom du dossier comporte des caractères invalides"; diff --git a/MailResources/Localizable/it.lproj/Localizable.strings b/MailResources/Localizable/it.lproj/Localizable.strings index a2d34b8ede..9af74fad76 100644 --- a/MailResources/Localizable/it.lproj/Localizable.strings +++ b/MailResources/Localizable/it.lproj/Localizable.strings @@ -5,7 +5,7 @@ * Locale: it, Italian * Tagged: ios * Exported by: Ambroise Decouttere - * Exported at: Wed, 09 Aug 2023 18:11:40 +0200 + * Exported at: Thu, 10 Aug 2023 14:44:55 +0200 */ /* loco:6256793050618f7416758a32 */ @@ -1223,12 +1223,12 @@ /* loco:64c21f7b8b1932323c047792 */ "newAccountStorageDrive" = "15 GB di memoria kDrive"; +/* loco:64ca16484dbf65eb9b0ea862 */ +"pleaseLogInFirst" = "Effettuare prima il login nell'app ikMail."; + /* loco:64cca81829e8aeaec602e1c2 */ "settingsThreadModeTitle" = "Modalità di visualizzazione dei messaggi"; -/* loco:64ccb58ceef1939fc6061fa2 */ -"contentDescriptionButtonDeleteSearch" = "Cancellare la ricerca"; - /* loco:64d209e0b3606abecd06b622 */ "errorNewFolderInvalidCharacter" = "Il nome della cartella contiene caratteri non validi";