@@ -28,15 +28,18 @@ import org.schabi.newpipe.util.DEFAULT_THROTTLE_TIMEOUT
2828import java.time.OffsetDateTime
2929import java.util.concurrent.TimeUnit
3030
31+ enum class ShowItems {
32+ WATCHED , PARTIALLY_WATCHED , DEFAULT
33+ }
3134class FeedViewModel (
3235 private val application : Application ,
3336 groupId : Long = FeedGroupEntity .GROUP_ALL_ID ,
34- initialShowPlayedItems : Boolean = true ,
37+ initialShowPlayedItems : ShowItems = ShowItems . DEFAULT ,
3538 initialShowFutureItems : Boolean = true
3639) : ViewModel() {
3740 private val feedDatabaseManager = FeedDatabaseManager (application)
3841
39- private val toggleShowPlayedItems = BehaviorProcessor .create<Boolean >()
42+ private val toggleShowPlayedItems = BehaviorProcessor .create<ShowItems >()
4043 private val toggleShowPlayedItemsFlowable = toggleShowPlayedItems
4144 .startWithItem(initialShowPlayedItems)
4245 .distinctUntilChanged()
@@ -57,7 +60,7 @@ class FeedViewModel(
5760 feedDatabaseManager.notLoadedCount(groupId),
5861 feedDatabaseManager.oldestSubscriptionUpdate(groupId),
5962
60- Function5 { t1: FeedEventManager .Event , t2: Boolean , t3: Boolean ,
63+ Function5 { t1: FeedEventManager .Event , t2: ShowItems , t3: Boolean ,
6164 t4: Long , t5: List <OffsetDateTime > ->
6265 return @Function5 CombineResultEventHolder (t1, t2, t3, t4, t5.firstOrNull())
6366 }
@@ -66,12 +69,21 @@ class FeedViewModel(
6669 .subscribeOn(Schedulers .io())
6770 .observeOn(Schedulers .io())
6871 .map { (event, showPlayedItems, showFutureItems, notLoadedCount, oldestUpdate) ->
69- val streamItems = if (event is SuccessResultEvent || event is IdleEvent )
72+ val streamItems = if (event is SuccessResultEvent || event is IdleEvent ) {
7073 feedDatabaseManager
71- .getStreams(groupId, showPlayedItems, showFutureItems)
74+ .getStreams(
75+ groupId,
76+ ! (
77+ showPlayedItems == ShowItems .WATCHED ||
78+ showPlayedItems == ShowItems .PARTIALLY_WATCHED
79+ ),
80+ showPlayedItems != ShowItems .PARTIALLY_WATCHED ,
81+ showFutureItems
82+ )
7283 .blockingGet(arrayListOf ())
73- else
84+ } else {
7485 arrayListOf ()
86+ }
7587
7688 CombineResultDataHolder (event, streamItems, notLoadedCount, oldestUpdate)
7789 }
@@ -98,7 +110,7 @@ class FeedViewModel(
98110
99111 private data class CombineResultEventHolder (
100112 val t1 : FeedEventManager .Event ,
101- val t2 : Boolean ,
113+ val t2 : ShowItems ,
102114 val t3 : Boolean ,
103115 val t4 : Long ,
104116 val t5 : OffsetDateTime ?
@@ -111,17 +123,20 @@ class FeedViewModel(
111123 val t4 : OffsetDateTime ?
112124 )
113125
114- fun togglePlayedItems (showPlayedItems : Boolean ) {
115- toggleShowPlayedItems.onNext(showPlayedItems )
126+ fun togglePlayedItems (showItems : ShowItems ) {
127+ toggleShowPlayedItems.onNext(showItems )
116128 }
117129
118- fun saveShowPlayedItemsToPreferences (showPlayedItems : Boolean ) =
130+ fun saveShowPlayedItemsToPreferences (showItems : ShowItems ) =
119131 PreferenceManager .getDefaultSharedPreferences(application).edit {
120- this .putBoolean(application.getString(R .string.feed_show_played_items_key), showPlayedItems)
132+ this .putString(
133+ application.getString(R .string.feed_show_played_items_key),
134+ showItems.toString()
135+ )
121136 this .apply ()
122137 }
123138
124- fun getShowPlayedItemsFromPreferences () = getShowPlayedItemsFromPreferences (application)
139+ fun getItemsVisibilityFromPreferences () = getItemsVisibilityFromPreferences (application)
125140
126141 fun toggleFutureItems (showFutureItems : Boolean ) {
127142 toggleShowFutureItems.onNext(showFutureItems)
@@ -136,9 +151,16 @@ class FeedViewModel(
136151 fun getShowFutureItemsFromPreferences () = getShowFutureItemsFromPreferences(application)
137152
138153 companion object {
139- private fun getShowPlayedItemsFromPreferences (context : Context ) =
140- PreferenceManager .getDefaultSharedPreferences(context)
141- .getBoolean(context.getString(R .string.feed_show_played_items_key), true )
154+
155+ private fun getItemsVisibilityFromPreferences (context : Context ): ShowItems {
156+ val s = PreferenceManager .getDefaultSharedPreferences(context)
157+ .getString(
158+ context.getString(R .string.feed_show_played_items_key),
159+ ShowItems .DEFAULT .toString()
160+ ) ? : ShowItems .DEFAULT .toString()
161+ return ShowItems .valueOf(s)
162+ }
163+
142164 private fun getShowFutureItemsFromPreferences (context : Context ) =
143165 PreferenceManager .getDefaultSharedPreferences(context)
144166 .getBoolean(context.getString(R .string.feed_show_future_items_key), true )
@@ -148,7 +170,7 @@ class FeedViewModel(
148170 App .getApp(),
149171 groupId,
150172 // Read initial value from preferences
151- getShowPlayedItemsFromPreferences (context.applicationContext),
173+ getItemsVisibilityFromPreferences (context.applicationContext),
152174 getShowFutureItemsFromPreferences(context.applicationContext)
153175 )
154176 }
0 commit comments