Skip to content

Commit 9c4cdfc

Browse files
committed
fix: Timer has the right time left now in ui
issue #452
1 parent 76e0058 commit 9c4cdfc

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

app/src/main/java/com/bnyro/clock/util/services/TimerService.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import android.os.Build
1919
import android.os.Handler
2020
import android.os.Looper
2121
import android.os.PowerManager
22+
import android.os.SystemClock
2223
import android.os.Vibrator
2324
import android.text.format.DateUtils
2425
import android.util.Log
@@ -45,6 +46,7 @@ class TimerService : Service() {
4546

4647
private var mediaPlayer: MediaPlayer? = null
4748
private var isPlaying = false
49+
private var oldnow = SystemClock.elapsedRealtime()
4850
private val vibrator: Vibrator by lazy {
4951
getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
5052
}
@@ -281,17 +283,21 @@ class TimerService : Service() {
281283
}
282284

283285
private fun updateState() {
286+
val now = System.currentTimeMillis()
287+
val delta = now - oldnow
288+
oldnow = now
289+
284290
timerObjects.forEach {
285291
if (it.state.value == WatchState.RUNNING) {
286292

287-
it.currentPosition.value -= UPDATE_DELAY
288-
293+
it.currentPosition.value =
294+
(it.currentPosition.value - delta.toInt())
295+
.coerceAtLeast(0)
289296

290297
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
291298
updateNotification(it)
292299
}
293300
}
294-
295301
}
296302
}
297303

0 commit comments

Comments
 (0)