Fix: Nullability of MutableLiveData

This commit is contained in:
machiav3lli 2022-06-29 04:06:57 +02:00
parent fc74146194
commit 38c7c74ebf
4 changed files with 18 additions and 17 deletions

View File

@ -19,20 +19,20 @@ import kotlinx.coroutines.withContext
class AppViewModelX(val db: DatabaseX, val packageName: String) : ViewModel() {
val products = MediatorLiveData<List<Product>>()
val repositories = MediatorLiveData<List<Repository>>()
val installedItem = MediatorLiveData<Installed?>()
val _productRepos = MutableLiveData<List<Pair<Product, Repository>>>()
val products: MediatorLiveData<List<Product>> = MediatorLiveData()
val repositories: MediatorLiveData<List<Repository>> = MediatorLiveData()
val installedItem: MediatorLiveData<Installed?> = MediatorLiveData()
val _productRepos: MutableLiveData<List<Pair<Product, Repository>>> = MutableLiveData()
var productRepos: List<Pair<Product, Repository>>
get() = _productRepos.value ?: emptyList()
set(value) {
_productRepos.value = value
}
val downloadState = MutableLiveData<DownloadState>()
val mainAction = MutableLiveData<ActionState>()
val actions = MediatorLiveData<Set<ActionState>>()
val secondaryAction = MutableLiveData<ActionState>()
val extras = MediatorLiveData<Extras>()
val downloadState: MutableLiveData<DownloadState> = MutableLiveData()
val mainAction: MutableLiveData<ActionState> = MutableLiveData()
val actions: MediatorLiveData<Set<ActionState>> = MediatorLiveData()
val secondaryAction: MutableLiveData<ActionState> = MutableLiveData()
val extras: MediatorLiveData<Extras> = MediatorLiveData()
init {
products.addSource(db.productDao.getLive(packageName)) { products.setValue(it.filterNotNull()) }
@ -81,7 +81,8 @@ class AppViewModelX(val db: DatabaseX, val packageName: String) : ViewModel() {
canLaunch -> ActionState.Launch
canInstall -> ActionState.Install
canShare -> ActionState.Share
else -> null
bookmarked == true -> ActionState.Bookmarked
else -> ActionState.Bookmark
}
val secondaryAction = when {
primaryAction != ActionState.Share && canShare -> ActionState.Share

View File

@ -27,11 +27,11 @@ class MainNavFragmentViewModelX(
) : ViewModel() {
// TODO add better sort/filter fields
var order = MutableLiveData(Order.LAST_UPDATE)
var order: MutableLiveData<Order> = MutableLiveData(Order.LAST_UPDATE)
private set
var sections = MutableLiveData<Section>(Section.All)
var sections: MutableLiveData<Section> = MutableLiveData(Section.All)
private set
var searchQuery = MutableLiveData("")
var searchQuery: MutableLiveData<String> = MutableLiveData("")
private set
fun request(source: Source): Request {

View File

@ -13,8 +13,8 @@ import kotlinx.coroutines.withContext
class RepositoriesViewModelX(val db: DatabaseX) : ViewModel() {
val repositories = MediatorLiveData<List<Repository>>()
val toLaunch = MediatorLiveData<Pair<Boolean, Long>?>()
val repositories: MediatorLiveData<List<Repository>> = MediatorLiveData()
val toLaunch: MediatorLiveData<Pair<Boolean, Long>?> = MediatorLiveData()
init {
repositories.addSource(db.repositoryDao.allLive, repositories::setValue)

View File

@ -12,8 +12,8 @@ import kotlinx.coroutines.withContext
class RepositoryViewModelX(val db: DatabaseX, val repositoryId: Long) : ViewModel() {
val repo = MediatorLiveData<Repository>()
val appsCount = MediatorLiveData<Long>()
val repo: MediatorLiveData<Repository> = MediatorLiveData()
val appsCount: MediatorLiveData<Long> = MediatorLiveData()
init {
repo.addSource(db.repositoryDao.getLive(repositoryId), repo::setValue)