Use stats for reading time estimation
This commit is contained in:
@@ -5,25 +5,27 @@ import org.koitharu.kotatsu.core.model.findById
|
|||||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||||
import org.koitharu.kotatsu.details.data.MangaDetails
|
import org.koitharu.kotatsu.details.data.MangaDetails
|
||||||
import org.koitharu.kotatsu.details.data.ReadingTime
|
import org.koitharu.kotatsu.details.data.ReadingTime
|
||||||
|
import org.koitharu.kotatsu.stats.data.StatsRepository
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import kotlin.math.roundToInt
|
import kotlin.math.roundToInt
|
||||||
|
|
||||||
class ReadingTimeUseCase @Inject constructor(
|
class ReadingTimeUseCase @Inject constructor(
|
||||||
private val settings: AppSettings,
|
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) {
|
if (!settings.isReadingTimeEstimationEnabled) {
|
||||||
return null
|
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)
|
val chapters = manga?.chapters?.get(branch)
|
||||||
if (chapters.isNullOrEmpty()) {
|
if (chapters.isNullOrEmpty()) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
val isOnHistoryBranch = history != null && chapters.findById(history.chapterId) != null
|
val isOnHistoryBranch = history != null && chapters.findById(history.chapterId) != null
|
||||||
// Impossible task, I guess. Good luck on this.
|
// 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) {
|
if (isOnHistoryBranch) {
|
||||||
averageTimeSec = (averageTimeSec * (1f - checkNotNull(history).percent)).roundToInt()
|
averageTimeSec = (averageTimeSec * (1f - checkNotNull(history).percent)).roundToInt()
|
||||||
}
|
}
|
||||||
@@ -36,4 +38,16 @@ class ReadingTimeUseCase @Inject constructor(
|
|||||||
isContinue = isOnHistoryBranch,
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,4 +23,15 @@ class StatsRepository @Inject constructor(
|
|||||||
}
|
}
|
||||||
result
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,12 +48,6 @@
|
|||||||
|
|
||||||
<PreferenceCategory android:title="@string/details">
|
<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
|
<ListPreference
|
||||||
android:defaultValue="0"
|
android:defaultValue="0"
|
||||||
android:entries="@array/details_tabs"
|
android:entries="@array/details_tabs"
|
||||||
|
|||||||
@@ -34,6 +34,12 @@
|
|||||||
android:title="@string/reading_stats"
|
android:title="@string/reading_stats"
|
||||||
app:allowDividerAbove="true" />
|
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">
|
<PreferenceCategory android:title="@string/tracking">
|
||||||
|
|
||||||
<Preference
|
<Preference
|
||||||
|
|||||||
Reference in New Issue
Block a user