Don't expose MutableLiveData in view models
This commit is contained in:
parent
597859eb23
commit
98c65d8ddb
4 changed files with 33 additions and 21 deletions
|
@ -1,6 +1,7 @@
|
|||
package org.schabi.newpipe.local.feed
|
||||
|
||||
import android.content.Context
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
|
@ -26,7 +27,8 @@ class FeedViewModel(applicationContext: Context, val groupId: Long = FeedGroupEn
|
|||
|
||||
private var feedDatabaseManager: FeedDatabaseManager = FeedDatabaseManager(applicationContext)
|
||||
|
||||
val stateLiveData = MutableLiveData<FeedState>()
|
||||
private val mutableStateLiveData = MutableLiveData<FeedState>()
|
||||
val stateLiveData: LiveData<FeedState> = mutableStateLiveData
|
||||
|
||||
private var combineDisposable = Flowable
|
||||
.combineLatest(
|
||||
|
@ -48,7 +50,7 @@ class FeedViewModel(applicationContext: Context, val groupId: Long = FeedGroupEn
|
|||
val oldestUpdateCalendar =
|
||||
oldestUpdate?.let { Calendar.getInstance().apply { time = it } }
|
||||
|
||||
stateLiveData.postValue(when (event) {
|
||||
mutableStateLiveData.postValue(when (event) {
|
||||
is IdleEvent -> FeedState.LoadedState(listFromDB, oldestUpdateCalendar, notLoadedCount)
|
||||
is ProgressEvent -> FeedState.ProgressState(event.currentProgress, event.maxProgress, event.progressMessage)
|
||||
is SuccessResultEvent -> FeedState.LoadedState(listFromDB, oldestUpdateCalendar, notLoadedCount, event.itemsErrors)
|
||||
|
|
|
@ -2,6 +2,7 @@ package org.schabi.newpipe.local.subscription
|
|||
|
||||
import android.app.Application
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import com.xwray.groupie.Group
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
|
@ -12,19 +13,21 @@ import org.schabi.newpipe.util.DEFAULT_THROTTLE_TIMEOUT
|
|||
import java.util.concurrent.TimeUnit
|
||||
|
||||
class SubscriptionViewModel(application: Application) : AndroidViewModel(application) {
|
||||
val stateLiveData = MutableLiveData<SubscriptionState>()
|
||||
val feedGroupsLiveData = MutableLiveData<List<Group>>()
|
||||
|
||||
private var feedDatabaseManager: FeedDatabaseManager = FeedDatabaseManager(application)
|
||||
private var subscriptionManager = SubscriptionManager(application)
|
||||
|
||||
private val mutableStateLiveData = MutableLiveData<SubscriptionState>()
|
||||
private val mutableFeedGroupsLiveData = MutableLiveData<List<Group>>()
|
||||
val stateLiveData: LiveData<SubscriptionState> = mutableStateLiveData
|
||||
val feedGroupsLiveData: LiveData<List<Group>> = mutableFeedGroupsLiveData
|
||||
|
||||
private var feedGroupItemsDisposable = feedDatabaseManager.groups()
|
||||
.throttleLatest(DEFAULT_THROTTLE_TIMEOUT, TimeUnit.MILLISECONDS)
|
||||
.map { it.map(::FeedGroupCardItem) }
|
||||
.subscribeOn(Schedulers.io())
|
||||
.subscribe(
|
||||
{ feedGroupsLiveData.postValue(it) },
|
||||
{ stateLiveData.postValue(SubscriptionState.ErrorState(it)) }
|
||||
{ mutableFeedGroupsLiveData.postValue(it) },
|
||||
{ mutableStateLiveData.postValue(SubscriptionState.ErrorState(it)) }
|
||||
)
|
||||
|
||||
private var stateItemsDisposable = subscriptionManager.subscriptions()
|
||||
|
@ -32,8 +35,8 @@ class SubscriptionViewModel(application: Application) : AndroidViewModel(applica
|
|||
.map { it.map { entity -> ChannelItem(entity.toChannelInfoItem(), entity.uid, ChannelItem.ItemVersion.MINI) } }
|
||||
.subscribeOn(Schedulers.io())
|
||||
.subscribe(
|
||||
{ stateLiveData.postValue(SubscriptionState.LoadedState(it)) },
|
||||
{ stateLiveData.postValue(SubscriptionState.ErrorState(it)) }
|
||||
{ mutableStateLiveData.postValue(SubscriptionState.LoadedState(it)) },
|
||||
{ mutableStateLiveData.postValue(SubscriptionState.ErrorState(it)) }
|
||||
)
|
||||
|
||||
override fun onCleared() {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package org.schabi.newpipe.local.subscription.dialog
|
||||
|
||||
import android.content.Context
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
|
@ -27,21 +28,24 @@ class FeedGroupDialogViewModel(applicationContext: Context, val groupId: Long =
|
|||
private var feedDatabaseManager: FeedDatabaseManager = FeedDatabaseManager(applicationContext)
|
||||
private var subscriptionManager = SubscriptionManager(applicationContext)
|
||||
|
||||
val groupLiveData = MutableLiveData<FeedGroupEntity>()
|
||||
val subscriptionsLiveData = MutableLiveData<Pair<List<SubscriptionEntity>, Set<Long>>>()
|
||||
val dialogEventLiveData = MutableLiveData<DialogEvent>()
|
||||
private val mutableGroupLiveData = MutableLiveData<FeedGroupEntity>()
|
||||
private val mutableSubscriptionsLiveData = MutableLiveData<Pair<List<SubscriptionEntity>, Set<Long>>>()
|
||||
private val mutableDialogEventLiveData = MutableLiveData<DialogEvent>()
|
||||
val groupLiveData: LiveData<FeedGroupEntity> = mutableGroupLiveData
|
||||
val subscriptionsLiveData: LiveData<Pair<List<SubscriptionEntity>, Set<Long>>> = mutableSubscriptionsLiveData
|
||||
val dialogEventLiveData: LiveData<DialogEvent> = mutableDialogEventLiveData
|
||||
|
||||
private var actionProcessingDisposable: Disposable? = null
|
||||
|
||||
private var feedGroupDisposable = feedDatabaseManager.getGroup(groupId)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.subscribe(groupLiveData::postValue)
|
||||
.subscribe(mutableGroupLiveData::postValue)
|
||||
|
||||
private var subscriptionsDisposable = Flowable
|
||||
.combineLatest(subscriptionManager.subscriptions(), feedDatabaseManager.subscriptionIdsForGroup(groupId),
|
||||
BiFunction { t1: List<SubscriptionEntity>, t2: List<Long> -> t1 to t2.toSet() })
|
||||
.subscribeOn(Schedulers.io())
|
||||
.subscribe(subscriptionsLiveData::postValue)
|
||||
.subscribe(mutableSubscriptionsLiveData::postValue)
|
||||
|
||||
override fun onCleared() {
|
||||
super.onCleared()
|
||||
|
@ -68,11 +72,11 @@ class FeedGroupDialogViewModel(applicationContext: Context, val groupId: Long =
|
|||
|
||||
private fun doAction(completable: Completable) {
|
||||
if (actionProcessingDisposable == null) {
|
||||
dialogEventLiveData.value = DialogEvent.ProcessingEvent
|
||||
mutableDialogEventLiveData.value = DialogEvent.ProcessingEvent
|
||||
|
||||
actionProcessingDisposable = completable
|
||||
.subscribeOn(Schedulers.io())
|
||||
.subscribe { dialogEventLiveData.postValue(DialogEvent.SuccessEvent) }
|
||||
.subscribe { mutableDialogEventLiveData.postValue(DialogEvent.SuccessEvent) }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ package org.schabi.newpipe.local.subscription.dialog
|
|||
|
||||
import android.app.Application
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import io.reactivex.Completable
|
||||
import io.reactivex.disposables.Disposable
|
||||
|
@ -12,15 +13,17 @@ import org.schabi.newpipe.local.feed.FeedDatabaseManager
|
|||
class FeedGroupReorderDialogViewModel(application: Application) : AndroidViewModel(application) {
|
||||
private var feedDatabaseManager: FeedDatabaseManager = FeedDatabaseManager(application)
|
||||
|
||||
val groupsLiveData = MutableLiveData<List<FeedGroupEntity>>()
|
||||
val dialogEventLiveData = MutableLiveData<DialogEvent>()
|
||||
private val mutableGroupsLiveData = MutableLiveData<List<FeedGroupEntity>>()
|
||||
private val mutableDialogEventLiveData = MutableLiveData<DialogEvent>()
|
||||
val groupsLiveData: LiveData<List<FeedGroupEntity>> = mutableGroupsLiveData
|
||||
val dialogEventLiveData: LiveData<DialogEvent> = mutableDialogEventLiveData
|
||||
|
||||
private var actionProcessingDisposable: Disposable? = null
|
||||
|
||||
private var groupsDisposable = feedDatabaseManager.groups()
|
||||
.limit(1)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.subscribe(groupsLiveData::postValue)
|
||||
.subscribe(mutableGroupsLiveData::postValue)
|
||||
|
||||
override fun onCleared() {
|
||||
super.onCleared()
|
||||
|
@ -34,11 +37,11 @@ class FeedGroupReorderDialogViewModel(application: Application) : AndroidViewMod
|
|||
|
||||
private fun doAction(completable: Completable) {
|
||||
if (actionProcessingDisposable == null) {
|
||||
dialogEventLiveData.value = DialogEvent.ProcessingEvent
|
||||
mutableDialogEventLiveData.value = DialogEvent.ProcessingEvent
|
||||
|
||||
actionProcessingDisposable = completable
|
||||
.subscribeOn(Schedulers.io())
|
||||
.subscribe { dialogEventLiveData.postValue(DialogEvent.SuccessEvent) }
|
||||
.subscribe { mutableDialogEventLiveData.postValue(DialogEvent.SuccessEvent) }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue