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
1 changed files with 29 additions and 14 deletions

View File

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