diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/database/dao/MessageDao.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/database/dao/MessageDao.kt index 8574a5dc5f..d172e334e8 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/database/dao/MessageDao.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/database/dao/MessageDao.kt @@ -70,6 +70,13 @@ abstract class MessageDao : BaseDao { msgStates: Collection = listOf(MessageState.SENDING.value, MessageState.SENT_WITHOUT_LOCAL_COPY.value)): List + @Query("SELECT * FROM messages WHERE email = :account AND folder = :label AND state NOT IN (:msgStates)") + abstract suspend fun getOutboxMsgsExceptSentSuspend(account: String?, + label: String = JavaEmailConstants.FOLDER_OUTBOX, + msgStates: Collection = listOf( + MessageState.SENDING.value, + MessageState.SENT_WITHOUT_LOCAL_COPY.value)): List + @Query("SELECT * FROM messages WHERE email = :account AND folder = :label AND state IN (:msgStateValue)") abstract fun getOutboxMsgsByState(account: String?, label: String = JavaEmailConstants.FOLDER_OUTBOX, msgStateValue: Int): List diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/MessagesViewModel.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/MessagesViewModel.kt index ae63cf0843..816b2c27c4 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/MessagesViewModel.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/MessagesViewModel.kt @@ -100,7 +100,7 @@ class MessagesViewModel(application: Application) : BaseAndroidViewModel(applica } } - fun deleteOutgoingMsg(messageEntity: MessageEntity) { + fun deleteOutgoingMsg(messageEntity: MessageEntity, updateOutboxMsgsCount: Boolean = false) { val app = getApplication() viewModelScope.launch { @@ -109,15 +109,6 @@ class MessagesViewModel(application: Application) : BaseAndroidViewModel(applica } if (isMsgDeleted) { - val account = getActiveAccountSuspend() - val outgoingMsgCount = roomDatabase.msgDao().getOutboxMsgsExceptSent(account?.email).size - val outboxLabel = roomDatabase.labelDao().getLabelSuspend(account?.email, - JavaEmailConstants.FOLDER_OUTBOX) - - outboxLabel?.let { - roomDatabase.labelDao().updateSuspend(it.copy(messageCount = outgoingMsgCount)) - } - if (messageEntity.hasAttachments == true) { try { val parentDirName = messageEntity.attachmentsDirectory @@ -127,17 +118,48 @@ class MessagesViewModel(application: Application) : BaseAndroidViewModel(applica e.printStackTrace() } } + + if (updateOutboxMsgsCount) { + updateOutboxMsgsCount() + } } } } + private suspend fun updateOutboxMsgsCount() { + val account = getActiveAccountSuspend() + val outgoingMsgCount = roomDatabase.msgDao().getOutboxMsgsExceptSentSuspend(account?.email).size + val outboxLabel = roomDatabase.labelDao().getLabelSuspend(account?.email, + JavaEmailConstants.FOLDER_OUTBOX) + + outboxLabel?.let { + roomDatabase.labelDao().updateSuspend(it.copy(messageCount = outgoingMsgCount)) + } + } + fun changeMsgsState(ids: Collection, localFolder: LocalFolder, newMsgState: MessageState) { viewModelScope.launch { val entities = roomDatabase.msgDao().getMsgsByIDSuspend(localFolder.account, localFolder.fullName, ids.map { it }) - val candidates = prepareCandidates(entities, newMsgState) - roomDatabase.msgDao().updateSuspend(candidates) - msgStatesLiveData.postValue(newMsgState) + + if (JavaEmailConstants.FOLDER_OUTBOX.equals(localFolder.fullName, ignoreCase = true)) { + when (newMsgState) { + MessageState.PENDING_DELETING -> { + for (entity in entities) { + deleteOutgoingMsg(entity) + } + + updateOutboxMsgsCount() + } + + else -> { + } + } + } else { + val candidates = prepareCandidates(entities, newMsgState) + roomDatabase.msgDao().updateSuspend(candidates) + msgStatesLiveData.postValue(newMsgState) + } } } diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/EmailListFragment.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/EmailListFragment.kt index 2f49689703..7e7e0a12b4 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/EmailListFragment.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/EmailListFragment.kt @@ -597,13 +597,16 @@ class EmailListFragment : BaseSyncFragment(), SwipeRefreshLayout.OnRefreshListen val menuItemArchiveMsg = menu?.findItem(R.id.menuActionArchiveMessage) menuItemArchiveMsg?.isVisible = isArchiveActionEnabled() - if (tracker?.selection?.size() == 1) { + val menuActionMarkRead = menu?.findItem(R.id.menuActionMarkRead) + menuActionMarkRead?.isVisible = isChangeSeenStateActionEnabled() + + val menuActionMarkUnread = menu?.findItem(R.id.menuActionMarkUnread) + menuActionMarkUnread?.isVisible = isChangeSeenStateActionEnabled() + + if (isChangeSeenStateActionEnabled() && tracker?.selection?.size() == 1) { val id = tracker?.selection?.first() ?: return true val msgEntity = adapter.getMsgEntity(keyProvider?.getPosition(id)) - val menuActionMarkUnread = menu?.findItem(R.id.menuActionMarkUnread) - val menuActionMarkRead = menu?.findItem(R.id.menuActionMarkRead) - menuActionMarkUnread?.isVisible = msgEntity?.isSeen == true menuActionMarkRead?.isVisible = msgEntity?.isSeen != true } @@ -665,6 +668,18 @@ class EmailListFragment : BaseSyncFragment(), SwipeRefreshLayout.OnRefreshListen return isEnabled } + private fun isChangeSeenStateActionEnabled(): Boolean { + return when (FoldersManager.getFolderType(listener?.currentFolder)) { + FoldersManager.FolderType.OUTBOX -> { + false + } + + else -> { + true + } + } + } + interface OnManageEmailsListener { val currentAccountDao: AccountDao? val currentFolder: LocalFolder? diff --git a/FlowCrypt/src/main/res/drawable/selector_ic_markunread_white.xml b/FlowCrypt/src/main/res/drawable/selector_ic_mark_unread_white.xml similarity index 100% rename from FlowCrypt/src/main/res/drawable/selector_ic_markunread_white.xml rename to FlowCrypt/src/main/res/drawable/selector_ic_mark_unread_white.xml diff --git a/FlowCrypt/src/main/res/menu/fragment_message_details.xml b/FlowCrypt/src/main/res/menu/fragment_message_details.xml index e468f90bf6..8484b3aa2a 100644 --- a/FlowCrypt/src/main/res/menu/fragment_message_details.xml +++ b/FlowCrypt/src/main/res/menu/fragment_message_details.xml @@ -26,7 +26,7 @@ \ No newline at end of file diff --git a/FlowCrypt/src/main/res/menu/message_list_context_menu.xml b/FlowCrypt/src/main/res/menu/message_list_context_menu.xml index b9cf7ccb50..3b1041d106 100644 --- a/FlowCrypt/src/main/res/menu/message_list_context_menu.xml +++ b/FlowCrypt/src/main/res/menu/message_list_context_menu.xml @@ -20,13 +20,13 @@ \ No newline at end of file diff --git a/FlowCrypt/src/main/res/values/strings.xml b/FlowCrypt/src/main/res/values/strings.xml index 5d8f205b2e..9570c05231 100644 --- a/FlowCrypt/src/main/res/values/strings.xml +++ b/FlowCrypt/src/main/res/values/strings.xml @@ -422,4 +422,5 @@ Reconnect Access was not granted Can\'t send message: auth failure + Mark read