Reshow feed if grid/list view mode changed

This commit is contained in:
Stypox 2021-06-15 18:40:25 +02:00
parent fdb6679d2d
commit 32df4d39a4
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
1 changed files with 37 additions and 7 deletions

View File

@ -22,6 +22,7 @@ package org.schabi.newpipe.local.feed
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.Activity import android.app.Activity
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences
import android.content.res.Configuration import android.content.res.Configuration
import android.os.Bundle import android.os.Bundle
import android.os.Parcelable import android.os.Parcelable
@ -94,6 +95,9 @@ class FeedFragment : BaseStateFragment<FeedState>() {
private lateinit var groupAdapter: GroupAdapter<GroupieViewHolder> private lateinit var groupAdapter: GroupAdapter<GroupieViewHolder>
@State @JvmField var showPlayedItems: Boolean = true @State @JvmField var showPlayedItems: Boolean = true
private var onSettingsChangeListener: SharedPreferences.OnSharedPreferenceChangeListener? = null
private var updateListViewModeOnResume = false
init { init {
setHasOptionsMenu(true) setHasOptionsMenu(true)
} }
@ -104,6 +108,14 @@ class FeedFragment : BaseStateFragment<FeedState>() {
groupId = arguments?.getLong(KEY_GROUP_ID, FeedGroupEntity.GROUP_ALL_ID) groupId = arguments?.getLong(KEY_GROUP_ID, FeedGroupEntity.GROUP_ALL_ID)
?: FeedGroupEntity.GROUP_ALL_ID ?: FeedGroupEntity.GROUP_ALL_ID
groupName = arguments?.getString(KEY_GROUP_NAME) ?: "" groupName = arguments?.getString(KEY_GROUP_NAME) ?: ""
onSettingsChangeListener = SharedPreferences.OnSharedPreferenceChangeListener { _, key ->
if (key.equals(getString(R.string.list_view_mode_key))) {
updateListViewModeOnResume = true
}
}
PreferenceManager.getDefaultSharedPreferences(activity)
.registerOnSharedPreferenceChangeListener(onSettingsChangeListener)
} }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
@ -122,15 +134,10 @@ class FeedFragment : BaseStateFragment<FeedState>() {
groupAdapter = GroupAdapter<GroupieViewHolder>().apply { groupAdapter = GroupAdapter<GroupieViewHolder>().apply {
setOnItemClickListener(listenerStreamItem) setOnItemClickListener(listenerStreamItem)
setOnItemLongClickListener(listenerStreamItem) setOnItemLongClickListener(listenerStreamItem)
spanCount = if (shouldUseGridLayout()) getGridSpanCount() else 1
} }
feedBinding.itemsList.apply { feedBinding.itemsList.adapter = groupAdapter
layoutManager = GridLayoutManager(requireContext(), groupAdapter.spanCount).apply { setupListViewMode()
spanSizeLookup = groupAdapter.spanSizeLookup
}
adapter = groupAdapter
}
} }
override fun onPause() { override fun onPause() {
@ -141,6 +148,23 @@ class FeedFragment : BaseStateFragment<FeedState>() {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
updateRelativeTimeViews() updateRelativeTimeViews()
if (updateListViewModeOnResume) {
updateListViewModeOnResume = false
setupListViewMode()
if (viewModel.stateLiveData.value != null) {
handleResult(viewModel.stateLiveData.value!!)
}
}
}
fun setupListViewMode() {
// does everything needed to setup the layouts for grid or list modes
groupAdapter.spanCount = if (shouldUseGridLayout()) getGridSpanCount() else 1
feedBinding.itemsList.layoutManager = GridLayoutManager(requireContext(), groupAdapter.spanCount).apply {
spanSizeLookup = groupAdapter.spanSizeLookup
}
} }
override fun setUserVisibleHint(isVisibleToUser: Boolean) { override fun setUserVisibleHint(isVisibleToUser: Boolean) {
@ -210,6 +234,12 @@ class FeedFragment : BaseStateFragment<FeedState>() {
override fun onDestroy() { override fun onDestroy() {
disposables.dispose() disposables.dispose()
if (onSettingsChangeListener != null) {
PreferenceManager.getDefaultSharedPreferences(activity)
.unregisterOnSharedPreferenceChangeListener(onSettingsChangeListener)
onSettingsChangeListener = null
}
super.onDestroy() super.onDestroy()
activity?.supportActionBar?.subtitle = null activity?.supportActionBar?.subtitle = null
} }