From 2949fdd2c6c87ea38d02cf32ee7e8d9c9459c39d Mon Sep 17 00:00:00 2001 From: Koitharu Date: Wed, 2 Aug 2023 10:54:56 +0300 Subject: [PATCH] Recover history if chapterId id changed --- .../kotatsu/history/data/HistoryRepository.kt | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/history/data/HistoryRepository.kt b/app/src/main/kotlin/org/koitharu/kotatsu/history/data/HistoryRepository.kt index dce60aa30..4b9db3ecf 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/history/data/HistoryRepository.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/history/data/HistoryRepository.kt @@ -115,7 +115,7 @@ class HistoryRepository @Inject constructor( } suspend fun getOne(manga: Manga): MangaHistory? { - return db.historyDao.find(manga.id)?.toMangaHistory() + return db.historyDao.find(manga.id)?.recoverIfNeeded(manga)?.toMangaHistory() } suspend fun getProgress(mangaId: Long): Float { @@ -178,4 +178,17 @@ class HistoryRepository @Inject constructor( } } } + + private suspend fun HistoryEntity.recoverIfNeeded(manga: Manga): HistoryEntity { + val chapters = manga.chapters + if (chapters.isNullOrEmpty() || chapters.any { it.id == chapterId }) { + return this + } + val newChapterId = chapters.getOrNull( + (chapters.size * percent).toInt(), + )?.id ?: return this + val newEntity = copy(chapterId = newChapterId) + db.historyDao.update(newEntity) + return newEntity + } }