From fe1646caa00e8791c131f2be92b973afb95a3032 Mon Sep 17 00:00:00 2001 From: Grady Clark <50427197+GradyClark@users.noreply.github.com> Date: Tue, 14 Apr 2020 13:15:07 -0500 Subject: [PATCH] Changed "Remove Watched": - Will now execute on the io thread - Added confirmation dialog - Warning the user, and asking if they also want to remove partially watched videos --- .../local/playlist/LocalPlaylistFragment.java | 76 ++++++++++++++----- app/src/main/res/values/strings.xml | 5 ++ 2 files changed, 63 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java b/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java index 8e8ad44ec..6af10bf21 100644 --- a/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java +++ b/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java @@ -2,6 +2,7 @@ package org.schabi.newpipe.local.playlist; import android.app.Activity; import android.content.Context; +import android.content.DialogInterface; import android.os.Bundle; import android.os.Parcelable; import android.text.TextUtils; @@ -29,6 +30,7 @@ import org.schabi.newpipe.R; import org.schabi.newpipe.database.LocalItem; import org.schabi.newpipe.database.history.model.StreamHistoryEntry; import org.schabi.newpipe.database.playlist.PlaylistStreamEntry; +import org.schabi.newpipe.database.stream.model.StreamStateEntity; import org.schabi.newpipe.extractor.stream.StreamInfoItem; import org.schabi.newpipe.extractor.stream.StreamType; import org.schabi.newpipe.info_list.InfoItemDialog; @@ -360,7 +362,18 @@ public class LocalPlaylistFragment extends BaseLocalListFragment removeWatchedStreams(false)) + .setNeutralButton( + R.string.remove_watched_popup_yes_and_partially_watched_videos, + (DialogInterface d, int id) -> removeWatchedStreams(true)) + .setNegativeButton(R.string.remove_watched_popup_cancel, + (DialogInterface d, int id) -> d.cancel()); + builder.create().show(); break; default: return super.onOptionsItemSelected(item); @@ -368,21 +381,32 @@ public class LocalPlaylistFragment extends BaseLocalListFragment playlist) -> { + removeWatchedDisposable = Flowable.just(Flowable.just(removePartiallyWatched, + playlistManager.getPlaylistStreams(playlistId).blockingFirst())) + .subscribeOn(Schedulers.io()) + .map(flow -> { + boolean localRemovePartiallyWatched = (boolean) flow.blockingFirst(); + List playlist + = (List) flow.blockingLast(); HistoryRecordManager recordManager = new HistoryRecordManager(getContext()); Iterator playlistIter = playlist.iterator(); Iterator historyIter = recordManager .getStreamHistorySortedById().blockingFirst().iterator(); + List notWatchedItems = new ArrayList<>(); + Iterator streamStatesIter = null; + boolean thumbnailVideoRemoved = false; + + if (!localRemovePartiallyWatched) { + streamStatesIter = recordManager.loadLocalStreamStateBatch(playlist) + .blockingGet().iterator(); + } // already sorted by ^ getStreamHistorySortedById(), binary search can be used ArrayList historyStreamIds = new ArrayList<>(); @@ -390,19 +414,35 @@ public class LocalPlaylistFragment extends BaseLocalListFragment notWatchedItems = new ArrayList<>(); - boolean thumbnailVideoRemoved = false; - while (playlistIter.hasNext()) { - PlaylistStreamEntry playlistItem = playlistIter.next(); - int indexInHistory = Collections.binarySearch(historyStreamIds, - playlistItem.getStreamId()); + if (localRemovePartiallyWatched) { + while (playlistIter.hasNext()) { + PlaylistStreamEntry playlistItem = playlistIter.next(); + int indexInHistory = Collections.binarySearch(historyStreamIds, + playlistItem.getStreamId()); - if (indexInHistory < 0) { - notWatchedItems.add(playlistItem); - } else if (!thumbnailVideoRemoved - && playlistManager.getPlaylistThumbnail(playlistId) - .equals(playlistItem.getStreamEntity().getThumbnailUrl())) { - thumbnailVideoRemoved = true; + if (indexInHistory < 0) { + notWatchedItems.add(playlistItem); + } else if (!thumbnailVideoRemoved + && playlistManager.getPlaylistThumbnail(playlistId) + .equals(playlistItem.getStreamEntity().getThumbnailUrl())) { + thumbnailVideoRemoved = true; + } + } + } else { + boolean hasState = false; + while (playlistIter.hasNext()) { + PlaylistStreamEntry playlistItem = playlistIter.next(); + int indexInHistory = Collections.binarySearch(historyStreamIds, + playlistItem.getStreamId()); + + hasState = streamStatesIter.next() != null; + if (indexInHistory < 0 || hasState) { + notWatchedItems.add(playlistItem); + } else if (!thumbnailVideoRemoved + && playlistManager.getPlaylistThumbnail(playlistId) + .equals(playlistItem.getStreamEntity().getThumbnailUrl())) { + thumbnailVideoRemoved = true; + } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e0591e7fd..5df137c54 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -594,6 +594,11 @@ App language System default Remove watched + Remove watched videos? + "Videos that have been watched\nbefore and after being added to the playlist will be removed.\nAre you sure? This cannot be undone! + Yes + Cancel + Yes, and partially watched videos Due to ExoPlayer constraints the seek duration was set to %d seconds