Recover history if chapterId id changed

revert-444-Background_status_compat
Koitharu 3 years ago
parent 829ea01b18
commit 2949fdd2c6
Signed by: Koitharu
GPG Key ID: 676DEE768C17A9D7

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

Loading…
Cancel
Save