feat: throttle push watch progress while playback

This commit is contained in:
tapframe 2026-05-30 12:35:51 +05:30
parent 614ab1f523
commit 43b35310cf
5 changed files with 20 additions and 11 deletions

View file

@ -21,7 +21,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
private const val FOREGROUND_PULL_DELAY_MS = 2500L
private const val FOREGROUND_PULL_MIN_INTERVAL_MS = 60_000L
private const val FOREGROUND_PULL_MIN_INTERVAL_MS = 30 * 60_000L
object SyncManager {
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)

View file

@ -1857,6 +1857,7 @@ fun PlayerScreen(
WatchProgressRepository.upsertPlaybackProgress(
session = playbackSession,
snapshot = playbackSnapshot,
syncRemote = false,
)
}

View file

@ -147,13 +147,14 @@ object WatchedRepository {
markWatched(items = items, traktHistorySync = WatchedTraktHistorySync.Mirror)
}
internal fun markWatchedFromPlaybackCompletion(item: WatchedItem) {
markWatched(items = listOf(item), traktHistorySync = WatchedTraktHistorySync.Skip)
internal fun markWatchedFromPlaybackCompletion(item: WatchedItem, syncRemote: Boolean = true) {
markWatched(items = listOf(item), traktHistorySync = WatchedTraktHistorySync.Skip, syncRemote = syncRemote)
}
private fun markWatched(
items: Collection<WatchedItem>,
traktHistorySync: WatchedTraktHistorySync,
syncRemote: Boolean = true,
) {
ensureLoaded()
if (items.isEmpty()) return
@ -167,7 +168,9 @@ object WatchedRepository {
}
publish()
persist()
pushMarksToServer(timestampedItems, traktHistorySync)
if (syncRemote) {
pushMarksToServer(timestampedItems, traktHistorySync)
}
}
fun unmarkWatched(item: WatchedItem) {

View file

@ -117,7 +117,7 @@ object WatchingActions {
)
}
fun onProgressEntryUpdated(entry: WatchProgressEntry) {
fun onProgressEntryUpdated(entry: WatchProgressEntry, syncRemote: Boolean = true) {
if (!entry.isCompleted) return
val watchedItem = WatchedItem(
@ -129,9 +129,9 @@ object WatchingActions {
episode = entry.episodeNumber,
markedAtEpochMs = entry.lastUpdatedEpochMs,
)
WatchedRepository.markWatchedFromPlaybackCompletion(watchedItem)
WatchedRepository.markWatchedFromPlaybackCompletion(watchedItem, syncRemote = syncRemote)
if (!entry.isEpisode) return
if (!syncRemote || !entry.isEpisode) return
actionScope.launch {
val meta = runCatching {
MetaDetailsRepository.fetch(

View file

@ -419,17 +419,19 @@ object WatchProgressRepository {
fun upsertPlaybackProgress(
session: WatchProgressPlaybackSession,
snapshot: PlayerPlaybackSnapshot,
syncRemote: Boolean = true,
) {
ensureLoaded()
upsert(session = session, snapshot = snapshot, persist = true)
upsert(session = session, snapshot = snapshot, persist = true, syncRemote = syncRemote)
}
fun flushPlaybackProgress(
session: WatchProgressPlaybackSession,
snapshot: PlayerPlaybackSnapshot,
syncRemote: Boolean = true,
) {
ensureLoaded()
upsert(session = session, snapshot = snapshot, persist = true)
upsert(session = session, snapshot = snapshot, persist = true, syncRemote = syncRemote)
}
fun clearProgress(videoId: String) {
@ -561,6 +563,7 @@ object WatchProgressRepository {
session: WatchProgressPlaybackSession,
snapshot: PlayerPlaybackSnapshot,
persist: Boolean,
syncRemote: Boolean,
) {
val positionMs = snapshot.positionMs.coerceAtLeast(0L)
val durationMs = snapshot.durationMs.coerceAtLeast(0L)
@ -613,9 +616,11 @@ object WatchProgressRepository {
if (entry.poster.isNullOrBlank() || entry.background.isNullOrBlank()) {
resolveRemoteMetadata()
}
pushScrobbleToServer(entry)
if (syncRemote) {
pushScrobbleToServer(entry)
}
if (shouldCascadeCompletedProgressToWatchedHistory(entry, useTraktProgress)) {
WatchingActions.onProgressEntryUpdated(entry)
WatchingActions.onProgressEntryUpdated(entry, syncRemote = syncRemote)
}
}