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

Commit

Permalink
Fix related shows not loading correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbanes committed Jun 22, 2020
1 parent 974eb77 commit bcefed4
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 28 deletions.
1 change: 0 additions & 1 deletion base/src/main/java/app/tivi/base/InvokeStatus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package app.tivi.base

sealed class InvokeStatus
object InvokeIdle : InvokeStatus()
object InvokeStarted : InvokeStatus()
object InvokeSuccess : InvokeStatus()
data class InvokeError(val throwable: Throwable) : InvokeStatus()
3 changes: 2 additions & 1 deletion common-ui-view/src/main/java/app/tivi/ReduxViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
Expand Down Expand Up @@ -78,7 +79,7 @@ abstract class ReduxViewModel<S>(
}

private fun <A> selectSubscribe(prop1: KProperty1<S, A>): Flow<A> {
return state.map { prop1.get(it) }
return state.map { prop1.get(it) }.distinctUntilChanged()
}

protected suspend fun setState(reducer: S.() -> S) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ApplicationComponent
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import javax.inject.Singleton

typealias ShowStore = Store<Long, TiviShow>
Expand All @@ -41,28 +39,17 @@ object ShowStoreModule {
lastRequestStore: ShowLastRequestStore,
traktShowDataSource: TraktShowDataSource
): ShowStore {
return StoreBuilder.fromNonFlow { showId: Long ->
val localShow = showDao.getShowWithId(showId)
?: throw IllegalArgumentException("No show with id $showId in database")

coroutineScope {
val traktResult = async {
traktShowDataSource.getShow(localShow)
}

// Update our last request timestamp
if (traktResult is Success<*>) {
lastRequestStore.updateLastRequest(showId)
}

traktResult.await().get() ?: TiviShow.EMPTY_SHOW
}
return StoreBuilder.fromNonFlow { id: Long ->
traktShowDataSource.getShow(showDao.getShowWithIdOrThrow(id))
.also { if (it is Success<*>) lastRequestStore.updateLastRequest(id) }
.getOrThrow()
}.persister(
reader = showDao::getShowWithIdFlow,
writer = { id, response ->
showDao.withTransaction {
val local = showDao.getShowWithId(id) ?: TiviShow.EMPTY_SHOW
showDao.insertOrUpdate(mergeShows(local = local, trakt = response))
showDao.insertOrUpdate(
mergeShows(local = showDao.getShowWithIdOrThrow(id), trakt = response)
)
}
},
delete = showDao::delete,
Expand Down
9 changes: 7 additions & 2 deletions data/src/main/java/app/tivi/data/daos/TiviShowDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ abstract class TiviShowDao : EntityDao<TiviShow>() {
@Query("SELECT * FROM shows WHERE id = :id")
abstract suspend fun getShowWithId(id: Long): TiviShow?

suspend fun getShowWithIdOrThrow(id: Long): TiviShow {
return getShowWithId(id)
?: throw IllegalArgumentException("No show with id $id in database")
}

@Query("SELECT trakt_id FROM shows WHERE id = :id")
abstract suspend fun getTraktIdForShowId(id: Long): Int?

Expand Down Expand Up @@ -76,8 +81,8 @@ abstract class TiviShowDao : EntityDao<TiviShow>() {
// Great, the entities are matching
idForTraktId
} else {
val showForTmdbId = getShowWithId(idForTmdbId)!!
val showForTraktId = getShowWithId(idForTraktId)!!
val showForTmdbId = getShowWithIdOrThrow(idForTmdbId)
val showForTraktId = getShowWithIdOrThrow(idForTraktId)
deleteEntity(showForTmdbId)
return insertOrUpdate(mergeShows(showForTraktId, showForTraktId, showForTmdbId))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ class UpdateRelatedShows @Inject constructor(
) : Interactor<Params>() {
override suspend fun doWork(params: Params) {
withContext(dispatchers.io) {
relatedShowsStore.fetchCollection(params.showId, forceFresh = params.forceLoad) {
relatedShowsStore.fetchCollection(params.showId, params.forceLoad) {
// Refresh if our local data is over 28 days old
lastRequestStore.isRequestExpired(params.showId, Period.ofDays(28))
}.forEach {
}.forEach { relatedShow ->
// yield here to to let other calls potentially run
yield()

showsStore.fetch(it.showId)
showImagesStore.fetchCollection(it.showId)
showsStore.fetch(relatedShow.otherShowId)
showImagesStore.fetchCollection(relatedShow.otherShowId)
}
}
}
Expand Down

0 comments on commit bcefed4

Please sign in to comment.