From 52bd186e6303a8b01c182e48531094dd1fae23ed Mon Sep 17 00:00:00 2001 From: DenBond7 Date: Wed, 25 Dec 2019 15:50:09 +0200 Subject: [PATCH] Removed resetMsgsWithSendingState() in MessageDaoSource. Refactored code.| #793 --- .../email/database/dao/MessagesDao.kt | 10 +++++++++ .../dao/source/imap/MessageDaoSource.kt | 21 ------------------- .../jobscheduler/MessagesSenderJobService.kt | 4 ++-- 3 files changed, 12 insertions(+), 23 deletions(-) diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/database/dao/MessagesDao.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/database/dao/MessagesDao.kt index a3f5ac9d26..f81a246cbc 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/database/dao/MessagesDao.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/database/dao/MessagesDao.kt @@ -104,6 +104,16 @@ abstract class MessagesDao : BaseDao { @Query("UPDATE messages SET state=:newValues WHERE email = :account AND folder = :label AND state = :oldValue") abstract fun changeMsgsState(account: String?, label: String?, oldValue: Int, newValues: Int): Int + /** + * Add the messages which have a current state equal [MessageState.SENDING] to the sending queue again. + * + * @param account The email that the message linked + */ + @Query("UPDATE messages SET state=2 WHERE email = :account AND folder = :label AND state =:oldValue") + abstract fun resetMsgsWithSendingState(account: String?, + label: String = JavaEmailConstants.FOLDER_OUTBOX, + oldValue: Int = MessageState.SENDING.value): Int + @Query("SELECT uid, flags FROM messages WHERE email = :account AND folder = :label") abstract fun getUIDAndFlagsPairs(account: String?, label: String): List diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/database/dao/source/imap/MessageDaoSource.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/database/dao/source/imap/MessageDaoSource.kt index b4bd24e87b..6ea1e906d6 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/database/dao/source/imap/MessageDaoSource.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/database/dao/source/imap/MessageDaoSource.kt @@ -11,7 +11,6 @@ import android.database.Cursor import android.os.Build import android.provider.BaseColumns import android.text.TextUtils -import com.flowcrypt.email.api.email.JavaEmailConstants import com.flowcrypt.email.api.email.model.GeneralMessageDetails import com.flowcrypt.email.api.email.model.LocalFolder import com.flowcrypt.email.api.email.model.MessageFlag @@ -288,26 +287,6 @@ class MessageDaoSource : BaseDaoSource() { return details } - /** - * Add the messages which have a current state equal [MessageState.SENDING] to the sending queue again. - * - * @param context Interface to global information about an application environment - * @param email The email that the message linked - * @return The count of the updated row or -1 up. - */ - fun resetMsgsWithSendingState(context: Context, email: String?): Int { - val contentValues = ContentValues() - contentValues.put(COL_STATE, MessageState.QUEUED.value) - - val contentResolver = context.contentResolver - return if (email != null && contentResolver != null) { - val selection = "$COL_EMAIL= ? AND $COL_FOLDER = ? AND $COL_STATE = ? " - val selectionArgs = arrayOf(email, JavaEmailConstants.FOLDER_OUTBOX, MessageState.SENDING.value.toString()) - contentResolver.update(baseContentUri, contentValues, selection, selectionArgs) - } else - -1 - } - private fun parseFlags(string: String): Array { return parseArray(string, "\\s") } diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/jobscheduler/MessagesSenderJobService.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/jobscheduler/MessagesSenderJobService.kt index cc9a1f616b..d0a4b173ec 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/jobscheduler/MessagesSenderJobService.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/jobscheduler/MessagesSenderJobService.kt @@ -118,7 +118,7 @@ class MessagesSenderJobService : JobService() { val attsCacheDir = File(context.cacheDir, Constants.ATTACHMENTS_CACHE_DIR) if (account != null) { - msgDaoSource.resetMsgsWithSendingState(context, account.email) + roomDatabase.msgDao().resetMsgsWithSendingState(account.email) val queuedMsgs = roomDatabase.msgDao().getOutboxMessages(account = account.email, msgStateValue = MessageState.QUEUED.value) @@ -214,7 +214,7 @@ class MessagesSenderJobService : JobService() { val msgLabel = msgEntity.folder try { - msgDaoSource.resetMsgsWithSendingState(context, email) + roomDatabase.msgDao().resetMsgsWithSendingState(account.email) msgDaoSource.updateMsgState(context, msgEmail, msgLabel, msgUid, MessageState.SENDING) Thread.sleep(2000)