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