Skip to content

Commit 18ce86c

Browse files
authored
Merge pull request #7370 from iambaji/issue_7362
added show watched items toggle preference
2 parents d5f25e0 + 90cc8e2 commit 18ce86c

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt

+4-2
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,9 @@ class FeedFragment : BaseStateFragment<FeedState>() {
126126
_feedBinding = FragmentFeedBinding.bind(rootView)
127127
super.onViewCreated(rootView, savedInstanceState)
128128

129-
val factory = FeedViewModel.Factory(requireContext(), groupId, showPlayedItems)
129+
val factory = FeedViewModel.Factory(requireContext(), groupId)
130130
viewModel = ViewModelProvider(this, factory).get(FeedViewModel::class.java)
131+
showPlayedItems = viewModel.getShowPlayedItemsFromPreferences()
131132
viewModel.stateLiveData.observe(viewLifecycleOwner, { it?.let(::handleResult) })
132133

133134
groupAdapter = GroupieAdapter().apply {
@@ -158,7 +159,7 @@ class FeedFragment : BaseStateFragment<FeedState>() {
158159
}
159160
}
160161

161-
fun setupListViewMode() {
162+
private fun setupListViewMode() {
162163
// does everything needed to setup the layouts for grid or list modes
163164
groupAdapter.spanCount = if (shouldUseGridLayout(context)) getGridSpanCountStreams(context) else 1
164165
feedBinding.itemsList.layoutManager = GridLayoutManager(requireContext(), groupAdapter.spanCount).apply {
@@ -213,6 +214,7 @@ class FeedFragment : BaseStateFragment<FeedState>() {
213214
showPlayedItems = !item.isChecked
214215
updateTogglePlayedItemsButton(item)
215216
viewModel.togglePlayedItems(showPlayedItems)
217+
viewModel.saveShowPlayedItemsToPreferences(showPlayedItems)
216218
}
217219

218220
return super.onOptionsItemSelected(item)

app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt

+25-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
package org.schabi.newpipe.local.feed
22

33
import android.content.Context
4+
import androidx.core.content.edit
45
import androidx.lifecycle.LiveData
56
import androidx.lifecycle.MutableLiveData
67
import androidx.lifecycle.ViewModel
78
import androidx.lifecycle.ViewModelProvider
9+
import androidx.preference.PreferenceManager
810
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
911
import io.reactivex.rxjava3.core.Flowable
1012
import io.reactivex.rxjava3.functions.Function4
1113
import io.reactivex.rxjava3.processors.BehaviorProcessor
1214
import io.reactivex.rxjava3.schedulers.Schedulers
15+
import org.schabi.newpipe.R
1316
import org.schabi.newpipe.database.feed.model.FeedGroupEntity
1417
import org.schabi.newpipe.database.stream.StreamWithState
1518
import org.schabi.newpipe.local.feed.item.StreamItem
@@ -23,7 +26,7 @@ import java.time.OffsetDateTime
2326
import java.util.concurrent.TimeUnit
2427

2528
class FeedViewModel(
26-
applicationContext: Context,
29+
private val applicationContext: Context,
2730
groupId: Long = FeedGroupEntity.GROUP_ALL_ID,
2831
initialShowPlayedItems: Boolean = true
2932
) : ViewModel() {
@@ -81,14 +84,32 @@ class FeedViewModel(
8184
toggleShowPlayedItems.onNext(showPlayedItems)
8285
}
8386

87+
fun saveShowPlayedItemsToPreferences(showPlayedItems: Boolean) =
88+
PreferenceManager.getDefaultSharedPreferences(applicationContext).edit {
89+
this.putBoolean(applicationContext.getString(R.string.feed_show_played_items_key), showPlayedItems)
90+
this.apply()
91+
}
92+
93+
fun getShowPlayedItemsFromPreferences() = getShowPlayedItemsFromPreferences(applicationContext)
94+
95+
companion object {
96+
private fun getShowPlayedItemsFromPreferences(context: Context) =
97+
PreferenceManager.getDefaultSharedPreferences(context)
98+
.getBoolean(context.getString(R.string.feed_show_played_items_key), true)
99+
}
100+
84101
class Factory(
85102
private val context: Context,
86-
private val groupId: Long = FeedGroupEntity.GROUP_ALL_ID,
87-
private val showPlayedItems: Boolean
103+
private val groupId: Long = FeedGroupEntity.GROUP_ALL_ID
88104
) : ViewModelProvider.Factory {
89105
@Suppress("UNCHECKED_CAST")
90106
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
91-
return FeedViewModel(context.applicationContext, groupId, showPlayedItems) as T
107+
return FeedViewModel(
108+
context.applicationContext,
109+
groupId,
110+
// Read initial value from preferences
111+
getShowPlayedItemsFromPreferences(context.applicationContext)
112+
) as T
92113
}
93114
}
94115
}

app/src/main/res/values/settings_keys.xml

+1
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@
263263

264264
<string name="feed_update_threshold_key" translatable="false">feed_update_threshold_key</string>
265265
<string name="feed_update_threshold_default_value" translatable="false">300</string>
266+
<string name="feed_show_played_items_key" translatable="false">feed_show_played_items</string>
266267

267268
<string name="show_thumbnail_key" translatable="false">show_thumbnail_key</string>
268269

0 commit comments

Comments
 (0)