From 7fd2ebc25293bc0f083e65de23c082ce703cf359 Mon Sep 17 00:00:00 2001 From: Nathan Schulzke Date: Mon, 26 Jul 2021 20:51:41 -0600 Subject: [PATCH] Add ability to mark an item as played --- .../schabi/newpipe/local/feed/FeedFragment.kt | 6 ++-- .../local/history/HistoryRecordManager.java | 36 +++++++++++++++++++ .../newpipe/util/StreamDialogEntry.java | 13 ++++++- app/src/main/res/values/strings.xml | 1 + 4 files changed, 53 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt b/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt index 03d39b13c..ab32e5fbb 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt @@ -331,7 +331,8 @@ class FeedFragment : BaseStateFragment() { StreamDialogEntry.start_here_on_background, StreamDialogEntry.append_playlist, StreamDialogEntry.share, - StreamDialogEntry.open_in_browser + StreamDialogEntry.open_in_browser, + StreamDialogEntry.mark_as_played ) ) } else { @@ -341,7 +342,8 @@ class FeedFragment : BaseStateFragment() { StreamDialogEntry.start_here_on_popup, StreamDialogEntry.append_playlist, StreamDialogEntry.share, - StreamDialogEntry.open_in_browser + StreamDialogEntry.open_in_browser, + StreamDialogEntry.mark_as_played ) ) } diff --git a/app/src/main/java/org/schabi/newpipe/local/history/HistoryRecordManager.java b/app/src/main/java/org/schabi/newpipe/local/history/HistoryRecordManager.java index 38ebe504e..3d151bad8 100644 --- a/app/src/main/java/org/schabi/newpipe/local/history/HistoryRecordManager.java +++ b/app/src/main/java/org/schabi/newpipe/local/history/HistoryRecordManager.java @@ -42,6 +42,7 @@ import org.schabi.newpipe.database.stream.model.StreamEntity; import org.schabi.newpipe.database.stream.model.StreamStateEntity; import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.stream.StreamInfo; +import org.schabi.newpipe.extractor.stream.StreamInfoItem; import org.schabi.newpipe.player.playqueue.PlayQueueItem; import java.time.OffsetDateTime; @@ -81,6 +82,41 @@ public class HistoryRecordManager { // Watch History /////////////////////////////////////////////////////// + public Maybe markAsPlayed(final StreamInfoItem info) { + if (!isStreamHistoryEnabled()) { + return Maybe.empty(); + } + + final OffsetDateTime currentTime = OffsetDateTime.now(ZoneOffset.UTC); + return Maybe.fromCallable(() -> database.runInTransaction(() -> { + final long streamId = streamTable.upsert(new StreamEntity(info)); + + final List states = streamStateTable.getState(streamId) + .blockingFirst(); + if (!states.isEmpty()) { + final StreamStateEntity entity = states.get(0); + entity.setProgressMillis(info.getDuration() * 1000); + streamStateTable.update(entity); + } else { + final StreamStateEntity entity = new StreamStateEntity( + streamId, + info.getDuration() * 1000 + ); + streamStateTable.insert(entity); + } + + final StreamHistoryEntity latestEntry = streamHistoryTable.getLatestEntry(streamId); + if (latestEntry != null) { + streamHistoryTable.delete(latestEntry); + latestEntry.setAccessDate(currentTime); + latestEntry.setRepeatCount(latestEntry.getRepeatCount() + 1); + return streamHistoryTable.insert(latestEntry); + } else { + return streamHistoryTable.insert(new StreamHistoryEntity(streamId, currentTime)); + } + })).subscribeOn(Schedulers.io()); + } + public Maybe onViewed(final StreamInfo info) { if (!isStreamHistoryEnabled()) { return Maybe.empty(); diff --git a/app/src/main/java/org/schabi/newpipe/util/StreamDialogEntry.java b/app/src/main/java/org/schabi/newpipe/util/StreamDialogEntry.java index 8bfd428b8..828e4ebfc 100644 --- a/app/src/main/java/org/schabi/newpipe/util/StreamDialogEntry.java +++ b/app/src/main/java/org/schabi/newpipe/util/StreamDialogEntry.java @@ -9,6 +9,7 @@ import org.schabi.newpipe.R; import org.schabi.newpipe.extractor.stream.StreamInfoItem; import org.schabi.newpipe.local.dialog.PlaylistAppendDialog; import org.schabi.newpipe.local.dialog.PlaylistCreationDialog; +import org.schabi.newpipe.local.history.HistoryRecordManager; import org.schabi.newpipe.player.MainPlayer; import org.schabi.newpipe.player.helper.PlayerHolder; import org.schabi.newpipe.player.playqueue.SinglePlayQueue; @@ -18,6 +19,8 @@ import org.schabi.newpipe.util.external_communication.ShareUtils; import java.util.Collections; import java.util.List; +import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers; + import static org.schabi.newpipe.player.MainPlayer.PlayerType.AUDIO; import static org.schabi.newpipe.player.MainPlayer.PlayerType.POPUP; @@ -92,9 +95,17 @@ public enum StreamDialogEntry { item.getThumbnailUrl())), open_in_browser(R.string.open_in_browser, (fragment, item) -> - ShareUtils.openUrlInBrowser(fragment.getContext(), item.getUrl())); + ShareUtils.openUrlInBrowser(fragment.getContext(), item.getUrl())), + mark_as_played(R.string.mark_as_played, (fragment, item) -> { + new HistoryRecordManager(fragment.getContext()) + .markAsPlayed(item) + .onErrorComplete() + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(); + }); + /////////////// // variables // /////////////// diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f6d0246dd..f8c7f4515 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -9,6 +9,7 @@ Cancel https://f-droid.org/repository/browse/?fdfilter=vlc&fdid=org.videolan.vlc Open in browser + Mark as played Open in popup mode Open with Share