Skip to content

Commit

Permalink
Optimised 'removeWatchedStreams'
Browse files Browse the repository at this point in the history
Removed merge mistake
Reordered code
Refactored 'removeWatchedWorker' to 'removeWatchedDisposable'
  • Loading branch information
GradyTheDev committed Mar 18, 2020
1 parent ff255a0 commit 6223c36
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
private View headerPopupButton;
private View headerBackgroundButton;

private Disposable removeWatchedWorker;
private Disposable removeWatchedDisposable;

@State
protected Long playlistId;
Expand Down Expand Up @@ -273,12 +273,12 @@ public void onDestroy() {
if (debouncedSaveSignal != null) debouncedSaveSignal.onComplete();
if (disposables != null) disposables.dispose();

if (removeWatchedWorker != null) removeWatchedWorker.dispose();
removeWatchedWorker = null;
if (removeWatchedDisposable != null) removeWatchedDisposable.dispose();

debouncedSaveSignal = null;
playlistManager = null;
disposables = null;
removeWatchedDisposable = null;

isLoadingComplete = null;
isModified = null;
Expand Down Expand Up @@ -336,49 +336,55 @@ public boolean onOptionsItemSelected(MenuItem item) {
public void removeWatchedStreams() {
showLoading();

if (removeWatchedWorker != null) {
if (removeWatchedDisposable != null) {
// In case this is called twice
removeWatchedWorker.dispose();
removeWatchedDisposable.dispose();
}

removeWatchedWorker = Flowable.just(playlistManager.getPlaylistStreams(playlistId).blockingFirst())
removeWatchedDisposable = Flowable.just(playlistManager.getPlaylistStreams(playlistId).blockingFirst())
.subscribeOn(Schedulers.newThread())
.map((@NonNull List<PlaylistStreamEntry> playlist) -> {
List<PlaylistStreamEntry> localItems = new ArrayList<>();
boolean thumbnailVideoRemoved = false;
Long removedItemCount = 0l;

HistoryRecordManager recordManager = new HistoryRecordManager(getContext());
Iterator<StreamHistoryEntry> it_history;
StreamHistoryEntry history_item;

Iterator<PlaylistStreamEntry> it_playlist = playlist.iterator();
PlaylistStreamEntry playlist_item = null;

boolean isNonDuplicate;

while (it_playlist.hasNext()) {
playlist_item = it_playlist.next();
Iterator<StreamHistoryEntry> it_history = recordManager.getStreamHistory().blockingFirst().iterator();
ArrayList<Long> history_streamIds = new ArrayList<>();

it_history = recordManager.getStreamHistory().blockingFirst().iterator();
while(it_history.hasNext())
{
history_streamIds.add(it_history.next().getStreamId());
}

while(it_playlist.hasNext())
{
playlist_item = it_playlist.next();
isNonDuplicate = true;
while (it_history.hasNext()) {
history_item = it_history.next();
if (history_item.getStreamId() == playlist_item.getStreamId()) {

for (long history_id : history_streamIds) {
if (history_id == playlist_item.getStreamId()) {
isNonDuplicate = false;
break;
}
}

if (isNonDuplicate) {
localItems.add(playlist_item);
} else {
removedItemCount++;
if (playlistManager.getPlaylistThumbnail(playlistId).equals(playlist_item.getStreamEntity().getThumbnailUrl())) {
if (!thumbnailVideoRemoved && playlistManager.getPlaylistThumbnail(playlistId).equals(playlist_item.getStreamEntity().getThumbnailUrl())) {
thumbnailVideoRemoved = true;
}
}
}

return Flowable.just(localItems, removedItemCount, thumbnailVideoRemoved);
}
)
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,6 @@
<string name="app_language_title">App language</string>
<string name="systems_language">System default</string>
<string name="remove_watched">Remove watched</string>
<string name="dynamic_seek_duration_description">%s seconds</string>
<string name="new_seek_duration_toast">Due to ExoPlayer constraints the seek duration was set to %d seconds</string>
<plurals name="dynamic_seek_duration_description">
<item quantity="one">%s second</item>
Expand Down

0 comments on commit 6223c36

Please sign in to comment.