Skip to content
Permalink
Browse files

Fix Followed shows not fetching episode watches after DB upgrade

  • Loading branch information...
chrisbanes committed Jun 5, 2019
1 parent 56b8887 commit 53e0b10d0dbec5b616577ec7269d881f73af827d
@@ -18,6 +18,7 @@ package app.tivi.home.followed

import androidx.lifecycle.viewModelScope
import androidx.paging.PagedList
import app.tivi.data.entities.RefreshType
import app.tivi.data.entities.SortOption
import app.tivi.data.resultentities.FollowedShowEntryWithShow
import app.tivi.extensions.debounceLoading
@@ -127,7 +128,7 @@ class FollowedViewModel @AssistedInject constructor(
private fun refreshFollowed(fromUserInteraction: Boolean) {
loadingState.addLoader()
viewModelScope.launchInteractor(updateFollowedShows,
UpdateFollowedShows.Params(fromUserInteraction, UpdateFollowedShows.RefreshType.QUICK))
UpdateFollowedShows.Params(fromUserInteraction, RefreshType.QUICK))
.invokeOnCompletion { loadingState.removeLoader() }
}

@@ -0,0 +1,19 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package app.tivi.data.entities

enum class RefreshType { QUICK, FULL }
@@ -20,6 +20,7 @@ import app.tivi.data.entities.ActionDate
import app.tivi.data.entities.Episode
import app.tivi.data.entities.EpisodeWatchEntry
import app.tivi.data.entities.PendingAction
import app.tivi.data.entities.RefreshType
import app.tivi.data.entities.Season
import app.tivi.data.entities.Success
import app.tivi.data.instantInPast
@@ -103,6 +104,34 @@ class SeasonsEpisodesRepository @Inject constructor(
seasonsEpisodesStore.save(mergeEpisode(local, trakt, tmdb))
}

suspend fun updateShowEpisodeWatchesIfNeeded(
showId: Long,
refreshType: RefreshType,
forceRefresh: Boolean,
lastUpdated: OffsetDateTime? = null
) {
if (refreshType == RefreshType.QUICK) {
// If we have a lastUpdated time and we've already fetched the watched episodes, we can try
// and do a delta fetch
if (lastUpdated != null && episodeWatchLastLastRequestStore.hasBeenRequested(showId)) {
if (forceRefresh || needShowEpisodeWatchesSync(showId, lastUpdated.toInstant())) {
updateShowEpisodeWatches(showId, lastUpdated.plusSeconds(1))
}
} else {
// We don't have a trakt date/time to use as a delta, so we'll do a full refresh.
// If the user hasn't watched the show, this should be empty anyway
if (forceRefresh || needShowEpisodeWatchesSync(showId)) {
updateShowEpisodeWatches(showId)
}
}
} else if (refreshType == RefreshType.FULL) {
// A full refresh is requested, so we pull down all history
if (forceRefresh || needShowEpisodeWatchesSync(showId)) {
updateShowEpisodeWatches(showId)
}
}
}

suspend fun updateShowEpisodeWatches(showId: Long, since: OffsetDateTime? = null) {
if (traktAuthState.get() == TraktAuthState.LOGGED_IN) {
if (since != null) {
@@ -35,7 +35,7 @@ android {

dependencies {
implementation project(":base")
implementation project(":data")
api project(":data")

api Libs.AndroidX.Paging.common
implementation Libs.AndroidX.Paging.rxjava2
@@ -33,17 +33,17 @@ class UpdateFollowedShowSeasonData @Inject constructor(
override suspend fun invoke(params: Params) {
if (followedShowsRepository.isShowFollowed(params.showId)) {
// Then update the seasons/episodes
if (params.forceLoad || seasonsEpisodesRepository.needShowSeasonsUpdate(params.showId)) {
if (params.forceRefresh || seasonsEpisodesRepository.needShowSeasonsUpdate(params.showId)) {
seasonsEpisodesRepository.updateSeasonsEpisodes(params.showId)
}
// Finally update any watched progress
if (params.forceLoad || seasonsEpisodesRepository.needShowEpisodeWatchesSync(params.showId)) {
if (params.forceRefresh || seasonsEpisodesRepository.needShowEpisodeWatchesSync(params.showId)) {
seasonsEpisodesRepository.syncEpisodeWatchesForShow(params.showId)
}
} else {
seasonsEpisodesRepository.removeShowSeasonData(params.showId)
}
}

data class Params(val showId: Long, val forceLoad: Boolean)
data class Params(val showId: Long, val forceRefresh: Boolean)
}
@@ -16,6 +16,7 @@

package app.tivi.interactors

import app.tivi.data.entities.RefreshType
import app.tivi.data.instantInPast
import app.tivi.data.repositories.episodes.SeasonsEpisodesRepository
import app.tivi.data.repositories.followedshows.FollowedShowsRepository
@@ -58,36 +59,16 @@ class UpdateFollowedShows @Inject constructor(
seasonEpisodeRepository.updateSeasonsEpisodes(it.showId)
}

// And sync the episode watches
if (params.type == RefreshType.QUICK) {
val showWatchedEntry = watchedShowsRepository.getWatchedShow(it.showId)
if (showWatchedEntry != null) {
// TODO: We should really use last_updated_at. Waiting on trakt-java support in
seasonEpisodeRepository.updateShowEpisodeWatchesIfNeeded(
it.showId,
params.type,
params.forceRefresh,
// TODO: We should use last_updated_at. Waiting on trakt-java support in
// https://github.com/UweTrottmann/trakt-java/pull/106
val lastWatchUpdate = showWatchedEntry.lastWatched

if (params.forceRefresh || seasonEpisodeRepository.needShowEpisodeWatchesSync(
it.showId, lastWatchUpdate.toInstant())) {
seasonEpisodeRepository.updateShowEpisodeWatches(it.showId,
lastWatchUpdate.plusSeconds(1))
}
} else {
// We don't have a trakt date/time to use as a delta, so we'll do a full refresh.
// If the user hasn't watched the show, this should be empty anyway
if (params.forceRefresh || seasonEpisodeRepository.needShowEpisodeWatchesSync(it.showId)) {
seasonEpisodeRepository.updateShowEpisodeWatches(it.showId)
}
}
} else if (params.type == RefreshType.FULL) {
// A full refresh is requested, so we pull down all history
if (params.forceRefresh || seasonEpisodeRepository.needShowEpisodeWatchesSync(it.showId)) {
seasonEpisodeRepository.updateShowEpisodeWatches(it.showId)
}
}
watchedShowsRepository.getWatchedShow(it.showId)?.lastWatched
)
}
}

data class Params(val forceRefresh: Boolean, val type: RefreshType)

enum class RefreshType { QUICK, FULL }
}
@@ -19,6 +19,7 @@ package app.tivi.tasks
import android.content.Context
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import app.tivi.data.entities.RefreshType
import app.tivi.interactors.UpdateFollowedShows
import app.tivi.tasks.inject.ChildWorkerFactory
import app.tivi.util.Logger
@@ -42,7 +43,7 @@ class SyncAllFollowedShows @AssistedInject constructor(

override suspend fun doWork(): Result {
logger.d("$TAG worker running")
updateFollowedShows(UpdateFollowedShows.Params(true, UpdateFollowedShows.RefreshType.FULL))
updateFollowedShows(UpdateFollowedShows.Params(true, RefreshType.FULL))
return Result.success()
}

0 comments on commit 53e0b10

Please sign in to comment.
You can’t perform that action at this time.