Skip to content

Commit 135fc08

Browse files
committed
Implemented the "remove duplicates" feature.
1 parent eb3363d commit 135fc08

4 files changed

Lines changed: 36 additions & 14 deletions

File tree

app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistStreamDAO.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,22 @@ default Flowable<List<PlaylistStreamEntity>> listByService(final int serviceId)
8484
+ " ORDER BY " + PLAYLIST_NAME + " COLLATE NOCASE ASC")
8585
Flowable<List<PlaylistMetadataEntry>> getPlaylistMetadata();
8686

87+
@RewriteQueriesToDropUnusedColumns
8788
@Transaction
88-
@Query("DELETE FROM " + PLAYLIST_STREAM_JOIN_TABLE
89-
+ " WHERE " + JOIN_PLAYLIST_ID + "=:playlistId"
90-
+ " AND " + JOIN_STREAM_ID + " IN ("
91-
+ " SELECT " + JOIN_STREAM_ID
89+
@Query("SELECT *, MIN(" + JOIN_INDEX + ") FROM " + STREAM_TABLE + " INNER JOIN "
90+
+ "(SELECT " + JOIN_STREAM_ID + "," + JOIN_INDEX
9291
+ " FROM " + PLAYLIST_STREAM_JOIN_TABLE
93-
+ " WHERE " + JOIN_PLAYLIST_ID + "=:playlistId"
94-
+ " GROUP BY " + JOIN_STREAM_ID
95-
+ " HAVING COUNT(*) > 1 )" )
96-
Flowable<List<PlaylistMetadataEntry>> removeDuplicates(long playlistId);
92+
+ " WHERE " + JOIN_PLAYLIST_ID + " = :playlistId)"
93+
+ " ON " + STREAM_ID + " = " + JOIN_STREAM_ID
94+
+ " LEFT JOIN "
95+
+ "(SELECT " + JOIN_STREAM_ID + " AS " + JOIN_STREAM_ID_ALIAS + ", "
96+
+ STREAM_PROGRESS_MILLIS
97+
+ " FROM " + STREAM_STATE_TABLE + " )"
98+
+ " ON " + STREAM_ID + " = " + JOIN_STREAM_ID_ALIAS
99+
+ " GROUP BY " + STREAM_ID
100+
+ " ORDER BY " + JOIN_INDEX + " ASC")
101+
Flowable<List<PlaylistStreamEntry>> getStreamsWithoutDuplicates(long playlistId);
102+
103+
104+
97105
}

app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,8 +627,8 @@ private void updateThumbnailUrl() {
627627
private void openRemoveDuplicatesDialog() {
628628
final AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
629629

630-
builder.setTitle("R.string.duplicate_stream_in_playlist_title")
631-
.setMessage("test")
630+
builder.setTitle(R.string.remove_duplicates_title)
631+
.setMessage(R.string.remove_duplicates_message)
632632
.setPositiveButton(android.R.string.yes, (dialog, i) -> {
633633
removeDuplicatesInPlaylist();
634634
})
@@ -638,7 +638,20 @@ private void openRemoveDuplicatesDialog() {
638638
}
639639

640640
private void removeDuplicatesInPlaylist() {
641+
final List<PlaylistStreamEntry> itemsToKeep = playlistManager
642+
.getDistinctPlaylistStreams(playlistId).blockingFirst();
641643

644+
itemListAdapter.clearStreamItemList();
645+
itemListAdapter.addItems(itemsToKeep);
646+
saveChanges();
647+
648+
final long videoCount = itemListAdapter.getItemsList().size();
649+
setVideoCount(videoCount);
650+
if (videoCount == 0) {
651+
showEmptyState();
652+
}
653+
//TODO: Do we have to show loading?
654+
//hideLoading();
642655
}
643656

644657
private void deleteItem(final PlaylistStreamEntry item) {

app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistManager.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,9 @@ public Flowable<List<PlaylistMetadataEntry>> getPlaylists() {
8686
return playlistStreamTable.getPlaylistMetadata().subscribeOn(Schedulers.io());
8787
}
8888

89-
public Flowable<List<PlaylistMetadataEntry>> removeDuplicateStreams() {
90-
// TODO: Delete Duplicates and rebuild the index
91-
// TODO: Rebuild the index
92-
return playlistStreamTable.getPlaylistMetadata().subscribeOn(Schedulers.io());
89+
public Flowable<List<PlaylistStreamEntry>> getDistinctPlaylistStreams(final long playlistId) {
90+
return playlistStreamTable
91+
.getStreamsWithoutDuplicates(playlistId).subscribeOn(Schedulers.io());
9392
}
9493

9594
public Flowable<List<PlaylistStreamEntry>> getPlaylistStreams(final long playlistId) {

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,8 @@
627627
<string name="remove_watched">Remove watched</string>
628628
<string name="remove_watched_popup_title">Remove watched videos?</string>
629629
<string name="remove_duplicates">Remove duplicates</string>
630+
<string name="remove_duplicates_title">Remove duplicates?</string>
631+
<string name="remove_duplicates_message">Do you want to remove all duplicate streams in this playlist?</string>
630632
<string name="remove_watched_popup_warning">Videos that have been watched before and after being added to the playlist will be removed.
631633
\nAre you sure\? This cannot be undone!</string>
632634
<string name="remove_watched_popup_yes_and_partially_watched_videos">Yes, and partially watched videos</string>

0 commit comments

Comments
 (0)