From 8b6e110635a0404a1faea8bba252823fffa49f31 Mon Sep 17 00:00:00 2001 From: Jared Fantaye Date: Mon, 12 Dec 2022 20:12:06 +0100 Subject: [PATCH] Fixed the functionality, improved performance & general code cleanup --- .../playlist/dao/PlaylistStreamDAO.java | 3 +- .../local/dialog/PlaylistAppendDialog.java | 68 +++++++++++-------- .../local/playlist/LocalPlaylistManager.java | 7 +- app/src/main/res/layout/dialog_playlists.xml | 17 ++++- .../res/layout/list_playlist_mini_item.xml | 12 ---- app/src/main/res/values/strings.xml | 1 + 6 files changed, 60 insertions(+), 48 deletions(-) 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 a9719c9fa..27eb34b90 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 @@ -57,7 +57,7 @@ public interface PlaylistStreamDAO extends BasicDAO { + " WHERE " + JOIN_PLAYLIST_ID + " = :playlistId " + " AND " + STREAM_URL + " = :streamURL" ) - Flowable getDuplicates(long playlistId, String streamURL); + Flowable getDuplicateCount(long playlistId, String streamURL); @Query("SELECT " + JOIN_PLAYLIST_ID + " FROM " + STREAM_TABLE @@ -67,7 +67,6 @@ public interface PlaylistStreamDAO extends BasicDAO { ) Flowable> getDuplicatePlaylists(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 9616b06b3..5767d6163 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 @@ -16,7 +16,6 @@ import org.schabi.newpipe.NewPipeDatabase; import org.schabi.newpipe.R; import org.schabi.newpipe.database.playlist.PlaylistMetadataEntry; import org.schabi.newpipe.database.stream.model.StreamEntity; -import org.schabi.newpipe.fragments.OnScrollBelowItemsListener; import org.schabi.newpipe.local.LocalItemListAdapter; import org.schabi.newpipe.local.playlist.LocalPlaylistManager; @@ -28,8 +27,12 @@ import io.reactivex.rxjava3.disposables.CompositeDisposable; public final class PlaylistAppendDialog extends PlaylistDialog { private static final String TAG = PlaylistAppendDialog.class.getCanonicalName(); + private static final float DEFAULT_ALPHA = 1f; + private static final float GRAYED_OUT_ALPHA = 0.3f; + private RecyclerView playlistRecyclerView; private LocalItemListAdapter playlistAdapter; + private List duplicateIds; private final CompositeDisposable playlistDisposables = new CompositeDisposable(); @@ -62,6 +65,9 @@ public final class PlaylistAppendDialog extends PlaylistDialog { final LocalPlaylistManager playlistManager = new LocalPlaylistManager(NewPipeDatabase.getInstance(requireContext())); + duplicateIds = playlistManager.getDuplicatePlaylists(getStreamEntities().get(0).getUrl()) + .blockingFirst(); + playlistAdapter = new LocalItemListAdapter(getActivity()); playlistAdapter.setHasStableIds(true); playlistAdapter.setSelectedListener(selectedItem -> { @@ -126,43 +132,43 @@ public final class PlaylistAppendDialog extends PlaylistDialog { playlistAdapter.addItems(playlists); playlistRecyclerView.setVisibility(View.VISIBLE); - playlistRecyclerView.addOnScrollListener(new DefaultItemListOnScrolledDownListener()); + playlistRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { + @Override + public void onScrolled(@NonNull final RecyclerView recyclerView, final int dx, + final int dy) { + showDuplicateIndicators(recyclerView); + } + }); initDuplicateIndicators(playlistRecyclerView); } } - public class DefaultItemListOnScrolledDownListener extends OnScrollBelowItemsListener { - @Override - public void onScrolledDown(final RecyclerView recyclerView) { - showDuplicateIndicators(recyclerView); + public void initDuplicateIndicators(@NonNull final RecyclerView view) { + showDuplicateIndicators(view); + + if (!duplicateIds.isEmpty()) { + final View indicatorExplanation = getView() + .findViewById(R.id.playlist_duplicate); + indicatorExplanation.setVisibility(View.VISIBLE); } } - public void initDuplicateIndicators(@NonNull final RecyclerView view) { - view.postDelayed(new Runnable() { - @Override - public void run() { - showDuplicateIndicators(view); - } - }, 50); - } - - public void showDuplicateIndicators(final RecyclerView view) { - final LocalPlaylistManager playlistManager = - new LocalPlaylistManager(NewPipeDatabase.getInstance(requireContext())); - final List duplicateIds = playlistManager.getDuplicatePlaylist(getStreamEntities() - .get(0).getUrl()).blockingFirst(); - + public void showDuplicateIndicators(@NonNull final RecyclerView view) { if (view.getAdapter() == null) { return; } final int count = view.getAdapter().getItemCount(); for (int i = 0; i < count; i++) { - if (view.findViewHolderForAdapterPosition(i) != null - && duplicateIds.contains(playlistAdapter.getItemId(i))) { - view.findViewHolderForAdapterPosition(i).itemView - .findViewById(R.id.checkmark2).setVisibility(View.VISIBLE); + + final RecyclerView.ViewHolder viewHolder = view.findViewHolderForAdapterPosition(i); + if (viewHolder != null) { + if (duplicateIds.contains(view.getAdapter().getItemId(i))) { + viewHolder.itemView.setAlpha(GRAYED_OUT_ALPHA); + } else { + viewHolder.itemView.setAlpha(DEFAULT_ALPHA); + } + } } } @@ -171,10 +177,10 @@ public final class PlaylistAppendDialog extends PlaylistDialog { @NonNull final PlaylistMetadataEntry playlist, @NonNull final List streams) { - final int numOfDuplicates = manager.getPlaylistDuplicates(playlist.uid, + final int numberOfDuplicates = manager.getPlaylistDuplicateCount(playlist.uid, streams.get(0).getUrl()).blockingFirst(); - if (numOfDuplicates > 0) { - createDuplicateDialog(numOfDuplicates, manager, playlist, streams); + if (numberOfDuplicates > 0) { + createDuplicateDialog(numberOfDuplicates, manager, playlist, streams); } else { addStreamToPlaylist(manager, playlist, streams); } @@ -197,22 +203,24 @@ 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, + private void createDuplicateDialog(final int numberOfDuplicates, @NonNull final LocalPlaylistManager manager, @NonNull final PlaylistMetadataEntry playlist, @NonNull final List streams) { 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)); + numberOfDuplicates)); 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 d017eac6c..eaccb1e71 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,11 +91,12 @@ 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 Flowable getPlaylistDuplicateCount(final long playlistId, + final String streamURL) { + return playlistStreamTable.getDuplicateCount(playlistId, streamURL); } - public Flowable> getDuplicatePlaylist(final String streamURL) { + public Flowable> getDuplicatePlaylists(final String streamURL) { return playlistStreamTable.getDuplicatePlaylists(streamURL); } diff --git a/app/src/main/res/layout/dialog_playlists.xml b/app/src/main/res/layout/dialog_playlists.xml index 18b08d93c..5771b400f 100644 --- a/app/src/main/res/layout/dialog_playlists.xml +++ b/app/src/main/res/layout/dialog_playlists.xml @@ -34,11 +34,26 @@ tools:ignore="RtlHardcoded" /> + + diff --git a/app/src/main/res/layout/list_playlist_mini_item.xml b/app/src/main/res/layout/list_playlist_mini_item.xml index 1b7065f23..a1f70144d 100644 --- a/app/src/main/res/layout/list_playlist_mini_item.xml +++ b/app/src/main/res/layout/list_playlist_mini_item.xml @@ -1,6 +1,5 @@ - - - "Loading requested content" New Playlist + The playlists that are grayed out already contain this item. Rename Name Add to playlist