Skip to content

Commit

Permalink
Removed getMapOfUIDAndMsgFlags() in MessageDaoSource. Refactored code.|
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed Dec 25, 2019
1 parent 56783cd commit d284efa
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,8 @@ class EmailUtil {
* @return An array of the messages which are candidates for updating iin the local database.
*/
@JvmStatic
fun genUpdateCandidates(map: Map<Long, String>, folder: IMAPFolder, msgs: Array<Message>): Array<Message> {
fun genUpdateCandidates(map: Map<Long, String?>, folder: IMAPFolder, msgs: Array<Message>):
Array<Message> {
val updateCandidates = mutableListOf<Message>()
try {
for (msg in msgs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,17 @@ abstract class MessagesDao : BaseDao<MessageEntity> {
/**
* Switch [MessageState] for messages of the given folder of the given account
*
* @param email The email that the message linked.
* @param account The email that the message linked.
* @param label The folder label.
* @param oldValue The old value.
* @param newValues The new value.
* @return The count of the changed rows or -1 up.
*/
@Query("UPDATE messages SET state=:newValues WHERE state = :oldValue")
abstract fun changeMsgsState(email: String?, label: String?, oldValue: Int, newValues: Int): Int
@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

@Query("SELECT uid, flags FROM messages WHERE email = :account AND folder = :label")
abstract fun getUIDAndFlagsPairs(account: String?, label: String): List<UidFlagsPair>

@Transaction
open fun deleteByUIDs(email: String?, label: String?, msgsUID: Collection<Long>) {
Expand Down Expand Up @@ -181,4 +184,18 @@ abstract class MessagesDao : BaseDao<MessageEntity> {

update(modifiedMsgEntities)
}

/**
* Get a map of UID and flags of all messages in the database for some label.
*
* @param email The user email.
* @param label The label name.
* @return The map of UID and flags of all messages in the database for some label.
*/
@Transaction
open fun getMapOfUIDAndMsgFlags(email: String, label: String): Map<Long, String?> {
return getUIDAndFlagsPairs(email, label).map { it.uid to it.flags }.toMap()
}

data class UidFlagsPair(val uid: Long, val flags: String? = null)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package com.flowcrypt.email.database.dao.source.imap

import android.annotation.SuppressLint
import android.content.ContentValues
import android.content.Context
import android.database.Cursor
Expand Down Expand Up @@ -289,34 +288,6 @@ class MessageDaoSource : BaseDaoSource() {
return details
}

/**
* Get a map of UID and flags of all messages in the database for some label.
*
* @param context Interface to global information about an application environment.
* @param email The user email.
* @param label The label name.
* @return The map of UID and flags of all messages in the database for some label.
*/
@SuppressLint("UseSparseArrays")
fun getMapOfUIDAndMsgFlags(context: Context, email: String, label: String): Map<Long, String> {
val contentResolver = context.contentResolver
val uidList = HashMap<Long, String>()
val projection = arrayOf(COL_UID, COL_FLAGS)
val selection = "$COL_EMAIL = ? AND $COL_FOLDER = ?"
val selectionArgs = arrayOf(email, label)

val cursor = contentResolver.query(baseContentUri, projection, selection, selectionArgs, null)

if (cursor != null) {
while (cursor.moveToNext()) {
uidList[cursor.getLong(cursor.getColumnIndex(COL_UID))] = cursor.getString(cursor.getColumnIndex(COL_FLAGS))
}
cursor.close()
}

return uidList
}

/**
* Add the messages which have a current state equal [MessageState.SENDING] to the sending queue again.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,14 @@ class SyncJobService : JobService(), SyncListener {
updateMsgs: Array<Message>, ownerKey: String, requestCode: Int) {
try {
val msgDaoSource = MessageDaoSource()
val roomDatabase = FlowCryptRoomDatabase.getDatabase(applicationContext)

val folderName = localFolder.fullName
val mapOfUIDsAndMsgsFlags = msgDaoSource.getMapOfUIDAndMsgFlags(applicationContext, account.email, folderName)
val mapOfUIDsAndMsgsFlags = roomDatabase.msgDao().getMapOfUIDAndMsgFlags(account.email, folderName)
val uidSet = HashSet(mapOfUIDsAndMsgsFlags.keys)
val deleteCandidatesUIDs = EmailUtil.genDeleteCandidates(uidSet, remoteFolder, updateMsgs)

val generalMsgDetailsBeforeUpdate = msgDaoSource.getNewMsgs(applicationContext, account.email, folderName)
val roomDatabase = FlowCryptRoomDatabase.getDatabase(applicationContext)
roomDatabase.msgDao().deleteByUIDs(account.email, folderName, deleteCandidatesUIDs)

val updateCandidates = EmailUtil.genUpdateCandidates(mapOfUIDsAndMsgsFlags, remoteFolder,
Expand Down Expand Up @@ -198,8 +199,10 @@ class SyncJobService : JobService(), SyncListener {
val isEncryptedModeEnabled = AccountDaoSource().isEncryptedModeEnabled(context, account.email)

val msgDaoSource = MessageDaoSource()
val roomDatabase = FlowCryptRoomDatabase.getDatabase(applicationContext)

val folderName = localFolder.fullName
val mapOfUIDAndMsgFlags = msgDaoSource.getMapOfUIDAndMsgFlags(context, account.email, folderName)
val mapOfUIDAndMsgFlags = roomDatabase.msgDao().getMapOfUIDAndMsgFlags(account.email, folderName)

val uids = HashSet(mapOfUIDAndMsgFlags.keys)

Expand All @@ -216,7 +219,6 @@ class SyncJobService : JobService(), SyncListener {
areAllMsgsEncrypted = isEncryptedModeEnabled
)

val roomDatabase = FlowCryptRoomDatabase.getDatabase(applicationContext)
roomDatabase.msgDao().insert(msgEntities)

if (!GeneralUtil.isAppForegrounded()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ class EmailSyncService : BaseService(), SyncListener {
val msgsDaoSource = MessageDaoSource()
val roomDatabase = FlowCryptRoomDatabase.getDatabase(applicationContext)

val mapOfUIDAndMsgFlags = msgsDaoSource.getMapOfUIDAndMsgFlags(this, email, folderName)
val mapOfUIDAndMsgFlags = roomDatabase.msgDao().getMapOfUIDAndMsgFlags(email, folderName)
val msgsUIDs = HashSet(mapOfUIDAndMsgFlags.keys)
val deleteCandidatesUIDs = EmailUtil.genDeleteCandidates(msgsUIDs, remoteFolder, updateMsgs)

Expand Down

0 comments on commit d284efa

Please sign in to comment.