Merge pull request #9298 from Stypox/fix-inconsistent-channel-groups

Fix inconsistent channel group list and items view mode
This commit is contained in:
Tobi 2022-11-05 21:11:51 +01:00 committed by GitHub
commit da61c9f915
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 15 deletions

View File

@ -254,7 +254,11 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
viewModel = ViewModelProvider(this)[SubscriptionViewModel::class.java] viewModel = ViewModelProvider(this)[SubscriptionViewModel::class.java]
viewModel.stateLiveData.observe(viewLifecycleOwner) { it?.let(this::handleResult) } viewModel.stateLiveData.observe(viewLifecycleOwner) { it?.let(this::handleResult) }
viewModel.feedGroupsLiveData.observe(viewLifecycleOwner) { it?.let(this::handleFeedGroups) } viewModel.feedGroupsLiveData.observe(viewLifecycleOwner) {
it?.let { (groups, listViewMode) ->
handleFeedGroups(groups, listViewMode)
}
}
setupInitialLayout() setupInitialLayout()
} }
@ -405,17 +409,12 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
} }
} }
private fun handleFeedGroups(groups: List<Group>) { private fun handleFeedGroups(groups: List<Group>, listViewMode: Boolean) {
val listViewMode = viewModel.getListViewMode()
if (feedGroupsCarouselState != null) { if (feedGroupsCarouselState != null) {
feedGroupsCarousel.onRestoreInstanceState(feedGroupsCarouselState) feedGroupsCarousel.onRestoreInstanceState(feedGroupsCarouselState)
feedGroupsCarouselState = null feedGroupsCarouselState = null
} }
feedGroupsCarousel.listViewMode = listViewMode
feedGroupsSortMenuItem.showSortButton = groups.size > 1
feedGroupsSortMenuItem.listViewMode = listViewMode
binding.itemsList.post { binding.itemsList.post {
if (context == null) { if (context == null) {
// since this part was posted to the next UI cycle, the fragment might have been // since this part was posted to the next UI cycle, the fragment might have been
@ -423,6 +422,9 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
return@post return@post
} }
feedGroupsCarousel.listViewMode = listViewMode
feedGroupsSortMenuItem.showSortButton = groups.size > 1
feedGroupsSortMenuItem.listViewMode = listViewMode
feedGroupsCarousel.notifyChanged(FeedGroupCarouselItem.PAYLOAD_UPDATE_LIST_VIEW_MODE) feedGroupsCarousel.notifyChanged(FeedGroupCarouselItem.PAYLOAD_UPDATE_LIST_VIEW_MODE)
feedGroupsSortMenuItem.notifyChanged(GroupsHeader.PAYLOAD_UPDATE_ICONS) feedGroupsSortMenuItem.notifyChanged(GroupsHeader.PAYLOAD_UPDATE_ICONS)

View File

@ -27,9 +27,9 @@ class SubscriptionViewModel(application: Application) : AndroidViewModel(applica
private val listViewModeFlowable = listViewMode.distinctUntilChanged() private val listViewModeFlowable = listViewMode.distinctUntilChanged()
private val mutableStateLiveData = MutableLiveData<SubscriptionState>() private val mutableStateLiveData = MutableLiveData<SubscriptionState>()
private val mutableFeedGroupsLiveData = MutableLiveData<List<Group>>() private val mutableFeedGroupsLiveData = MutableLiveData<Pair<List<Group>, Boolean>>()
val stateLiveData: LiveData<SubscriptionState> = mutableStateLiveData val stateLiveData: LiveData<SubscriptionState> = mutableStateLiveData
val feedGroupsLiveData: LiveData<List<Group>> = mutableFeedGroupsLiveData val feedGroupsLiveData: LiveData<Pair<List<Group>, Boolean>> = mutableFeedGroupsLiveData
private var feedGroupItemsDisposable = Flowable private var feedGroupItemsDisposable = Flowable
.combineLatest( .combineLatest(
@ -39,7 +39,10 @@ class SubscriptionViewModel(application: Application) : AndroidViewModel(applica
) )
.throttleLatest(DEFAULT_THROTTLE_TIMEOUT, TimeUnit.MILLISECONDS) .throttleLatest(DEFAULT_THROTTLE_TIMEOUT, TimeUnit.MILLISECONDS)
.map { (feedGroups, listViewMode) -> .map { (feedGroups, listViewMode) ->
feedGroups.map(if (listViewMode) ::FeedGroupCardItem else ::FeedGroupCardGridItem) Pair(
feedGroups.map(if (listViewMode) ::FeedGroupCardItem else ::FeedGroupCardGridItem),
listViewMode
)
} }
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.subscribe( .subscribe(

View File

@ -10,7 +10,7 @@ import com.xwray.groupie.viewbinding.GroupieViewHolder
import org.schabi.newpipe.R import org.schabi.newpipe.R
import org.schabi.newpipe.databinding.FeedItemCarouselBinding import org.schabi.newpipe.databinding.FeedItemCarouselBinding
import org.schabi.newpipe.util.DeviceUtils import org.schabi.newpipe.util.DeviceUtils
import java.lang.Integer.max import org.schabi.newpipe.util.ThemeHelper.getGridSpanCount
class FeedGroupCarouselItem( class FeedGroupCarouselItem(
private val carouselAdapter: GroupAdapter<GroupieViewHolder<FeedItemCarouselBinding>>, private val carouselAdapter: GroupAdapter<GroupieViewHolder<FeedItemCarouselBinding>>,
@ -71,10 +71,7 @@ class FeedGroupCarouselItem(
carouselLayoutManager = if (listViewMode) { carouselLayoutManager = if (listViewMode) {
LinearLayoutManager(context) LinearLayoutManager(context)
} else { } else {
GridLayoutManager( GridLayoutManager(context, getGridSpanCount(context, DeviceUtils.dpToPx(112, context)))
context,
max(1, viewBinding.recyclerView.width / DeviceUtils.dpToPx(112, context))
)
} }
viewBinding.recyclerView.apply { viewBinding.recyclerView.apply {