From 1aa1a0287e47ddddaf19bcdcafcb9c3e3f3ecc8b Mon Sep 17 00:00:00 2001 From: Samuel Wu Date: Tue, 25 Oct 2022 02:01:57 +1100 Subject: [PATCH] Could toggle between list view and grid view...once. Requires bug fixing on refreshing --- .../local/subscription/SubscriptionFragment.kt | 15 ++++++++++++++- .../local/subscription/SubscriptionViewModel.kt | 13 +++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.kt b/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.kt index eafa9f62c..d282e58c1 100644 --- a/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.kt +++ b/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionFragment.kt @@ -356,10 +356,11 @@ class SubscriptionFragment : BaseStateFragment() { } binding.itemsList.adapter = groupAdapter - //TODO: change viewModel or create another one + // TODO: change viewModel or create another one viewModel = ViewModelProvider(this).get(SubscriptionViewModel::class.java) viewModel.stateLiveData.observe(viewLifecycleOwner) { it?.let(this::handleResult) } viewModel.feedGroupsLiveData.observe(viewLifecycleOwner) { it?.let(this::handleFeedGroups) } + viewModel.feedGroupsVerticalLiveData.observe(viewLifecycleOwner) { it?.let(this::handleFeedGroupsVertical) } } private fun showLongTapDialog(selectedItem: ChannelInfoItem) { @@ -486,6 +487,18 @@ class SubscriptionFragment : BaseStateFragment() { binding.itemsList.post { feedGroupsSortMenuItem.notifyChanged(PAYLOAD_UPDATE_VISIBILITY_MENU_ITEM) } } + private fun handleFeedGroupsVertical(groups: List) { + feedGroupsSection.update(groups) + + if (feedGroupsListState != null) { + feedGroupsCarousel?.onRestoreInstanceState(feedGroupsListState) + feedGroupsListVerticalState = null + } + + feedGroupsSortMenuItem.showMenuItem = groups.size > 1 + binding.itemsList.post { feedGroupsSortMenuItem.notifyChanged(PAYLOAD_UPDATE_VISIBILITY_MENU_ITEM) } + } + // ///////////////////////////////////////////////////////////////////////// // Contract // ///////////////////////////////////////////////////////////////////////// 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 da009e1a0..3f378929e 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 @@ -9,6 +9,7 @@ import io.reactivex.rxjava3.schedulers.Schedulers import org.schabi.newpipe.local.feed.FeedDatabaseManager import org.schabi.newpipe.local.subscription.item.ChannelItem import org.schabi.newpipe.local.subscription.item.FeedGroupCardItem +import org.schabi.newpipe.local.subscription.item.FeedGroupCardVerticalItem import org.schabi.newpipe.util.DEFAULT_THROTTLE_TIMEOUT import java.util.concurrent.TimeUnit @@ -18,8 +19,10 @@ class SubscriptionViewModel(application: Application) : AndroidViewModel(applica private val mutableStateLiveData = MutableLiveData() private val mutableFeedGroupsLiveData = MutableLiveData>() + private val mutableFeedGroupsVerticalLiveData = MutableLiveData>() val stateLiveData: LiveData = mutableStateLiveData val feedGroupsLiveData: LiveData> = mutableFeedGroupsLiveData + val feedGroupsVerticalLiveData: LiveData> = mutableFeedGroupsVerticalLiveData private var feedGroupItemsDisposable = feedDatabaseManager.groups() .throttleLatest(DEFAULT_THROTTLE_TIMEOUT, TimeUnit.MILLISECONDS) @@ -30,6 +33,15 @@ class SubscriptionViewModel(application: Application) : AndroidViewModel(applica { mutableStateLiveData.postValue(SubscriptionState.ErrorState(it)) } ) + private var feedGroupVerticalItemsDisposable = feedDatabaseManager.groups() + .throttleLatest(DEFAULT_THROTTLE_TIMEOUT, TimeUnit.MILLISECONDS) + .map { it.map(::FeedGroupCardVerticalItem) } + .subscribeOn(Schedulers.io()) + .subscribe( + { mutableFeedGroupsVerticalLiveData.postValue(it) }, + { mutableStateLiveData.postValue(SubscriptionState.ErrorState(it)) } + ) + private var stateItemsDisposable = subscriptionManager.subscriptions() .throttleLatest(DEFAULT_THROTTLE_TIMEOUT, TimeUnit.MILLISECONDS) .map { it.map { entity -> ChannelItem(entity.toChannelInfoItem(), entity.uid, ChannelItem.ItemVersion.MINI) } } @@ -43,6 +55,7 @@ class SubscriptionViewModel(application: Application) : AndroidViewModel(applica super.onCleared() stateItemsDisposable.dispose() feedGroupItemsDisposable.dispose() + feedGroupVerticalItemsDisposable.dispose() } sealed class SubscriptionState {