From 63291f81011757bc6a6709f9e4a4671f9e9471a2 Mon Sep 17 00:00:00 2001 From: Baji Shaik Date: Sun, 7 Nov 2021 23:11:10 -0500 Subject: [PATCH 1/4] added show watched items toggle preference default sharedpreference is used to persist and retrieve show watched menu option toggle state --- .../org/schabi/newpipe/local/feed/FeedFragment.kt | 4 +++- .../org/schabi/newpipe/local/feed/FeedViewModel.kt | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt b/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt index 965075bf3..61805715c 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt @@ -128,6 +128,7 @@ class FeedFragment : BaseStateFragment() { val factory = FeedViewModel.Factory(requireContext(), groupId, showPlayedItems) viewModel = ViewModelProvider(this, factory).get(FeedViewModel::class.java) + showPlayedItems = viewModel.getSavedPlayedItemsToggle() viewModel.stateLiveData.observe(viewLifecycleOwner, { it?.let(::handleResult) }) groupAdapter = GroupieAdapter().apply { @@ -158,7 +159,7 @@ class FeedFragment : BaseStateFragment() { } } - fun setupListViewMode() { + private fun setupListViewMode() { // does everything needed to setup the layouts for grid or list modes groupAdapter.spanCount = if (shouldUseGridLayout(context)) getGridSpanCountStreams(context) else 1 feedBinding.itemsList.layoutManager = GridLayoutManager(requireContext(), groupAdapter.spanCount).apply { @@ -213,6 +214,7 @@ class FeedFragment : BaseStateFragment() { showPlayedItems = !item.isChecked updateTogglePlayedItemsButton(item) viewModel.togglePlayedItems(showPlayedItems) + viewModel.savePlayedItemsToggle(showPlayedItems) } return super.onOptionsItemSelected(item) diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt b/app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt index 8bdf412b5..5f18b6342 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt @@ -1,10 +1,12 @@ package org.schabi.newpipe.local.feed import android.content.Context +import androidx.core.content.edit import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider +import androidx.preference.PreferenceManager import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers import io.reactivex.rxjava3.core.Flowable import io.reactivex.rxjava3.functions.Function4 @@ -28,6 +30,10 @@ class FeedViewModel( initialShowPlayedItems: Boolean = true ) : ViewModel() { private var feedDatabaseManager: FeedDatabaseManager = FeedDatabaseManager(applicationContext) + private var sharedPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext) + companion object { + const val SHOW_PLAYED_ITEMS_PREFERENCE = "show_played_items_preference_tag" + } private val toggleShowPlayedItems = BehaviorProcessor.create() private val streamItems = toggleShowPlayedItems @@ -81,6 +87,13 @@ class FeedViewModel( toggleShowPlayedItems.onNext(showPlayedItems) } + fun savePlayedItemsToggle(showPlayedItems: Boolean) = sharedPreferences.edit { + this.putBoolean(SHOW_PLAYED_ITEMS_PREFERENCE, showPlayedItems) + this.apply() + } + + fun getSavedPlayedItemsToggle() = sharedPreferences.getBoolean(SHOW_PLAYED_ITEMS_PREFERENCE, true) + class Factory( private val context: Context, private val groupId: Long = FeedGroupEntity.GROUP_ALL_ID, From c35fe4f3f199b59e3763c8983ac8851e2488eefd Mon Sep 17 00:00:00 2001 From: Baji Shaik Date: Wed, 10 Nov 2021 16:16:17 -0500 Subject: [PATCH 2/4] moved preference key from viewmodel to settings_keys.xml --- .../org/schabi/newpipe/local/feed/FeedViewModel.kt | 10 ++++------ app/src/main/res/values/settings_keys.xml | 1 + 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt b/app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt index 5f18b6342..cea787c5a 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt @@ -12,6 +12,7 @@ import io.reactivex.rxjava3.core.Flowable import io.reactivex.rxjava3.functions.Function4 import io.reactivex.rxjava3.processors.BehaviorProcessor import io.reactivex.rxjava3.schedulers.Schedulers +import org.schabi.newpipe.R import org.schabi.newpipe.database.feed.model.FeedGroupEntity import org.schabi.newpipe.database.stream.StreamWithState import org.schabi.newpipe.local.feed.item.StreamItem @@ -25,15 +26,12 @@ import java.time.OffsetDateTime import java.util.concurrent.TimeUnit class FeedViewModel( - applicationContext: Context, + val applicationContext: Context, groupId: Long = FeedGroupEntity.GROUP_ALL_ID, initialShowPlayedItems: Boolean = true ) : ViewModel() { private var feedDatabaseManager: FeedDatabaseManager = FeedDatabaseManager(applicationContext) private var sharedPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext) - companion object { - const val SHOW_PLAYED_ITEMS_PREFERENCE = "show_played_items_preference_tag" - } private val toggleShowPlayedItems = BehaviorProcessor.create() private val streamItems = toggleShowPlayedItems @@ -88,11 +86,11 @@ class FeedViewModel( } fun savePlayedItemsToggle(showPlayedItems: Boolean) = sharedPreferences.edit { - this.putBoolean(SHOW_PLAYED_ITEMS_PREFERENCE, showPlayedItems) + this.putBoolean(applicationContext.getString(R.string.show_played_items_filter_key), showPlayedItems) this.apply() } - fun getSavedPlayedItemsToggle() = sharedPreferences.getBoolean(SHOW_PLAYED_ITEMS_PREFERENCE, true) + fun getSavedPlayedItemsToggle() = sharedPreferences.getBoolean(applicationContext.getString(R.string.show_played_items_filter_key), true) class Factory( private val context: Context, diff --git a/app/src/main/res/values/settings_keys.xml b/app/src/main/res/values/settings_keys.xml index 1c57178b4..e60cf17c9 100644 --- a/app/src/main/res/values/settings_keys.xml +++ b/app/src/main/res/values/settings_keys.xml @@ -14,6 +14,7 @@ saved_tabs_key + show_played_items_preference_key download_path download_path_audio From adf9badbf6a8e49a7aba8953f977283a125ab985 Mon Sep 17 00:00:00 2001 From: litetex <40789489+litetex@users.noreply.github.com> Date: Thu, 11 Nov 2021 19:46:15 +0100 Subject: [PATCH 3/4] Fixed toggle not in sync with list after app restart + refactored the code a bit --- .../schabi/newpipe/local/feed/FeedFragment.kt | 6 ++-- .../newpipe/local/feed/FeedViewModel.kt | 30 ++++++++++++------- app/src/main/res/values/settings_keys.xml | 2 +- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt b/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt index 61805715c..305802cf5 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt @@ -126,9 +126,9 @@ class FeedFragment : BaseStateFragment() { _feedBinding = FragmentFeedBinding.bind(rootView) super.onViewCreated(rootView, savedInstanceState) - val factory = FeedViewModel.Factory(requireContext(), groupId, showPlayedItems) + val factory = FeedViewModel.Factory(requireContext(), groupId) viewModel = ViewModelProvider(this, factory).get(FeedViewModel::class.java) - showPlayedItems = viewModel.getSavedPlayedItemsToggle() + showPlayedItems = viewModel.getShowPlayedItemsFromPreferences() viewModel.stateLiveData.observe(viewLifecycleOwner, { it?.let(::handleResult) }) groupAdapter = GroupieAdapter().apply { @@ -214,7 +214,7 @@ class FeedFragment : BaseStateFragment() { showPlayedItems = !item.isChecked updateTogglePlayedItemsButton(item) viewModel.togglePlayedItems(showPlayedItems) - viewModel.savePlayedItemsToggle(showPlayedItems) + viewModel.saveShowPlayedItemsToPreferences(showPlayedItems) } return super.onOptionsItemSelected(item) diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt b/app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt index cea787c5a..ecdcb7349 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt @@ -26,12 +26,11 @@ import java.time.OffsetDateTime import java.util.concurrent.TimeUnit class FeedViewModel( - val applicationContext: Context, + private val applicationContext: Context, groupId: Long = FeedGroupEntity.GROUP_ALL_ID, initialShowPlayedItems: Boolean = true ) : ViewModel() { private var feedDatabaseManager: FeedDatabaseManager = FeedDatabaseManager(applicationContext) - private var sharedPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext) private val toggleShowPlayedItems = BehaviorProcessor.create() private val streamItems = toggleShowPlayedItems @@ -85,21 +84,32 @@ class FeedViewModel( toggleShowPlayedItems.onNext(showPlayedItems) } - fun savePlayedItemsToggle(showPlayedItems: Boolean) = sharedPreferences.edit { - this.putBoolean(applicationContext.getString(R.string.show_played_items_filter_key), showPlayedItems) - this.apply() - } + fun saveShowPlayedItemsToPreferences(showPlayedItems: Boolean) = + PreferenceManager.getDefaultSharedPreferences(applicationContext).edit { + this.putBoolean(applicationContext.getString(R.string.feed_show_played_items_key), showPlayedItems) + this.apply() + } - fun getSavedPlayedItemsToggle() = sharedPreferences.getBoolean(applicationContext.getString(R.string.show_played_items_filter_key), true) + fun getShowPlayedItemsFromPreferences() = getShowPlayedItemsFromPreferences(applicationContext) + + companion object { + private fun getShowPlayedItemsFromPreferences(context: Context) = + PreferenceManager.getDefaultSharedPreferences(context) + .getBoolean(context.getString(R.string.feed_show_played_items_key), true) + } class Factory( private val context: Context, - private val groupId: Long = FeedGroupEntity.GROUP_ALL_ID, - private val showPlayedItems: Boolean + private val groupId: Long = FeedGroupEntity.GROUP_ALL_ID ) : ViewModelProvider.Factory { @Suppress("UNCHECKED_CAST") override fun create(modelClass: Class): T { - return FeedViewModel(context.applicationContext, groupId, showPlayedItems) as T + return FeedViewModel( + context.applicationContext, + groupId, + // Read initial value from preferences + getShowPlayedItemsFromPreferences(context.applicationContext) + ) as T } } } diff --git a/app/src/main/res/values/settings_keys.xml b/app/src/main/res/values/settings_keys.xml index e60cf17c9..01de6e977 100644 --- a/app/src/main/res/values/settings_keys.xml +++ b/app/src/main/res/values/settings_keys.xml @@ -14,7 +14,7 @@ saved_tabs_key - show_played_items_preference_key + feed_show_played_items download_path download_path_audio From 90cc8e2144b93db9d3557e16c376c09b7922402f Mon Sep 17 00:00:00 2001 From: litetex <40789489+litetex@users.noreply.github.com> Date: Thu, 11 Nov 2021 19:49:46 +0100 Subject: [PATCH 4/4] A feed settings-key better fits there --- app/src/main/res/values/settings_keys.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/res/values/settings_keys.xml b/app/src/main/res/values/settings_keys.xml index 01de6e977..9db147deb 100644 --- a/app/src/main/res/values/settings_keys.xml +++ b/app/src/main/res/values/settings_keys.xml @@ -14,7 +14,6 @@ saved_tabs_key - feed_show_played_items download_path download_path_audio @@ -264,6 +263,7 @@ feed_update_threshold_key 300 + feed_show_played_items show_thumbnail_key