From f9443f74217350c44ca3e4b696108199ff2cf3a1 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Fri, 29 Jul 2022 06:40:55 +0530 Subject: [PATCH 1/2] Refactor removeWatchedStreams() in LocalPlaylistFragment. --- .../local/playlist/LocalPlaylistFragment.java | 56 +++++++------------ 1 file changed, 21 insertions(+), 35 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 11d54f1ef..2938fe0b5 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 @@ -11,6 +11,7 @@ import android.os.Parcelable; import android.text.InputType; import android.text.TextUtils; import android.util.Log; +import android.util.Pair; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -34,7 +35,6 @@ import org.schabi.newpipe.database.LocalItem; import org.schabi.newpipe.database.history.model.StreamHistoryEntry; import org.schabi.newpipe.database.playlist.PlaylistStreamEntry; import org.schabi.newpipe.database.stream.model.StreamEntity; -import org.schabi.newpipe.database.stream.model.StreamStateEntity; import org.schabi.newpipe.databinding.DialogEditTextBinding; import org.schabi.newpipe.databinding.LocalPlaylistHeaderBinding; import org.schabi.newpipe.databinding.PlaylistControlBinding; @@ -55,7 +55,6 @@ import org.schabi.newpipe.util.external_communication.ShareUtils; import java.util.ArrayList; import java.util.Collections; -import java.util.Iterator; import java.util.List; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; @@ -63,7 +62,6 @@ import java.util.stream.Collectors; import icepick.State; import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers; -import io.reactivex.rxjava3.core.Flowable; import io.reactivex.rxjava3.core.Single; import io.reactivex.rxjava3.disposables.CompositeDisposable; import io.reactivex.rxjava3.disposables.Disposable; @@ -309,7 +307,7 @@ public class LocalPlaylistFragment extends BaseLocalListFragment> getPlaylistObserver() { - return new Subscriber>() { + return new Subscriber<>() { @Override public void onSubscribe(final Subscription s) { showLoading(); @@ -395,31 +393,19 @@ public class LocalPlaylistFragment extends BaseLocalListFragment playlist) -> { - // Playlist data - final Iterator playlistIter = playlist.iterator(); - - // History data - final HistoryRecordManager recordManager = - new HistoryRecordManager(getContext()); - final Iterator historyIter = recordManager - .getStreamHistorySortedById().blockingFirst().iterator(); - + final var recordManager = new HistoryRecordManager(getContext()); + final var historyIdsFlowable = recordManager.getStreamHistorySortedById() + // already sorted by ^ getStreamHistorySortedById(), binary search can be used + .map(historyList -> historyList.stream().map(StreamHistoryEntry::getStreamId) + .collect(Collectors.toList())); + final var streamsFlowable = playlistManager.getPlaylistStreams(playlistId) + .zipWith(historyIdsFlowable, (playlist, historyStreamIds) -> { // Remove Watched, Functionality data final List notWatchedItems = new ArrayList<>(); boolean thumbnailVideoRemoved = false; - // already sorted by ^ getStreamHistorySortedById(), binary search can be used - final ArrayList historyStreamIds = new ArrayList<>(); - while (historyIter.hasNext()) { - historyStreamIds.add(historyIter.next().getStreamId()); - } - if (removePartiallyWatched) { - while (playlistIter.hasNext()) { - final PlaylistStreamEntry playlistItem = playlistIter.next(); + for (final var playlistItem : playlist) { final int indexInHistory = Collections.binarySearch(historyStreamIds, playlistItem.getStreamId()); @@ -432,14 +418,15 @@ public class LocalPlaylistFragment extends BaseLocalListFragment streamStatesIter = recordManager - .loadLocalStreamStateBatch(playlist).blockingGet().iterator(); + final var streamStates = recordManager.loadLocalStreamStateBatch(playlist) + .blockingGet(); + + for (int i = 0; i < playlist.size(); i++) { + final var playlistItem = playlist.get(i); + final var streamStateEntity = streamStates.get(i); - while (playlistIter.hasNext()) { - final PlaylistStreamEntry playlistItem = playlistIter.next(); final int indexInHistory = Collections.binarySearch(historyStreamIds, playlistItem.getStreamId()); - final StreamStateEntity streamStateEntity = streamStatesIter.next(); final long duration = playlistItem.toStreamInfoItem().getDuration(); if (indexInHistory < 0 || (streamStateEntity != null @@ -453,19 +440,18 @@ public class LocalPlaylistFragment extends BaseLocalListFragment(notWatchedItems, thumbnailVideoRemoved); + }); + disposables.add(streamsFlowable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(flow -> { - final List notWatchedItems = - (List) flow.blockingFirst(); - final boolean thumbnailVideoRemoved = (Boolean) flow.blockingLast(); + final List notWatchedItems = flow.first; + final boolean thumbnailVideoRemoved = flow.second; itemListAdapter.clearStreamItemList(); itemListAdapter.addItems(notWatchedItems); saveChanges(); - if (thumbnailVideoRemoved) { updateThumbnailUrl(); } From 6399e3950711e63d7fe4d5b299a8091d4327f4f4 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Sun, 7 Aug 2022 13:40:10 +0530 Subject: [PATCH 2/2] Remove from playlist only upon selecting the option and not afterwards. --- .../local/playlist/LocalPlaylistFragment.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 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 2938fe0b5..9ace11a46 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 @@ -394,12 +394,14 @@ public class LocalPlaylistFragment extends BaseLocalListFragment historyList.stream().map(StreamHistoryEntry::getStreamId) .collect(Collectors.toList())); - final var streamsFlowable = playlistManager.getPlaylistStreams(playlistId) - .zipWith(historyIdsFlowable, (playlist, historyStreamIds) -> { + final var streamsMaybe = playlistManager.getPlaylistStreams(playlistId) + .firstElement() + .zipWith(historyIdsMaybe, (playlist, historyStreamIds) -> { // Remove Watched, Functionality data final List notWatchedItems = new ArrayList<>(); boolean thumbnailVideoRemoved = false; @@ -418,8 +420,8 @@ public class LocalPlaylistFragment extends BaseLocalListFragment(notWatchedItems, thumbnailVideoRemoved); }); - disposables.add(streamsFlowable.subscribeOn(Schedulers.io()) + + disposables.add(streamsMaybe.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(flow -> { final List notWatchedItems = flow.first;