aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils/Timer.kt
blob: 55ea90b52302f70d54ae6afa7c142970494667f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package at.hannibal2.skyhanni.utils

import com.google.gson.annotations.Expose
import kotlin.time.Duration

class Timer(
    @Expose
    var duration: Duration,

    @Expose
    private var started: SimpleTimeMark = SimpleTimeMark.now(),

    startPaused: Boolean = false
): Comparable<Timer> {

    @Expose
    private var paused: SimpleTimeMark? = null

    init {
        if (startPaused) {
            paused = started
        }
    }

    val ended get() = !remaining.isPositive()
    val remaining get() = duration - elapsed
    val elapsed get() = paused?.let { it - started } ?: started.passedSince()

    fun pause() {
        paused = SimpleTimeMark.now()
    }

    fun resume() {
        paused?.let {
            started = it
            duration = it - started
            paused = null
        }
    }

    override fun compareTo(other: Timer): Int = remaining.compareTo(other.remaining)

}