Skip to content
Permalink
Browse files

Add more logging to syncing followed shows

  • Loading branch information...
chrisbanes committed Apr 24, 2019
1 parent a7480a7 commit 8bde052cab661cfce9d113e69d6f04a93b401b97
@@ -34,7 +34,7 @@ class EntityInserter @Inject constructor(
}

suspend fun <E : TiviEntity> insertOrUpdate(dao: EntityDao<E>, entity: E): Long {
logger.d("insertOrUpdate. entity: %s", entity)
logger.d("insertOrUpdate: %s", entity)
return when {
entity.id == 0L -> dao.insert(entity)
else -> {
@@ -29,6 +29,7 @@ import app.tivi.data.entities.Request
import app.tivi.data.entities.Season
import app.tivi.data.resultentities.SeasonWithEpisodesAndWatches
import app.tivi.data.syncers.syncerForEntity
import app.tivi.util.Logger
import io.reactivex.Flowable
import org.threeten.bp.temporal.TemporalAmount
import javax.inject.Inject
@@ -39,24 +40,28 @@ class LocalSeasonsEpisodesStore @Inject constructor(
private val seasonsDao: SeasonsDao,
private val episodesDao: EpisodesDao,
private val episodeWatchEntryDao: EpisodeWatchEntryDao,
private val lastRequestDao: LastRequestDao
private val lastRequestDao: LastRequestDao,
private val logger: Logger
) {
private val seasonSyncer = syncerForEntity(
seasonsDao,
{ it.traktId },
{ entity, id -> entity.copy(id = id ?: 0) }
{ entity, id -> entity.copy(id = id ?: 0) },
logger
)

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

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

fun observeEpisode(episodeId: Long): Flowable<Episode> {
@@ -24,6 +24,7 @@ import app.tivi.data.repositories.shows.ShowRepository
import app.tivi.extensions.parallelForEach
import app.tivi.inject.Trakt
import app.tivi.trakt.TraktAuthState
import app.tivi.util.Logger
import org.threeten.bp.Duration
import org.threeten.bp.OffsetDateTime
import javax.inject.Inject
@@ -36,7 +37,8 @@ class FollowedShowsRepository @Inject constructor(
private val localShowStore: LocalShowStore,
@Trakt private val dataSource: FollowedShowsDataSource,
private val showRepository: ShowRepository,
private val traktAuthState: Provider<TraktAuthState>
private val traktAuthState: Provider<TraktAuthState>,
private val logger: Logger
) {
fun observeFollowedShows() = localStore.observeForPaging()

@@ -63,6 +65,9 @@ class FollowedShowsRepository @Inject constructor(

suspend fun addFollowedShow(showId: Long) {
val entry = localStore.getEntryForShowId(showId)

logger.d("addFollowedShow. Current entry: %s", entry)

if (entry == null || entry.pendingAction == PendingAction.DELETE) {
// If we don't have an entry, or it is marked for deletion, lets update it to be uploaded
val newEntry = FollowedShowEntry(
@@ -71,7 +76,10 @@ class FollowedShowsRepository @Inject constructor(
followedAt = entry?.followedAt ?: OffsetDateTime.now(),
pendingAction = PendingAction.UPLOAD
)
localStore.save(newEntry)
val newEntryId = localStore.save(newEntry)

logger.d("addFollowedShow. Entry saved with ID: %s - %s", newEntryId, newEntry)

// Now sync it up
syncFollowedShows()
}
@@ -103,6 +111,8 @@ class FollowedShowsRepository @Inject constructor(

private suspend fun pullDownTraktFollowedList(listId: Int) {
val response = dataSource.getListShows(listId)
logger.d("pullDownTraktFollowedList. Response: %s", response)

when (response) {
is Success ->
response.data.map { (entry, show) ->
@@ -111,7 +121,7 @@ class FollowedShowsRepository @Inject constructor(
// Create a followed show entry with the show id
entry.copy(showId = showId)
}.also { entries ->
// Save the related entries
// Save the show entries
localStore.sync(entries)
// Now update all of the followed shows if needed
entries.parallelForEach { entry ->
@@ -125,13 +135,19 @@ class FollowedShowsRepository @Inject constructor(

private suspend fun processPendingAdditions(listId: Int?) {
val pending = localStore.getEntriesWithAddAction()
logger.d("processPendingAdditions. listId: %s, Entries: %s", listId, pending)

if (pending.isEmpty()) {
return
}

if (listId != null && traktAuthState.get() == TraktAuthState.LOGGED_IN) {
val shows = pending.mapNotNull { localShowStore.getShow(it.showId) }
logger.d("processPendingAdditions. Entries mapped: %s", shows)

val response = dataSource.addShowIdsToList(listId, shows)
logger.d("processPendingAdditions. Trakt response: %s", response)

if (response is Success) {
// Now update the database
localStore.updateEntriesWithAction(pending.map { it.id }, PendingAction.NOTHING)
@@ -144,13 +160,19 @@ class FollowedShowsRepository @Inject constructor(

private suspend fun processPendingDelete(listId: Int?) {
val pending = localStore.getEntriesWithDeleteAction()
logger.d("processPendingDelete. listId: %s, Entries: %s", listId, pending)

if (pending.isEmpty()) {
return
}

if (listId != null && traktAuthState.get() == TraktAuthState.LOGGED_IN) {
val shows = pending.mapNotNull { localShowStore.getShow(it.showId) }
logger.d("processPendingDelete. Entries mapped: %s", shows)

val response = dataSource.removeShowIdsFromList(listId, shows)
logger.d("processPendingDelete. Trakt response: %s", response)

if (response is Success) {
// Now update the database
localStore.deleteEntriesInIds(pending.map { it.id })
@@ -46,11 +46,9 @@ class LocalFollowedShowsStore @Inject constructor(

private val syncer = syncerForEntity(
followedShowsDao,
{
logger.d("getTraktIdForShowId. Show: %s", it)
showDao.getTraktIdForShowId(it.showId)!!
},
{ entity, id -> entity.copy(id = id ?: 0) }
{ showDao.getTraktIdForShowId(it.showId)!! },
{ entity, id -> entity.copy(id = id ?: 0) },
logger
)

suspend fun getEntryForShowId(showId: Long): FollowedShowEntry? = followedShowsDao.entryWithShowId(showId)
@@ -88,7 +86,5 @@ class LocalFollowedShowsStore @Inject constructor(
return lastRequestDao.isRequestBefore(Request.FOLLOWED_SHOWS, 0, threshold)
}

suspend fun save(entry: FollowedShowEntry) {
entityInserter.insertOrUpdate(followedShowsDao, entry)
}
suspend fun save(entry: FollowedShowEntry) = entityInserter.insertOrUpdate(followedShowsDao, entry)
}
@@ -32,7 +32,7 @@ class ItemSyncer<ET : TiviEntity, NT, NID>(
private val localEntityToIdFunc: suspend (ET) -> NID,
private val networkEntityToIdFunc: suspend (NT) -> NID,
private val networkEntityToLocalEntityMapperFunc: suspend (NT, Long?) -> ET,
private val logger: Logger? = null
private val logger: Logger
) {
suspend fun sync(currentValues: Collection<ET>, networkValues: Collection<NT>): ItemSyncerResult<ET> {
val currentDbEntities = ArrayList(currentValues)
@@ -42,16 +42,21 @@ class ItemSyncer<ET : TiviEntity, NT, NID>(
val updated = ArrayList<ET>()

networkValues.forEach { networkEntity ->
logger.d("Syncing item from network: %s", networkEntity)

val remoteId = networkEntityToIdFunc(networkEntity)
logger.d("Mapped to remote ID: %s", remoteId)

val dbEntityForId = currentDbEntities.find { localEntityToIdFunc(it) == remoteId }
logger.d("Matched database entity for remote ID %s : %s", remoteId, dbEntityForId)

if (dbEntityForId != null) {
var entity = networkEntityToLocalEntityMapperFunc(networkEntity, dbEntityForId.id)
val entity = networkEntityToLocalEntityMapperFunc(networkEntity, dbEntityForId.id)
logger.d("Mapped network entity to local entity: %s", entity)
if (dbEntityForId != entity) {
// This is currently in the DB, so lets merge it with the saved version and update it
entity = networkEntityToLocalEntityMapperFunc(networkEntity, dbEntityForId.id)
entryUpdateFunc(entity)
logger?.d("Updated entry with remote id: $remoteId")
logger.d("Updated entry with remote id: %s", remoteId)
}
// Remove it from the list so that it is not deleted
currentDbEntities.remove(dbEntityForId)
@@ -60,15 +65,15 @@ class ItemSyncer<ET : TiviEntity, NT, NID>(
// Not currently in the DB, so lets insert
val entity = networkEntityToLocalEntityMapperFunc(networkEntity, null)
entryInsertFunc(entity)
logger?.d("Insert entry with remote id: $remoteId")
logger.d("Inserted entry with remote id: %s", remoteId)
added += entity
}
}

// Anything left in the set needs to be deleted from the database
currentDbEntities.forEach {
logger?.d("Remove entry with remote id: $it")
entryDeleteFunc(it)
logger.d("Deleted entry: ", it)
removed += it
}

@@ -87,7 +92,7 @@ fun <ET : TiviEntity, NT, NID> syncerForEntity(
localEntityToIdFunc: suspend (ET) -> NID,
networkEntityToIdFunc: suspend (NT) -> NID,
networkEntityToLocalEntityMapperFunc: suspend (NT, Long?) -> ET,
logger: Logger? = null
logger: Logger
) = ItemSyncer(
entityDao::insert,
entityDao::update,
@@ -102,7 +107,7 @@ fun <ET : TiviEntity, NID> syncerForEntity(
entityDao: EntityDao<ET>,
localEntityToIdFunc: suspend (ET) -> NID,
mapper: suspend (ET, Long?) -> ET,
logger: Logger? = null
logger: Logger
) = ItemSyncer(
entityDao::insert,
entityDao::update,
@@ -18,17 +18,20 @@ package app.tivi.interactors

import app.tivi.data.repositories.followedshows.FollowedShowsRepository
import app.tivi.util.AppCoroutineDispatchers
import app.tivi.util.Logger
import io.reactivex.Flowable
import kotlinx.coroutines.CoroutineDispatcher
import javax.inject.Inject

class ChangeShowFollowStatus @Inject constructor(
dispatchers: AppCoroutineDispatchers,
private val followedShowsRepository: FollowedShowsRepository
private val followedShowsRepository: FollowedShowsRepository,
private val logger: Logger
) : SubjectInteractor<ChangeShowFollowStatus.Params, ChangeShowFollowStatus.ExecuteParams, Boolean>() {
override val dispatcher: CoroutineDispatcher = dispatchers.io

override suspend fun doWork(params: Params, executeParams: ExecuteParams) {
logger.d("ChangeShowFollowStatus started: %s - %s", params, executeParams)
when (executeParams.action) {
Action.TOGGLE -> followedShowsRepository.toggleFollowedShow(params.showId)
Action.FOLLOW -> followedShowsRepository.addFollowedShow(params.showId)

0 comments on commit 8bde052

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