Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ability to remove all watched videos from a local playlist #3065

Merged
merged 14 commits into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ public Flowable<List<StreamHistoryEntity>> listByService(final int serviceId) {
+ " ORDER BY " + STREAM_ACCESS_DATE + " DESC")
public abstract Flowable<List<StreamHistoryEntry>> getHistory();


@Query("SELECT * FROM " + STREAM_TABLE
+ " INNER JOIN " + STREAM_HISTORY_TABLE
+ " ON " + STREAM_ID + " = " + JOIN_STREAM_ID
+ " ORDER BY " + STREAM_ID + " ASC")
public abstract Flowable<List<StreamHistoryEntry>> getHistorySortedById();

@Query("SELECT * FROM " + STREAM_HISTORY_TABLE + " WHERE " + JOIN_STREAM_ID
+ " = :streamId ORDER BY " + STREAM_ACCESS_DATE + " DESC LIMIT 1")
@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ public Flowable<List<StreamHistoryEntry>> getStreamHistory() {
return streamHistoryTable.getHistory().subscribeOn(Schedulers.io());
}

public Flowable<List<StreamHistoryEntry>> getStreamHistorySortedById() {
return streamHistoryTable.getHistorySortedById().subscribeOn(Schedulers.io());
}

public Flowable<List<StreamStatisticsEntry>> getStreamStatistics() {
return streamHistoryTable.getStatistics().subscribeOn(Schedulers.io());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
Expand All @@ -24,11 +28,14 @@
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.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;
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;
Expand All @@ -39,15 +46,18 @@

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;
import io.reactivex.disposables.Disposables;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.PublishSubject;

import static org.schabi.newpipe.util.AnimationUtils.animateView;
Expand All @@ -71,13 +81,15 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
private View headerPlayAllButton;
private View headerPopupButton;
private View headerBackgroundButton;

private ItemTouchHelper itemTouchHelper;

private LocalPlaylistManager playlistManager;
private Subscription databaseSubscription;

private PublishSubject<Long> debouncedSaveSignal;
private CompositeDisposable disposables;
private Disposable removeWatchedDisposable;

/* Has the playlist been fully loaded from db */
private AtomicBoolean isLoadingComplete;
Expand Down Expand Up @@ -244,6 +256,16 @@ public void onPause() {
saveImmediate();
}

@Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
if (DEBUG) {
Log.d(TAG, "onCreateOptionsMenu() called with: menu = [" + menu
+ "], inflater = [" + inflater + "]");
GradyTheDev marked this conversation as resolved.
Show resolved Hide resolved
}
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_local_playlist, menu);
}

@Override
public void onDestroyView() {
super.onDestroyView();
Expand Down Expand Up @@ -282,9 +304,14 @@ public void onDestroy() {
disposables.dispose();
}

if (removeWatchedDisposable != null) {
removeWatchedDisposable.dispose();
}

debouncedSaveSignal = null;
playlistManager = null;
disposables = null;
removeWatchedDisposable = null;

isLoadingComplete = null;
isModified = null;
Expand Down Expand Up @@ -331,6 +358,121 @@ public void onComplete() { }
};
}

@Override
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_removeWatched:
android.app.AlertDialog.Builder builder =
new android.app.AlertDialog.Builder(getActivity());
builder.setMessage(R.string.remove_watched_popup_warning)
.setTitle(R.string.remove_watched_popup_title)
.setPositiveButton(R.string.remove_watched_popup_yes,
(DialogInterface d, int id) -> 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();
GradyTheDev marked this conversation as resolved.
Show resolved Hide resolved
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}

