From 912c37e3ff5727726bfa03f2494d80add6100047 Mon Sep 17 00:00:00 2001 From: Florian Renaud Date: Mon, 6 Feb 2023 10:36:40 +0100 Subject: [PATCH] Reset elapsed time on stop action and add a start method to the CountUpTimer --- .../im/vector/lib/attachmentviewer/VideoViewHolder.kt | 2 +- .../im/vector/lib/core/utils/timer/CountUpTimer.kt | 10 +++++++--- .../im/vector/lib/core/utils/timer/CountUpTimerTest.kt | 6 ++---- .../home/room/detail/composer/AudioMessageHelper.kt | 10 +++------- .../detail/composer/voice/VoiceMessageRecorderView.kt | 2 +- .../features/location/live/map/LiveLocationUserItem.kt | 8 +++++--- .../listening/VoiceBroadcastPlayerImpl.kt | 7 ++----- .../recording/VoiceBroadcastRecorderQ.kt | 6 +++--- 8 files changed, 24 insertions(+), 27 deletions(-) diff --git a/library/attachment-viewer/src/main/java/im/vector/lib/attachmentviewer/VideoViewHolder.kt b/library/attachment-viewer/src/main/java/im/vector/lib/attachmentviewer/VideoViewHolder.kt index 30da59750e..64bd31cd8c 100644 --- a/library/attachment-viewer/src/main/java/im/vector/lib/attachmentviewer/VideoViewHolder.kt +++ b/library/attachment-viewer/src/main/java/im/vector/lib/attachmentviewer/VideoViewHolder.kt @@ -110,7 +110,7 @@ class VideoViewHolder constructor(itemView: View) : // Log.v("FOO", "isPlaying $isPlaying $progress/$duration") eventListener?.get()?.onEvent(AttachmentEvents.VideoEvent(isPlaying, progress, duration)) } - it.resume() + it.start() } } try { 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 435a8603e8..3ed63a407b 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 @@ -23,18 +23,16 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.launch import java.util.concurrent.atomic.AtomicLong -@OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class) class CountUpTimer( private val coroutineScope: CoroutineScope = CoroutineScope(Dispatchers.Main), private val clock: Clock = DefaultClock(), private val intervalInMs: Long = 1_000, - initialTime: Long = 0L, ) { private var counterJob: Job? = null private val lastTime: AtomicLong = AtomicLong(clock.epochMillis()) - private val elapsedTime: AtomicLong = AtomicLong(initialTime) + private val elapsedTime: AtomicLong = AtomicLong(0) private fun startCounter() { counterJob = coroutineScope.launch { @@ -56,6 +54,11 @@ class CountUpTimer( } } + fun start(initialTime: Long = 0L) { + elapsedTime.set(initialTime) + resume() + } + fun pause() { tickListener?.onTick(elapsedTime()) counterJob?.cancel() @@ -71,6 +74,7 @@ class CountUpTimer( tickListener?.onTick(elapsedTime()) counterJob?.cancel() counterJob = null + elapsedTime.set(0L) } fun interface TickListener { diff --git a/library/core-utils/src/test/java/im/vector/lib/core/utils/timer/CountUpTimerTest.kt b/library/core-utils/src/test/java/im/vector/lib/core/utils/timer/CountUpTimerTest.kt index df517751ed..83f11900b1 100644 --- a/library/core-utils/src/test/java/im/vector/lib/core/utils/timer/CountUpTimerTest.kt +++ b/library/core-utils/src/test/java/im/vector/lib/core/utils/timer/CountUpTimerTest.kt @@ -42,10 +42,9 @@ internal class CountUpTimerTest { coroutineScope = this, clock = fakeClock, intervalInMs = AN_INTERVAL, - initialTime = 0, ).also { it.tickListener = tickListener } - timer.resume() + timer.start() advanceTimeBy(AN_INTERVAL / 2) // no tick timer.pause() // tick advanceTimeBy(AN_INTERVAL * 10) // no tick @@ -71,10 +70,9 @@ internal class CountUpTimerTest { coroutineScope = this, clock = fakeClock, intervalInMs = AN_INTERVAL, - initialTime = AN_INITIAL_TIME, ).also { it.tickListener = tickListener } - timer.resume() + timer.start(AN_INITIAL_TIME) advanceTimeBy(AN_INTERVAL) // tick timer.pause() // tick advanceTimeBy(AN_INTERVAL * 10) // no tick diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/composer/AudioMessageHelper.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/composer/AudioMessageHelper.kt index 1929abcc4f..beec925f09 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/composer/AudioMessageHelper.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/composer/AudioMessageHelper.kt @@ -200,7 +200,7 @@ class AudioMessageHelper @Inject constructor( amplitudeTicker?.stop() amplitudeTicker = CountUpTimer(intervalInMs = 50).apply { tickListener = CountUpTimer.TickListener { onAmplitudeTick() } - resume() + start() } } @@ -217,11 +217,7 @@ class AudioMessageHelper @Inject constructor( stopRecordingAmplitudes() } } - - private fun resumeRecordingAmplitudes() { - amplitudeTicker?.resume() - } - + private fun stopRecordingAmplitudes() { amplitudeTicker?.stop() amplitudeTicker = null @@ -231,7 +227,7 @@ class AudioMessageHelper @Inject constructor( playbackTicker?.stop() playbackTicker = CountUpTimer().apply { tickListener = CountUpTimer.TickListener { onPlaybackTick(id) } - resume() + start() } onPlaybackTick(id) } diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/composer/voice/VoiceMessageRecorderView.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/composer/voice/VoiceMessageRecorderView.kt index 526c774b6d..76656457b9 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/composer/voice/VoiceMessageRecorderView.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/composer/voice/VoiceMessageRecorderView.kt @@ -193,7 +193,7 @@ class VoiceMessageRecorderView @JvmOverloads constructor( val isLocked = startFromLocked || lastKnownState is RecordingUiState.Locked onRecordingTick(isLocked, milliseconds + startMs) } - resume() + start() } onRecordingTick(startFromLocked, milliseconds = startMs) } diff --git a/vector/src/main/java/im/vector/app/features/location/live/map/LiveLocationUserItem.kt b/vector/src/main/java/im/vector/app/features/location/live/map/LiveLocationUserItem.kt index 6d87ef99f7..fee267a46c 100644 --- a/vector/src/main/java/im/vector/app/features/location/live/map/LiveLocationUserItem.kt +++ b/vector/src/main/java/im/vector/app/features/location/live/map/LiveLocationUserItem.kt @@ -79,10 +79,12 @@ abstract class LiveLocationUserItem : VectorEpoxyModel onTick(tick) } + it.start() } }