Skip to content

Commit

Permalink
fix: List snooze folder when computing what folder are impacted by an…
Browse files Browse the repository at this point in the history
… action
  • Loading branch information
LunarX authored and KevinBoulongne committed Mar 7, 2025
1 parent fc73910 commit d1d7d61
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
24 changes: 12 additions & 12 deletions app/src/main/java/com/infomaniak/mail/ui/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,9 @@ import com.infomaniak.mail.R
import com.infomaniak.mail.data.LocalSettings
import com.infomaniak.mail.data.api.ApiRepository
import com.infomaniak.mail.data.cache.RealmDatabase
import com.infomaniak.mail.data.cache.mailboxContent.FolderController
import com.infomaniak.mail.data.cache.mailboxContent.MessageController
import com.infomaniak.mail.data.cache.mailboxContent.RefreshController
import com.infomaniak.mail.data.cache.mailboxContent.*
import com.infomaniak.mail.data.cache.mailboxContent.RefreshController.RefreshCallbacks
import com.infomaniak.mail.data.cache.mailboxContent.RefreshController.RefreshMode
import com.infomaniak.mail.data.cache.mailboxContent.ThreadController
import com.infomaniak.mail.data.cache.mailboxInfo.MailboxController
import com.infomaniak.mail.data.cache.mailboxInfo.PermissionsController
import com.infomaniak.mail.data.cache.mailboxInfo.QuotasController
Expand Down Expand Up @@ -552,7 +549,8 @@ class MainViewModel @Inject constructor(
threadController.updateIsLocallyMovedOutStatus(threadsUids, hasBeenMovedOut = false)

val undoDestinationId = message?.folderId ?: threads.first().folderId
val undoFoldersIds = (messages.getFoldersIds(exception = undoDestinationId) + trashId).filterNotNull()
val undoFoldersIds = messages.getFoldersIds(exception = undoDestinationId)
if (trashId != null) undoFoldersIds += trashId
showDeleteSnackbar(
apiResponses,
shouldPermanentlyDelete,
Expand All @@ -569,7 +567,7 @@ class MainViewModel @Inject constructor(
shouldPermanentlyDelete: Boolean,
message: Message?,
undoResources: List<String>,
undoFoldersIds: List<String>,
undoFoldersIds: ImpactedFolders,
undoDestinationId: String?,
numberOfImpactedThreads: Int,
) {
Expand Down Expand Up @@ -608,7 +606,7 @@ class MainViewModel @Inject constructor(

if (apiResponse.isSuccess() && mailbox.uuid == targetMailboxUuid) {
val draftFolderId = folderController.getFolder(FolderRole.DRAFT)!!.id
refreshFoldersAsync(mailbox, listOf(draftFolderId))
refreshFoldersAsync(mailbox, ImpactedFolders(mutableSetOf(draftFolderId)))
}

showDeletedDraftSnackbar(apiResponse)
Expand All @@ -626,7 +624,7 @@ class MainViewModel @Inject constructor(
with(ApiRepository.rescheduleDraft(resource, scheduleDate)) {
if (isSuccess()) {
val scheduledDraftsFolderId = folderController.getFolder(FolderRole.SCHEDULED_DRAFTS)!!.id
refreshFoldersAsync(currentMailbox.value!!, listOf(scheduledDraftsFolderId))
refreshFoldersAsync(currentMailbox.value!!, ImpactedFolders(mutableSetOf(scheduledDraftsFolderId)))
} else {
snackbarManager.postValue(title = appContext.getString(translatedError))
}
Expand All @@ -645,7 +643,7 @@ class MainViewModel @Inject constructor(

if (apiResponse.isSuccess()) {
val scheduledDraftsFolderId = folderController.getFolder(FolderRole.SCHEDULED_DRAFTS)!!.id
refreshFoldersAsync(mailbox, listOf(scheduledDraftsFolderId))
refreshFoldersAsync(mailbox, ImpactedFolders(mutableSetOf(scheduledDraftsFolderId)))
onSuccess()
} else {
snackbarManager.postValue(title = appContext.getString(apiResponse.translatedError))
Expand All @@ -658,7 +656,7 @@ class MainViewModel @Inject constructor(

if (apiResponse.isSuccess()) {
val scheduledDraftsFolderId = folderController.getFolder(FolderRole.SCHEDULED_DRAFTS)!!.id
refreshFoldersAsync(mailbox, listOf(scheduledDraftsFolderId))
refreshFoldersAsync(mailbox, ImpactedFolders(mutableSetOf(scheduledDraftsFolderId)))
}

showUnscheduledDraftSnackbar(apiResponse)
Expand Down Expand Up @@ -734,9 +732,11 @@ class MainViewModel @Inject constructor(
null
} else {
val undoDestinationId = message?.folderId ?: threads.first().folderId
val foldersIds = messages.getFoldersIds(exception = undoDestinationId)
foldersIds += destinationFolder.id
UndoData(
resources = apiResponses.mapNotNull { it.data?.undoResource },
foldersIds = messages.getFoldersIds(exception = undoDestinationId) + destinationFolder.id,
foldersIds = foldersIds,
destinationFolderId = undoDestinationId,
)
}
Expand Down Expand Up @@ -1114,7 +1114,7 @@ class MainViewModel @Inject constructor(

private fun refreshFoldersAsync(
mailbox: Mailbox,
messagesFoldersIds: List<String>,
messagesFoldersIds: ImpactedFolders,
destinationFolderId: String? = null,
callbacks: RefreshCallbacks? = null,
) = viewModelScope.launch(ioCoroutineContext) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Infomaniak Mail - Android
* Copyright (C) 2023-2024 Infomaniak Network SA
* Copyright (C) 2023-2025 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -23,6 +23,7 @@ import androidx.fragment.app.FragmentActivity
import com.google.android.material.snackbar.Snackbar
import com.infomaniak.lib.core.utils.SingleLiveEvent
import com.infomaniak.lib.core.utils.SnackbarUtils.showSnackbar
import com.infomaniak.mail.data.cache.mailboxContent.ImpactedFolders
import javax.inject.Inject
import javax.inject.Singleton
import com.infomaniak.lib.core.R as RCore
Expand Down Expand Up @@ -91,7 +92,7 @@ class SnackbarManager @Inject constructor() {

data class UndoData(
val resources: List<String>,
val foldersIds: List<String>,
val foldersIds: ImpactedFolders,
val destinationFolderId: String?,
)
}
8 changes: 5 additions & 3 deletions app/src/main/java/com/infomaniak/mail/utils/SharedUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.infomaniak.mail.R
import com.infomaniak.mail.data.LocalSettings
import com.infomaniak.mail.data.api.ApiRepository
import com.infomaniak.mail.data.cache.RealmDatabase
import com.infomaniak.mail.data.cache.mailboxContent.ImpactedFolders
import com.infomaniak.mail.data.cache.mailboxContent.MessageController
import com.infomaniak.mail.data.cache.mailboxContent.RefreshController
import com.infomaniak.mail.data.cache.mailboxContent.RefreshController.RefreshCallbacks
Expand Down Expand Up @@ -111,22 +112,23 @@ class SharedUtils @Inject constructor(

suspend fun refreshFolders(
mailbox: Mailbox,
messagesFoldersIds: List<String>,
messagesFoldersIds: ImpactedFolders,
destinationFolderId: String? = null,
currentFolderId: String? = null,
callbacks: RefreshCallbacks? = null,
) {
val realm = mailboxContentRealm()

// We always want to refresh the `destinationFolder` last, to avoid any blink on the UI.
val foldersIds = messagesFoldersIds.toMutableSet()
val foldersIds = messagesFoldersIds.getFolderIds(realm).toMutableSet()
destinationFolderId?.let(foldersIds::add)

foldersIds.forEach { folderId ->
refreshController.refreshThreads(
refreshMode = RefreshMode.REFRESH_FOLDER,
mailbox = mailbox,
folderId = folderId,
realm = mailboxContentRealm(),
realm = realm,
callbacks = if (folderId == currentFolderId) callbacks else null,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ import com.infomaniak.mail.MainApplication
import com.infomaniak.mail.R
import com.infomaniak.mail.data.LocalSettings.ThreadDensity
import com.infomaniak.mail.data.cache.mailboxContent.FolderController
import com.infomaniak.mail.data.cache.mailboxContent.ImpactedFolders
import com.infomaniak.mail.data.models.Attachment
import com.infomaniak.mail.data.models.Folder
import com.infomaniak.mail.data.models.Folder.FolderRole
import com.infomaniak.mail.data.models.SnoozeState
import com.infomaniak.mail.data.models.correspondent.Correspondent
import com.infomaniak.mail.data.models.correspondent.MergedContact
import com.infomaniak.mail.data.models.correspondent.Recipient
Expand Down Expand Up @@ -409,7 +411,22 @@ fun List<Folder>.addDividerBeforeFirstCustomFolder(dividerType: Any): List<Any>
//endregion

//region Messages
fun List<Message>.getFoldersIds(exception: String? = null) = mapNotNull { if (it.folderId == exception) null else it.folderId }
fun List<Message>.getFoldersIds(exception: String? = null): ImpactedFolders {
val impactedFolders = ImpactedFolders()

forEach { message ->
when {
message.folderId == exception -> Unit
message.snoozeState == SnoozeState.Snoozed -> {
impactedFolders += message.folderId
impactedFolders += FolderRole.SNOOZED
}
else -> impactedFolders += message.folderId
}
}

return impactedFolders
}

fun List<Message>.getUids(): List<String> = map { it.uid }
//endregion
Expand Down

0 comments on commit d1d7d61

Please sign in to comment.