Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ class BangumiSyncCore(
return@withContext Result.failure(AppError.ScrapingError.ApiError("Bangumi", bangumiSyncNoEpisodesMessage()))
}

when (val collection = ensureSubjectCollection(subjectId)) {
is Result.Success -> Unit
is Result.Error -> return@withContext collection
val subjectCollection = when (val current = bangumiService.getSubjectCollection(subjectId)) {
is Result.Success -> current.data
is Result.Error -> return@withContext current
}

val remoteCollections = when (val result = bangumiService.getEpisodeCollections(subjectId)) {
Expand Down Expand Up @@ -118,7 +118,26 @@ class BangumiSyncCore(
.map { it.episodeId }
.toSet()
val toPush = localWatchedRemoteIds.distinct().filterNot { it in remoteDoneIds }
var subjectCollectionType = subjectCollection?.type
if (toPush.isNotEmpty()) {
// 只有真的推送单集时才动整部状态:未收藏 / 想看 提成「在看」;
// 线上整部是「看过」但这集线上没标过(还在看)也恢复成「在看」。绝不写「看过」
val remoteType = subjectCollection?.type
if (
remoteType == null ||
remoteType == BangumiSubjectCollectionType.WISH.value ||
remoteType == BangumiSubjectCollectionType.DONE.value
) {
when (
val ensured = bangumiService.upsertSubjectCollection(
subjectId,
BangumiSubjectCollectionType.DOING
)
) {
is Result.Success -> subjectCollectionType = BangumiSubjectCollectionType.DOING.value
is Result.Error -> return@withContext ensured
}
}
when (
val updated = bangumiService.updateEpisodeCollections(
subjectId = subjectId,
Expand All @@ -132,15 +151,6 @@ class BangumiSyncCore(
}

val watchedCount = (remoteDoneIds + localWatchedRemoteIds).size
val targetSubjectType = if (watchedCount >= episodes.count { it.bangumiEpisodeId != null }.coerceAtLeast(1)) {
BangumiSubjectCollectionType.DONE
} else {
BangumiSubjectCollectionType.DOING
}
when (val subject = bangumiService.upsertSubjectCollection(subjectId, targetSubjectType)) {
is Result.Success -> Unit
is Result.Error -> return@withContext subject
}

when (
val cachedEpisodes = metadataRepository.cacheEpisodes(
Expand All @@ -160,7 +170,7 @@ class BangumiSyncCore(
when (
val cachedAnime = metadataRepository.cacheMetadata(
anime.copy(
bangumiCollectionType = targetSubjectType.value,
bangumiCollectionType = subjectCollectionType,
bangumiEpStatus = watchedCount
)
)
Expand All @@ -176,7 +186,7 @@ class BangumiSyncCore(
pushedEpisodes = toPush.size,
pulledEpisodes = pulled,
remoteWatchedEpisodes = watchedCount,
subjectCollectionType = targetSubjectType.value
subjectCollectionType = subjectCollectionType
)
)
} catch (e: Exception) {
Expand All @@ -199,7 +209,8 @@ class BangumiSyncCore(
}
val subjectId = anime.bangumiId
if (subjectId != null) {
when (val collection = ensureSubjectCollection(subjectId)) {
val restoreFromDone = episode.bangumiCollectionType != BangumiEpisodeCollectionType.DONE.value
when (val collection = ensureSubjectCollection(subjectId, restoreFromDone)) {
is Result.Success -> Unit
is Result.Error -> return@withContext collection
}
Expand Down Expand Up @@ -235,15 +246,20 @@ class BangumiSyncCore(
}
}

private suspend fun ensureSubjectCollection(subjectId: Int): Result<Unit> {
private suspend fun ensureSubjectCollection(
subjectId: Int,
restoreFromDone: Boolean = false,
): Result<Unit> {
val current = when (val result = bangumiService.getSubjectCollection(subjectId)) {
is Result.Success -> result.data
is Result.Error -> return result
}
if (current == null || current.type == BangumiSubjectCollectionType.WISH.value) {
return bangumiService.upsertSubjectCollection(subjectId, BangumiSubjectCollectionType.DOING)
}
return Result.success(Unit)
val type = current?.type
val needsDoing = current == null ||
type == BangumiSubjectCollectionType.WISH.value ||
(restoreFromDone && type == BangumiSubjectCollectionType.DONE.value)
if (!needsDoing) return Result.success(Unit)
return bangumiService.upsertSubjectCollection(subjectId, BangumiSubjectCollectionType.DOING)
}

private fun isLocallyWatched(episode: Episode, positionMs: Long?, playCount: Int): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class BangumiSyncCoreTest {
@Test
fun `syncAnime pulls remote watched episodes into local progress`() = runBlocking {
val service = FakeBangumiCollectionService(
subjectCollections = mutableMapOf(
1001 to BangumiSubjectCollection(subjectId = 1001, type = BangumiSubjectCollectionType.DOING.value, rate = 0, epStatus = 1),
),
episodeCollections = mutableMapOf(
1001 to listOf(BangumiEpisodeCollection(episodeId = 5001, episodeNumber = 1, type = BangumiEpisodeCollectionType.DONE.value)),
),
Expand All @@ -64,10 +67,11 @@ class BangumiSyncCoreTest {
assertEquals(BangumiEpisodeCollectionType.DONE.value, metadata.episodes.getValue("frieren")[0].bangumiCollectionType)
assertEquals(BangumiSubjectCollectionType.DOING.value, metadata.anime?.bangumiCollectionType)
assertEquals(1, metadata.anime?.bangumiEpStatus)
assertEquals(emptyList<Pair<Int, BangumiSubjectCollectionType>>(), service.subjectUpdates)
}

@Test
fun `syncAnime pushes locally completed episodes and marks subject done when all remote ids are watched`() = runBlocking {
fun `syncAnime pushes locally completed episodes without marking the subject done`() = runBlocking {
val service = FakeBangumiCollectionService()
val metadata = FakeMetadataRepository(
anime = Anime(id = "frieren", title = "Frieren", bangumiId = 1001),
Expand All @@ -93,11 +97,105 @@ class BangumiSyncCoreTest {
assertEquals(2, summary.remoteWatchedEpisodes)
assertEquals(listOf(5001, 5002), service.updatedEpisodeBatches.single().episodeIds)
assertEquals(BangumiEpisodeCollectionType.DONE, service.updatedEpisodeBatches.single().type)
assertEquals(1001 to BangumiSubjectCollectionType.DONE, service.subjectUpdates.last())
assertEquals(BangumiSubjectCollectionType.DONE.value, metadata.anime?.bangumiCollectionType)
assertEquals(listOf(1001 to BangumiSubjectCollectionType.DOING), service.subjectUpdates)
assertEquals(BangumiSubjectCollectionType.DOING.value, summary.subjectCollectionType)
assertEquals(BangumiSubjectCollectionType.DOING.value, metadata.anime?.bangumiCollectionType)
assertEquals(2, metadata.anime?.bangumiEpStatus)
}

@Test
fun `syncAnime restores a done subject to doing when pushing an episode the remote has not watched`() = runBlocking {
val service = FakeBangumiCollectionService(
subjectCollections = mutableMapOf(
1001 to BangumiSubjectCollection(subjectId = 1001, type = BangumiSubjectCollectionType.DONE.value, rate = 0, epStatus = 0),
),
)
val metadata = FakeMetadataRepository(
anime = Anime(id = "frieren", title = "Frieren", bangumiId = 1001),
episodes = listOf(episode(id = "ep1", remoteId = 5001, duration = 10_000L)),
)
val progress = FakeProgressRepository(
records = mutableMapOf("ep1" to ProgressRecord("ep1", positionMs = 10_000L, lastWatched = 1L)),
)
val core = BangumiSyncCore(service, metadata, progress)

val result = core.syncAnime("frieren")

assertTrue(result is Result.Success)
val summary = (result as Result.Success).data
assertEquals(1, summary.pushedEpisodes)
assertEquals(listOf(1001 to BangumiSubjectCollectionType.DOING), service.subjectUpdates)
assertEquals(BangumiSubjectCollectionType.DOING.value, summary.subjectCollectionType)
assertEquals(BangumiSubjectCollectionType.DOING.value, metadata.anime?.bangumiCollectionType)
}

@Test
fun `syncAnime leaves a done subject alone when there is nothing to push`() = runBlocking {
val service = FakeBangumiCollectionService(
subjectCollections = mutableMapOf(
1001 to BangumiSubjectCollection(subjectId = 1001, type = BangumiSubjectCollectionType.DONE.value, rate = 0, epStatus = 1),
),
episodeCollections = mutableMapOf(
1001 to listOf(BangumiEpisodeCollection(episodeId = 5001, episodeNumber = 1, type = BangumiEpisodeCollectionType.DONE.value)),
),
)
val metadata = FakeMetadataRepository(
anime = Anime(id = "frieren", title = "Frieren", bangumiId = 1001),
episodes = listOf(episode(id = "ep1", remoteId = 5001, duration = 10_000L)),
)
val progress = FakeProgressRepository(
records = mutableMapOf("ep1" to ProgressRecord("ep1", positionMs = 10_000L, lastWatched = 1L)),
)
val core = BangumiSyncCore(service, metadata, progress)

val result = core.syncAnime("frieren")

assertTrue(result is Result.Success)
val summary = (result as Result.Success).data
assertEquals(0, summary.pushedEpisodes)
assertEquals(emptyList<Pair<Int, BangumiSubjectCollectionType>>(), service.subjectUpdates)
assertEquals(BangumiSubjectCollectionType.DONE.value, summary.subjectCollectionType)
assertEquals(BangumiSubjectCollectionType.DONE.value, metadata.anime?.bangumiCollectionType)
}

@Test
fun `markEpisodeWatched restores a done subject to doing for an unwatched episode`() = runBlocking {
val service = FakeBangumiCollectionService(
subjectCollections = mutableMapOf(
1001 to BangumiSubjectCollection(subjectId = 1001, type = BangumiSubjectCollectionType.DONE.value, rate = 0, epStatus = 0),
),
)
val metadata = FakeMetadataRepository(
anime = Anime(id = "frieren", title = "Frieren", bangumiId = 1001),
episodes = listOf(episode(id = "ep1", remoteId = 5001, duration = 10_000L)),
)
val core = BangumiSyncCore(service, metadata, FakeProgressRepository())

val result = core.markEpisodeWatched("ep1")

assertTrue(result is Result.Success)
assertEquals(listOf(1001 to BangumiSubjectCollectionType.DOING), service.subjectUpdates)
}

@Test
fun `markEpisodeWatched keeps a done subject when the episode was already watched`() = runBlocking {
val service = FakeBangumiCollectionService(
subjectCollections = mutableMapOf(
1001 to BangumiSubjectCollection(subjectId = 1001, type = BangumiSubjectCollectionType.DONE.value, rate = 0, epStatus = 1),
),
)
val metadata = FakeMetadataRepository(
anime = Anime(id = "frieren", title = "Frieren", bangumiId = 1001),
episodes = listOf(episode(id = "ep1", remoteId = 5001, duration = 10_000L, collectionType = 2)),
)
val core = BangumiSyncCore(service, metadata, FakeProgressRepository())

val result = core.markEpisodeWatched("ep1")

assertTrue(result is Result.Success)
assertEquals(emptyList<Pair<Int, BangumiSubjectCollectionType>>(), service.subjectUpdates)
}

@Test
fun `markEpisodeWatched updates remote episode and cached local episode`() = runBlocking {
val service = FakeBangumiCollectionService()
Expand Down Expand Up @@ -150,7 +248,7 @@ class BangumiSyncCoreTest {

assertEquals(Result.failure(failure), result)
assertEquals(emptyList<UpdatedEpisodeBatch>(), service.updatedEpisodeBatches)
assertEquals(listOf(1001 to BangumiSubjectCollectionType.DOING), service.subjectUpdates)
assertEquals(emptyList<Pair<Int, BangumiSubjectCollectionType>>(), service.subjectUpdates)
}

@Test
Expand All @@ -173,6 +271,7 @@ class BangumiSyncCoreTest {
id: String,
remoteId: Int?,
duration: Long,
collectionType: Int? = null,
): Episode =
Episode(
id = id,
Expand All @@ -182,6 +281,7 @@ class BangumiSyncCoreTest {
fileName = "$id.mkv",
duration = duration,
bangumiEpisodeId = remoteId,
bangumiCollectionType = collectionType,
)

private class FakeBangumiCollectionService(
Expand Down
Loading