From 11cd5609bbe67c1fc303af7b5bf6896ae120196b Mon Sep 17 00:00:00 2001 From: Koitharu Date: Thu, 29 Feb 2024 12:12:09 +0200 Subject: [PATCH] Use stats for reading time estimation --- .../details/domain/ReadingTimeUseCase.kt | 20 ++++++++++++++++--- .../kotatsu/stats/data/StatsRepository.kt | 11 ++++++++++ app/src/main/res/xml/pref_appearance.xml | 6 ------ app/src/main/res/xml/pref_services.xml | 6 ++++++ 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/details/domain/ReadingTimeUseCase.kt b/app/src/main/kotlin/org/koitharu/kotatsu/details/domain/ReadingTimeUseCase.kt index cab72330a..1e7b3ef0e 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/details/domain/ReadingTimeUseCase.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/details/domain/ReadingTimeUseCase.kt @@ -5,25 +5,27 @@ import org.koitharu.kotatsu.core.model.findById import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.details.data.MangaDetails import org.koitharu.kotatsu.details.data.ReadingTime +import org.koitharu.kotatsu.stats.data.StatsRepository +import java.util.concurrent.TimeUnit import javax.inject.Inject import kotlin.math.roundToInt class ReadingTimeUseCase @Inject constructor( private val settings: AppSettings, + private val statsRepository: StatsRepository, ) { - fun invoke(manga: MangaDetails?, branch: String?, history: MangaHistory?): ReadingTime? { + suspend fun invoke(manga: MangaDetails?, branch: String?, history: MangaHistory?): ReadingTime? { if (!settings.isReadingTimeEstimationEnabled) { return null } - // FIXME MAXIMUM HARDCODE!!! To do calculation with user's page read speed and his favourites/history mangas average pages in chapter val chapters = manga?.chapters?.get(branch) if (chapters.isNullOrEmpty()) { return null } val isOnHistoryBranch = history != null && chapters.findById(history.chapterId) != null // Impossible task, I guess. Good luck on this. - var averageTimeSec: Int = 20 * 10 * chapters.size // 20 pages, 10 seconds per page + var averageTimeSec: Int = 20 /* pages */ * getSecondsPerPage(manga.id) * chapters.size if (isOnHistoryBranch) { averageTimeSec = (averageTimeSec * (1f - checkNotNull(history).percent)).roundToInt() } @@ -36,4 +38,16 @@ class ReadingTimeUseCase @Inject constructor( isContinue = isOnHistoryBranch, ) } + + private suspend fun getSecondsPerPage(mangaId: Long): Int { + var time = if (settings.isStatsEnabled) { + TimeUnit.MILLISECONDS.toSeconds(statsRepository.getTimePerPage(mangaId)).toInt() + } else { + 0 + } + if (time == 0) { + time = 10 // default + } + return time + } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/stats/data/StatsRepository.kt b/app/src/main/kotlin/org/koitharu/kotatsu/stats/data/StatsRepository.kt index 7d81ff121..88f8f55f7 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/stats/data/StatsRepository.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/stats/data/StatsRepository.kt @@ -23,4 +23,15 @@ class StatsRepository @Inject constructor( } result } + + suspend fun getTimePerPage(mangaId: Long): Long = db.withTransaction { + val dao = db.getStatsDao() + val pages = dao.getReadPagesCount(mangaId) + val time = if (pages >= 10) { + dao.getAverageTimePerPage(mangaId) + } else { + dao.getAverageTimePerPage() + } + time + } } diff --git a/app/src/main/res/xml/pref_appearance.xml b/app/src/main/res/xml/pref_appearance.xml index 4e4d2492b..aee3d9b37 100644 --- a/app/src/main/res/xml/pref_appearance.xml +++ b/app/src/main/res/xml/pref_appearance.xml @@ -48,12 +48,6 @@ - - + +