From b14603c384d61e62b31c1387523995385fc8ef67 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Mon, 18 Mar 2024 14:28:29 +0200 Subject: [PATCH] Fix history migration --- .../kotatsu/alternatives/domain/MigrateUseCase.kt | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/alternatives/domain/MigrateUseCase.kt b/app/src/main/kotlin/org/koitharu/kotatsu/alternatives/domain/MigrateUseCase.kt index b771bd9a3..a3183fe09 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/alternatives/domain/MigrateUseCase.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/alternatives/domain/MigrateUseCase.kt @@ -75,11 +75,16 @@ class MigrateUseCase @Inject constructor( if (oldManga.chapters.isNullOrEmpty()) { // probably broken manga/source val branch = newManga.getPreferredBranch(null) val chapters = checkNotNull(newManga.getChapters(branch)) + val currentChapter = if (history.percent in 0f..1f) { + chapters[(chapters.lastIndex * history.percent).toInt()] + } else { + chapters.first() + } return HistoryEntity( mangaId = newManga.id, createdAt = history.createdAt, updatedAt = System.currentTimeMillis(), - chapterId = chapters[(chapters.lastIndex * history.percent).toInt()].id, + chapterId = currentChapter.id, page = history.page, scroll = history.scroll, percent = history.percent, @@ -91,7 +96,11 @@ class MigrateUseCase @Inject constructor( val oldChapters = checkNotNull(oldManga.getChapters(branch)) var index = oldChapters.indexOfFirst { it.id == history.chapterId } if (index < 0) { - index = (oldChapters.lastIndex * history.percent).toInt() + index = if (history.percent in 0f..1f) { + (oldChapters.lastIndex * history.percent).toInt() + } else { + 0 + } } val newChapters = checkNotNull(newManga.chapters).groupBy { it.branch } val newBranch = if (newChapters.containsKey(branch)) {