-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored code. Modified MessagesViewModel.| #793
- Loading branch information
Showing
12 changed files
with
118 additions
and
294 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.