Update progress
parent
6993cec85e
commit
ee1c532d53
@ -0,0 +1,54 @@
|
|||||||
|
package org.koitharu.kotatsu.details.domain
|
||||||
|
|
||||||
|
import org.koitharu.kotatsu.core.model.findChapter
|
||||||
|
import org.koitharu.kotatsu.core.model.isLocal
|
||||||
|
import org.koitharu.kotatsu.core.parser.MangaRepository
|
||||||
|
import org.koitharu.kotatsu.history.data.HistoryRepository
|
||||||
|
import org.koitharu.kotatsu.history.data.PROGRESS_NONE
|
||||||
|
import org.koitharu.kotatsu.local.data.LocalMangaRepository
|
||||||
|
import org.koitharu.kotatsu.parsers.model.Manga
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
class ProgressUpdateUseCase @Inject constructor(
|
||||||
|
private val mangaRepositoryFactory: MangaRepository.Factory,
|
||||||
|
private val historyRepository: HistoryRepository,
|
||||||
|
private val localMangaRepository: LocalMangaRepository,
|
||||||
|
) {
|
||||||
|
|
||||||
|
suspend operator fun invoke(manga: Manga): Float {
|
||||||
|
val history = historyRepository.getOne(manga) ?: return PROGRESS_NONE
|
||||||
|
val seed = if (manga.isLocal) {
|
||||||
|
localMangaRepository.getRemoteManga(manga) ?: manga
|
||||||
|
} else {
|
||||||
|
manga
|
||||||
|
}
|
||||||
|
val repo = mangaRepositoryFactory.create(seed.source)
|
||||||
|
val details = if (manga.source != seed.source || seed.chapters.isNullOrEmpty()) {
|
||||||
|
repo.getDetails(seed)
|
||||||
|
} else {
|
||||||
|
seed
|
||||||
|
}
|
||||||
|
val chapter = details.findChapter(history.chapterId) ?: return PROGRESS_NONE
|
||||||
|
val chapters = details.getChapters(chapter.branch) ?: return PROGRESS_NONE
|
||||||
|
val chaptersCount = chapters.size
|
||||||
|
if (chaptersCount == 0) {
|
||||||
|
return PROGRESS_NONE
|
||||||
|
}
|
||||||
|
val chapterIndex = chapters.indexOfFirst { x -> x.id == history.chapterId }
|
||||||
|
val pagesCount = repo.getPages(chapter).size
|
||||||
|
if (pagesCount == 0) {
|
||||||
|
return PROGRESS_NONE
|
||||||
|
}
|
||||||
|
val pagePercent = (history.page + 1) / pagesCount.toFloat()
|
||||||
|
val ppc = 1f / chaptersCount
|
||||||
|
val result = ppc * chapterIndex + ppc * pagePercent
|
||||||
|
historyRepository.addOrUpdate(
|
||||||
|
manga = details,
|
||||||
|
chapterId = chapter.id,
|
||||||
|
page = history.page,
|
||||||
|
scroll = history.scroll,
|
||||||
|
percent = result,
|
||||||
|
)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue