Implemented the feature and fixed some small issues

This commit is contained in:
Jared Fantaye 2023-02-01 23:10:31 +01:00
parent 3d36eb5baf
commit 9c82441c19
8 changed files with 82 additions and 50 deletions

View File

@ -71,8 +71,8 @@ abstract class FeedDAO {
:includePartiallyPlayed
OR sh.stream_id IS NULL
OR sst.stream_id IS NULL
OR (sst.progress_time < ${StreamStateEntity.PLAYBACK_SAVE_THRESHOLD_START_MILLISECONDS}
AND sst.progress_time < s.duration * 1000 / 4)
OR (sst.progress_time <= ${StreamStateEntity.PLAYBACK_SAVE_THRESHOLD_START_MILLISECONDS}
AND sst.progress_time <= s.duration * 1000 / 4)
)
AND (
:uploadDateBefore IS NULL

View File

@ -100,9 +100,13 @@ class FeedFragment : BaseStateFragment<FeedState>() {
private var oldestSubscriptionUpdate: OffsetDateTime? = null
private lateinit var groupAdapter: GroupieAdapter
@State @JvmField var showPlayedItems: ShowItems = ShowItems.DEFAULT
@State @JvmField var feedVisibilityStatus: StreamVisibilityStatus = StreamVisibilityStatus.DEFAULT
@State @JvmField var showFutureItems: Boolean = true
private lateinit var showAllMenuItem: MenuItem
private lateinit var hideWatchedMenuItem: MenuItem
private lateinit var hidePartiallyWatchedMenuItem: MenuItem
private var onSettingsChangeListener: SharedPreferences.OnSharedPreferenceChangeListener? = null
private var updateListViewModeOnResume = false
private var isRefreshing = false
@ -140,7 +144,7 @@ class FeedFragment : BaseStateFragment<FeedState>() {
val factory = FeedViewModel.getFactory(requireContext(), groupId)
viewModel = ViewModelProvider(this, factory)[FeedViewModel::class.java]
showPlayedItems = viewModel.getItemsVisibilityFromPreferences()
feedVisibilityStatus = viewModel.getItemsVisibilityFromPreferences()
showFutureItems = viewModel.getShowFutureItemsFromPreferences()
viewModel.stateLiveData.observe(viewLifecycleOwner) { it?.let(::handleResult) }
@ -216,7 +220,15 @@ class FeedFragment : BaseStateFragment<FeedState>() {
activity.supportActionBar?.subtitle = groupName
inflater.inflate(R.menu.menu_feed_fragment, menu)
updateTogglePlayedItemsButton(menu.findItem(R.id.menu_item_feed_toggle_played_items))
val itemVisibilityMenu = menu.findItem(R.id.menu_item_feed_toggle_played_items).subMenu
if (itemVisibilityMenu != null) {
showAllMenuItem = itemVisibilityMenu.findItem(R.id.menu_item_feed_toggle_show_all_items)
hideWatchedMenuItem = itemVisibilityMenu.findItem(R.id.menu_item_feed_toggle_show_played_items)
hidePartiallyWatchedMenuItem = itemVisibilityMenu.findItem(R.id.menu_item_feed_toggle_partially_played_items)
}
updateItemVisibilityMenu(menu.findItem(R.id.menu_item_feed_toggle_played_items))
updateToggleFutureItemsButton(menu.findItem(R.id.menu_item_feed_toggle_future_items))
}
@ -243,11 +255,11 @@ class FeedFragment : BaseStateFragment<FeedState>() {
.show()
return true
} else if (item.itemId == R.id.menu_item_feed_toggle_show_all_items) {
setShowPlayedItemsMethod(item, ShowItems.DEFAULT)
changeItemsVisibilityStatus(item, StreamVisibilityStatus.DEFAULT)
} else if (item.itemId == R.id.menu_item_feed_toggle_show_played_items) {
setShowPlayedItemsMethod(item, ShowItems.WATCHED)
changeItemsVisibilityStatus(item, StreamVisibilityStatus.HIDE_WATCHED)
} else if (item.itemId == R.id.menu_item_feed_toggle_partially_played_items) {
setShowPlayedItemsMethod(item, ShowItems.PARTIALLY_WATCHED)
changeItemsVisibilityStatus(item, StreamVisibilityStatus.HIDE_PARTIALLY_WATCHED)
} else if (item.itemId == R.id.menu_item_feed_toggle_future_items) {
showFutureItems = !item.isChecked
updateToggleFutureItemsButton(item)
@ -258,11 +270,11 @@ class FeedFragment : BaseStateFragment<FeedState>() {
return super.onOptionsItemSelected(item)
}
private fun setShowPlayedItemsMethod(item: MenuItem, showItems: ShowItems) {
showPlayedItems = showItems
viewModel.togglePlayedItems(showPlayedItems)
updateTogglePlayedItemsButton(item)
viewModel.saveShowPlayedItemsToPreferences(showPlayedItems)
private fun changeItemsVisibilityStatus(item: MenuItem, streamVisibilityStatus: StreamVisibilityStatus) {
feedVisibilityStatus = streamVisibilityStatus
viewModel.changeVisibilityState(feedVisibilityStatus)
updateItemVisibilityMenu(item)
viewModel.saveStreamVisibilityStateToPreferences(feedVisibilityStatus)
}
override fun onDestroyOptionsMenu() {
@ -291,10 +303,28 @@ class FeedFragment : BaseStateFragment<FeedState>() {
super.onDestroyView()
}
private fun updateTogglePlayedItemsButton(menuItem: MenuItem) {
private fun updateItemVisibilityMenu(menuItem: MenuItem) {
when (feedVisibilityStatus) {
StreamVisibilityStatus.DEFAULT -> {
showAllMenuItem.isVisible = false
hideWatchedMenuItem.isVisible = true
hidePartiallyWatchedMenuItem.isVisible = true
}
StreamVisibilityStatus.HIDE_WATCHED -> {
showAllMenuItem.isVisible = true
hideWatchedMenuItem.isVisible = false
hidePartiallyWatchedMenuItem.isVisible = true
}
else -> {
showAllMenuItem.isVisible = true
hideWatchedMenuItem.isVisible = true
hidePartiallyWatchedMenuItem.isVisible = false
}
}
MenuItemCompat.setTooltipText(
menuItem,
getString(R.string.feed_toggle_show_hide_played_items)
getString(R.string.feed_change_stream_visibility_state)
)
}

View File

@ -28,20 +28,17 @@ import org.schabi.newpipe.util.DEFAULT_THROTTLE_TIMEOUT
import java.time.OffsetDateTime
import java.util.concurrent.TimeUnit
enum class ShowItems {
WATCHED, PARTIALLY_WATCHED, DEFAULT
}
class FeedViewModel(
private val application: Application,
groupId: Long = FeedGroupEntity.GROUP_ALL_ID,
initialShowPlayedItems: ShowItems = ShowItems.DEFAULT,
initialStreamVisibility: StreamVisibilityStatus = StreamVisibilityStatus.DEFAULT,
initialShowFutureItems: Boolean = true
) : ViewModel() {
private val feedDatabaseManager = FeedDatabaseManager(application)
private val toggleShowPlayedItems = BehaviorProcessor.create<ShowItems>()
private val toggleShowPlayedItemsFlowable = toggleShowPlayedItems
.startWithItem(initialShowPlayedItems)
private val streamVisibilityState = BehaviorProcessor.create<StreamVisibilityStatus>()
private val streamVisibilityStateFlowable = streamVisibilityState
.startWithItem(initialStreamVisibility)
.distinctUntilChanged()
private val toggleShowFutureItems = BehaviorProcessor.create<Boolean>()
@ -55,12 +52,12 @@ class FeedViewModel(
private var combineDisposable = Flowable
.combineLatest(
FeedEventManager.events(),
toggleShowPlayedItemsFlowable,
streamVisibilityStateFlowable,
toggleShowFutureItemsFlowable,
feedDatabaseManager.notLoadedCount(groupId),
feedDatabaseManager.oldestSubscriptionUpdate(groupId),
Function5 { t1: FeedEventManager.Event, t2: ShowItems, t3: Boolean,
Function5 { t1: FeedEventManager.Event, t2: StreamVisibilityStatus, t3: Boolean,
t4: Long, t5: List<OffsetDateTime> ->
return@Function5 CombineResultEventHolder(t1, t2, t3, t4, t5.firstOrNull())
}
@ -74,10 +71,10 @@ class FeedViewModel(
.getStreams(
groupId,
!(
showPlayedItems == ShowItems.WATCHED ||
showPlayedItems == ShowItems.PARTIALLY_WATCHED
showPlayedItems == StreamVisibilityStatus.HIDE_WATCHED ||
showPlayedItems == StreamVisibilityStatus.HIDE_PARTIALLY_WATCHED
),
showPlayedItems != ShowItems.PARTIALLY_WATCHED,
showPlayedItems != StreamVisibilityStatus.HIDE_PARTIALLY_WATCHED,
showFutureItems
)
.blockingGet(arrayListOf())
@ -110,7 +107,7 @@ class FeedViewModel(
private data class CombineResultEventHolder(
val t1: FeedEventManager.Event,
val t2: ShowItems,
val t2: StreamVisibilityStatus,
val t3: Boolean,
val t4: Long,
val t5: OffsetDateTime?
@ -123,20 +120,20 @@ class FeedViewModel(
val t4: OffsetDateTime?
)
fun togglePlayedItems(showItems: ShowItems) {
toggleShowPlayedItems.onNext(showItems)
fun changeVisibilityState(streamVisibilityStatus: StreamVisibilityStatus) {
streamVisibilityState.onNext(streamVisibilityStatus)
}
fun saveShowPlayedItemsToPreferences(showItems: ShowItems) =
fun saveStreamVisibilityStateToPreferences(streamVisibilityStatus: StreamVisibilityStatus) =
PreferenceManager.getDefaultSharedPreferences(application).edit {
this.putString(
application.getString(R.string.feed_show_played_items_key),
showItems.toString()
application.getString(R.string.feed_stream_visibility_state_key),
streamVisibilityStatus.toString()
)
this.apply()
}
fun getItemsVisibilityFromPreferences() = getItemsVisibilityFromPreferences(application)
fun getItemsVisibilityFromPreferences() = getStreamVisibilityStateFromPreferences(application)
fun toggleFutureItems(showFutureItems: Boolean) {
toggleShowFutureItems.onNext(showFutureItems)
@ -152,13 +149,13 @@ class FeedViewModel(
companion object {
private fun getItemsVisibilityFromPreferences(context: Context): ShowItems {
private fun getStreamVisibilityStateFromPreferences(context: Context): StreamVisibilityStatus {
val s = PreferenceManager.getDefaultSharedPreferences(context)
.getString(
context.getString(R.string.feed_show_played_items_key),
ShowItems.DEFAULT.toString()
) ?: ShowItems.DEFAULT.toString()
return ShowItems.valueOf(s)
context.getString(R.string.feed_stream_visibility_state_key),
StreamVisibilityStatus.DEFAULT.toString()
) ?: StreamVisibilityStatus.DEFAULT.toString()
return StreamVisibilityStatus.valueOf(s)
}
private fun getShowFutureItemsFromPreferences(context: Context) =
@ -170,7 +167,7 @@ class FeedViewModel(
App.getApp(),
groupId,
// Read initial value from preferences
getItemsVisibilityFromPreferences(context.applicationContext),
getStreamVisibilityStateFromPreferences(context.applicationContext),
getShowFutureItemsFromPreferences(context.applicationContext)
)
}

View File

@ -0,0 +1,5 @@
package org.schabi.newpipe.local.feed
enum class StreamVisibilityStatus {
DEFAULT, HIDE_WATCHED, HIDE_PARTIALLY_WATCHED
}

View File

@ -87,7 +87,7 @@ public class HistoryRecordManager {
* Marks a stream item as watched such that it is hidden from the feed if watched videos are
* hidden. Adds a history entry and updates the stream progress to 100%.
*
* @see FeedViewModel#togglePlayedItems
* @see FeedViewModel#changeVisibilityState
* @param info the item to mark as watched
* @return a Maybe containing the ID of the item if successful
*/

View File

@ -7,18 +7,18 @@
android:checkable="false"
android:checked="false"
android:icon="@drawable/ic_visibility_on"
android:title="@string/feed_toggle_show_hide_played_items"
android:title="@string/feed_change_stream_visibility_state"
app:showAsAction="ifRoom">
<menu>
<item
android:id="@+id/menu_item_feed_toggle_show_all_items"
android:title="@string/feed_toggle_show_items"/>
android:title="@string/feed_stream_visibility_show_all"/>
<item
android:id="@+id/menu_item_feed_toggle_show_played_items"
android:title="@string/feed_toggle_show_watched_items"/>
android:title="@string/feed_stream_visibility_hide_watched"/>
<item
android:id="@+id/menu_item_feed_toggle_partially_played_items"
android:title="@string/feed_toggle_show_partially_watched_items"/>
android:title="@string/feed_stream_visibility_hide_partially_watched"/>
</menu>
</item>

View File

@ -283,7 +283,7 @@
<string name="feed_update_threshold_key">feed_update_threshold_key</string>
<string name="feed_update_threshold_default_value">300</string>
<string name="feed_show_played_items_key">feed_show_items</string>
<string name="feed_stream_visibility_state_key">feed_stream_visibility_state</string>
<string name="feed_show_future_items_key">feed_show_future_items</string>
<string name="show_thumbnail_key">show_thumbnail_key</string>

View File

@ -691,7 +691,7 @@
\nYouTube is an example of a service that offers this fast method with its RSS feed.
\n
\nSo the choice boils down to what you prefer: speed or precise information.</string>
<string name="feed_toggle_show_hide_played_items">Show/hide watched items</string>
<string name="feed_change_stream_visibility_state">Show/hide watched streams</string>
<string name="content_not_supported">This content is not yet supported by NewPipe.\n\nIt will hopefully be supported in a future version.</string>
<string name="detail_sub_channel_thumbnail_view_description">Channel\'s avatar thumbnail</string>
<string name="channel_created_by">Created by %s</string>
@ -759,8 +759,8 @@
<string name="unknown_quality">Unknown quality</string>
<string name="feed_toggle_show_future_items">Show future items</string>
<string name="feed_toggle_hide_future_items">Hide future items</string>
<string name="feed_toggle_show_partially_watched_items">Hide Watched and Partially Watched </string>
<string name="feed_toggle_show_watched_items">Hide Watched</string>
<string name="feed_toggle_show_items">Show All</string>
<string name="feed_stream_visibility_hide_partially_watched">Hide Watched </string>
<string name="feed_stream_visibility_hide_watched">Hide Fully Watched</string>
<string name="feed_stream_visibility_show_all">Show All</string>
<string name="sort">Sort</string>
</resources>