Skip to content

Commit

Permalink
Refactored code. Modified MessagesViewModel.| #793
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed Dec 18, 2019
1 parent 68b6f6b commit bb91dc1
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 294 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,11 @@ class FoldersManager {
* @param folderAlias The folder alias name.
* @return [LocalFolder].
*/
fun getFolderByAlias(folderAlias: String): LocalFolder? {
fun getFolderByAlias(folderAlias: String?): LocalFolder? {
folderAlias ?: return null

for ((_, value) in folders) {
if (value.folderAlias == folderAlias) {
if (value.folderAlias?.equals(folderAlias) == true) {
return value
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ class DatabaseUtil {
* @param folder A folder in a local database.
*/
@JvmStatic
fun cleanFolderCache(context: Context, email: String?, folder: String) {
fun cleanFolderCache(context: Context?, email: String?, folder: String?) {
if (!JavaEmailConstants.FOLDER_OUTBOX.equals(folder, ignoreCase = true)) {
MessageDaoSource().deleteCachedMsgs(context, email, folder)
AttachmentDaoSource().deleteCachedAttInfo(context, email, folder)
context?.let {
MessageDaoSource().deleteCachedMsgs(it, email, folder)
AttachmentDaoSource().deleteCachedAttInfo(it, email, folder)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ import com.flowcrypt.email.database.entity.AccountEntity
interface AccountDao {

@Query("SELECT * FROM accounts WHERE is_active = 1")
suspend fun getActiveAccount(): AccountEntity
suspend fun getActiveAccount(): AccountEntity?
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ interface MessagesDao {
@Query("SELECT * FROM messages WHERE email = :account AND folder = :folder")
fun getMessages(account: String, folder: String): LiveData<MessageEntity>

@Query("SELECT * FROM messages WHERE email = :account AND folder = :folder")
fun getMessagesDataSourceFactory(account: String, folder: String): DataSource.Factory<Int, MessageEntity>

@Query("SELECT * FROM messages")
fun msgs(): DataSource.Factory<Int, MessageEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,12 @@ class AccountDaoSource : BaseDaoSource() {
* @param email An email of the active account.
* @return true if need to show only encrypted messages
*/
fun isEncryptedModeEnabled(context: Context, email: String): Boolean {
val emailInLowerCase = if (TextUtils.isEmpty(email)) email else email.toLowerCase(Locale.getDefault())
fun isEncryptedModeEnabled(context: Context?, email: String?): Boolean {
val emailInLowerCase = if (TextUtils.isEmpty(email)) email else email?.toLowerCase(Locale.getDefault())

val selection = "$COL_EMAIL = ?"
val selectionArgs = arrayOf(emailInLowerCase)
val cursor = context.contentResolver.query(baseContentUri, null, selection, selectionArgs, null)
val cursor = context?.contentResolver?.query(baseContentUri, null, selection, selectionArgs, null)

var isEncryptedModeEnabled = false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,49 @@
package com.flowcrypt.email.jetpack.viewmodel

import android.app.Application
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import androidx.lifecycle.Transformations
import androidx.lifecycle.liveData
import androidx.paging.PagedList
import androidx.paging.toLiveData
import com.flowcrypt.email.api.email.model.LocalFolder
import com.flowcrypt.email.database.FlowCryptRoomDatabase
import com.flowcrypt.email.database.entity.AccountEntity
import com.flowcrypt.email.database.entity.MessageEntity


/**
* @author Denis Bondarenko
* Date: 12/17/19
* Time: 4:37 PM
* E-mail: [email protected]
*/
class MessagesViewModel(application: Application) : BaseAndroidViewModel(application) {
private var currentLocalFolder: LocalFolder? = null
private val roomDatabase = FlowCryptRoomDatabase.getDatabase(application)
val concertList: LiveData<PagedList<MessageEntity>> = roomDatabase.msgDao().msgs().toLiveData(pageSize = 20)

val accountLiveData: LiveData<AccountEntity?> = liveData {
val accountEntity = roomDatabase.accountDao().getActiveAccount()
emit(accountEntity)
}

var msgsLiveData: LiveData<PagedList<MessageEntity>>? = null

fun loadMsgs(lifecycleOwner: LifecycleOwner, localFolder: LocalFolder?, observer: Observer<PagedList<MessageEntity>>) {
this.currentLocalFolder = localFolder
msgsLiveData?.removeObserver(observer)
msgsLiveData = Transformations.switchMap(accountLiveData) {
val account = it?.email ?: ""
val label = currentLocalFolder?.fullName ?: ""
roomDatabase.msgDao().getMessagesDataSourceFactory(account, label).toLiveData(pageSize = 20)
}

msgsLiveData?.observe(lifecycleOwner, observer)
}

fun getActiveAccount(): AccountEntity? {
return accountLiveData.value
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -403,17 +403,14 @@ class EmailManagerActivity : BaseEmailListActivity(), NavigationView.OnNavigatio
}

if (currentFolder == null) {
currentFolder = foldersManager!!.folderInbox
currentFolder = foldersManager?.folderInbox
if (currentFolder == null) {
currentFolder = foldersManager!!.findInboxFolder()
currentFolder = foldersManager?.findInboxFolder()
}

onFolderChanged(false)
} else {
val newestLocalFolderInfo = foldersManager!!.getFolderByAlias(currentFolder!!.folderAlias!!)
if (newestLocalFolderInfo != null) {
currentFolder = newestLocalFolderInfo
}
foldersManager?.getFolderByAlias(currentFolder?.folderAlias)?.let { currentFolder = it }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ abstract class BaseEmailListActivity : BaseSyncActivity(), EmailListFragment.OnM
when (resultCode) {
EmailSyncService.REPLY_RESULT_CODE_NEED_UPDATE -> {
hasMoreMsgs = true
onNextMsgsLoaded(true)
onNextMsgsLoaded()
}

else -> {
hasMoreMsgs = false
onNextMsgsLoaded(false)
onNextMsgsLoaded()
}
}

Expand Down Expand Up @@ -136,13 +136,13 @@ abstract class BaseEmailListActivity : BaseSyncActivity(), EmailListFragment.OnM
* Update the list of emails after changing the folder.
*/
protected fun onFolderChanged(isForceClearCacheNeeded: Boolean) {
toolbar?.title = currentFolder?.folderAlias

val emailListFragment = supportFragmentManager
.findFragmentById(R.id.emailListFragment) as EmailListFragment?

if (emailListFragment != null) {
emailListFragment.updateList(isFolderChanged = true, isForceClearCacheNeeded = isForceClearCacheNeeded)
updateActionProgressState(100, null)
}
emailListFragment?.updateList(true, isForceClearCacheNeeded)
updateActionProgressState(100, null)

if (currentFolder != null) {
val isOutbox = JavaEmailConstants.FOLDER_OUTBOX.equals(currentFolder!!.fullName, ignoreCase = true)
Expand Down Expand Up @@ -174,15 +174,13 @@ abstract class BaseEmailListActivity : BaseSyncActivity(), EmailListFragment.OnM

/**
* Handle a result from the load next messages action.
*
* @param needToRefreshList true if we must reload the emails list.
*/
protected fun onNextMsgsLoaded(needToRefreshList: Boolean) {
protected fun onNextMsgsLoaded() {
val emailListFragment = supportFragmentManager
.findFragmentById(R.id.emailListFragment) as EmailListFragment?

if (emailListFragment != null) {
emailListFragment.onNextMsgsLoaded(needToRefreshList)
emailListFragment.onNextMsgsLoaded()
emailListFragment.setActionProgress(100, null)
}
}
Expand Down
Loading

0 comments on commit bb91dc1

Please sign in to comment.