Refactor reading time estimator
parent
a1120ea709
commit
c5d88f8700
@ -0,0 +1,21 @@
|
|||||||
|
package org.koitharu.kotatsu.details.data
|
||||||
|
|
||||||
|
import android.content.res.Resources
|
||||||
|
import org.koitharu.kotatsu.R
|
||||||
|
|
||||||
|
data class ReadingTime(
|
||||||
|
val minutes: Int,
|
||||||
|
val hours: Int,
|
||||||
|
val isContinue: Boolean,
|
||||||
|
) {
|
||||||
|
|
||||||
|
fun format(resources: Resources): String = when {
|
||||||
|
hours == 0 -> resources.getQuantityString(R.plurals.minutes, minutes, minutes)
|
||||||
|
minutes == 0 -> resources.getQuantityString(R.plurals.hours, hours, hours)
|
||||||
|
else -> resources.getString(
|
||||||
|
R.string.remaining_time_pattern,
|
||||||
|
resources.getQuantityString(R.plurals.hours, hours, hours),
|
||||||
|
resources.getQuantityString(R.plurals.minutes, minutes, minutes),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,33 @@
|
|||||||
|
package org.koitharu.kotatsu.details.domain
|
||||||
|
|
||||||
|
import org.koitharu.kotatsu.core.model.MangaHistory
|
||||||
|
import org.koitharu.kotatsu.core.model.findById
|
||||||
|
import org.koitharu.kotatsu.details.data.MangaDetails
|
||||||
|
import org.koitharu.kotatsu.details.data.ReadingTime
|
||||||
|
import javax.inject.Inject
|
||||||
|
import kotlin.math.roundToInt
|
||||||
|
|
||||||
|
class ReadingTimeUseCase @Inject constructor() {
|
||||||
|
|
||||||
|
fun invoke(manga: MangaDetails?, branch: String?, history: MangaHistory?): ReadingTime? {
|
||||||
|
// 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
|
||||||
|
if (isOnHistoryBranch) {
|
||||||
|
averageTimeSec = (averageTimeSec * (1f - checkNotNull(history).percent)).roundToInt()
|
||||||
|
}
|
||||||
|
if (averageTimeSec < 60) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
return ReadingTime(
|
||||||
|
minutes = averageTimeSec / 60,
|
||||||
|
hours = averageTimeSec / 3600,
|
||||||
|
isContinue = isOnHistoryBranch,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue