Skip to content

Commit 9c82441

Browse files
committed
Implemented the feature and fixed some small issues
1 parent 3d36eb5 commit 9c82441

8 files changed

Lines changed: 82 additions & 50 deletions

File tree

app/src/main/java/org/schabi/newpipe/database/feed/dao/FeedDAO.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ abstract class FeedDAO {
7171
:includePartiallyPlayed
7272
OR sh.stream_id IS NULL
7373
OR sst.stream_id IS NULL
74-
OR (sst.progress_time < ${StreamStateEntity.PLAYBACK_SAVE_THRESHOLD_START_MILLISECONDS}
75-
AND sst.progress_time < s.duration * 1000 / 4)
74+
OR (sst.progress_time <= ${StreamStateEntity.PLAYBACK_SAVE_THRESHOLD_START_MILLISECONDS}
75+
AND sst.progress_time <= s.duration * 1000 / 4)
7676
)
7777
AND (
7878
:uploadDateBefore IS NULL

app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,13 @@ class FeedFragment : BaseStateFragment<FeedState>() {
100100
private var oldestSubscriptionUpdate: OffsetDateTime? = null
101101

102102
private lateinit var groupAdapter: GroupieAdapter
103-
@State @JvmField var showPlayedItems: ShowItems = ShowItems.DEFAULT
103+
@State @JvmField var feedVisibilityStatus: StreamVisibilityStatus = StreamVisibilityStatus.DEFAULT
104104
@State @JvmField var showFutureItems: Boolean = true
105105

106+
private lateinit var showAllMenuItem: MenuItem
107+
private lateinit var hideWatchedMenuItem: MenuItem
108+
private lateinit var hidePartiallyWatchedMenuItem: MenuItem
109+
106110
private var onSettingsChangeListener: SharedPreferences.OnSharedPreferenceChangeListener? = null
107111
private var updateListViewModeOnResume = false
108112
private var isRefreshing = false
@@ -140,7 +144,7 @@ class FeedFragment : BaseStateFragment<FeedState>() {
140144

141145
val factory = FeedViewModel.getFactory(requireContext(), groupId)
142146
viewModel = ViewModelProvider(this, factory)[FeedViewModel::class.java]
143-
showPlayedItems = viewModel.getItemsVisibilityFromPreferences()
147+
feedVisibilityStatus = viewModel.getItemsVisibilityFromPreferences()
144148
showFutureItems = viewModel.getShowFutureItemsFromPreferences()
145149
viewModel.stateLiveData.observe(viewLifecycleOwner) { it?.let(::handleResult) }
146150

@@ -216,7 +220,15 @@ class FeedFragment : BaseStateFragment<FeedState>() {
216220
activity.supportActionBar?.subtitle = groupName
217221

218222
inflater.inflate(R.menu.menu_feed_fragment, menu)
219-
updateTogglePlayedItemsButton(menu.findItem(R.id.menu_item_feed_toggle_played_items))
223+
224+
val itemVisibilityMenu = menu.findItem(R.id.menu_item_feed_toggle_played_items).subMenu
225+
if (itemVisibilityMenu != null) {
226+
showAllMenuItem = itemVisibilityMenu.findItem(R.id.menu_item_feed_toggle_show_all_items)
227+
hideWatchedMenuItem = itemVisibilityMenu.findItem(R.id.menu_item_feed_toggle_show_played_items)
228+
hidePartiallyWatchedMenuItem = itemVisibilityMenu.findItem(R.id.menu_item_feed_toggle_partially_played_items)
229+
}
230+
231+
updateItemVisibilityMenu(menu.findItem(R.id.menu_item_feed_toggle_played_items))
220232
updateToggleFutureItemsButton(menu.findItem(R.id.menu_item_feed_toggle_future_items))
221233
}
222234

@@ -243,11 +255,11 @@ class FeedFragment : BaseStateFragment<FeedState>() {
243255
.show()
244256
return true
245257
} else if (item.itemId == R.id.menu_item_feed_toggle_show_all_items) {
246-
setShowPlayedItemsMethod(item, ShowItems.DEFAULT)
258+
changeItemsVisibilityStatus(item, StreamVisibilityStatus.DEFAULT)
247259
} else if (item.itemId == R.id.menu_item_feed_toggle_show_played_items) {
248-
setShowPlayedItemsMethod(item, ShowItems.WATCHED)
260+
changeItemsVisibilityStatus(item, StreamVisibilityStatus.HIDE_WATCHED)
249261
} else if (item.itemId == R.id.menu_item_feed_toggle_partially_played_items) {
250-
setShowPlayedItemsMethod(item, ShowItems.PARTIALLY_WATCHED)
262+
changeItemsVisibilityStatus(item, StreamVisibilityStatus.HIDE_PARTIALLY_WATCHED)
251263
} else if (item.itemId == R.id.menu_item_feed_toggle_future_items) {
252264
showFutureItems = !item.isChecked
253265
updateToggleFutureItemsButton(item)
@@ -258,11 +270,11 @@ class FeedFragment : BaseStateFragment<FeedState>() {
258270
return super.onOptionsItemSelected(item)
259271
}
260272

261-
private fun setShowPlayedItemsMethod(item: MenuItem, showItems: ShowItems) {
262-
showPlayedItems = showItems
263-
viewModel.togglePlayedItems(showPlayedItems)
264-
updateTogglePlayedItemsButton(item)
265-
viewModel.saveShowPlayedItemsToPreferences(showPlayedItems)
273+
private fun changeItemsVisibilityStatus(item: MenuItem, streamVisibilityStatus: StreamVisibilityStatus) {
274+
feedVisibilityStatus = streamVisibilityStatus
275+
viewModel.changeVisibilityState(feedVisibilityStatus)
276+
updateItemVisibilityMenu(item)
277+
viewModel.saveStreamVisibilityStateToPreferences(feedVisibilityStatus)
266278
}
267279

268280
override fun onDestroyOptionsMenu() {
@@ -291,10 +303,28 @@ class FeedFragment : BaseStateFragment<FeedState>() {
291303
super.onDestroyView()
292304
}
293305

294-
private fun updateTogglePlayedItemsButton(menuItem: MenuItem) {
306+
private fun updateItemVisibilityMenu(menuItem: MenuItem) {
307+
when (feedVisibilityStatus) {
308+
StreamVisibilityStatus.DEFAULT -> {
309+
showAllMenuItem.isVisible = false
310+
hideWatchedMenuItem.isVisible = true
311+
hidePartiallyWatchedMenuItem.isVisible = true
312+
}
313+
StreamVisibilityStatus.HIDE_WATCHED -> {
314+
showAllMenuItem.isVisible = true
315+
hideWatchedMenuItem.isVisible = false
316+
hidePartiallyWatchedMenuItem.isVisible = true
317+
}
318+
else -> {
319+
showAllMenuItem.isVisible = true
320+
hideWatchedMenuItem.isVisible = true
321+
hidePartiallyWatchedMenuItem.isVisible = false
322+
}
323+
}
324+
295325
MenuItemCompat.setTooltipText(
296326
menuItem,
297-
getString(R.string.feed_toggle_show_hide_played_items)
327+
getString(R.string.feed_change_stream_visibility_state)
298328
)
299329
}
300330

app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,17 @@ import org.schabi.newpipe.util.DEFAULT_THROTTLE_TIMEOUT
2828
import java.time.OffsetDateTime
2929
import java.util.concurrent.TimeUnit
3030

31-
enum class ShowItems {
32-
WATCHED, PARTIALLY_WATCHED, DEFAULT
33-
}
3431
class FeedViewModel(
3532
private val application: Application,
3633
groupId: Long = FeedGroupEntity.GROUP_ALL_ID,
37-
initialShowPlayedItems: ShowItems = ShowItems.DEFAULT,
34+
initialStreamVisibility: StreamVisibilityStatus = StreamVisibilityStatus.DEFAULT,
3835
initialShowFutureItems: Boolean = true
3936
) : ViewModel() {
4037
private val feedDatabaseManager = FeedDatabaseManager(application)
4138

42-
private val toggleShowPlayedItems = BehaviorProcessor.create<ShowItems>()
43-
private val toggleShowPlayedItemsFlowable = toggleShowPlayedItems
44-
.startWithItem(initialShowPlayedItems)
39+
private val streamVisibilityState = BehaviorProcessor.create<StreamVisibilityStatus>()
40+
private val streamVisibilityStateFlowable = streamVisibilityState
41+
.startWithItem(initialStreamVisibility)
4542
.distinctUntilChanged()
4643

4744
private val toggleShowFutureItems = BehaviorProcessor.create<Boolean>()
@@ -55,12 +52,12 @@ class FeedViewModel(
5552
private var combineDisposable = Flowable
5653
.combineLatest(
5754
FeedEventManager.events(),
58-
toggleShowPlayedItemsFlowable,
55+
streamVisibilityStateFlowable,
5956
toggleShowFutureItemsFlowable,
6057
feedDatabaseManager.notLoadedCount(groupId),
6158
feedDatabaseManager.oldestSubscriptionUpdate(groupId),
6259

63-
Function5 { t1: FeedEventManager.Event, t2: ShowItems, t3: Boolean,
60+
Function5 { t1: FeedEventManager.Event, t2: StreamVisibilityStatus, t3: Boolean,
6461
t4: Long, t5: List<OffsetDateTime> ->
6562
return@Function5 CombineResultEventHolder(t1, t2, t3, t4, t5.firstOrNull())
6663
}
@@ -74,10 +71,10 @@ class FeedViewModel(
7471
.getStreams(
7572
groupId,
7673
!(
77-
showPlayedItems == ShowItems.WATCHED ||
78-
showPlayedItems == ShowItems.PARTIALLY_WATCHED
74+
showPlayedItems == StreamVisibilityStatus.HIDE_WATCHED ||
75+
showPlayedItems == StreamVisibilityStatus.HIDE_PARTIALLY_WATCHED
7976
),
80-
showPlayedItems != ShowItems.PARTIALLY_WATCHED,
77+
showPlayedItems != StreamVisibilityStatus.HIDE_PARTIALLY_WATCHED,
8178
showFutureItems
8279
)
8380
.blockingGet(arrayListOf())
@@ -110,7 +107,7 @@ class FeedViewModel(
110107

111108
private data class CombineResultEventHolder(
112109
val t1: FeedEventManager.Event,
113-
val t2: ShowItems,
110+
val t2: StreamVisibilityStatus,
114111
val t3: Boolean,
115112
val t4: Long,
116113
val t5: OffsetDateTime?
@@ -123,20 +120,20 @@ class FeedViewModel(
123120
val t4: OffsetDateTime?
124121
)
125122

126-
fun togglePlayedItems(showItems: ShowItems) {
127-
toggleShowPlayedItems.onNext(showItems)
123+
fun changeVisibilityState(streamVisibilityStatus: StreamVisibilityStatus) {
124+
streamVisibilityState.onNext(streamVisibilityStatus)
128125
}
129126

130-
fun saveShowPlayedItemsToPreferences(showItems: ShowItems) =
127+
fun saveStreamVisibilityStateToPreferences(streamVisibilityStatus: StreamVisibilityStatus) =
131128
PreferenceManager.getDefaultSharedPreferences(application).edit {
132129
this.putString(
133-
application.getString(R.string.feed_show_played_items_key),
134-
showItems.toString()
130+
application.getString(R.string.feed_stream_visibility_state_key),
131+
streamVisibilityStatus.toString()
135132
)
136133
this.apply()
137134
}
138135

139-
fun getItemsVisibilityFromPreferences() = getItemsVisibilityFromPreferences(application)
136+
fun getItemsVisibilityFromPreferences() = getStreamVisibilityStateFromPreferences(application)
140137

141138
fun toggleFutureItems(showFutureItems: Boolean) {
142139
toggleShowFutureItems.onNext(showFutureItems)
@@ -152,13 +149,13 @@ class FeedViewModel(
152149

153150
companion object {
154151

155-
private fun getItemsVisibilityFromPreferences(context: Context): ShowItems {
152+
private fun getStreamVisibilityStateFromPreferences(context: Context): StreamVisibilityStatus {
156153
val s = PreferenceManager.getDefaultSharedPreferences(context)
157154
.getString(
158-
context.getString(R.string.feed_show_played_items_key),
159-
ShowItems.DEFAULT.toString()
160-
) ?: ShowItems.DEFAULT.toString()
161-
return ShowItems.valueOf(s)
155+
context.getString(R.string.feed_stream_visibility_state_key),
156+
StreamVisibilityStatus.DEFAULT.toString()
157+
) ?: StreamVisibilityStatus.DEFAULT.toString()
158+
return StreamVisibilityStatus.valueOf(s)
162159
}
163160

164161
private fun getShowFutureItemsFromPreferences(context: Context) =
@@ -170,7 +167,7 @@ class FeedViewModel(
170167
App.getApp(),
171168
groupId,
172169
// Read initial value from preferences
173-
getItemsVisibilityFromPreferences(context.applicationContext),
170+
getStreamVisibilityStateFromPreferences(context.applicationContext),
174171
getShowFutureItemsFromPreferences(context.applicationContext)
175172
)
176173
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.schabi.newpipe.local.feed
2+
3+
enum class StreamVisibilityStatus {
4+
DEFAULT, HIDE_WATCHED, HIDE_PARTIALLY_WATCHED
5+
}

app/src/main/java/org/schabi/newpipe/local/history/HistoryRecordManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public HistoryRecordManager(final Context context) {
8787
* Marks a stream item as watched such that it is hidden from the feed if watched videos are
8888
* hidden. Adds a history entry and updates the stream progress to 100%.
8989
*
90-
* @see FeedViewModel#togglePlayedItems
90+
* @see FeedViewModel#changeVisibilityState
9191
* @param info the item to mark as watched
9292
* @return a Maybe containing the ID of the item if successful
9393
*/

app/src/main/res/menu/menu_feed_fragment.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77
android:checkable="false"
88
android:checked="false"
99
android:icon="@drawable/ic_visibility_on"
10-
android:title="@string/feed_toggle_show_hide_played_items"
10+
android:title="@string/feed_change_stream_visibility_state"
1111
app:showAsAction="ifRoom">
1212
<menu>
1313
<item
1414
android:id="@+id/menu_item_feed_toggle_show_all_items"
15-
android:title="@string/feed_toggle_show_items"/>
15+
android:title="@string/feed_stream_visibility_show_all"/>
1616
<item
1717
android:id="@+id/menu_item_feed_toggle_show_played_items"
18-
android:title="@string/feed_toggle_show_watched_items"/>
18+
android:title="@string/feed_stream_visibility_hide_watched"/>
1919
<item
2020
android:id="@+id/menu_item_feed_toggle_partially_played_items"
21-
android:title="@string/feed_toggle_show_partially_watched_items"/>
21+
android:title="@string/feed_stream_visibility_hide_partially_watched"/>
2222
</menu>
2323
</item>
2424

app/src/main/res/values/settings_keys.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@
283283

284284
<string name="feed_update_threshold_key">feed_update_threshold_key</string>
285285
<string name="feed_update_threshold_default_value">300</string>
286-
<string name="feed_show_played_items_key">feed_show_items</string>
286+
<string name="feed_stream_visibility_state_key">feed_stream_visibility_state</string>
287287
<string name="feed_show_future_items_key">feed_show_future_items</string>
288288

289289
<string name="show_thumbnail_key">show_thumbnail_key</string>

app/src/main/res/values/strings.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@
691691
\nYouTube is an example of a service that offers this fast method with its RSS feed.
692692
\n
693693
\nSo the choice boils down to what you prefer: speed or precise information.</string>
694-
<string name="feed_toggle_show_hide_played_items">Show/hide watched items</string>
694+
<string name="feed_change_stream_visibility_state">Show/hide watched streams</string>
695695
<string name="content_not_supported">This content is not yet supported by NewPipe.\n\nIt will hopefully be supported in a future version.</string>
696696
<string name="detail_sub_channel_thumbnail_view_description">Channel\'s avatar thumbnail</string>
697697
<string name="channel_created_by">Created by %s</string>
@@ -759,8 +759,8 @@
759759
<string name="unknown_quality">Unknown quality</string>
760760
<string name="feed_toggle_show_future_items">Show future items</string>
761761
<string name="feed_toggle_hide_future_items">Hide future items</string>
762-
<string name="feed_toggle_show_partially_watched_items">Hide Watched and Partially Watched </string>
763-
<string name="feed_toggle_show_watched_items">Hide Watched</string>
764-
<string name="feed_toggle_show_items">Show All</string>
762+
<string name="feed_stream_visibility_hide_partially_watched">Hide Watched </string>
763+
<string name="feed_stream_visibility_hide_watched">Hide Fully Watched</string>
764+
<string name="feed_stream_visibility_show_all">Show All</string>
765765
<string name="sort">Sort</string>
766766
</resources>

0 commit comments

Comments
 (0)