Skip to content

Commit

Permalink
Removed getMsgsWithState() in MessageDaoSource. Refactored code.| #793
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed Dec 25, 2019
1 parent ef0bc5b commit 7851d87
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.flowcrypt.email.api.email.sync.SyncListener
import com.flowcrypt.email.database.FlowCryptRoomDatabase
import com.flowcrypt.email.database.MessageState
import com.flowcrypt.email.database.dao.source.AccountDao
import com.flowcrypt.email.database.dao.source.imap.MessageDaoSource
import com.sun.mail.imap.IMAPFolder
import javax.mail.Folder
import javax.mail.Message
Expand All @@ -32,11 +31,11 @@ class ArchiveMsgsSyncTask(ownerKey: String, requestCode: Int) : BaseSyncTask(own
val foldersManager = FoldersManager.fromDatabase(context, account.email)
val inboxFolder = foldersManager.findInboxFolder() ?: return
val allMailFolder = foldersManager.folderAll ?: return
val msgDaoSource = MessageDaoSource()
val roomDatabase = FlowCryptRoomDatabase.getDatabase(context)

while (true) {
val candidatesForArchiving = msgDaoSource.getMsgsWithState(context, account.email,
MessageState.PENDING_ARCHIVING)
val candidatesForArchiving = roomDatabase.msgDao().getMsgsWithState(account.email,
MessageState.PENDING_ARCHIVING.value)

if (candidatesForArchiving.isEmpty()) {
break
Expand All @@ -49,7 +48,7 @@ class ArchiveMsgsSyncTask(ownerKey: String, requestCode: Int) : BaseSyncTask(own

if (msgs.isNotEmpty()) {
remoteSrcFolder.moveMessages(msgs.toTypedArray(), remoteDestFolder)
FlowCryptRoomDatabase.getDatabase(context).msgDao().deleteByUIDs(account.email, inboxFolder.fullName, uidList)
roomDatabase.msgDao().deleteByUIDs(account.email, inboxFolder.fullName, uidList)
}

remoteSrcFolder.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package com.flowcrypt.email.api.email.sync.tasks

import android.content.Context
import com.flowcrypt.email.api.email.sync.SyncListener
import com.flowcrypt.email.database.FlowCryptRoomDatabase
import com.flowcrypt.email.database.MessageState
import com.flowcrypt.email.database.dao.source.AccountDao
import com.flowcrypt.email.database.dao.source.imap.MessageDaoSource
Expand Down Expand Up @@ -40,13 +41,14 @@ class ChangeMsgsReadStateSyncTask(ownerKey: String, requestCode: Int) : BaseSync

private fun changeMsgsReadState(context: Context, account: AccountDao,
msgDaoSource: MessageDaoSource, store: Store, state: MessageState) {
val candidatesForMark = msgDaoSource.getMsgsWithState(context, account.email, state)
val roomDatabase = FlowCryptRoomDatabase.getDatabase(context)
val candidatesForMark = roomDatabase.msgDao().getMsgsWithState(account.email, state.value)

if (candidatesForMark.isNotEmpty()) {
val setOfFolders = candidatesForMark.map { it.label }.toSet()
val setOfFolders = candidatesForMark.map { it.folder }.toSet()

for (folder in setOfFolders) {
val filteredMsgs = candidatesForMark.filter { it.label == folder }
val filteredMsgs = candidatesForMark.filter { it.folder == folder }

if (filteredMsgs.isEmpty()) {
continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.flowcrypt.email.api.email.sync.SyncListener
import com.flowcrypt.email.database.FlowCryptRoomDatabase
import com.flowcrypt.email.database.MessageState
import com.flowcrypt.email.database.dao.source.AccountDao
import com.flowcrypt.email.database.dao.source.imap.MessageDaoSource
import com.sun.mail.imap.IMAPFolder
import javax.mail.Folder
import javax.mail.Message
Expand All @@ -35,19 +34,19 @@ class DeleteMessagesSyncTask(ownerKey: String,
val context = listener.context
val foldersManager = FoldersManager.fromDatabase(context, account.email)
val trash = foldersManager.folderTrash ?: return
val msgDaoSource = MessageDaoSource()
val roomDatabase = FlowCryptRoomDatabase.getDatabase(context)

while (true) {
val candidatesForDeleting = msgDaoSource.getMsgsWithState(
context, account.email, MessageState.PENDING_DELETING)
val candidatesForDeleting = roomDatabase.msgDao().getMsgsWithState(
account.email, MessageState.PENDING_DELETING.value)

if (candidatesForDeleting.isEmpty()) {
break
} else {
val setOfFolders = candidatesForDeleting.map { it.label }.toSet()
val setOfFolders = candidatesForDeleting.map { it.folder }.toSet()

for (folder in setOfFolders) {
val filteredMsgs = candidatesForDeleting.filter { it.label == folder }
val filteredMsgs = candidatesForDeleting.filter { it.folder == folder }

if (filteredMsgs.isEmpty() || JavaEmailConstants.FOLDER_OUTBOX.equals(folder, ignoreCase = true)) {
continue
Expand All @@ -62,7 +61,7 @@ class DeleteMessagesSyncTask(ownerKey: String,

if (msgs.isNotEmpty()) {
remoteSrcFolder.moveMessages(msgs.toTypedArray(), remoteDestFolder)
FlowCryptRoomDatabase.getDatabase(context).msgDao().deleteByUIDs(account.email, folder, uidList)
roomDatabase.msgDao().deleteByUIDs(account.email, folder, uidList)
}

remoteSrcFolder.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.flowcrypt.email.api.email.sync.SyncListener
import com.flowcrypt.email.database.FlowCryptRoomDatabase
import com.flowcrypt.email.database.MessageState
import com.flowcrypt.email.database.dao.source.AccountDao
import com.flowcrypt.email.database.dao.source.imap.MessageDaoSource
import com.sun.mail.imap.IMAPFolder
import javax.mail.Folder
import javax.mail.Message
Expand All @@ -32,19 +31,19 @@ class MovingToInboxSyncTask(ownerKey: String, requestCode: Int) : BaseSyncTask(o
val context = listener.context
val foldersManager = FoldersManager.fromDatabase(context, account.email)
val inboxFolder = foldersManager.findInboxFolder() ?: return
val msgDaoSource = MessageDaoSource()
val roomDatabase = FlowCryptRoomDatabase.getDatabase(context)

while (true) {
val candidatesForMovingToInbox = msgDaoSource.getMsgsWithState(context, account.email,
MessageState.PENDING_MOVE_TO_INBOX)
val candidatesForMovingToInbox = roomDatabase.msgDao().getMsgsWithState(account.email,
MessageState.PENDING_MOVE_TO_INBOX.value)

if (candidatesForMovingToInbox.isEmpty()) {
break
} else {
val setOfFolders = candidatesForMovingToInbox.map { it.label }.toSet()
val setOfFolders = candidatesForMovingToInbox.map { it.folder }.toSet()

for (folder in setOfFolders) {
val filteredMsgs = candidatesForMovingToInbox.filter { it.label == folder }
val filteredMsgs = candidatesForMovingToInbox.filter { it.folder == folder }

if (filteredMsgs.isEmpty() || JavaEmailConstants.FOLDER_OUTBOX.equals(folder, ignoreCase = true)) {
continue
Expand All @@ -59,7 +58,7 @@ class MovingToInboxSyncTask(ownerKey: String, requestCode: Int) : BaseSyncTask(o

if (msgs.isNotEmpty()) {
remoteSrcFolder.moveMessages(msgs.toTypedArray(), remoteDestFolder)
FlowCryptRoomDatabase.getDatabase(context).msgDao().deleteByUIDs(account.email, inboxFolder.fullName, uidList)
roomDatabase.msgDao().deleteByUIDs(account.email, inboxFolder.fullName, uidList)
}

remoteSrcFolder.close(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ abstract class MessagesDao : BaseDao<MessageEntity> {
@Query("SELECT uid, flags FROM messages WHERE email = :account AND folder = :label")
abstract fun getUIDAndFlagsPairs(account: String?, label: String): List<UidFlagsPair>

@Query("SELECT * FROM messages WHERE email = :account AND state =:stateValue")
abstract fun getMsgsWithState(account: String?, stateValue: Int): List<MessageEntity>

@Transaction
open fun deleteByUIDs(email: String?, label: String?, msgsUID: Collection<Long>) {
val step = 50
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,33 +199,6 @@ class MessageDaoSource : BaseDaoSource() {
return details
}

/**
* Get messages of some folder by the given state.
*
* @param context Interface to global information about an application environment.
* @param email The email of the [LocalFolder].
* @param state The message state.
* @return A list of [GeneralMessageDetails] objects.
*/
fun getMsgsWithState(context: Context, email: String, state: MessageState):
List<GeneralMessageDetails> {
val contentResolver = context.contentResolver
val selection = "$COL_EMAIL= ? AND $COL_STATE = ?"
val cursor = contentResolver.query(baseContentUri,
null, selection, arrayOf(email, state.value.toString()), null)

val generalMsgDetailsList = mutableListOf<GeneralMessageDetails>()

if (cursor != null) {
while (cursor.moveToNext()) {
generalMsgDetailsList.add(getMsgInfo(cursor))
}
cursor.close()
}

return generalMsgDetailsList
}

/**
* Get new messages.
*
Expand Down

0 comments on commit 7851d87

Please sign in to comment.