mirror of
https://github.com/tapframe/NuvioStreaming.git
synced 2026-07-27 06:52:18 +00:00
Use snapshot sync on mobile startup
This commit is contained in:
parent
dccf264a27
commit
dd0debb8b9
2 changed files with 57 additions and 2 deletions
|
|
@ -55,11 +55,11 @@ object SyncManager {
|
|||
.onFailure { log.e(it) { "Library pull failed" } }
|
||||
}
|
||||
launch {
|
||||
runCatching { WatchProgressRepository.pullFromServer(profileId) }
|
||||
runCatching { WatchProgressRepository.forceSnapshotRefreshFromServer(profileId) }
|
||||
.onFailure { log.e(it) { "WatchProgress pull failed" } }
|
||||
}
|
||||
launch {
|
||||
runCatching { WatchedRepository.pullFromServer(profileId) }
|
||||
runCatching { WatchedRepository.forceSnapshotRefreshFromServer(profileId) }
|
||||
.onFailure { log.e(it) { "Watched pull failed" } }
|
||||
}
|
||||
launch {
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ import com.nuvio.app.features.watching.sync.SupabaseWatchedSyncAdapter
|
|||
import com.nuvio.app.features.watching.sync.TraktWatchedSyncAdapter
|
||||
import com.nuvio.app.features.watching.sync.WatchedDeltaEvent
|
||||
import com.nuvio.app.features.watching.sync.WatchedSyncAdapter
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
|
|
@ -171,6 +172,60 @@ object WatchedRepository {
|
|||
}
|
||||
}
|
||||
|
||||
suspend fun forceSnapshotRefreshFromServer(profileId: Int) {
|
||||
TraktAuthRepository.ensureLoaded()
|
||||
TraktSettingsRepository.ensureLoaded()
|
||||
val operationGeneration = activeOperationGeneration(profileId) ?: run {
|
||||
log.d { "Skipping watched snapshot pull for inactive profile $profileId" }
|
||||
return
|
||||
}
|
||||
val pullStartedEpochMs = WatchedClock.nowEpochMs()
|
||||
val localBeforePull = itemsByKey.values
|
||||
.map(WatchedItem::normalizedMarkedAt)
|
||||
.toList()
|
||||
val lastPushEpochMs = lastSuccessfulPushEpochMs
|
||||
runCatching {
|
||||
if (shouldUseTraktWatchedSync()) {
|
||||
pullFullFromAdapter(
|
||||
adapter = TraktWatchedSyncAdapter,
|
||||
profileId = profileId,
|
||||
localBeforePull = localBeforePull,
|
||||
lastPushEpochMs = lastPushEpochMs,
|
||||
pullStartedEpochMs = pullStartedEpochMs,
|
||||
resetDeltaState = true,
|
||||
operationGeneration = operationGeneration,
|
||||
)
|
||||
return@runCatching
|
||||
}
|
||||
|
||||
val cursorBeforeSnapshot = try {
|
||||
syncAdapter.getDeltaCursor(profileId)
|
||||
} catch (error: CancellationException) {
|
||||
throw error
|
||||
} catch (_: Throwable) {
|
||||
null
|
||||
}
|
||||
pullFullFromAdapter(
|
||||
adapter = syncAdapter,
|
||||
profileId = profileId,
|
||||
localBeforePull = localBeforePull,
|
||||
lastPushEpochMs = lastPushEpochMs,
|
||||
pullStartedEpochMs = pullStartedEpochMs,
|
||||
resetDeltaState = cursorBeforeSnapshot == null,
|
||||
operationGeneration = operationGeneration,
|
||||
)
|
||||
if (!isActiveOperation(profileId, operationGeneration)) return@runCatching
|
||||
if (cursorBeforeSnapshot != null) {
|
||||
deltaCursorEventId = cursorBeforeSnapshot
|
||||
deltaInitialized = true
|
||||
persist()
|
||||
}
|
||||
}.onFailure { e ->
|
||||
if (e is CancellationException) throw e
|
||||
log.e(e) { "Failed to pull watched items snapshot from server" }
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun pullFullFromAdapter(
|
||||
adapter: WatchedSyncAdapter,
|
||||
profileId: Int,
|
||||
|
|
|
|||
Loading…
Reference in a new issue