Use stats for reading time estimation

This commit is contained in:
Koitharu
2024-02-29 12:12:09 +02:00
parent fda59996aa
commit 11cd5609bb
4 changed files with 34 additions and 9 deletions

View File

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

View File

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

View File

@@ -48,12 +48,6 @@
<PreferenceCategory android:title="@string/details">
<SwitchPreferenceCompat
android:defaultValue="true"
android:key="reading_time"
android:summary="@string/reading_time_estimation_summary"
android:title="@string/reading_time_estimation" />
<ListPreference
android:defaultValue="0"
android:entries="@array/details_tabs"

View File

@@ -34,6 +34,12 @@
android:title="@string/reading_stats"
app:allowDividerAbove="true" />
<SwitchPreferenceCompat
android:defaultValue="true"
android:key="reading_time"
android:summary="@string/reading_time_estimation_summary"
android:title="@string/reading_time_estimation" />
<PreferenceCategory android:title="@string/tracking">
<Preference