@@ -70,17 +70,17 @@ class ThreadController @Inject constructor(
7070 }
7171
7272 /* *
73- * Initialize and retrieve the search threads obtained from the API.
73+ * Initialize and retrieve the search Threads obtained from the API.
7474 * - Format the remote threads to make them compatible with the existing logic.
7575 * - Preserve old message data if it already exists locally.
7676 * - Handle duplicates using the existing logic.
77- * @param remoteThreads The list of API threads that need to be processed.
78- * @param folderRole The role of the selected folder. This is only useful when selecting the spam or trash folder .
79- * @return A list of search threads . The search only returns messages from spam or trash if we explicitly selected those folders
77+ * @param remoteThreads The list of API Threads that need to be processed.
78+ * @param filterFolder The selected Folder on which we filter the Search .
79+ * @return A list of search Threads . The search only returns Messages from SPAM or TRASH if we explicitly selected those folders
8080 */
8181 suspend fun initAndGetSearchFolderThreads (
8282 remoteThreads : List <Thread >,
83- folderRole : FolderRole ? ,
83+ filterFolder : Folder ? ,
8484 ): List <Thread > = withContext(ioDispatcher) {
8585
8686 fun MutableRealm.keepOldMessagesAndAddToSearchFolder (remoteThread : Thread , searchFolder : Folder ) {
@@ -90,12 +90,12 @@ class ThreadController @Inject constructor(
9090
9191 val localMessage = MessageController .getMessage(remoteMessage.uid, realm = this )
9292
93- // The Search only returns Messages from SPAM or TRASH if we explicitly selected those folders ,
94- // which is the reason why we can compute `isSpam` and `isTrashed` values so loosely.
93+ // The Search only returns Messages from TRASH if we explicitly selected this folder ,
94+ // which is the reason why we can compute the `isTrashed` value so loosely.
9595 remoteMessage.initLocalValues(
9696 date = localMessage?.date ? : remoteMessage.date,
9797 isFullyDownloaded = localMessage?.isFullyDownloaded() ? : false ,
98- isTrashed = folderRole == FolderRole .TRASH ,
98+ isTrashed = filterFolder?.role == FolderRole .TRASH ,
9999 isFromSearch = localMessage == null ,
100100 draftLocalUuid = localMessage?.draftLocalUuid,
101101 latestCalendarEventResponse = null ,
@@ -113,7 +113,13 @@ class ThreadController @Inject constructor(
113113 remoteThreads.map { remoteThread ->
114114 ensureActive()
115115 remoteThread.isFromSearch = true
116- remoteThread.folderId = remoteThread.messages.first().folderId
116+
117+ val folderId = if (remoteThread.messages.count() == 1 ) {
118+ remoteThread.messages.single().folderId
119+ } else {
120+ filterFolder!! .id
121+ }
122+ remoteThread.folderId = folderId
117123
118124 keepOldMessagesAndAddToSearchFolder(remoteThread, searchFolder)
119125
0 commit comments