@@ -11,7 +11,7 @@ import androidx.lifecycle.viewmodel.viewModelFactory
1111import androidx.preference.PreferenceManager
1212import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
1313import io.reactivex.rxjava3.core.Flowable
14- import io.reactivex.rxjava3.functions.Function5
14+ import io.reactivex.rxjava3.functions.Function6
1515import io.reactivex.rxjava3.processors.BehaviorProcessor
1616import io.reactivex.rxjava3.schedulers.Schedulers
1717import org.schabi.newpipe.App
@@ -31,18 +31,24 @@ import java.util.concurrent.TimeUnit
3131class FeedViewModel (
3232 private val application : Application ,
3333 groupId : Long = FeedGroupEntity .GROUP_ALL_ID ,
34- initialStreamVisibility : StreamVisibilityStatus = StreamVisibilityStatus .DEFAULT ,
34+ initialShowPlayedItems : Boolean = true ,
35+ initialShowPartiallyPlayedItems : Boolean = true ,
3536 initialShowFutureItems : Boolean = true
3637) : ViewModel() {
3738 private val feedDatabaseManager = FeedDatabaseManager (application)
3839
39- private val streamVisibilityState = BehaviorProcessor .create<StreamVisibilityStatus >()
40- private val streamVisibilityStateFlowable = streamVisibilityState
41- .startWithItem(initialStreamVisibility )
40+ private val showPlayedItems = BehaviorProcessor .create<Boolean >()
41+ private val showPlayedItemsFlowable = showPlayedItems
42+ .startWithItem(initialShowPlayedItems )
4243 .distinctUntilChanged()
4344
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
4652 .startWithItem(initialShowFutureItems)
4753 .distinctUntilChanged()
4854
@@ -52,35 +58,27 @@ class FeedViewModel(
5258 private var combineDisposable = Flowable
5359 .combineLatest(
5460 FeedEventManager .events(),
55- streamVisibilityStateFlowable,
56- toggleShowFutureItemsFlowable,
61+ showPlayedItemsFlowable,
62+ showPartiallyPlayedItemsFlowable,
63+ showFutureItemsFlowable,
5764 feedDatabaseManager.notLoadedCount(groupId),
5865 feedDatabaseManager.oldestSubscriptionUpdate(groupId),
5966
60- Function5 { t1: FeedEventManager .Event , t2: StreamVisibilityStatus , 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())
6370 }
6471 )
6572 .throttleLatest(DEFAULT_THROTTLE_TIMEOUT , TimeUnit .MILLISECONDS )
6673 .subscribeOn(Schedulers .io())
6774 .observeOn(Schedulers .io())
68- .map { (event, showPlayedItems, showFutureItems, notLoadedCount, oldestUpdate) ->
69- val streamItems = if (event is SuccessResultEvent || event is IdleEvent ) {
75+ .map { (event, showPlayedItems, showPartiallyPlayedItems, showFutureItems, notLoadedCount, oldestUpdate) ->
76+ val streamItems = if (event is SuccessResultEvent || event is IdleEvent )
7077 feedDatabaseManager
71- .getStreams(
72- groupId,
73- ! (
74- showPlayedItems == StreamVisibilityStatus .HIDE_WATCHED ||
75- showPlayedItems == StreamVisibilityStatus .HIDE_PARTIALLY_WATCHED
76- ),
77- showPlayedItems != StreamVisibilityStatus .HIDE_PARTIALLY_WATCHED ,
78- showFutureItems
79- )
78+ .getStreams(groupId, showPlayedItems, showPartiallyPlayedItems, showFutureItems)
8079 .blockingGet(arrayListOf ())
81- } else {
80+ else
8281 arrayListOf ()
83- }
8482
8583 CombineResultDataHolder (event, streamItems, notLoadedCount, oldestUpdate)
8684 }
@@ -107,10 +105,11 @@ class FeedViewModel(
107105
108106 private data class CombineResultEventHolder (
109107 val t1 : FeedEventManager .Event ,
110- val t2 : StreamVisibilityStatus ,
108+ val t2 : Boolean ,
111109 val t3 : Boolean ,
112- val t4 : Long ,
113- val t5 : OffsetDateTime ?
110+ val t4 : Boolean ,
111+ val t5 : Long ,
112+ val t6 : OffsetDateTime ?
114113 )
115114
116115 private data class CombineResultDataHolder (
@@ -120,23 +119,32 @@ class FeedViewModel(
120119 val t4 : OffsetDateTime ?
121120 )
122121
123- fun changeVisibilityState ( streamVisibilityStatus : StreamVisibilityStatus ) {
124- streamVisibilityState. onNext(streamVisibilityStatus )
122+ fun setShowPlayedItems ( showPlayedItems : Boolean ) {
123+ this .showPlayedItems. onNext(showPlayedItems )
125124 }
126125
127- fun saveStreamVisibilityStateToPreferences ( streamVisibilityStatus : StreamVisibilityStatus ) =
126+ fun saveShowPlayedItemsToPreferences ( showPlayedItems : Boolean ) =
128127 PreferenceManager .getDefaultSharedPreferences(application).edit {
129- this .putString(
130- application.getString(R .string.feed_stream_visibility_state_key),
131- streamVisibilityStatus.toString()
132- )
128+ this .putBoolean(application.getString(R .string.feed_show_watched_items_key), showPlayedItems)
133129 this .apply ()
134130 }
135131
136- fun getItemsVisibilityFromPreferences () = getStreamVisibilityStateFromPreferences (application)
132+ fun getShowPlayedItemsFromPreferences () = getShowPlayedItemsFromPreferences (application)
137133
138- fun toggleFutureItems (showFutureItems : Boolean ) {
139- toggleShowFutureItems.onNext(showFutureItems)
134+ fun setShowPartiallyPlayedItems (showPartiallyPlayedItems : Boolean ) {
135+ this .showPartiallyPlayedItems.onNext(showPartiallyPlayedItems)
136+ }
137+
138+ fun saveShowPartiallyPlayedItemsToPreferences (showPartiallyPlayedItems : Boolean ) =
139+ PreferenceManager .getDefaultSharedPreferences(application).edit {
140+ this .putBoolean(application.getString(R .string.feed_show_partially_watched_items_key), showPartiallyPlayedItems)
141+ this .apply ()
142+ }
143+
144+ fun getShowPartiallyPlayedItemsFromPreferences () = getShowPartiallyPlayedItemsFromPreferences(application)
145+
146+ fun setShowFutureItems (showFutureItems : Boolean ) {
147+ this .showFutureItems.onNext(showFutureItems)
140148 }
141149
142150 fun saveShowFutureItemsToPreferences (showFutureItems : Boolean ) =
@@ -148,16 +156,13 @@ class FeedViewModel(
148156 fun getShowFutureItemsFromPreferences () = getShowFutureItemsFromPreferences(application)
149157
150158 companion object {
159+ private fun getShowPlayedItemsFromPreferences (context : Context ) =
160+ PreferenceManager .getDefaultSharedPreferences(context)
161+ .getBoolean(context.getString(R .string.feed_show_watched_items_key), true )
151162
152- private fun getStreamVisibilityStateFromPreferences (context : Context ): StreamVisibilityStatus {
153- val s = PreferenceManager .getDefaultSharedPreferences(context)
154- .getString(
155- context.getString(R .string.feed_stream_visibility_state_key),
156- StreamVisibilityStatus .DEFAULT .toString()
157- ) ? : StreamVisibilityStatus .DEFAULT .toString()
158- return StreamVisibilityStatus .valueOf(s)
159- }
160-
163+ private fun getShowPartiallyPlayedItemsFromPreferences (context : Context ) =
164+ PreferenceManager .getDefaultSharedPreferences(context)
165+ .getBoolean(context.getString(R .string.feed_show_partially_watched_items_key), true )
161166 private fun getShowFutureItemsFromPreferences (context : Context ) =
162167 PreferenceManager .getDefaultSharedPreferences(context)
163168 .getBoolean(context.getString(R .string.feed_show_future_items_key), true )
@@ -167,7 +172,8 @@ class FeedViewModel(
167172 App .getApp(),
168173 groupId,
169174 // Read initial value from preferences
170- getStreamVisibilityStateFromPreferences(context.applicationContext),
175+ getShowPlayedItemsFromPreferences(context.applicationContext),
176+ getShowPartiallyPlayedItemsFromPreferences(context.applicationContext),
171177 getShowFutureItemsFromPreferences(context.applicationContext)
172178 )
173179 }
0 commit comments