@@ -11,7 +11,7 @@ import androidx.lifecycle.viewmodel.viewModelFactory
11
11
import androidx.preference.PreferenceManager
12
12
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
13
13
import io.reactivex.rxjava3.core.Flowable
14
- import io.reactivex.rxjava3.functions.Function5
14
+ import io.reactivex.rxjava3.functions.Function6
15
15
import io.reactivex.rxjava3.processors.BehaviorProcessor
16
16
import io.reactivex.rxjava3.schedulers.Schedulers
17
17
import org.schabi.newpipe.App
@@ -31,18 +31,24 @@ import java.util.concurrent.TimeUnit
31
31
class FeedViewModel (
32
32
private val application : Application ,
33
33
groupId : Long = FeedGroupEntity .GROUP_ALL_ID ,
34
- initialShowPlayedItems : Boolean = true ,
35
- initialShowFutureItems : Boolean = true
34
+ initialShowPlayedItems : Boolean ,
35
+ initialShowPartiallyPlayedItems : Boolean ,
36
+ initialShowFutureItems : Boolean
36
37
) : ViewModel() {
37
38
private val feedDatabaseManager = FeedDatabaseManager (application)
38
39
39
- private val toggleShowPlayedItems = BehaviorProcessor .create<Boolean >()
40
- private val toggleShowPlayedItemsFlowable = toggleShowPlayedItems
40
+ private val showPlayedItems = BehaviorProcessor .create<Boolean >()
41
+ private val showPlayedItemsFlowable = showPlayedItems
41
42
.startWithItem(initialShowPlayedItems)
42
43
.distinctUntilChanged()
43
44
44
- private val toggleShowFutureItems = BehaviorProcessor .create<Boolean >()
45
- private val toggleShowFutureItemsFlowable = toggleShowFutureItems
45
+ private val showPartiallyPlayedItems = BehaviorProcessor .create<Boolean >()
46
+ private val showPartiallyPlayedItemsFlowable = showPartiallyPlayedItems
47
+ .startWithItem(initialShowPartiallyPlayedItems)
48
+ .distinctUntilChanged()
49
+
50
+ private val showFutureItems = BehaviorProcessor .create<Boolean >()
51
+ private val showFutureItemsFlowable = showFutureItems
46
52
.startWithItem(initialShowFutureItems)
47
53
.distinctUntilChanged()
48
54
@@ -52,23 +58,24 @@ class FeedViewModel(
52
58
private var combineDisposable = Flowable
53
59
.combineLatest(
54
60
FeedEventManager .events(),
55
- toggleShowPlayedItemsFlowable,
56
- toggleShowFutureItemsFlowable,
61
+ showPlayedItemsFlowable,
62
+ showPartiallyPlayedItemsFlowable,
63
+ showFutureItemsFlowable,
57
64
feedDatabaseManager.notLoadedCount(groupId),
58
65
feedDatabaseManager.oldestSubscriptionUpdate(groupId),
59
66
60
- Function5 { t1: FeedEventManager .Event , t2: Boolean , t3: Boolean ,
61
- t4 : Long , t5 : List <OffsetDateTime > ->
62
- return @Function5 CombineResultEventHolder (t1, t2, t3, t4, t5.firstOrNull())
67
+ Function6 { t1: FeedEventManager .Event , t2: Boolean , t3: Boolean , t4 : Boolean ,
68
+ t5 : Long , t6 : List <OffsetDateTime > ->
69
+ return @Function6 CombineResultEventHolder (t1, t2, t3, t4, t5, t6 .firstOrNull())
63
70
}
64
71
)
65
72
.throttleLatest(DEFAULT_THROTTLE_TIMEOUT , TimeUnit .MILLISECONDS )
66
73
.subscribeOn(Schedulers .io())
67
74
.observeOn(Schedulers .io())
68
- .map { (event, showPlayedItems, showFutureItems, notLoadedCount, oldestUpdate) ->
75
+ .map { (event, showPlayedItems, showPartiallyPlayedItems, showFutureItems, notLoadedCount, oldestUpdate) ->
69
76
val streamItems = if (event is SuccessResultEvent || event is IdleEvent )
70
77
feedDatabaseManager
71
- .getStreams(groupId, showPlayedItems, showFutureItems)
78
+ .getStreams(groupId, showPlayedItems, showPartiallyPlayedItems, showFutureItems)
72
79
.blockingGet(arrayListOf ())
73
80
else
74
81
arrayListOf ()
@@ -100,8 +107,9 @@ class FeedViewModel(
100
107
val t1 : FeedEventManager .Event ,
101
108
val t2 : Boolean ,
102
109
val t3 : Boolean ,
103
- val t4 : Long ,
104
- val t5 : OffsetDateTime ?
110
+ val t4 : Boolean ,
111
+ val t5 : Long ,
112
+ val t6 : OffsetDateTime ?
105
113
)
106
114
107
115
private data class CombineResultDataHolder (
@@ -111,44 +119,57 @@ class FeedViewModel(
111
119
val t4 : OffsetDateTime ?
112
120
)
113
121
114
- fun togglePlayedItems (showPlayedItems : Boolean ) {
115
- toggleShowPlayedItems.onNext(showPlayedItems)
116
- }
117
-
118
- fun saveShowPlayedItemsToPreferences (showPlayedItems : Boolean ) =
122
+ fun setSaveShowPlayedItems (showPlayedItems : Boolean ) {
123
+ this .showPlayedItems.onNext(showPlayedItems)
119
124
PreferenceManager .getDefaultSharedPreferences(application).edit {
120
- this .putBoolean(application.getString(R .string.feed_show_played_items_key ), showPlayedItems)
125
+ this .putBoolean(application.getString(R .string.feed_show_watched_items_key ), showPlayedItems)
121
126
this .apply ()
122
127
}
128
+ }
123
129
124
130
fun getShowPlayedItemsFromPreferences () = getShowPlayedItemsFromPreferences(application)
125
131
126
- fun toggleFutureItems (showFutureItems : Boolean ) {
127
- toggleShowFutureItems.onNext(showFutureItems)
132
+ fun setSaveShowPartiallyPlayedItems (showPartiallyPlayedItems : Boolean ) {
133
+ this .showPartiallyPlayedItems.onNext(showPartiallyPlayedItems)
134
+ PreferenceManager .getDefaultSharedPreferences(application).edit {
135
+ this .putBoolean(application.getString(R .string.feed_show_partially_watched_items_key), showPartiallyPlayedItems)
136
+ this .apply ()
137
+ }
128
138
}
129
139
130
- fun saveShowFutureItemsToPreferences (showFutureItems : Boolean ) =
140
+ fun getShowPartiallyPlayedItemsFromPreferences () = getShowPartiallyPlayedItemsFromPreferences(application)
141
+
142
+ fun setSaveShowFutureItems (showFutureItems : Boolean ) {
143
+ this .showFutureItems.onNext(showFutureItems)
131
144
PreferenceManager .getDefaultSharedPreferences(application).edit {
132
145
this .putBoolean(application.getString(R .string.feed_show_future_items_key), showFutureItems)
133
146
this .apply ()
134
147
}
148
+ }
135
149
136
150
fun getShowFutureItemsFromPreferences () = getShowFutureItemsFromPreferences(application)
137
151
138
152
companion object {
139
153
private fun getShowPlayedItemsFromPreferences (context : Context ) =
140
154
PreferenceManager .getDefaultSharedPreferences(context)
141
- .getBoolean(context.getString(R .string.feed_show_played_items_key), true )
155
+ .getBoolean(context.getString(R .string.feed_show_watched_items_key), true )
156
+
157
+ private fun getShowPartiallyPlayedItemsFromPreferences (context : Context ) =
158
+ PreferenceManager .getDefaultSharedPreferences(context)
159
+ .getBoolean(context.getString(R .string.feed_show_partially_watched_items_key), true )
160
+
142
161
private fun getShowFutureItemsFromPreferences (context : Context ) =
143
162
PreferenceManager .getDefaultSharedPreferences(context)
144
163
.getBoolean(context.getString(R .string.feed_show_future_items_key), true )
164
+
145
165
fun getFactory (context : Context , groupId : Long ) = viewModelFactory {
146
166
initializer {
147
167
FeedViewModel (
148
168
App .getApp(),
149
169
groupId,
150
170
// Read initial value from preferences
151
171
getShowPlayedItemsFromPreferences(context.applicationContext),
172
+ getShowPartiallyPlayedItemsFromPreferences(context.applicationContext),
152
173
getShowFutureItemsFromPreferences(context.applicationContext)
153
174
)
154
175
}
0 commit comments