1
1
package org.schabi.newpipe.local.feed
2
2
3
3
import android.content.Context
4
+ import androidx.core.content.edit
4
5
import androidx.lifecycle.LiveData
5
6
import androidx.lifecycle.MutableLiveData
6
7
import androidx.lifecycle.ViewModel
7
8
import androidx.lifecycle.ViewModelProvider
9
+ import androidx.preference.PreferenceManager
8
10
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
9
11
import io.reactivex.rxjava3.core.Flowable
10
12
import io.reactivex.rxjava3.functions.Function4
11
13
import io.reactivex.rxjava3.processors.BehaviorProcessor
12
14
import io.reactivex.rxjava3.schedulers.Schedulers
15
+ import org.schabi.newpipe.R
13
16
import org.schabi.newpipe.database.feed.model.FeedGroupEntity
14
17
import org.schabi.newpipe.database.stream.StreamWithState
15
18
import org.schabi.newpipe.local.feed.item.StreamItem
@@ -23,7 +26,7 @@ import java.time.OffsetDateTime
23
26
import java.util.concurrent.TimeUnit
24
27
25
28
class FeedViewModel (
26
- applicationContext : Context ,
29
+ private val applicationContext : Context ,
27
30
groupId : Long = FeedGroupEntity .GROUP_ALL_ID ,
28
31
initialShowPlayedItems : Boolean = true
29
32
) : ViewModel() {
@@ -81,14 +84,32 @@ class FeedViewModel(
81
84
toggleShowPlayedItems.onNext(showPlayedItems)
82
85
}
83
86
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
+
84
101
class Factory (
85
102
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
88
104
) : ViewModelProvider.Factory {
89
105
@Suppress(" UNCHECKED_CAST" )
90
106
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
92
113
}
93
114
}
94
115
}
0 commit comments