Skip to content

Commit 1ce9594

Browse files
committed
Update intervalometer
1 parent e42c3ad commit 1ce9594

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

app/src/main/java/com/kylecorry/trailsensecore/infrastructure/time/Intervalometer.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.kylecorry.trailsensecore.infrastructure.time
22

33
import android.os.Handler
44
import android.os.Looper
5+
import android.os.SystemClock
56
import java.time.Duration
67

78
class Intervalometer(private val runnable: Runnable) {
@@ -23,13 +24,17 @@ class Intervalometer(private val runnable: Runnable) {
2324
runnable.run()
2425
val nextRunnable = intervalRunnable
2526
if (nextRunnable != null) {
26-
handler.postDelayed(nextRunnable, periodMillis)
27+
handler.postAtTime(nextRunnable, SystemClock.uptimeMillis() + periodMillis)
2728
}
2829
}
2930

3031
intervalRunnable = r
3132

32-
handler.postDelayed(r, initialDelayMillis)
33+
if (initialDelayMillis == 0L) {
34+
handler.post(r)
35+
} else {
36+
handler.postAtTime(r, SystemClock.uptimeMillis() + initialDelayMillis)
37+
}
3338
}
3439

3540
fun interval(period: Duration, initialDelay: Duration = Duration.ZERO) {
@@ -42,7 +47,11 @@ class Intervalometer(private val runnable: Runnable) {
4247
}
4348

4449
running = true
45-
handler.postDelayed(runnable, delayMillis)
50+
if (delayMillis == 0L){
51+
handler.post(runnable)
52+
} else {
53+
handler.postAtTime(runnable, SystemClock.uptimeMillis() + delayMillis)
54+
}
4655
}
4756

4857
fun once(delay: Duration) {

0 commit comments

Comments
 (0)