From 98c65d8ddbadb78d25da29cb7b646ff894643553 Mon Sep 17 00:00:00 2001 From: Mauricio Colli Date: Thu, 5 Mar 2020 19:47:00 -0300 Subject: [PATCH] Don't expose MutableLiveData in view models --- .../schabi/newpipe/local/feed/FeedViewModel.kt | 6 ++++-- .../subscription/SubscriptionViewModel.kt | 17 ++++++++++------- .../dialog/FeedGroupDialogViewModel.kt | 18 +++++++++++------- .../dialog/FeedGroupReorderDialogViewModel.kt | 13 ++++++++----- 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt b/app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt index 1a7757b8b..adc262ecb 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt @@ -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() + private val mutableStateLiveData = MutableLiveData() + val stateLiveData: LiveData = 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) diff --git a/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionViewModel.kt b/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionViewModel.kt index 1a9c0e5b1..6454cc912 100644 --- a/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionViewModel.kt +++ b/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionViewModel.kt @@ -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() - val feedGroupsLiveData = MutableLiveData>() - private var feedDatabaseManager: FeedDatabaseManager = FeedDatabaseManager(application) private var subscriptionManager = SubscriptionManager(application) + private val mutableStateLiveData = MutableLiveData() + private val mutableFeedGroupsLiveData = MutableLiveData>() + val stateLiveData: LiveData = mutableStateLiveData + val feedGroupsLiveData: LiveData> = 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() { diff --git a/app/src/main/java/org/schabi/newpipe/local/subscription/dialog/FeedGroupDialogViewModel.kt b/app/src/main/java/org/schabi/newpipe/local/subscription/dialog/FeedGroupDialogViewModel.kt index 07d4003c0..bd57a2639 100644 --- a/app/src/main/java/org/schabi/newpipe/local/subscription/dialog/FeedGroupDialogViewModel.kt +++ b/app/src/main/java/org/schabi/newpipe/local/subscription/dialog/FeedGroupDialogViewModel.kt @@ -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() - val subscriptionsLiveData = MutableLiveData, Set>>() - val dialogEventLiveData = MutableLiveData() + private val mutableGroupLiveData = MutableLiveData() + private val mutableSubscriptionsLiveData = MutableLiveData, Set>>() + private val mutableDialogEventLiveData = MutableLiveData() + val groupLiveData: LiveData = mutableGroupLiveData + val subscriptionsLiveData: LiveData, Set>> = mutableSubscriptionsLiveData + val dialogEventLiveData: LiveData = 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, t2: List -> 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) } } } diff --git a/app/src/main/java/org/schabi/newpipe/local/subscription/dialog/FeedGroupReorderDialogViewModel.kt b/app/src/main/java/org/schabi/newpipe/local/subscription/dialog/FeedGroupReorderDialogViewModel.kt index ec738eed6..8ef5bb55c 100644 --- a/app/src/main/java/org/schabi/newpipe/local/subscription/dialog/FeedGroupReorderDialogViewModel.kt +++ b/app/src/main/java/org/schabi/newpipe/local/subscription/dialog/FeedGroupReorderDialogViewModel.kt @@ -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>() - val dialogEventLiveData = MutableLiveData() + private val mutableGroupsLiveData = MutableLiveData>() + private val mutableDialogEventLiveData = MutableLiveData() + val groupsLiveData: LiveData> = mutableGroupsLiveData + val dialogEventLiveData: LiveData = 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) } } }