1
0
mirror of https://github.com/TeamNewPipe/NewPipe.git synced 2024-11-22 11:02:35 +01:00

Refactoring removeDuplicates function and preventing concurrent calls.

This commit is contained in:
Jared Fantaye 2023-02-28 16:43:58 +01:00
parent d5b54c85ed
commit d8ed2c8503

View File

@ -95,8 +95,8 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
private AtomicBoolean isLoadingComplete;
/* Has the playlist been modified (e.g. items reordered or deleted) */
private AtomicBoolean isModified;
/* Is the playlist currently being processed to remove watched videos */
private boolean isRemovingWatched = false;
/* Flag to prevent simultaneous rewrites of the playlist */
private boolean isRewritingPlaylist = false;
public static LocalPlaylistFragment getInstance(final long playlistId, final String name) {
final LocalPlaylistFragment instance = new LocalPlaylistFragment();
@ -353,7 +353,7 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
} else if (item.getItemId() == R.id.menu_item_rename_playlist) {
createRenameDialog();
} else if (item.getItemId() == R.id.menu_item_remove_watched) {
if (!isRemovingWatched) {
if (!isRewritingPlaylist) {
new AlertDialog.Builder(requireContext())
.setMessage(R.string.remove_watched_popup_warning)
.setTitle(R.string.remove_watched_popup_title)
@ -368,7 +368,9 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
.show();
}
} else if (item.getItemId() == R.id.menu_item_remove_duplicates) {
openRemoveDuplicatesDialog();
if (!isRewritingPlaylist) {
openRemoveDuplicatesDialog();
}
} else {
return super.onOptionsItemSelected(item);
}
@ -390,10 +392,10 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
}
public void removeWatchedStreams(final boolean removePartiallyWatched) {
if (isRemovingWatched) {
if (isRewritingPlaylist) {
return;
}
isRemovingWatched = true;
isRewritingPlaylist = true;
showLoading();
final var recordManager = new HistoryRecordManager(getContext());
@ -469,7 +471,7 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
}
hideLoading();
isRemovingWatched = false;
isRewritingPlaylist = false;
}, throwable -> showError(new ErrorInfo(throwable, UserAction.REQUESTED_BOOKMARK,
"Removing watched videos, partially watched=" + removePartiallyWatched))));
}
@ -623,7 +625,6 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
changeThumbnailUrl(newThumbnailUrl);
}
private void openRemoveDuplicatesDialog() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
@ -638,14 +639,28 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
}
private void removeDuplicatesInPlaylist() {
final List<PlaylistStreamEntry> 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) {