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

Merge pull request #8706 from Isira-Seneviratne/Improve_LocalPlaylistFragment

Refactor removeWatchedStreams() in LocalPlaylistFragment.
This commit is contained in:
Stypox 2022-10-30 22:03:39 +01:00 committed by GitHub
commit 25795c3a96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,6 +11,7 @@ import android.os.Parcelable;
import android.text.InputType; import android.text.InputType;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.util.Pair;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
@ -35,7 +36,6 @@ import org.schabi.newpipe.database.LocalItem;
import org.schabi.newpipe.database.history.model.StreamHistoryEntry; import org.schabi.newpipe.database.history.model.StreamHistoryEntry;
import org.schabi.newpipe.database.playlist.PlaylistStreamEntry; import org.schabi.newpipe.database.playlist.PlaylistStreamEntry;
import org.schabi.newpipe.database.stream.model.StreamEntity; 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.DialogEditTextBinding;
import org.schabi.newpipe.databinding.LocalPlaylistHeaderBinding; import org.schabi.newpipe.databinding.LocalPlaylistHeaderBinding;
import org.schabi.newpipe.databinding.PlaylistControlBinding; import org.schabi.newpipe.databinding.PlaylistControlBinding;
@ -56,7 +56,6 @@ import org.schabi.newpipe.util.external_communication.ShareUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
@ -64,7 +63,6 @@ import java.util.stream.Collectors;
import icepick.State; import icepick.State;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers; import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.Single; import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.disposables.CompositeDisposable; import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.disposables.Disposable; import io.reactivex.rxjava3.disposables.Disposable;
@ -310,7 +308,7 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
private Subscriber<List<PlaylistStreamEntry>> getPlaylistObserver() { private Subscriber<List<PlaylistStreamEntry>> getPlaylistObserver() {
return new Subscriber<List<PlaylistStreamEntry>>() { return new Subscriber<>() {
@Override @Override
public void onSubscribe(final Subscription s) { public void onSubscribe(final Subscription s) {
showLoading(); showLoading();
@ -396,31 +394,21 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
isRemovingWatched = true; isRemovingWatched = true;
showLoading(); showLoading();
disposables.add(playlistManager.getPlaylistStreams(playlistId) final var recordManager = new HistoryRecordManager(getContext());
.subscribeOn(Schedulers.io()) final var historyIdsMaybe = recordManager.getStreamHistorySortedById()
.map((List<PlaylistStreamEntry> playlist) -> { .firstElement()
// Playlist data // already sorted by ^ getStreamHistorySortedById(), binary search can be used
final Iterator<PlaylistStreamEntry> playlistIter = playlist.iterator(); .map(historyList -> historyList.stream().map(StreamHistoryEntry::getStreamId)
.collect(Collectors.toList()));
// History data final var streamsMaybe = playlistManager.getPlaylistStreams(playlistId)
final HistoryRecordManager recordManager = .firstElement()
new HistoryRecordManager(getContext()); .zipWith(historyIdsMaybe, (playlist, historyStreamIds) -> {
final Iterator<StreamHistoryEntry> historyIter = recordManager
.getStreamHistorySortedById().blockingFirst().iterator();
// Remove Watched, Functionality data // Remove Watched, Functionality data
final List<PlaylistStreamEntry> notWatchedItems = new ArrayList<>(); final List<PlaylistStreamEntry> notWatchedItems = new ArrayList<>();
boolean thumbnailVideoRemoved = false; boolean thumbnailVideoRemoved = false;
// already sorted by ^ getStreamHistorySortedById(), binary search can be used
final ArrayList<Long> historyStreamIds = new ArrayList<>();
while (historyIter.hasNext()) {
historyStreamIds.add(historyIter.next().getStreamId());
}
if (removePartiallyWatched) { if (removePartiallyWatched) {
while (playlistIter.hasNext()) { for (final var playlistItem : playlist) {
final PlaylistStreamEntry playlistItem = playlistIter.next();
final int indexInHistory = Collections.binarySearch(historyStreamIds, final int indexInHistory = Collections.binarySearch(historyStreamIds,
playlistItem.getStreamId()); playlistItem.getStreamId());
@ -433,14 +421,15 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
} }
} }
} else { } else {
final Iterator<StreamStateEntity> streamStatesIter = recordManager final var streamStates = recordManager
.loadLocalStreamStateBatch(playlist).blockingGet().iterator(); .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, final int indexInHistory = Collections.binarySearch(historyStreamIds,
playlistItem.getStreamId()); playlistItem.getStreamId());
final StreamStateEntity streamStateEntity = streamStatesIter.next();
final long duration = playlistItem.toStreamInfoItem().getDuration(); final long duration = playlistItem.toStreamInfoItem().getDuration();
if (indexInHistory < 0 || (streamStateEntity != null if (indexInHistory < 0 || (streamStateEntity != null
@ -454,19 +443,19 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
} }
} }
return Flowable.just(notWatchedItems, thumbnailVideoRemoved); return new Pair<>(notWatchedItems, thumbnailVideoRemoved);
}) });
disposables.add(streamsMaybe.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(flow -> { .subscribe(flow -> {
final List<PlaylistStreamEntry> notWatchedItems = final List<PlaylistStreamEntry> notWatchedItems = flow.first;
(List<PlaylistStreamEntry>) flow.blockingFirst(); final boolean thumbnailVideoRemoved = flow.second;
final boolean thumbnailVideoRemoved = (Boolean) flow.blockingLast();
itemListAdapter.clearStreamItemList(); itemListAdapter.clearStreamItemList();
itemListAdapter.addItems(notWatchedItems); itemListAdapter.addItems(notWatchedItems);
saveChanges(); saveChanges();
if (thumbnailVideoRemoved) { if (thumbnailVideoRemoved) {
updateThumbnailUrl(); updateThumbnailUrl();
} }