From f1bd83d43d8ef357e42920d0ae08b6b912da18b9 Mon Sep 17 00:00:00 2001 From: DenBond7 Date: Fri, 20 Dec 2019 14:18:11 +0200 Subject: [PATCH] Removed MessageDaoSource.deleteMsgsByUID() method. Modified the database structure: added a foreign key for 'attachment' table. Refactored code.| #793 --- .../20.json | 36 +++++++++-- .../email/sync/tasks/ArchiveMsgsSyncTask.kt | 3 +- .../sync/tasks/DeleteMessagesSyncTask.kt | 3 +- .../email/sync/tasks/MovingToInboxSyncTask.kt | 3 +- .../email/database/FlowCryptRoomDatabase.kt | 13 ++++ .../email/database/dao/MessagesDao.kt | 37 ++++++++++-- .../dao/source/imap/MessageDaoSource.kt | 60 ------------------- .../email/database/entity/AttachmentEntity.kt | 8 ++- .../jetpack/viewmodel/MessagesViewModel.kt | 1 - .../email/jobscheduler/SyncJobService.kt | 2 +- .../email/service/EmailSyncService.kt | 2 +- .../email/ui/activity/EmailManagerActivity.kt | 14 +++-- .../ui/activity/fragment/EmailListFragment.kt | 21 ++++--- .../ui/activity/fragment/base/BaseFragment.kt | 5 +- FlowCrypt/src/main/res/values/ids.xml | 2 +- 15 files changed, 117 insertions(+), 93 deletions(-) diff --git a/FlowCrypt/schemas/com.flowcrypt.email.database.FlowCryptRoomDatabase/20.json b/FlowCrypt/schemas/com.flowcrypt.email.database.FlowCryptRoomDatabase/20.json index ff5a376f62..2c802e85b9 100644 --- a/FlowCrypt/schemas/com.flowcrypt.email.database.FlowCryptRoomDatabase/20.json +++ b/FlowCrypt/schemas/com.flowcrypt.email.database.FlowCryptRoomDatabase/20.json @@ -2,7 +2,7 @@ "formatVersion": 1, "database": { "version": 20, - "identityHash": "142525ec1065962bd52b6c45a0e7a640", + "identityHash": "b34e1ed31528ece202469e8317791361", "entities": [ { "tableName": "accounts_aliases", @@ -332,7 +332,7 @@ }, { "tableName": "attachment", - "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`_id` INTEGER PRIMARY KEY AUTOINCREMENT, `email` TEXT NOT NULL, `folder` TEXT NOT NULL, `uid` INTEGER NOT NULL, `name` TEXT NOT NULL, `encodedSize` INTEGER DEFAULT 0, `type` TEXT NOT NULL, `attachment_id` TEXT, `file_uri` TEXT, `forwarded_folder` TEXT, `forwarded_uid` INTEGER DEFAULT -1, `path` TEXT NOT NULL)", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`_id` INTEGER PRIMARY KEY AUTOINCREMENT, `email` TEXT NOT NULL, `folder` TEXT NOT NULL, `uid` INTEGER NOT NULL, `name` TEXT NOT NULL, `encodedSize` INTEGER DEFAULT 0, `type` TEXT NOT NULL, `attachment_id` TEXT, `file_uri` TEXT, `forwarded_folder` TEXT, `forwarded_uid` INTEGER DEFAULT -1, `path` TEXT NOT NULL, FOREIGN KEY(`email`, `folder`, `uid`) REFERENCES `messages`(`email`, `folder`, `uid`) ON UPDATE NO ACTION ON DELETE CASCADE )", "fields": [ { "fieldPath": "id", @@ -426,9 +426,35 @@ "path" ], "createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `email_uid_folder_path_in_attachment` ON `${TABLE_NAME}` (`email`, `uid`, `folder`, `path`)" + }, + { + "name": "email_folder_uid_in_attachment", + "unique": false, + "columnNames": [ + "email", + "folder", + "uid" + ], + "createSql": "CREATE INDEX IF NOT EXISTS `email_folder_uid_in_attachment` ON `${TABLE_NAME}` (`email`, `folder`, `uid`)" } ], - "foreignKeys": [] + "foreignKeys": [ + { + "table": "messages", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "email", + "folder", + "uid" + ], + "referencedColumns": [ + "email", + "folder", + "uid" + ] + } + ] }, { "tableName": "contacts", @@ -770,7 +796,7 @@ "defaultValue": "NULL" }, { - "fieldPath": "isMessageHasAttachments", + "fieldPath": "hasAttachments", "columnName": "is_message_has_attachments", "affinity": "INTEGER", "notNull": false, @@ -892,7 +918,7 @@ "views": [], "setupQueries": [ "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", - "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '142525ec1065962bd52b6c45a0e7a640')" + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'b34e1ed31528ece202469e8317791361')" ] } } \ No newline at end of file diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/ArchiveMsgsSyncTask.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/ArchiveMsgsSyncTask.kt index 6a39d5263b..e7dfc2beda 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/ArchiveMsgsSyncTask.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/ArchiveMsgsSyncTask.kt @@ -7,6 +7,7 @@ package com.flowcrypt.email.api.email.sync.tasks import com.flowcrypt.email.api.email.FoldersManager 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 @@ -48,7 +49,7 @@ class ArchiveMsgsSyncTask(ownerKey: String, requestCode: Int) : BaseSyncTask(own if (msgs.isNotEmpty()) { remoteSrcFolder.moveMessages(msgs.toTypedArray(), remoteDestFolder) - msgDaoSource.deleteMsgsByUID(context, account.email, inboxFolder.fullName, uidList) + FlowCryptRoomDatabase.getDatabase(context).msgDao().deleteByUIDs(account.email, inboxFolder.fullName, uidList) } remoteSrcFolder.close() diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/DeleteMessagesSyncTask.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/DeleteMessagesSyncTask.kt index d3cb716c0a..149738649b 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/DeleteMessagesSyncTask.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/DeleteMessagesSyncTask.kt @@ -8,6 +8,7 @@ package com.flowcrypt.email.api.email.sync.tasks import com.flowcrypt.email.api.email.FoldersManager import com.flowcrypt.email.api.email.JavaEmailConstants 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 @@ -61,7 +62,7 @@ class DeleteMessagesSyncTask(ownerKey: String, if (msgs.isNotEmpty()) { remoteSrcFolder.moveMessages(msgs.toTypedArray(), remoteDestFolder) - msgDaoSource.deleteMsgsByUID(context, account.email, folder, uidList) + FlowCryptRoomDatabase.getDatabase(context).msgDao().deleteByUIDs(account.email, folder, uidList) } remoteSrcFolder.close() diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/MovingToInboxSyncTask.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/MovingToInboxSyncTask.kt index 1c703bcbca..0ce990bba1 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/MovingToInboxSyncTask.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/MovingToInboxSyncTask.kt @@ -8,6 +8,7 @@ package com.flowcrypt.email.api.email.sync.tasks import com.flowcrypt.email.api.email.FoldersManager import com.flowcrypt.email.api.email.JavaEmailConstants 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 @@ -58,7 +59,7 @@ class MovingToInboxSyncTask(ownerKey: String, requestCode: Int) : BaseSyncTask(o if (msgs.isNotEmpty()) { remoteSrcFolder.moveMessages(msgs.toTypedArray(), remoteDestFolder) - msgDaoSource.deleteMsgsByUID(context, account.email, folder, uidList) + FlowCryptRoomDatabase.getDatabase(context).msgDao().deleteByUIDs(account.email, inboxFolder.fullName, uidList) } remoteSrcFolder.close(false) diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/database/FlowCryptRoomDatabase.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/database/FlowCryptRoomDatabase.kt index 8b04827641..c994626544 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/database/FlowCryptRoomDatabase.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/database/FlowCryptRoomDatabase.kt @@ -360,6 +360,19 @@ abstract class FlowCryptRoomDatabase : RoomDatabase() { database.execSQL("INSERT INTO contacts SELECT * FROM contacts_temp;") database.execSQL("DROP TABLE IF EXISTS contacts_temp;") + //Recreate 'attachment' table to use an ability of foreign keys + //delete non-OUTBOX attachments + database.delete("attachment", "folder NOT IN (?)", arrayOf(JavaEmailConstants.FOLDER_OUTBOX)) + val tempTableName = "attachment_temp" + + database.execSQL("CREATE TEMP TABLE IF NOT EXISTS $tempTableName AS SELECT * FROM attachment;") + database.execSQL("DROP TABLE IF EXISTS attachment;") + database.execSQL("CREATE TABLE `attachment` (`_id` INTEGER PRIMARY KEY AUTOINCREMENT, `email` TEXT NOT NULL, `folder` TEXT NOT NULL, `uid` INTEGER NOT NULL, `name` TEXT NOT NULL, `encodedSize` INTEGER DEFAULT 0, `type` TEXT NOT NULL, `attachment_id` TEXT, `file_uri` TEXT, `forwarded_folder` TEXT, `forwarded_uid` INTEGER DEFAULT -1, `path` TEXT NOT NULL, FOREIGN KEY(`email`, `folder`, `uid`) REFERENCES `messages`(`email`, `folder`, `uid`) ON UPDATE NO ACTION ON DELETE CASCADE );") + database.execSQL("CREATE UNIQUE INDEX `email_uid_folder_path_in_attachment` ON `attachment` (`email`, `uid`, `folder`, `path`);") + database.execSQL("CREATE INDEX `email_folder_uid_in_attachment` ON `attachment` (`email`, `folder`, `uid`);") + database.execSQL("INSERT INTO attachment SELECT * FROM $tempTableName;") + database.execSQL("DROP TABLE IF EXISTS $tempTableName;") + database.setTransactionSuccessful() } finally { database.endTransaction() 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 ba618f58d4..f37f4a7f7b 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 @@ -9,7 +9,9 @@ import androidx.lifecycle.LiveData import androidx.paging.DataSource import androidx.room.Dao import androidx.room.Query +import androidx.room.Transaction import com.flowcrypt.email.database.entity.MessageEntity +import java.util.* /** * This class describes available methods for [MessageEntity] @@ -20,16 +22,41 @@ import com.flowcrypt.email.database.entity.MessageEntity * E-mail: DenBond7@gmail.com */ @Dao -interface MessagesDao : BaseDao { +abstract class MessagesDao : BaseDao { @Query("SELECT * FROM messages WHERE email = :account AND folder = :folder") - fun getMessages(account: String, folder: String): LiveData + abstract fun getMessages(account: String, folder: String): LiveData @Query("SELECT * FROM messages WHERE email = :account AND folder = :folder ORDER BY received_date DESC") - fun getMessagesDataSourceFactory(account: String, folder: String): DataSource.Factory + abstract fun getMessagesDataSourceFactory(account: String, folder: String): DataSource + .Factory @Query("SELECT * FROM messages") - fun msgs(): DataSource.Factory + abstract fun msgs(): DataSource.Factory @Query("DELETE FROM messages WHERE email = :email AND folder = :label") - suspend fun delete(email: String?, label: String?): Int + abstract suspend fun delete(email: String?, label: String?): Int + + @Query("DELETE FROM messages WHERE email = :email AND folder = :label AND uid IN (:msgsUID)") + abstract fun delete(email: String?, label: String?, msgsUID: Collection): Int + + @Transaction + open fun deleteByUIDs(email: String?, label: String?, msgsUID: Collection) { + val step = 50 + val list = ArrayList(msgsUID) + + if (msgsUID.size <= step) { + delete(email, label, msgsUID) + } else { + var i = 0 + while (i < list.size) { + val stepUIDs = if (list.size - i > step) { + list.subList(i, i + step) + } else { + list.subList(i, list.size) + } + delete(email, label, stepUIDs) + i += step + } + } + } } \ No newline at end of file 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 3b91c94037..f593f455c5 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 @@ -49,66 +49,6 @@ class MessageDaoSource : BaseDaoSource() { override val tableName: String = TABLE_NAME_MESSAGES - /** - * This method delete cached messages. - * - * @param context Interface to global information about an application environment. - * @param email The email that the message linked. - * @param label The folder label. - * @param msgsUID The list of messages UID. - */ - fun deleteMsgsByUID(context: Context, email: String?, label: String?, msgsUID: Collection) { - val contentResolver = context.contentResolver - if (email != null && label != null && contentResolver != null) { - val step = 50 - - val selectionArgs = LinkedList() - selectionArgs.add(email) - selectionArgs.add(label) - - val list = ArrayList(msgsUID) - - if (msgsUID.size <= step) { - for (uid in msgsUID) { - selectionArgs.add(uid.toString()) - } - - val where = COL_EMAIL + "= ? AND " + COL_FOLDER + " = ? AND " + COL_UID + " IN (" + - prepareSelectionArgsString(msgsUID.toTypedArray()) + ");" - - contentResolver.delete(baseContentUri, where, selectionArgs.toTypedArray()) - } else { - val ops = ArrayList() - - var i = 0 - while (i < list.size) { - val stepUIDs = if (list.size - i > step) { - list.subList(i, i + step) - } else { - list.subList(i, list.size) - } - - val selectionArgsForStep = LinkedList(selectionArgs) - - for (uid in stepUIDs) { - selectionArgsForStep.add(uid.toString()) - } - - val selection = (COL_EMAIL + "= ? AND " + COL_FOLDER + " = ? AND " + COL_UID - + " IN (" + prepareSelectionArgsString(stepUIDs.toTypedArray()) + ");") - - ops.add(ContentProviderOperation.newDelete(baseContentUri) - .withSelection(selection, selectionArgsForStep.toTypedArray()) - .withYieldAllowed(true) - .build()) - i += step - } - - contentResolver.applyBatch(baseContentUri.authority!!, ops) - } - } - } - /** * This method update cached messages. * diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/database/entity/AttachmentEntity.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/database/entity/AttachmentEntity.kt index 3510ad22bd..d6f163e084 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/database/entity/AttachmentEntity.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/database/entity/AttachmentEntity.kt @@ -8,6 +8,7 @@ package com.flowcrypt.email.database.entity import android.provider.BaseColumns import androidx.room.ColumnInfo import androidx.room.Entity +import androidx.room.ForeignKey import androidx.room.Index import androidx.room.PrimaryKey @@ -19,7 +20,12 @@ import androidx.room.PrimaryKey */ @Entity(tableName = "attachment", indices = [ - Index(name = "email_uid_folder_path_in_attachment", value = ["email", "uid", "folder", "path"], unique = true) + Index(name = "email_uid_folder_path_in_attachment", value = ["email", "uid", "folder", "path"], unique = true), + Index(name = "email_folder_uid_in_attachment", value = ["email", "folder", "uid"]) + ], + foreignKeys = [ + ForeignKey(entity = MessageEntity::class, parentColumns = ["email", "folder", "uid"], + childColumns = ["email", "folder", "uid"], onDelete = ForeignKey.CASCADE) ] ) data class AttachmentEntity( diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/MessagesViewModel.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/MessagesViewModel.kt index b850f3214f..e5fb15cfcf 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/MessagesViewModel.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/MessagesViewModel.kt @@ -62,7 +62,6 @@ class MessagesViewModel(application: Application) : BaseAndroidViewModel(applica fun cleanFolderCache(folderName: String?) { viewModelScope.launch { - val t = roomDatabase.attachmentDao().delete(accountLiveData.value?.email, folderName) roomDatabase.msgDao().delete(accountLiveData.value?.email, folderName) } } diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/jobscheduler/SyncJobService.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/jobscheduler/SyncJobService.kt index 5418367d49..e5841f20a9 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/jobscheduler/SyncJobService.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/jobscheduler/SyncJobService.kt @@ -128,7 +128,7 @@ class SyncJobService : JobService(), SyncListener { val generalMsgDetailsBeforeUpdate = msgDaoSource.getNewMsgs(applicationContext, account.email, folderName) - msgDaoSource.deleteMsgsByUID(applicationContext, account.email, folderName, deleteCandidatesUIDs) + FlowCryptRoomDatabase.getDatabase(applicationContext).msgDao().deleteByUIDs(account.email, folderName, deleteCandidatesUIDs) msgDaoSource.updateMsgsByUID(applicationContext, account.email, folderName, remoteFolder, EmailUtil.genUpdateCandidates(mapOfUIDsAndMsgsFlags, remoteFolder, updateMsgs)) diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/service/EmailSyncService.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/service/EmailSyncService.kt index c5397824c1..dba06cbcbb 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/service/EmailSyncService.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/service/EmailSyncService.kt @@ -384,7 +384,7 @@ class EmailSyncService : BaseService(), SyncListener { val msgsUIDs = HashSet(mapOfUIDAndMsgFlags.keys) val deleteCandidatesUIDs = EmailUtil.genDeleteCandidates(msgsUIDs, remoteFolder, updateMsgs) - msgsDaoSource.deleteMsgsByUID(this, email, folderName, deleteCandidatesUIDs) + FlowCryptRoomDatabase.getDatabase(context).msgDao().deleteByUIDs(account.email, folderName, deleteCandidatesUIDs) val folderType = FoldersManager.getFolderType(localFolder) if (!GeneralUtil.isAppForegrounded() && folderType === FoldersManager.FolderType.INBOX) { diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/EmailManagerActivity.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/EmailManagerActivity.kt index a27b4a290c..ec605b7707 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/EmailManagerActivity.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/EmailManagerActivity.kt @@ -287,8 +287,8 @@ class EmailManagerActivity : BaseEmailListActivity(), NavigationView.OnNavigatio } } - R.id.syns_request_code_force_load_new_messages -> { - onFetchMsgsCompleted() + R.id.syns_request_code_refresh_msgs -> { + onRefreshMsgsCompleted() msgsIdlingResource.setIdleState(true) } @@ -304,10 +304,10 @@ class EmailManagerActivity : BaseEmailListActivity(), NavigationView.OnNavigatio override fun onErrorHappened(requestCode: Int, errorType: Int, e: Exception) { when (requestCode) { - R.id.syns_request_code_force_load_new_messages -> { - msgsIdlingResource.setIdleState(true) + R.id.syns_request_code_refresh_msgs -> { onErrorOccurred(requestCode, errorType, e) - onFetchMsgsCompleted() + onRefreshMsgsCompleted() + msgsIdlingResource.setIdleState(true) } R.id.syns_request_code_update_label_passive, R.id.syns_request_code_update_label_active -> { @@ -696,6 +696,10 @@ class EmailManagerActivity : BaseEmailListActivity(), NavigationView.OnNavigatio (supportFragmentManager.findFragmentById(R.id.emailListFragment) as? EmailListFragment?)?.onFetchMsgsCompleted() } + private fun onRefreshMsgsCompleted() { + (supportFragmentManager.findFragmentById(R.id.emailListFragment) as? EmailListFragment?)?.onRefreshMsgsCompleted() + } + /** * The custom realization of [ActionBarDrawerToggle]. Will be used to start a labels * update task when the drawer will be opened. diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/EmailListFragment.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/EmailListFragment.kt index d2259fe8f6..3d64c9726b 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/EmailListFragment.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/EmailListFragment.kt @@ -150,10 +150,10 @@ class EmailListFragment : BaseSyncFragment(), SwipeRefreshLayout.OnRefreshListen override fun onRefresh() { snackBar?.dismiss() - val localFolder = listener!!.currentFolder + val localFolder = listener?.currentFolder if (localFolder == null) { - swipeRefreshLayout!!.isRefreshing = false + swipeRefreshLayout?.isRefreshing = false return } @@ -215,8 +215,8 @@ class EmailListFragment : BaseSyncFragment(), SwipeRefreshLayout.OnRefreshListen } } - R.id.syns_request_code_force_load_new_messages -> { - swipeRefreshLayout!!.isRefreshing = false + R.id.syns_request_code_refresh_msgs -> { + swipeRefreshLayout?.isRefreshing = false when (errorType) { SyncErrorTypes.ACTION_FAILED_SHOW_TOAST -> Toast.makeText(context, R.string.failed_please_try_again_later, Toast.LENGTH_SHORT).show() @@ -291,6 +291,9 @@ class EmailListFragment : BaseSyncFragment(), SwipeRefreshLayout.OnRefreshListen msgsObserver.onChanged(messagesViewModel.msgsLiveData?.value) } + fun onRefreshMsgsCompleted() { + swipeRefreshLayout?.isRefreshing = false + } private fun showConnProblemHint() { showSnackbar(view!!, getString(R.string.can_not_connect_to_the_imap_server), getString(R.string.retry), @@ -392,7 +395,9 @@ class EmailListFragment : BaseSyncFragment(), SwipeRefreshLayout.OnRefreshListen */ private fun refreshMsgs() { listener?.msgsLoadingIdlingResource?.setIdleState(false) - baseSyncActivity.refreshMsgs(R.id.syns_request_code_force_load_new_messages, listener!!.currentFolder!!) + listener?.currentFolder?.let { + baseSyncActivity.refreshMsgs(R.id.syns_request_code_refresh_msgs, it) + } } /** @@ -401,7 +406,7 @@ class EmailListFragment : BaseSyncFragment(), SwipeRefreshLayout.OnRefreshListen * @param totalItemsCount The count of already loaded messages. */ private fun loadNextMsgs(totalItemsCount: Int) { - if (GeneralUtil.isConnected(context!!)) { + if (GeneralUtil.isConnected(context)) { if (totalItemsCount == 0) { contentView?.visibility = View.GONE statusView?.visibility = View.GONE @@ -411,7 +416,7 @@ class EmailListFragment : BaseSyncFragment(), SwipeRefreshLayout.OnRefreshListen footerProgressView?.visibility = View.VISIBLE listener?.msgsLoadingIdlingResource?.setIdleState(false) - val localFolder = listener!!.currentFolder + val localFolder = listener?.currentFolder if (TextUtils.isEmpty(localFolder!!.searchQuery)) { baseSyncActivity.loadNextMsgs(R.id.syns_request_code_load_next_messages, localFolder, totalItemsCount) } else { @@ -419,7 +424,7 @@ class EmailListFragment : BaseSyncFragment(), SwipeRefreshLayout.OnRefreshListen } } else { footerProgressView?.visibility = View.GONE - showSnackbar(view!!, getString(R.string.internet_connection_is_not_available), getString(R.string.retry), + showSnackbar(view, getString(R.string.internet_connection_is_not_available), getString(R.string.retry), Snackbar.LENGTH_LONG, View.OnClickListener { loadNextMsgs(totalItemsCount) }) } } diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/base/BaseFragment.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/base/BaseFragment.kt index b3f83889d4..5ed3365303 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/base/BaseFragment.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/base/BaseFragment.kt @@ -123,8 +123,9 @@ abstract class BaseFragment : Fragment(), LoaderManager.LoaderCallbacks - +