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 + } }