Skip to content

Commit

Permalink
Removed the last methods from MessageSource. Refactored code.| #793
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed Dec 26, 2019
1 parent 1b77a49 commit d860ab2
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 225 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.flowcrypt.email.api.email.FoldersManager
import com.flowcrypt.email.api.email.model.LocalFolder
import com.flowcrypt.email.database.FlowCryptRoomDatabase
import com.flowcrypt.email.database.dao.source.AccountDao
import com.flowcrypt.email.database.dao.source.imap.MessageDaoSource
import com.flowcrypt.email.util.GeneralUtil
import com.flowcrypt.email.util.LogsUtil
import com.flowcrypt.email.util.exception.ExceptionUtil
Expand Down Expand Up @@ -40,7 +39,6 @@ class IdleSyncRunnable constructor(account: AccountDao, syncListener: SyncListen
BaseSyncRunnable(account, syncListener), MessageCountListener, MessageChangedListener {
private var localFolder: LocalFolder? = null
private var remoteFolder: IMAPFolder? = null
private val msgDaoSource: MessageDaoSource = MessageDaoSource()
/**
* here we can have a lot of checks which help us decide can we run idling(wifi, 3G, a battery level and etc.)
*/
Expand Down
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.flowcrypt.email.util.exception.ExceptionUtil
import com.sun.mail.imap.IMAPFolder
import javax.mail.Flags
Expand All @@ -33,14 +32,12 @@ class ChangeMsgsReadStateSyncTask(ownerKey: String, requestCode: Int) : BaseSync

override fun runIMAPAction(account: AccountDao, session: Session, store: Store, listener: SyncListener) {
val context = listener.context
val msgDaoSource = MessageDaoSource()

changeMsgsReadState(context, account, msgDaoSource, store, MessageState.PENDING_MARK_UNREAD)
changeMsgsReadState(context, account, msgDaoSource, store, MessageState.PENDING_MARK_READ)
changeMsgsReadState(context, account, store, MessageState.PENDING_MARK_UNREAD)
changeMsgsReadState(context, account, store, MessageState.PENDING_MARK_READ)
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package com.flowcrypt.email.broadcastreceivers
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.flowcrypt.email.database.dao.source.imap.MessageDaoSource
import com.flowcrypt.email.service.MessagesNotificationManager
import com.flowcrypt.email.util.GeneralUtil
import com.flowcrypt.email.util.LogsUtil
Expand Down Expand Up @@ -36,9 +35,9 @@ class MarkMessagesAsOldBroadcastReceiver : BroadcastReceiver() {
val step = 500

if (uidList.isNotEmpty()) {
val daoSource = MessageDaoSource()
if (uidList.size <= step) {
daoSource.setOldStatus(context, email, label, uidList)
//todo-denbond7 #793
//daoSource.setOldStatus(context, email, label, uidList)
} else {
var i = 0
while (i < uidList.size) {
Expand All @@ -47,7 +46,8 @@ class MarkMessagesAsOldBroadcastReceiver : BroadcastReceiver() {
} else {
uidList.subList(i, uidList.size)
}
daoSource.setOldStatus(context, email, label, tempList)
//todo-denbond7 #793
//daoSource.setOldStatus(context, email, label, tempList)
i += step
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import com.flowcrypt.email.database.dao.source.AccountDaoSource
import com.flowcrypt.email.database.dao.source.ActionQueueDaoSource
import com.flowcrypt.email.database.dao.source.UserIdEmailsKeysDaoSource
import com.flowcrypt.email.database.dao.source.imap.AttachmentDaoSource
import com.flowcrypt.email.database.dao.source.imap.MessageDaoSource
import com.flowcrypt.email.database.entity.AccountAliasesEntity
import com.flowcrypt.email.database.entity.AccountEntity
import com.flowcrypt.email.database.entity.ActionQueueEntity
Expand Down Expand Up @@ -79,8 +78,7 @@ abstract class FlowCryptRoomDatabase : RoomDatabase() {
override fun migrate(database: SupportSQLiteDatabase) {
database.beginTransaction()
try {
database.execSQL("ALTER TABLE " + MessageDaoSource.TABLE_NAME_MESSAGES +
" ADD COLUMN " + MessageDaoSource.COL_IS_MESSAGE_HAS_ATTACHMENTS + " INTEGER DEFAULT 0;")
database.execSQL("ALTER TABLE messages ADD COLUMN is_message_has_attachments INTEGER DEFAULT 0;")

database.execSQL("ALTER TABLE " + AccountDaoSource.TABLE_NAME_ACCOUNTS +
" ADD COLUMN " + AccountDaoSource.COL_IS_ENABLE + " INTEGER DEFAULT 1;")
Expand Down Expand Up @@ -189,12 +187,9 @@ abstract class FlowCryptRoomDatabase : RoomDatabase() {
override fun migrate(database: SupportSQLiteDatabase) {
database.beginTransaction()
try {
database.execSQL("ALTER TABLE " + MessageDaoSource.TABLE_NAME_MESSAGES +
" ADD COLUMN " + MessageDaoSource.COL_IS_ENCRYPTED + " INTEGER DEFAULT -1;")
database.execSQL("ALTER TABLE " + MessageDaoSource.TABLE_NAME_MESSAGES +
" ADD COLUMN " + MessageDaoSource.COL_CC_ADDRESSES + " TEXT DEFAULT NULL;")
database.execSQL("ALTER TABLE " + MessageDaoSource.TABLE_NAME_MESSAGES +
" ADD COLUMN " + MessageDaoSource.COL_IS_NEW + " INTEGER DEFAULT 0;")
database.execSQL("ALTER TABLE messages ADD COLUMN is_encrypted INTEGER DEFAULT -1;")
database.execSQL("ALTER TABLE messages ADD COLUMN cc_address TEXT DEFAULT NULL;")
database.execSQL("ALTER TABLE messages ADD COLUMN is_new INTEGER DEFAULT 0;")

database.execSQL("ALTER TABLE " + AccountDaoSource.TABLE_NAME_ACCOUNTS +
" ADD COLUMN " + AccountDaoSource.COL_IS_SHOW_ONLY_ENCRYPTED + " INTEGER DEFAULT 0;")
Expand All @@ -214,8 +209,7 @@ abstract class FlowCryptRoomDatabase : RoomDatabase() {
override fun migrate(database: SupportSQLiteDatabase) {
database.beginTransaction()
try {
database.execSQL("ALTER TABLE " + MessageDaoSource.TABLE_NAME_MESSAGES +
" ADD COLUMN " + MessageDaoSource.COL_STATE + " INTEGER DEFAULT -1;")
database.execSQL("ALTER TABLE messages ADD COLUMN state INTEGER DEFAULT -1;")
database.setTransactionSuccessful()
} finally {
database.endTransaction()
Expand All @@ -231,8 +225,7 @@ abstract class FlowCryptRoomDatabase : RoomDatabase() {
" ADD COLUMN " + AttachmentDaoSource.COL_FORWARDED_FOLDER + " TEXT;")
database.execSQL("ALTER TABLE " + AttachmentDaoSource.TABLE_NAME_ATTACHMENT +
" ADD COLUMN " + AttachmentDaoSource.COL_FORWARDED_UID + " INTEGER DEFAULT -1;")
database.execSQL("ALTER TABLE " + MessageDaoSource.TABLE_NAME_MESSAGES +
" ADD COLUMN " + MessageDaoSource.COL_ATTACHMENTS_DIRECTORY + " TEXT;")
database.execSQL("ALTER TABLE messages ADD COLUMN attachments_directory TEXT;")
database.setTransactionSuccessful()
} finally {
database.endTransaction()
Expand All @@ -244,8 +237,7 @@ abstract class FlowCryptRoomDatabase : RoomDatabase() {
override fun migrate(database: SupportSQLiteDatabase) {
database.beginTransaction()
try {
database.execSQL("ALTER TABLE " + MessageDaoSource.TABLE_NAME_MESSAGES +
" ADD COLUMN " + MessageDaoSource.COL_ERROR_MSG + " TEXT DEFAULT NULL;")
database.execSQL("ALTER TABLE messages ADD COLUMN error_msg TEXT DEFAULT NULL;")
database.setTransactionSuccessful()
} finally {
database.endTransaction()
Expand Down Expand Up @@ -290,15 +282,13 @@ abstract class FlowCryptRoomDatabase : RoomDatabase() {
override fun migrate(database: SupportSQLiteDatabase) {
database.beginTransaction()
try {
database.delete(MessageDaoSource.TABLE_NAME_MESSAGES,
MessageDaoSource.COL_FOLDER + " NOT IN(?,?) ", arrayOf("INBOX", "Outbox"))
database.delete("messages", "folder NOT IN(?,?) ", arrayOf("INBOX", "Outbox"))
database.delete(AttachmentDaoSource.TABLE_NAME_ATTACHMENT,
AttachmentDaoSource.COL_FOLDER + " NOT IN(?,?) ", arrayOf("INBOX", "Outbox"))

val contentValues = ContentValues()
contentValues.putNull(MessageDaoSource.COL_RAW_MESSAGE_WITHOUT_ATTACHMENTS)
database.update(MessageDaoSource.TABLE_NAME_MESSAGES, SQLiteDatabase.CONFLICT_NONE,
contentValues, MessageDaoSource.COL_FOLDER + " = ? ", arrayOf("INBOX"))
contentValues.putNull("raw_message_without_attachments")
database.update("messages", SQLiteDatabase.CONFLICT_NONE, contentValues, "folder = ? ", arrayOf("INBOX"))
database.setTransactionSuccessful()
} finally {
database.endTransaction()
Expand All @@ -310,8 +300,7 @@ abstract class FlowCryptRoomDatabase : RoomDatabase() {
override fun migrate(database: SupportSQLiteDatabase) {
database.beginTransaction()
try {
database.execSQL("ALTER TABLE " + MessageDaoSource.TABLE_NAME_MESSAGES +
" ADD COLUMN " + MessageDaoSource.COL_REPLY_TO + " TEXT DEFAULT NULL;")
database.execSQL("ALTER TABLE messages ADD COLUMN reply_to TEXT DEFAULT NULL;")
database.setTransactionSuccessful()
} finally {
database.endTransaction()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@

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

import android.content.ContentValues
import android.content.Context
import android.text.TextUtils
import com.flowcrypt.email.api.email.model.GeneralMessageDetails
import com.flowcrypt.email.api.email.model.MessageFlag
import com.flowcrypt.email.database.dao.source.BaseDaoSource
import java.util.*

/**
* This class describes the dao source for [GeneralMessageDetails] class.
Expand All @@ -22,104 +16,4 @@ import java.util.*
* E-mail: [email protected]
*/

class MessageDaoSource : BaseDaoSource() {

override val tableName: String = TABLE_NAME_MESSAGES

/**
* Mark message as seen in the local database.
*
* @param context Interface to global information about an application environment.
* @param email The email that the message linked.
* @param label The folder label.
* @param uid The message UID.
* @param isSeen true if seen
* @return The count of the updated row or -1 up.
*/
fun setSeenStatus(context: Context, email: String?, label: String?, uid: Long,
isSeen: Boolean = true): Int {
val resolver = context.contentResolver
return if (email != null && label != null && resolver != null) {
val values = ContentValues()
if (isSeen) {
values.put(COL_FLAGS, MessageFlag.SEEN.value)
} else {
values.put(COL_FLAGS, "")//todo-denbond7 maybe it is not a good idea to erase all flags
}

val where = "$COL_EMAIL= ? AND $COL_FOLDER = ? AND $COL_UID = ? "
resolver.update(baseContentUri, values, where, arrayOf(email, label, uid.toString()))
} else
-1
}

/**
* Mark messages as old in the local database.
*
* @param context Interface to global information about an application environment.
* @param email The email that the message linked.
* @param label The folder label.
* @return The count of the updated row or -1 up.
*/
fun setOldStatus(context: Context, email: String?, label: String?): Int {
val contentResolver = context.contentResolver
return if (email != null && label != null && contentResolver != null) {
val contentValues = ContentValues()
contentValues.put(COL_IS_NEW, false)
val where = "$COL_EMAIL= ? AND $COL_FOLDER = ?"
contentResolver.update(baseContentUri, contentValues, where, arrayOf(email, label))
} else
-1
}

/**
* Mark messages as old in the local database.
*
* @param context Interface to global information about an application environment.
* @param email The email that the message linked.
* @param label The folder label.
* @param uidList The list of the UIDs.
* @return The count of the updated row or -1 up.
*/
fun setOldStatus(context: Context, email: String?, label: String?, uidList: List<String>?): Int {
val contentResolver = context.contentResolver
return if (contentResolver != null && email != null && label != null && uidList != null && uidList.isNotEmpty()) {
val contentValues = ContentValues()
contentValues.put(COL_IS_NEW, false)

val args = ArrayList<String>()
args.add(0, email)
args.add(1, label)
args.addAll(uidList)

val where = "$COL_EMAIL= ? AND $COL_FOLDER = ? AND $COL_UID IN (" + TextUtils.join(",",
Collections.nCopies(uidList.size, "?")) + ")"

contentResolver.update(baseContentUri, contentValues, where, args.toTypedArray())
} else
-1
}

companion object {
const val TABLE_NAME_MESSAGES = "messages"

const val COL_EMAIL = "email"
const val COL_FOLDER = "folder"
const val COL_UID = "uid"
const val COL_RECEIVED_DATE = "received_date"
const val COL_SENT_DATE = "sent_date"
const val COL_FROM_ADDRESSES = "from_address"
const val COL_TO_ADDRESSES = "to_address"
const val COL_CC_ADDRESSES = "cc_address"
const val COL_SUBJECT = "subject"
const val COL_FLAGS = "flags"
const val COL_RAW_MESSAGE_WITHOUT_ATTACHMENTS = "raw_message_without_attachments"
const val COL_IS_MESSAGE_HAS_ATTACHMENTS = "is_message_has_attachments"
const val COL_IS_ENCRYPTED = "is_encrypted"
const val COL_IS_NEW = "is_new"
const val COL_STATE = "state"
const val COL_ATTACHMENTS_DIRECTORY = "attachments_directory"
const val COL_ERROR_MSG = "error_msg"
const val COL_REPLY_TO = "reply_to"
}
}
class MessageDaoSource
Loading

0 comments on commit d860ab2

Please sign in to comment.