Skip to content

Commit eaa6196

Browse files
authored
Merge pull request #1701 from Infomaniak/fix-search-wrong-folder-id
Fix wrong Folder ID in Search Messages
2 parents 445f896 + 84efd79 commit eaa6196

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

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

+15-9
Original file line numberDiff line numberDiff line change
@@ -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

app/src/main/java/com/infomaniak/mail/ui/main/search/SearchFragment.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ class SearchFragment : TwoPaneFragment() {
174174
popupMenu.setAdapter(searchAdapter)
175175

176176
updateFolderDropDownUi(
177-
folder = searchViewModel.currentFolder,
178-
title = requireContext().getLocalizedNameOrAllFolders(searchViewModel.currentFolder),
177+
folder = searchViewModel.filterFolder,
178+
title = requireContext().getLocalizedNameOrAllFolders(searchViewModel.filterFolder),
179179
)
180180
}
181181

app/src/main/java/com/infomaniak/mail/ui/main/search/SearchViewModel.kt

+7-10
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class SearchViewModel @Inject constructor(
6464
/** Needed to pass API request's validation, but won't be used by the API */
6565
private val dummyFolderId inline get() = savedStateHandle.get<String>(SearchFragmentArgs::dummyFolderId.name)!!
6666

67-
var currentFolder: Folder? = null
67+
var filterFolder: Folder? = null
6868
private set
6969
var currentSearchQuery: String = ""
7070
private set
@@ -95,7 +95,7 @@ class SearchViewModel @Inject constructor(
9595

9696
fun executePendingSearch() = viewModelScope.launch(ioCoroutineContext) {
9797
val hasPendingSearch = (lastExecutedSearchQuery != currentSearchQuery)
98-
|| (lastExecutedFolder != currentFolder)
98+
|| (lastExecutedFolder != filterFolder)
9999
|| (lastExecutedFilters != currentFilters)
100100

101101
if (hasPendingSearch) search()
@@ -111,7 +111,7 @@ class SearchViewModel @Inject constructor(
111111
}
112112

113113
fun selectFolder(folder: Folder?) = viewModelScope.launch(ioCoroutineContext) {
114-
search(folder = folder.also { currentFolder = it })
114+
search(folder = folder.also { filterFolder = it })
115115
}
116116

117117
fun setFilter(filter: ThreadFilter, isEnabled: Boolean = true) = viewModelScope.launch(ioCoroutineContext) {
@@ -164,7 +164,7 @@ class SearchViewModel @Inject constructor(
164164
query: String = currentSearchQuery,
165165
saveInHistory: Boolean = false,
166166
filters: Set<ThreadFilter> = currentFilters,
167-
folder: Folder? = currentFolder,
167+
folder: Folder? = filterFolder,
168168
shouldGetNextPage: Boolean = false,
169169
) = withContext(ioCoroutineContext) {
170170
searchJob?.cancel()
@@ -191,7 +191,7 @@ class SearchViewModel @Inject constructor(
191191

192192
val newFilters = if (folder == null) filters else (filters + ThreadFilter.FOLDER)
193193

194-
return if (newFilters.isEmpty() && query.isNullOrBlank() && currentFolder == null) {
194+
return if (newFilters.isEmpty() && query.isNullOrBlank() && filterFolder == null) {
195195
searchUtils.deleteRealmSearchData()
196196
visibilityMode.postValue(VisibilityMode.RECENT_SEARCHES)
197197
null
@@ -209,11 +209,8 @@ class SearchViewModel @Inject constructor(
209209

210210
suspend fun ApiResponse<ThreadResult>.initSearchFolderThreads() {
211211
runCatching {
212-
data?.threads?.let {
213-
threadController.initAndGetSearchFolderThreads(
214-
remoteThreads = it,
215-
folderRole = folder?.role
216-
)
212+
data?.threads?.let { remoteThreads ->
213+
threadController.initAndGetSearchFolderThreads(remoteThreads, folder)
217214
}
218215
}.getOrElse { exception ->
219216
exception.printStackTrace()

0 commit comments

Comments
 (0)