From d8ed2c850356de9c1aa1e4b3596745983b3d5344 Mon Sep 17 00:00:00 2001 From: Jared Fantaye Date: Tue, 28 Feb 2023 16:43:58 +0100 Subject: [PATCH] Refactoring removeDuplicates function and preventing concurrent calls. --- .../local/playlist/LocalPlaylistFragment.java | 43 +++++++++++++------ 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java b/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java index 36544a6a9..63cc1b57c 100644 --- a/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java +++ b/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java @@ -95,8 +95,8 @@ public class LocalPlaylistFragment extends BaseLocalListFragment showError(new ErrorInfo(throwable, UserAction.REQUESTED_BOOKMARK, "Removing watched videos, partially watched=" + removePartiallyWatched)))); } @@ -623,7 +625,6 @@ public class LocalPlaylistFragment extends BaseLocalListFragment itemsToKeep = playlistManager - .getDistinctPlaylistStreams(playlistId).blockingFirst(); + if (isRewritingPlaylist) { + return; + } + isRewritingPlaylist = true; + showLoading(); - itemListAdapter.clearStreamItemList(); - itemListAdapter.addItems(itemsToKeep); - setVideoCount(itemListAdapter.getItemsList().size()); + final var streamsMaybe = playlistManager + .getDistinctPlaylistStreams(playlistId).firstElement(); - saveChanges(); + + disposables.add(streamsMaybe.subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(itemsToKeep -> { + itemListAdapter.clearStreamItemList(); + itemListAdapter.addItems(itemsToKeep); + setVideoCount(itemListAdapter.getItemsList().size()); + saveChanges(); + + hideLoading(); + isRewritingPlaylist = false; + }, throwable -> showError(new ErrorInfo(throwable, UserAction.REQUESTED_BOOKMARK, + "Removing duplicated streams")))); } private void deleteItem(final PlaylistStreamEntry item) {