From 63291f81011757bc6a6709f9e4a4671f9e9471a2 Mon Sep 17 00:00:00 2001 From: Baji Shaik Date: Sun, 7 Nov 2021 23:11:10 -0500 Subject: [PATCH] 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,