Could toggle between list view and grid view...once. Requires bug fixing on refreshing

This commit is contained in:
Samuel Wu 2022-10-25 02:01:57 +11:00
parent 3bfcb16f9a
commit 1aa1a0287e
2 changed files with 27 additions and 1 deletions

View File

@ -356,10 +356,11 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
}
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<SubscriptionState>() {
binding.itemsList.post { feedGroupsSortMenuItem.notifyChanged(PAYLOAD_UPDATE_VISIBILITY_MENU_ITEM) }
}
private fun handleFeedGroupsVertical(groups: List<Group>) {
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
// /////////////////////////////////////////////////////////////////////////

View File

@ -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<SubscriptionState>()
private val mutableFeedGroupsLiveData = MutableLiveData<List<Group>>()
private val mutableFeedGroupsVerticalLiveData = MutableLiveData<List<Group>>()
val stateLiveData: LiveData<SubscriptionState> = mutableStateLiveData
val feedGroupsLiveData: LiveData<List<Group>> = mutableFeedGroupsLiveData
val feedGroupsVerticalLiveData: LiveData<List<Group>> = 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 {