From 66c95f901dfa31c193375abfdef8028f527834de Mon Sep 17 00:00:00 2001 From: Grady Clark <50427197+GradyClark@users.noreply.github.com> Date: Fri, 7 Feb 2020 23:57:36 -0600 Subject: [PATCH] Added the ability to remove all watched videos from local playlists Changes: - local_playlist_control.xml * A copy of playlist_control.xml * To hold the 'Remove Watched Videos' buttton - local_playlist_header.xml * Changed the include layout to now include local_playlist_control.xml - strings.xml * added string 'remove_watched' with value 'Remove Watched' - LocalPlaylistFragment.java * Added the functionality to remove watched videos, to the 'Remove Watched Videos' button in local_playlist_control.xml. In the background via AsyncTask. This will also change the playlist's thumbnail, if the thumbnail video is removed. Tested on: - Pixel --- .../local/playlist/LocalPlaylistFragment.java | 89 +++++++++++++- .../res/layout/local_playlist_control.xml | 115 ++++++++++++++++++ .../main/res/layout/local_playlist_header.xml | 2 +- app/src/main/res/values/strings.xml | 4 +- 4 files changed, 207 insertions(+), 3 deletions(-) create mode 100644 app/src/main/res/layout/local_playlist_control.xml 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 d430afa5c..a3611fa85 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.os.AsyncTask; import android.os.Bundle; import android.os.Parcelable; import android.text.TextUtils; @@ -24,11 +25,13 @@ import org.reactivestreams.Subscription; import org.schabi.newpipe.NewPipeDatabase; 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.extractor.stream.StreamInfoItem; import org.schabi.newpipe.extractor.stream.StreamType; import org.schabi.newpipe.info_list.InfoItemDialog; import org.schabi.newpipe.local.BaseLocalListFragment; +import org.schabi.newpipe.local.history.HistoryRecordManager; import org.schabi.newpipe.player.playqueue.PlayQueue; import org.schabi.newpipe.player.playqueue.SinglePlayQueue; import org.schabi.newpipe.report.UserAction; @@ -39,11 +42,13 @@ import org.schabi.newpipe.util.StreamDialogEntry; import java.util.ArrayList; import java.util.Collections; +import java.util.Iterator; import java.util.List; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import icepick.State; +import io.reactivex.Flowable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.CompositeDisposable; import io.reactivex.disposables.Disposable; @@ -71,6 +76,8 @@ public class LocalPlaylistFragment extends BaseLocalListFragment NavigationHelper.playOnBackgroundPlayer(activity, getPlayQueue(), false)); + headerRemoveWatchedButton.setOnClickListener( + view -> { + //Solution, Scorched Earth, Copy non duplicates, clear playlist, then copy back over + //Other options didn't work as intended, or crashed. Like deleteItem(playlist_item) crashes when called in this function. + new RemoveWatchedStreams().execute(); + } + ); headerPopupButton.setOnLongClickListener(view -> { NavigationHelper.enqueueOnPopupPlayer(activity, getPlayQueue(), true); @@ -684,5 +702,74 @@ public class LocalPlaylistFragment extends BaseLocalListFragment { + List localItems = new ArrayList<>(); + Long RemovedItemCount = 0l; + boolean thumbNailVideoRemoved = false; + + @Override + protected void onPreExecute() { + super.onPreExecute(); + showLoading(); + localItems.clear(); + } + + @Override + protected Long doInBackground(String... urls) { + + HistoryRecordManager recordManager = new HistoryRecordManager(getContext()); + Iterator it_history; + StreamHistoryEntry history_item; + + Flowable> playlist = playlistManager.getPlaylistStreams(playlistId); + Iterator it_playlist = playlist.blockingFirst().iterator(); + PlaylistStreamEntry playlist_item = null; + + boolean isNonDuplicate; + + while (it_playlist.hasNext()) { + playlist_item = it_playlist.next(); + + it_history = recordManager.getStreamHistory().blockingFirst().iterator(); + + isNonDuplicate = true; + while (it_history.hasNext()) { + history_item = it_history.next(); + if (history_item.streamId == playlist_item.streamId) { + isNonDuplicate = false; + break; + } + } + if (isNonDuplicate) { + localItems.add(playlist_item); + } + else + { + RemovedItemCount++; + if(playlistManager.getPlaylistThumbnail(playlistId).equals(playlist_item.thumbnailUrl)) + { + thumbNailVideoRemoved = true; + } + } + } + return this.RemovedItemCount; + } + + @Override + protected void onPostExecute(Long result) { + itemListAdapter.clearStreamItemList(); + itemListAdapter.addItems(localItems); + localItems.clear(); + + if (thumbNailVideoRemoved) + updateThumbnailUrl(); + + setVideoCount(itemListAdapter.getItemsList().size()); + + saveChanges(); + hideLoading(); + } + } } diff --git a/app/src/main/res/layout/local_playlist_control.xml b/app/src/main/res/layout/local_playlist_control.xml new file mode 100644 index 000000000..8465c9ba5 --- /dev/null +++ b/app/src/main/res/layout/local_playlist_control.xml @@ -0,0 +1,115 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/local_playlist_header.xml b/app/src/main/res/layout/local_playlist_header.xml index 420da04ee..372187a64 100644 --- a/app/src/main/res/layout/local_playlist_header.xml +++ b/app/src/main/res/layout/local_playlist_header.xml @@ -50,7 +50,7 @@ android:layout_height="wrap_content" android:layout_below="@id/playlist_stream_count"> - + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ef09be9f4..770d3d4e8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -637,4 +637,6 @@ Enable fast mode Disable fast mode Do you think feed loading is too slow? If so, try enabling fast loading (you can change it in settings or by pressing the button below).\n\nNewPipe offers two feed loading strategies:\n• Fetching the whole subscription channel, which is slow but complete.\n• Using a dedicated service endpoint, which is fast but usually not complete.\n\nThe difference between the two is that the fast one usually lacks some information, like the item\'s duration or type (can\'t distinguish between live videos and normal ones) and it may return less items.\n\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. - \ No newline at end of file + + Remove Watched +