From fdfeac081ad9fe6094582d29a8df72d00e7d2271 Mon Sep 17 00:00:00 2001 From: Jared Fantaye Date: Mon, 5 Dec 2022 00:15:05 +0100 Subject: [PATCH] Implemented a warning before adding duplicate to playlist. --- .../playlist/dao/PlaylistStreamDAO.java | 11 +++++++ .../local/dialog/PlaylistAppendDialog.java | 32 ++++++++++++++++++- .../local/playlist/LocalPlaylistManager.java | 4 +++ app/src/main/res/values/strings.xml | 3 ++ 4 files changed, 49 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistStreamDAO.java b/app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistStreamDAO.java index 2036dc205..911baf7f3 100644 --- a/app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistStreamDAO.java +++ b/app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistStreamDAO.java @@ -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 { + " 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 getDuplicates(long playlistId, String streamURL); + + @Query("SELECT COALESCE(MAX(" + JOIN_INDEX + "), -1)" + " FROM " + PLAYLIST_STREAM_JOIN_TABLE + " WHERE " + JOIN_PLAYLIST_ID + " = :playlistId") diff --git a/app/src/main/java/org/schabi/newpipe/local/dialog/PlaylistAppendDialog.java b/app/src/main/java/org/schabi/newpipe/local/dialog/PlaylistAppendDialog.java index 88dec3911..c80e919bf 100644 --- a/app/src/main/java/org/schabi/newpipe/local/dialog/PlaylistAppendDialog.java +++ b/app/src/main/java/org/schabi/newpipe/local/dialog/PlaylistAppendDialog.java @@ -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 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 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 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(); + } } diff --git a/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistManager.java b/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistManager.java index 4007d0e09..807c3c051 100644 --- a/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistManager.java +++ b/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistManager.java @@ -91,6 +91,10 @@ public class LocalPlaylistManager { return playlistStreamTable.getOrderedStreamsOf(playlistId).subscribeOn(Schedulers.io()); } + public Flowable getPlaylistDuplicates(final long playlistId, final String streamURL) { + return playlistStreamTable.getDuplicates(playlistId, streamURL); + } + public Single deletePlaylist(final long playlistId) { return Single.fromCallable(() -> playlistTable.deletePlaylist(playlistId)) .subscribeOn(Schedulers.io()); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c9e0bb492..5b398abd1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -447,6 +447,9 @@ Playlisted Playlist thumbnail changed. Auto-generated (no uploader found) + Duplicated Video Found + The playlist contains this stream + already %d time(s).\nDo you want to add it one more time? No Captions Fit