Permalink
Browse files

Finally fix episodes + seasons syncing

  • Loading branch information...
chrisbanes committed Feb 4, 2019
1 parent b619ad6 commit 3b1d25c443d2bbdb295684706a5e3da95394a0ca
@@ -164,7 +164,7 @@ class SeasonsEpisodesRepositoryTest : BaseDatabaseTest() {

// Assert that both are in the db
assertThat(seasonsDao.seasonsForShowId(showId), `is`(listOf(s1)))
assertThat(episodesDao.episodesFromSeasonId(s1_id), `is`(s1_episodes))
assertThat(episodesDao.episodesWithSeasonId(s1_id), `is`(s1_episodes))
}

@Test
@@ -179,7 +179,7 @@ class SeasonsEpisodesRepositoryTest : BaseDatabaseTest() {

// Assert that both are in the db
assertThat(seasonsDao.seasonsForShowId(showId), `is`(listOf(s1)))
assertThat(episodesDao.episodesFromSeasonId(s1_id), `is`(s1_episodes))
assertThat(episodesDao.episodesWithSeasonId(s1_id), `is`(s1_episodes))
}

@Test
@@ -193,7 +193,7 @@ class SeasonsEpisodesRepositoryTest : BaseDatabaseTest() {

// Assert the database is empty
assertThat(seasonsDao.seasonsForShowId(showId), `is`(emptyList()))
assertThat(episodesDao.episodesFromSeasonId(s1_id), `is`(emptyList()))
assertThat(episodesDao.episodesWithSeasonId(s1_id), `is`(emptyList()))
}

@Test
@@ -209,7 +209,7 @@ class SeasonsEpisodesRepositoryTest : BaseDatabaseTest() {

// Assert that both are in the db
assertThat(seasonsDao.seasonsForShowId(showId), `is`(listOf(s1)))
assertThat(episodesDao.episodesFromSeasonId(s1_id), `is`(s1_episodes))
assertThat(episodesDao.episodesWithSeasonId(s1_id), `is`(s1_episodes))
}

