aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils
diff options
context:
space:
mode:
authorAppability <appable@icloud.com>2023-08-15 10:31:16 -0700
committerAppability <appable@icloud.com>2023-08-15 14:08:55 -0700
commit6d4f9a049cf530930fe502a3a716b8ba5cb59827 (patch)
tree538df4f147edda4fbeacfe616bf1961409dec876 /src/main/java/at/hannibal2/skyhanni/utils
parentd5db9a43e940b6068c108f62ad86ecd20dec9821 (diff)
downloadskyhanni-6d4f9a049cf530930fe502a3a716b8ba5cb59827.tar.gz
skyhanni-6d4f9a049cf530930fe502a3a716b8ba5cb59827.tar.bz2
skyhanni-6d4f9a049cf530930fe502a3a716b8ba5cb59827.zip
fix formatDuration and refactor with Timer class
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/Timer.kt43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/Timer.kt b/src/main/java/at/hannibal2/skyhanni/utils/Timer.kt
new file mode 100644
index 000000000..55ea90b52
--- /dev/null
+++ b/src/main/java/at/hannibal2/skyhanni/utils/Timer.kt
@@ -0,0 +1,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)
+
+} \ No newline at end of file