Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Commit

Permalink
Handle TMDb seasons not updating (#1910)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbanes authored Jun 26, 2024
1 parent b90d454 commit 949c113
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 188 deletions.
124 changes: 0 additions & 124 deletions core/base/src/commonMain/kotlin/app/tivi/extensions/Combine.kt

This file was deleted.

8 changes: 0 additions & 8 deletions core/base/src/commonMain/kotlin/app/tivi/extensions/LetIf.kt

This file was deleted.

14 changes: 7 additions & 7 deletions core/base/src/commonMain/kotlin/app/tivi/util/Result.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ package app.tivi.util

import kotlinx.coroutines.CancellationException

inline fun Result<*>.onException(
block: (Throwable) -> Unit,
) {
val e = exceptionOrNull()
when {
e is CancellationException -> throw e
e != null -> block(e)
inline fun <T, R> T.cancellableRunCatching(block: T.() -> R): Result<R> {
return try {
Result.success(block())
} catch (ce: CancellationException) {
throw ce
} catch (e: Throwable) {
Result.failure(e)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import app.tivi.data.util.inPast
import app.tivi.data.util.syncerForEntity
import app.tivi.inject.ApplicationScope
import app.tivi.util.Logger
import app.tivi.util.cancellableRunCatching
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.hours
import kotlinx.coroutines.CancellationException
Expand Down Expand Up @@ -49,7 +50,7 @@ class SeasonsEpisodesRepository(
private val tmdbEpisodeDataSource: TmdbEpisodeDataSource,
private val traktEpisodeWatchesDataSource: EpisodeWatchesDataSource,
private val traktAuthRepository: TraktAuthRepository,
logger: Logger,
private val logger: Logger,
) {
private val seasonSyncer = syncerForEntity(
entityDao = seasonsDao,
Expand Down Expand Up @@ -106,17 +107,27 @@ class SeasonsEpisodesRepository(
val traktDeferred = async { traktSeasonsDataSource.getSeasonsEpisodes(showId) }
val tmdbDeferred = async { tmdbSeasonsDataSource.getSeasonsEpisodes(showId) }

val trakt = traktDeferred.await().distinctBy { it.first.number }
val tmdb = tmdbDeferred.await().distinctBy { it.first.number }
val traktResult = cancellableRunCatching {
traktDeferred.await().distinctBy { it.first.number }
}.onFailure {
logger.i(it) { "Error whilst fetching seasons from Trakt" }
}.getOrDefault(emptyList())

trakt.associate { (traktSeason, traktEpisodes) ->
val tmdbResult = cancellableRunCatching {
tmdbDeferred.await().distinctBy { it.first.number }
}.onFailure {
logger.i(it) { "Error whilst fetching seasons from TMDb" }
}.getOrNull()

traktResult.associate { (traktSeason, traktEpisodes) ->
val localSeason = seasonsDao.seasonWithTraktId(traktSeason.traktId!!)
?: Season(showId = showId)

val mergedSeason = mergeSeason(
local = localSeason,
trakt = traktSeason,
tmdb = tmdb.firstOrNull { it.first.number == traktSeason.number }?.first
tmdb = tmdbResult
?.firstOrNull { it.first.number == traktSeason.number }?.first
?: Season.EMPTY,
)

Expand Down Expand Up @@ -156,25 +167,14 @@ class SeasonsEpisodesRepository(
traktEpisodeDataSource.getEpisode(season.showId, season.number!!, local.number!!)
}
val tmdbDeferred = async {
runCatching {
cancellableRunCatching {
tmdbEpisodeDataSource.getEpisode(season.showId, season.number!!, local.number!!)
}.getOrNull()
}

val trakt = try {
traktDeferred.await()
} catch (ce: CancellationException) {
throw ce
} catch (e: Exception) {
null
}
val tmdb = try {
tmdbDeferred.await()
} catch (ce: CancellationException) {
throw ce
} catch (e: Exception) {
null
}
val trakt = cancellableRunCatching { traktDeferred.await() }.getOrNull()
val tmdb = cancellableRunCatching { tmdbDeferred.await() }.getOrNull()

check(trakt != null || tmdb != null)

episodesDao.upsert(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import app.tivi.screens.RecommendedShowsScreen
import app.tivi.screens.ShowDetailsScreen
import app.tivi.screens.TrendingShowsScreen
import app.tivi.util.Logger
import app.tivi.util.onException
import com.slack.circuit.retained.collectAsRetainedState
import com.slack.circuit.runtime.CircuitContext
import com.slack.circuit.runtime.Navigator
Expand Down Expand Up @@ -104,7 +103,7 @@ class DiscoverPresenter(
page = UpdatePopularShows.Page.REFRESH,
forceRefresh = event.fromUser,
),
).onException { e ->
).onFailure { e ->
logger.i(e)
uiMessageManager.emitMessage(UiMessage(e))
}
Expand All @@ -115,7 +114,7 @@ class DiscoverPresenter(
page = UpdateTrendingShows.Page.REFRESH,
forceRefresh = event.fromUser,
),
).onException { e ->
).onFailure { e ->
logger.i(e)
uiMessageManager.emitMessage(UiMessage(e))
}
Expand All @@ -125,7 +124,7 @@ class DiscoverPresenter(
updateRecommendedShows.value.invoke(
UpdateRecommendedShows.Params(forceRefresh = event.fromUser),
).also { result ->
result.onException { e ->
result.onFailure { e ->
logger.i(e)
uiMessageManager.emitMessage(UiMessage(e))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import app.tivi.screens.EpisodeTrackScreen
import app.tivi.screens.ShowDetailsScreen
import app.tivi.screens.ShowSeasonsScreen
import app.tivi.util.Logger
import app.tivi.util.onException
import com.slack.circuit.retained.collectAsRetainedState
import com.slack.circuit.runtime.CircuitContext
import com.slack.circuit.runtime.Navigator
Expand Down Expand Up @@ -78,7 +77,7 @@ class EpisodeDetailsPresenter(
scope.launch {
updateEpisodeDetails.value.invoke(
UpdateEpisodeDetails.Params(screen.id, event.fromUser),
).onException { e ->
).onFailure { e ->
logger.i(e)
uiMessageManager.emitMessage(UiMessage(e))
}
Expand All @@ -95,7 +94,7 @@ class EpisodeDetailsPresenter(
scope.launch {
removeEpisodeWatches.value.invoke(
RemoveEpisodeWatches.Params(screen.id),
).onException { e ->
).onFailure { e ->
logger.i(e)
uiMessageManager.emitMessage(UiMessage(e))
}
Expand All @@ -106,7 +105,7 @@ class EpisodeDetailsPresenter(
scope.launch {
removeEpisodeWatch.value.invoke(
RemoveEpisodeWatch.Params(event.id),
).onException { e ->
).onFailure { e ->
logger.i(e)
uiMessageManager.emitMessage(UiMessage(e))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import app.tivi.domain.interactors.UpdateEpisodeDetails
import app.tivi.domain.observers.ObserveEpisodeDetails
import app.tivi.screens.EpisodeTrackScreen
import app.tivi.util.Logger
import app.tivi.util.onException
import com.slack.circuit.retained.collectAsRetainedState
import com.slack.circuit.runtime.CircuitContext
import com.slack.circuit.runtime.Navigator
Expand Down Expand Up @@ -90,7 +89,7 @@ class EpisodeTrackPresenter(
scope.launch {
updateEpisodeDetails.value.invoke(
UpdateEpisodeDetails.Params(screen.id, event.fromUser),
).onException { e ->
).onFailure { e ->
logger.i(e)
uiMessageManager.emitMessage(UiMessage(e))
}
Expand Down Expand Up @@ -133,7 +132,7 @@ class EpisodeTrackPresenter(
if (result.isSuccess) {
navigator.pop()
}
result.onException { e ->
result.onFailure { e ->
logger.i(e)
uiMessageManager.emitMessage(UiMessage(e))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import app.tivi.screens.LibraryScreen
import app.tivi.screens.ShowDetailsScreen
import app.tivi.settings.TiviPreferences
import app.tivi.util.Logger
import app.tivi.util.onException
import com.slack.circuit.retained.collectAsRetainedState
import com.slack.circuit.retained.rememberRetained
import com.slack.circuit.runtime.CircuitContext
Expand Down Expand Up @@ -111,7 +110,7 @@ class LibraryPresenter(
if (getTraktAuthState.value.invoke().getOrThrow() == TraktAuthState.LOGGED_IN) {
updateLibraryShows.value.invoke(
UpdateLibraryShows.Params(event.fromUser),
).onException { e ->
).onFailure { e ->
logger.i(e)
uiMessageManager.emitMessage(UiMessage(e))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import app.tivi.domain.interactors.SearchShows
import app.tivi.screens.SearchScreen
import app.tivi.screens.ShowDetailsScreen
import app.tivi.util.Logger
import app.tivi.util.onException
import com.slack.circuit.retained.rememberRetained
import com.slack.circuit.runtime.CircuitContext
import com.slack.circuit.runtime.Navigator
Expand Down Expand Up @@ -70,7 +69,7 @@ class SearchPresenter(
val result = searchShows.value.invoke(SearchShows.Params(query))
results = result.getOrDefault(emptyList())

result.onException { e ->
result.onFailure { e ->
logger.i(e)
uiMessageManager.emitMessage(UiMessage(e))
}
Expand Down
Loading

0 comments on commit 949c113

Please sign in to comment.