@Test
@@ -225,7 +225,7 @@ class SeasonsEpisodesRepositoryTest : BaseDatabaseTest() {

// Assert that both are in the db
assertThat(seasonsDao.seasonsForShowId(showId), `is`(listOf(s1, s2)))
assertThat(episodesDao.episodesFromSeasonId(s1_id), `is`(listOf(s1e1)))
assertThat(episodesDao.episodesFromSeasonId(s2_id), `is`(listOf(s2e1)))
assertThat(episodesDao.episodesWithSeasonId(s1_id), `is`(listOf(s1e1)))
assertThat(episodesDao.episodesWithSeasonId(s2_id), `is`(listOf(s2e1)))
}
}
@@ -24,7 +24,7 @@ import io.reactivex.Flowable
@Dao
abstract class EpisodesDao : EntityDao<Episode> {
@Query("SELECT * from episodes WHERE season_id = :seasonId ORDER BY number")
abstract fun episodesFromSeasonId(seasonId: Long): List<Episode>
abstract fun episodesWithSeasonId(seasonId: Long): List<Episode>

@Query("DELETE FROM episodes WHERE season_id = :seasonId")
abstract fun deleteWithSeasonId(seasonId: Long)
@@ -46,7 +46,7 @@ abstract class SeasonsDao : EntityDao<Season> {
abstract fun traktIdForId(id: Long): Int?

@Query("SELECT * FROM seasons WHERE trakt_id = :traktId")
abstract fun seasonWithSeasonTraktId(traktId: Int): Season?
abstract fun seasonWithTraktId(traktId: Int): Season?

@Query("SELECT * FROM seasons WHERE show_id = :showId AND number = :number")
abstract fun seasonWithShowIdAndNumber(showId: Long, number: Int): Season?
@@ -41,7 +41,19 @@ class LocalSeasonsEpisodesStore @Inject constructor(
private val episodeWatchEntryDao: EpisodeWatchEntryDao,
private val lastRequestDao: LastRequestDao
) {
private val syncer = syncerForEntity(
private val seasonSyncer = syncerForEntity(
seasonsDao,
{ it.traktId },
{ entity, id -> entity.copy(id = id ?: 0) }
)

private val episodeSyncer = syncerForEntity(
episodesDao,
{ it.traktId },
{ entity, id -> entity.copy(id = id ?: 0) }
)

private val episodeWatchSyncer = syncerForEntity(
episodeWatchEntryDao,
{ it.traktId },
{ entity, id -> entity.copy(id = id ?: 0) }
@@ -78,9 +90,9 @@ class LocalSeasonsEpisodesStore @Inject constructor(

fun getSeason(id: Long) = seasonsDao.seasonWithId(id)

fun getSeasonWithTraktId(traktId: Int) = seasonsDao.seasonWithSeasonTraktId(traktId)
fun getSeasonWithTraktId(traktId: Int) = seasonsDao.seasonWithTraktId(traktId)

fun getEpisodesInSeason(seasonId: Long) = episodesDao.episodesFromSeasonId(seasonId)
fun getEpisodesInSeason(seasonId: Long) = episodesDao.episodesWithSeasonId(seasonId)

fun getEpisode(id: Long) = episodesDao.episodeWithId(id)

@@ -92,12 +104,12 @@ class LocalSeasonsEpisodesStore @Inject constructor(

fun saveWatches(watches: List<EpisodeWatchEntry>) = entityInserter.insertOrUpdate(episodeWatchEntryDao, watches)

fun save(data: List<Pair<Season, List<Episode>>>) = transactionRunner {
fun save(showId: Long, data: Map<Season, List<Episode>>) = transactionRunner {
seasonSyncer.sync(seasonsDao.seasonsForShowId(showId), data.keys)
data.forEach { (season, episodes) ->
val seasonId = entityInserter.insertOrUpdate(seasonsDao, season)
episodes.forEach {
entityInserter.insertOrUpdate(episodesDao, it.copy(seasonId = seasonId))
}
val seasonId = seasonsDao.seasonWithTraktId(season.traktId!!)!!.id
val updatedEpisodes = episodes.map { if (it.seasonId != seasonId) it.copy(seasonId = seasonId) else it }
episodeSyncer.sync(episodesDao.episodesWithSeasonId(seasonId), updatedEpisodes)
}
}

@@ -142,11 +154,11 @@ class LocalSeasonsEpisodesStore @Inject constructor(

fun syncShowWatchEntries(showId: Long, watches: List<EpisodeWatchEntry>) = transactionRunner {
val currentWatches = episodeWatchEntryDao.entriesForShowIdWithNoPendingAction(showId)
syncer.sync(currentWatches, watches)
episodeWatchSyncer.sync(currentWatches, watches)
}

fun syncEpisodeWatchEntries(episodeId: Long, watches: List<EpisodeWatchEntry>) = transactionRunner {
val currentWatches = episodeWatchEntryDao.watchesForEpisode(episodeId)
syncer.sync(currentWatches, watches)
episodeWatchSyncer.sync(currentWatches, watches)
}
}
@@ -60,20 +60,18 @@ class SeasonsEpisodesRepository @Inject constructor(
val result = traktSeasonsDataSource.getSeasonsEpisodes(showId)
when (result) {
is Success -> {
result.data.map { (season, episodes) ->
val localSeason = localStore.getSeasonWithTraktId(season.traktId!!) ?: Season(showId = showId)
result.data.distinctBy { it.first.number }.associate { (season, episodes) ->
val localSeason = localStore.getSeasonWithTraktId(season.traktId!!)
?: Season(showId = showId)
val mergedSeason = mergeSeason(localSeason, season, Season.EMPTY)

val mergedEpisodes = episodes.map {
val mergedEpisodes = episodes.distinctBy(Episode::number).map {
val localEpisode = localStore.getEpisodeWithTraktId(it.traktId!!)
?: Episode(seasonId = mergedSeason.showId)
?: Episode(seasonId = mergedSeason.id)
mergeEpisode(localEpisode, it, Episode.EMPTY)
}
(mergedSeason to mergedEpisodes)
}.also {
// Save the seasons + episodes
localStore.save(it)
}
mergedSeason to mergedEpisodes
}.also { localStore.save(showId, it) }
}
}
if (result is Success) {

0 comments on commit 3b1d25c

Please sign in to comment.