Skip to content

Commit

Permalink
Quicksync custom date bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
michaldrabik committed Jun 26, 2024
1 parent eda13c3 commit 0704063
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class MovieContextMenuMyMoviesCase @Inject constructor(
with(quickSyncManager) {
clearWatchlistMovies(listOf(traktId.id))
clearHiddenMovies(listOf(traktId.id))
scheduleMovies(listOf(traktId.id))
scheduleMovies(listOf(traktId.id), customDate)
}

RemoveTraktUiEvent(removeWatchlist = isWatchlist, removeHidden = isHidden)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.michaldrabik.ui_base.trakt.quicksync
import androidx.work.WorkManager
import com.michaldrabik.common.Mode
import com.michaldrabik.common.extensions.nowUtcMillis
import com.michaldrabik.common.extensions.toMillis
import com.michaldrabik.common.extensions.toUtcZone
import com.michaldrabik.data_local.LocalDataSource
import com.michaldrabik.data_local.database.model.TraktSyncQueue
import com.michaldrabik.data_local.database.model.TraktSyncQueue.Operation
Expand All @@ -11,6 +13,7 @@ import com.michaldrabik.data_local.utilities.TransactionsProvider
import com.michaldrabik.repository.UserTraktManager
import com.michaldrabik.repository.settings.SettingsRepository
import timber.log.Timber
import java.time.ZonedDateTime
import javax.inject.Inject
import javax.inject.Singleton

Expand All @@ -26,25 +29,26 @@ class QuickSyncManager @Inject constructor(
suspend fun scheduleEpisodes(
episodesIds: List<Long>,
showId: Long,
customDate: ZonedDateTime?,
clearProgress: Boolean = false
) {
if (!ensureQuickSync() && !(clearProgress && ensureQuickRemove())) {
return
}

val time = nowUtcMillis()
val items = episodesIds.map { TraktSyncQueue.createEpisode(it, showId, time, time, clearProgress) }
val timestamp = customDate?.toUtcZone()?.toMillis() ?: nowUtcMillis()
val items = episodesIds.map { TraktSyncQueue.createEpisode(it, showId, timestamp, timestamp, clearProgress) }
localSource.traktSyncQueue.insert(items)
Timber.d("Episodes added into sync queue. Count: ${items.size}")

QuickSyncWorker.schedule(workManager)
}

suspend fun scheduleMovies(moviesIds: List<Long>) {
suspend fun scheduleMovies(moviesIds: List<Long>, customDate: ZonedDateTime?) {
if (!ensureQuickSync()) return

val time = nowUtcMillis()
val items = moviesIds.map { TraktSyncQueue.createMovie(it, time, time) }
val timestamp = customDate?.toUtcZone()?.toMillis() ?: nowUtcMillis()
val items = moviesIds.map { TraktSyncQueue.createMovie(it, timestamp, timestamp) }
localSource.traktSyncQueue.insert(items)
Timber.d("Movies added into sync queue. Count: ${items.size}")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class MovieDetailsMyMoviesCase @Inject constructor(
suspend fun addToMyMovies(movie: Movie, customDate: ZonedDateTime?) {
withContext(dispatchers.IO) {
moviesRepository.myMovies.insert(movie.ids.trakt, customDate)
quickSyncManager.scheduleMovies(listOf(movie.traktId))
quickSyncManager.scheduleMovies(listOf(movie.traktId), customDate)
pinnedItemsRepository.removePinnedItem(movie)
announcementManager.refreshMoviesAnnouncements()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ProgressMoviesMainCase @Inject constructor(
suspend fun addToMyMovies(movie: Movie, customDate: ZonedDateTime?) {
moviesRepository.myMovies.insert(movie.ids.trakt, customDate)
pinnedItemsRepository.removePinnedItem(movie)
quickSyncManager.scheduleMovies(listOf(movie.ids.trakt.id))
quickSyncManager.scheduleMovies(listOf(movie.traktId), customDate)
}

suspend fun addToMyMovies(movieId: IdTrakt) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ProgressMoviesMainCaseTest : BaseMockTest() {

coVerify { moviesRepository.myMovies.insert(IdTrakt(123), null) }
coVerify { pinnedItemsRepository.removePinnedItem(movie) }
coVerify { quickSyncManager.scheduleMovies(listOf(123)) }
coVerify { quickSyncManager.scheduleMovies(listOf(123), null) }
}

@Test
Expand All @@ -56,6 +56,6 @@ class ProgressMoviesMainCaseTest : BaseMockTest() {

coVerify { moviesRepository.myMovies.insert(IdTrakt(123), null) }
coVerify { pinnedItemsRepository.removePinnedItem(any<Movie>()) }
coVerify { quickSyncManager.scheduleMovies(listOf(123)) }
coVerify { quickSyncManager.scheduleMovies(listOf(123), null) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class ProgressMainEpisodesCase @Inject constructor(
episodesManager.setEpisodeWatched(bundle, customDate)
quickSyncManager.scheduleEpisodes(
showId = bundle.show.traktId,
episodesIds = listOf(bundle.episode.ids.trakt.id)
episodesIds = listOf(bundle.episode.ids.trakt.id),
customDate = customDate
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class EpisodesSetEpisodeWatchedCase @Inject constructor(
quickSyncManager.scheduleEpisodes(
episodesIds = listOf(episode.ids.trakt.id),
showId = show.traktId,
customDate = customDate,
clearProgress = false
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class EpisodesSetSeasonWatchedCase @Inject constructor(
if (isMyShows) {
quickSyncManager.scheduleEpisodes(
showId = show.traktId,
episodesIds = episodesAdded.map { it.ids.trakt.id }
episodesIds = episodesAdded.map { it.ids.trakt.id },
customDate = customDate
)
}
return Result.SUCCESS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class ShowDetailsQuickProgressCase @Inject constructor(
quickSyncManager.scheduleEpisodes(
episodesIds = episodesIds,
showId = show.traktId,
customDate = customDate,
clearProgress = true
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class ShowDetailsWatchedSeasonCase @Inject constructor(
if (isMyShows) {
quickSyncManager.scheduleEpisodes(
showId = show.traktId,
episodesIds = episodesAdded.map { it.ids.trakt.id }
episodesIds = episodesAdded.map { it.ids.trakt.id },
customDate = customDate
)
}
return Result.SUCCESS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ class ProgressWidgetEpisodeCheckService : JobIntentService(), CoroutineScope {
episodesManager.setEpisodeWatched(episodeId, seasonId, IdTrakt(showId))
quickSyncManager.scheduleEpisodes(
showId = showId,
episodesIds = listOf(episodeId)
episodesIds = listOf(episodeId),
customDate = null
)
(applicationContext as WidgetsProvider).requestShowsWidgetsUpdate()
}
Expand Down

0 comments on commit 0704063

Please sign in to comment.