Estimate remeaning download time

This commit is contained in:
Koitharu
2022-04-20 11:17:35 +03:00
parent e63ae12c8c
commit d64bd9d9d3
5 changed files with 72 additions and 10 deletions

View File

@@ -6,7 +6,7 @@ import kotlinx.parcelize.Parcelize
@Parcelize
data class Progress(
val value: Int,
val total: Int
val total: Int,
) : Parcelable, Comparable<Progress> {
override fun compareTo(other: Progress): Int {

View File

@@ -0,0 +1,47 @@
package org.koitharu.kotatsu.utils.progress
import android.os.SystemClock
import kotlin.math.roundToInt
import kotlin.math.roundToLong
private const val MIN_ESTIMATE_TICKS = 4
private const val NO_TIME = -1L
class TimeLeftEstimator {
private var times = ArrayList<Int>()
private var lastTick: Tick? = null
fun tick(value: Int, total: Int) {
if (total < 0) {
emptyTick()
return
}
val tick = Tick(value, total, SystemClock.elapsedRealtime())
lastTick?.let {
val ticksCount = value - it.value
times.add(((tick.time - it.time) / ticksCount.toDouble()).roundToInt())
}
lastTick = tick
}
fun emptyTick() {
lastTick = null
}
fun getEstimatedTimeLeft(): Long {
val progress = lastTick ?: return NO_TIME
if (times.size < MIN_ESTIMATE_TICKS) {
return NO_TIME
}
val timePerTick = times.average()
val ticksLeft = progress.total - progress.value
return (ticksLeft * timePerTick).roundToLong()
}
private class Tick(
val value: Int,
val total: Int,
val time: Long,
)
}