Implemented a warning before adding duplicate to playlist.

This commit is contained in:
Jared Fantaye 2022-12-05 00:15:05 +01:00 committed by Stypox
parent 262b3a2945
commit fdfeac081a
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
4 changed files with 49 additions and 1 deletions

View File

@ -26,6 +26,7 @@ import static org.schabi.newpipe.database.playlist.model.PlaylistStreamEntity.PL
import static org.schabi.newpipe.database.stream.model.StreamEntity.STREAM_ID;
import static org.schabi.newpipe.database.stream.model.StreamEntity.STREAM_TABLE;
import static org.schabi.newpipe.database.stream.model.StreamEntity.STREAM_THUMBNAIL_URL;
import static org.schabi.newpipe.database.stream.model.StreamEntity.STREAM_URL;
import static org.schabi.newpipe.database.stream.model.StreamStateEntity.JOIN_STREAM_ID_ALIAS;
import static org.schabi.newpipe.database.stream.model.StreamStateEntity.STREAM_PROGRESS_MILLIS;
import static org.schabi.newpipe.database.stream.model.StreamStateEntity.STREAM_STATE_TABLE;
@ -49,6 +50,16 @@ public interface PlaylistStreamDAO extends BasicDAO<PlaylistStreamEntity> {
+ " WHERE " + JOIN_PLAYLIST_ID + " = :playlistId")
void deleteBatch(long playlistId);
@Query("SELECT COALESCE(COUNT(*), 0)"
+ " FROM " + STREAM_TABLE
+ " LEFT JOIN " + PLAYLIST_STREAM_JOIN_TABLE
+ " ON " + STREAM_ID + " = " + JOIN_STREAM_ID
+ " WHERE " + JOIN_PLAYLIST_ID + " = :playlistId "
+ " AND " + STREAM_URL + " = :streamURL"
)
Flowable<Integer> getDuplicates(long playlistId, String streamURL);
@Query("SELECT COALESCE(MAX(" + JOIN_INDEX + "), -1)"
+ " FROM " + PLAYLIST_STREAM_JOIN_TABLE
+ " WHERE " + JOIN_PLAYLIST_ID + " = :playlistId")

View File

@ -1,5 +1,6 @@
package org.schabi.newpipe.local.dialog;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
@ -128,6 +129,19 @@ public final class PlaylistAppendDialog extends PlaylistDialog {
private void onPlaylistSelected(@NonNull final LocalPlaylistManager manager,
@NonNull final PlaylistMetadataEntry playlist,
@NonNull final List<StreamEntity> streams) {
final int numOfDuplicates = manager.getPlaylistDuplicates(playlist.uid,
streams.get(0).getUrl()).blockingFirst();
if (numOfDuplicates > 0) {
createDuplicateDialog(numOfDuplicates, manager, playlist, streams);
} else {
addStreamToPlaylist(manager, playlist, streams);
}
}
private void addStreamToPlaylist(@NonNull final LocalPlaylistManager manager,
@NonNull final PlaylistMetadataEntry playlist,
@NonNull final List<StreamEntity> streams) {
final Toast successToast = Toast.makeText(getContext(),
R.string.playlist_add_stream_success, Toast.LENGTH_SHORT);
@ -142,7 +156,23 @@ public final class PlaylistAppendDialog extends PlaylistDialog {
playlistDisposables.add(manager.appendToPlaylist(playlist.uid, streams)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(ignored -> successToast.show()));
requireDialog().dismiss();
}
private void createDuplicateDialog(final int duplicates,
@NonNull final LocalPlaylistManager manager,
@NonNull final PlaylistMetadataEntry playlist,
@NonNull final List<StreamEntity> streams) {
//TODO: change color
final AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
builder.setTitle(R.string.duplicate_stream_in_playlist_title);
builder.setMessage(getString(R.string.duplicate_stream_in_playlist_description,
duplicates));
builder.setPositiveButton(android.R.string.yes, (dialog, i) -> {
addStreamToPlaylist(manager, playlist, streams);
});
builder.setNeutralButton(R.string.cancel, null);
builder.create().show();
}
}

View File

@ -91,6 +91,10 @@ public class LocalPlaylistManager {
return playlistStreamTable.getOrderedStreamsOf(playlistId).subscribeOn(Schedulers.io());
}
public Flowable<Integer> getPlaylistDuplicates(final long playlistId, final String streamURL) {
return playlistStreamTable.getDuplicates(playlistId, streamURL);
}
public Single<Integer> deletePlaylist(final long playlistId) {
return Single.fromCallable(() -> playlistTable.deletePlaylist(playlistId))
.subscribeOn(Schedulers.io());

View File

@ -447,6 +447,9 @@
<string name="playlist_add_stream_success">Playlisted</string>
<string name="playlist_thumbnail_change_success">Playlist thumbnail changed.</string>
<string name="playlist_no_uploader">Auto-generated (no uploader found)</string>
<string name="duplicate_stream_in_playlist_title">Duplicated Video Found</string>
<string name="duplicate_stream_in_playlist_description">The playlist contains this stream
already %d time(s).\nDo you want to add it one more time?</string>
<!-- Players -->
<string name="caption_none">No Captions</string>
<string name="resize_fit">Fit</string>