Skip to content

Commit

Permalink
Reshow feed if grid/list view mode changed
Browse files Browse the repository at this point in the history
  • Loading branch information
Stypox committed Jun 15, 2021
1 parent fdb6679 commit 32df4d3
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package org.schabi.newpipe.local.feed
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.content.SharedPreferences
import android.content.res.Configuration
import android.os.Bundle
import android.os.Parcelable
Expand Down Expand Up @@ -94,6 +95,9 @@ class FeedFragment : BaseStateFragment<FeedState>() {
private lateinit var groupAdapter: GroupAdapter<GroupieViewHolder>
@State @JvmField var showPlayedItems: Boolean = true

private var onSettingsChangeListener: SharedPreferences.OnSharedPreferenceChangeListener? = null
private var updateListViewModeOnResume = false

init {
setHasOptionsMenu(true)
}
Expand All @@ -104,6 +108,14 @@ class FeedFragment : BaseStateFragment<FeedState>() {
groupId = arguments?.getLong(KEY_GROUP_ID, FeedGroupEntity.GROUP_ALL_ID)
?: FeedGroupEntity.GROUP_ALL_ID
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? {
Expand All @@ -122,15 +134,10 @@ class FeedFragment : BaseStateFragment<FeedState>() {
groupAdapter = GroupAdapter<GroupieViewHolder>().apply {
setOnItemClickListener(listenerStreamItem)
setOnItemLongClickListener(listenerStreamItem)
spanCount = if (shouldUseGridLayout()) getGridSpanCount() else 1
}

feedBinding.itemsList.apply {
layoutManager = GridLayoutManager(requireContext(), groupAdapter.spanCount).apply {
spanSizeLookup = groupAdapter.spanSizeLookup
}
adapter = groupAdapter
}
feedBinding.itemsList.adapter = groupAdapter
setupListViewMode()
}

override fun onPause() {
Expand All @@ -141,6 +148,23 @@ class FeedFragment : BaseStateFragment<FeedState>() {
override fun onResume() {
super.onResume()
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) {
Expand Down Expand Up @@ -210,6 +234,12 @@ class FeedFragment : BaseStateFragment<FeedState>() {

override fun onDestroy() {
disposables.dispose()
if (onSettingsChangeListener != null) {
PreferenceManager.getDefaultSharedPreferences(activity)
.unregisterOnSharedPreferenceChangeListener(onSettingsChangeListener)
onSettingsChangeListener = null
}

super.onDestroy()
activity?.supportActionBar?.subtitle = null
}
Expand Down

0 comments on commit 32df4d3

Please sign in to comment.