Recover history if chapterId id changed

This commit is contained in:
Koitharu
2023-08-02 10:54:56 +03:00
parent 829ea01b18
commit 2949fdd2c6

View File

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