@@ -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- initialShowPlayedItems : Boolean = true ,
35- initialShowFutureItems : Boolean = true
34+ initialShowPlayedItems : Boolean ,
35+ initialShowPartiallyPlayedItems : Boolean ,
36+ initialShowFutureItems : Boolean
3637) : ViewModel() {
3738 private val feedDatabaseManager = FeedDatabaseManager (application)
3839
39- private val toggleShowPlayedItems = BehaviorProcessor .create<Boolean >()
40- private val toggleShowPlayedItemsFlowable = toggleShowPlayedItems
40+ private val showPlayedItems = BehaviorProcessor .create<Boolean >()
41+ private val showPlayedItemsFlowable = showPlayedItems
4142 .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,23 +58,24 @@ class FeedViewModel(
5258 private var combineDisposable = Flowable
5359 .combineLatest(
5460 FeedEventManager .events(),
55- toggleShowPlayedItemsFlowable,
56- toggleShowFutureItemsFlowable,
61+ showPlayedItemsFlowable,
62+ showPartiallyPlayedItemsFlowable,
63+ showFutureItemsFlowable,
5764 feedDatabaseManager.notLoadedCount(groupId),
5865 feedDatabaseManager.oldestSubscriptionUpdate(groupId),
5966
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())
6370 }
6471 )
6572 .throttleLatest(DEFAULT_THROTTLE_TIMEOUT , TimeUnit .MILLISECONDS )
6673 .subscribeOn(Schedulers .io())
6774 .observeOn(Schedulers .io())
68- .map { (event, showPlayedItems, showFutureItems, notLoadedCount, oldestUpdate) ->
75+ .map { (event, showPlayedItems, showPartiallyPlayedItems, showFutureItems, notLoadedCount, oldestUpdate) ->
6976 val streamItems = if (event is SuccessResultEvent || event is IdleEvent )
7077 feedDatabaseManager
71- .getStreams(groupId, showPlayedItems, showFutureItems)
78+ .getStreams(groupId, showPlayedItems, showPartiallyPlayedItems, showFutureItems)
7279 .blockingGet(arrayListOf ())
7380 else
7481 arrayListOf ()
@@ -100,8 +107,9 @@ class FeedViewModel(
100107 val t1 : FeedEventManager .Event ,
101108 val t2 : Boolean ,
102109 val t3 : Boolean ,
103- val t4 : Long ,
104- val t5 : OffsetDateTime ?
110+ val t4 : Boolean ,
111+ val t5 : Long ,
112+ val t6 : OffsetDateTime ?
105113 )
106114
107115 private data class CombineResultDataHolder (
@@ -111,44 +119,57 @@ class FeedViewModel(
111119 val t4 : OffsetDateTime ?
112120 )
113121
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)
119124 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)
121126 this .apply ()
122127 }
128+ }
123129
124130 fun getShowPlayedItemsFromPreferences () = getShowPlayedItemsFromPreferences(application)
125131
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+ }
128138 }
129139
130- fun saveShowFutureItemsToPreferences (showFutureItems : Boolean ) =
140+ fun getShowPartiallyPlayedItemsFromPreferences () = getShowPartiallyPlayedItemsFromPreferences(application)
141+
142+ fun setSaveShowFutureItems (showFutureItems : Boolean ) {
143+ this .showFutureItems.onNext(showFutureItems)
131144 PreferenceManager .getDefaultSharedPreferences(application).edit {
132145 this .putBoolean(application.getString(R .string.feed_show_future_items_key), showFutureItems)
133146 this .apply ()
134147 }
148+ }
135149
136150 fun getShowFutureItemsFromPreferences () = getShowFutureItemsFromPreferences(application)
137151
138152 companion object {
139153 private fun getShowPlayedItemsFromPreferences (context : Context ) =
140154 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+
142161 private fun getShowFutureItemsFromPreferences (context : Context ) =
143162 PreferenceManager .getDefaultSharedPreferences(context)
144163 .getBoolean(context.getString(R .string.feed_show_future_items_key), true )
164+
145165 fun getFactory (context : Context , groupId : Long ) = viewModelFactory {
146166 initializer {
147167 FeedViewModel (
148168 App .getApp(),
149169 groupId,
150170 // Read initial value from preferences
151171 getShowPlayedItemsFromPreferences(context.applicationContext),
172+ getShowPartiallyPlayedItemsFromPreferences(context.applicationContext),
152173 getShowFutureItemsFromPreferences(context.applicationContext)
153174 )
154175 }
0 commit comments