Skip to content

Commit

Permalink
Fixed a bug of deleting outgoing messages. Refactored code.| #793
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed Jan 13, 2020
1 parent 0f085e9 commit f229daa
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ abstract class MessageDao : BaseDao<MessageEntity> {
msgStates: Collection<Int> = listOf(MessageState.SENDING.value,
MessageState.SENT_WITHOUT_LOCAL_COPY.value)): List<MessageEntity>

@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<Int> = listOf(
MessageState.SENDING.value,
MessageState.SENT_WITHOUT_LOCAL_COPY.value)): List<MessageEntity>

@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<MessageEntity>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class MessagesViewModel(application: Application) : BaseAndroidViewModel(applica
}
}

fun deleteOutgoingMsg(messageEntity: MessageEntity) {
fun deleteOutgoingMsg(messageEntity: MessageEntity, updateOutboxMsgsCount: Boolean = false) {
val app = getApplication<Application>()

viewModelScope.launch {
Expand All @@ -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
Expand All @@ -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<Long>, 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)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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?
Expand Down
2 changes: 1 addition & 1 deletion FlowCrypt/src/main/res/menu/fragment_message_details.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

<item
android:id="@+id/menuActionMarkUnread"
android:icon="@drawable/selector_ic_markunread_white"
android:icon="@drawable/selector_ic_mark_unread_white"
android:title="@string/mark_unread"
app:showAsAction="ifRoom" />
</menu>
4 changes: 2 additions & 2 deletions FlowCrypt/src/main/res/menu/message_list_context_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@

<item
android:id="@+id/menuActionMarkUnread"
android:icon="@drawable/selector_ic_markunread_white"
android:icon="@drawable/selector_ic_mark_unread_white"
android:title="@string/mark_unread"
app:showAsAction="ifRoom" />

<item
android:id="@+id/menuActionMarkRead"
android:icon="@drawable/selector_ic_mark_read_white"
android:title="@string/mark_unread"
android:title="@string/mark_read"
app:showAsAction="ifRoom" />
</menu>
1 change: 1 addition & 0 deletions FlowCrypt/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -422,4 +422,5 @@
<string name="reconnect">Reconnect</string>
<string name="access_was_not_granted">Access was not granted</string>
<string name="can_not_send_due_to_auth_failure">Can\'t send message: auth failure</string>
<string name="mark_read">Mark read</string>
</resources>

0 comments on commit f229daa

Please sign in to comment.