public void removeWatchedStreams(final boolean removePartiallyWatched) {
GradyTheDev marked this conversation as resolved.
Show resolved Hide resolved
if (removeWatchedDisposable != null && !removeWatchedDisposable.isDisposed()) {
// already running
return;
}
showLoading();

removeWatchedDisposable = Flowable.just(Flowable.just(removePartiallyWatched,
GradyTheDev marked this conversation as resolved.
Show resolved Hide resolved
playlistManager.getPlaylistStreams(playlistId).blockingFirst()))
.subscribeOn(Schedulers.io())
.map(flow -> {
boolean localRemovePartiallyWatched = (boolean) flow.blockingFirst();
List<PlaylistStreamEntry> playlist
= (List<PlaylistStreamEntry>) flow.blockingLast();
HistoryRecordManager recordManager = new HistoryRecordManager(getContext());
Iterator<PlaylistStreamEntry> playlistIter = playlist.iterator();
Iterator<StreamHistoryEntry> historyIter = recordManager
.getStreamHistorySortedById().blockingFirst().iterator();
List<PlaylistStreamEntry> notWatchedItems = new ArrayList<>();
Iterator<StreamStateEntity> streamStatesIter = null;
boolean thumbnailVideoRemoved = false;
GradyTheDev marked this conversation as resolved.
Show resolved Hide resolved

if (!localRemovePartiallyWatched) {
streamStatesIter = recordManager.loadLocalStreamStateBatch(playlist)
GradyTheDev marked this conversation as resolved.
Show resolved Hide resolved
.blockingGet().iterator();
}

// already sorted by ^ getStreamHistorySortedById(), binary search can be used
ArrayList<Long> historyStreamIds = new ArrayList<>();
while (historyIter.hasNext()) {
historyStreamIds.add(historyIter.next().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;
}
}
} else {
boolean hasState = false;
GradyTheDev marked this conversation as resolved.
Show resolved Hide resolved
while (playlistIter.hasNext()) {
PlaylistStreamEntry playlistItem = playlistIter.next();
int indexInHistory = Collections.binarySearch(historyStreamIds,
playlistItem.getStreamId());

hasState = streamStatesIter.next() != null;
if (indexInHistory < 0 || hasState) {
GradyTheDev marked this conversation as resolved.
Show resolved Hide resolved
notWatchedItems.add(playlistItem);
} else if (!thumbnailVideoRemoved
&& playlistManager.getPlaylistThumbnail(playlistId)
.equals(playlistItem.getStreamEntity().getThumbnailUrl())) {
thumbnailVideoRemoved = true;
}
}
}

return Flowable.just(notWatchedItems, thumbnailVideoRemoved);
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(flow -> {
List<PlaylistStreamEntry> notWatchedItems =
(List<PlaylistStreamEntry>) flow.blockingFirst();
boolean thumbnailVideoRemoved = (Boolean) flow.blockingLast();

itemListAdapter.clearStreamItemList();
itemListAdapter.addItems(notWatchedItems);
saveChanges();


if (thumbnailVideoRemoved) {
updateThumbnailUrl();
}

long videoCount = itemListAdapter.getItemsList().size();
setVideoCount(videoCount);
if (videoCount == 0) {
showEmptyState();
}

hideLoading();
}, this::onError);
}

@Override
public void handleResult(@NonNull final List<PlaylistStreamEntry> result) {
super.handleResult(result);
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/local_playlist_header.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
android:layout_height="wrap_content"
android:layout_below="@id/playlist_stream_count">

<include layout="@layout/playlist_control"/>
<include layout="@layout/playlist_control" />
</LinearLayout>

</RelativeLayout>
10 changes: 10 additions & 0 deletions app/src/main/res/menu/menu_local_playlist.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<item
android:id="@+id/menu_item_removeWatched"
GradyTheDev marked this conversation as resolved.
Show resolved Hide resolved
android:title="@string/remove_watched"
app:showAsAction="never"/>
GradyTheDev marked this conversation as resolved.
Show resolved Hide resolved
</menu>
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,12 @@
<string name="choose_instance_prompt">Choose an instance</string>
<string name="app_language_title">App language</string>
<string name="systems_language">System default</string>
<string name="remove_watched">Remove watched</string>
<string name="remove_watched_popup_title">Remove watched videos?</string>
<string name="remove_watched_popup_warning">"Videos that have been watched\nbefore and after being added to the playlist will be removed.\nAre you sure? This cannot be undone!</string>
GradyTheDev marked this conversation as resolved.
Show resolved Hide resolved
<string name="remove_watched_popup_yes">Yes</string>
<string name="remove_watched_popup_cancel">Cancel</string>
GradyTheDev marked this conversation as resolved.
Show resolved Hide resolved
<string name="remove_watched_popup_yes_and_partially_watched_videos">Yes, and partially watched videos</string>
<string name="new_seek_duration_toast">Due to ExoPlayer constraints the seek duration was set to %d seconds</string>
<!-- Time duration plurals -->
<plurals name="seconds">
Expand Down