From ecc0cb5f0bb7ac32124be744b7cbd9d0a550d1cb Mon Sep 17 00:00:00 2001 From: Florian Renaud Date: Mon, 30 Jan 2023 14:25:02 +0100 Subject: [PATCH] CountUpTimer.elapsedTime now returns updated value --- .../lib/core/utils/timer/CountUpTimer.kt | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/library/core-utils/src/main/java/im/vector/lib/core/utils/timer/CountUpTimer.kt b/library/core-utils/src/main/java/im/vector/lib/core/utils/timer/CountUpTimer.kt index 0be20675af..c96b51a85d 100644 --- a/library/core-utils/src/main/java/im/vector/lib/core/utils/timer/CountUpTimer.kt +++ b/library/core-utils/src/main/java/im/vector/lib/core/utils/timer/CountUpTimer.kt @@ -44,7 +44,7 @@ class CountUpTimer(initialTime: Long = 0L, private val intervalInMs: Long = 1_00 private fun startCounter() { tickerFlow(coroutineScope, intervalInMs) .filter { resumed.get() } - .map { addAndGetElapsedTime() } + .map { elapsedTime() } .onEach { tickListener?.onTick(it) } .launchIn(coroutineScope) } @@ -52,11 +52,16 @@ class CountUpTimer(initialTime: Long = 0L, private val intervalInMs: Long = 1_00 var tickListener: TickListener? = null fun elapsedTime(): Long { - return elapsedTime.get() + return if (resumed.get()) { + val now = clock.epochMillis() + elapsedTime.addAndGet(now - lastTime.getAndSet(now)) + } else { + elapsedTime.get() + } } fun pause() { - tickListener?.onTick(addAndGetElapsedTime()) + tickListener?.onTick(elapsedTime()) resumed.set(false) } @@ -66,15 +71,10 @@ class CountUpTimer(initialTime: Long = 0L, private val intervalInMs: Long = 1_00 } fun stop() { - tickListener?.onTick(addAndGetElapsedTime()) + tickListener?.onTick(elapsedTime()) coroutineScope.cancel() } - private fun addAndGetElapsedTime(): Long { - val now = clock.epochMillis() - return elapsedTime.addAndGet(now - lastTime.getAndSet(now)) - } - fun interface TickListener { fun onTick(milliseconds: Long) }