From af67705778bff9a769a85b12018f9d8cdd341e8d Mon Sep 17 00:00:00 2001 From: Florian Renaud Date: Thu, 26 Jan 2023 18:26:34 +0100 Subject: [PATCH] Add optional initial time parameter in CountUpTimer --- .../lib/attachmentviewer/VideoViewHolder.kt | 2 +- .../lib/core/utils/timer/CountUpTimer.kt | 18 ++++++++++-------- .../app/features/call/webrtc/WebRtcCall.kt | 2 +- .../room/detail/composer/AudioMessageHelper.kt | 2 +- .../location/live/map/LiveLocationUserItem.kt | 2 +- .../listening/VoiceBroadcastPlayerImpl.kt | 5 ++++- 6 files changed, 18 insertions(+), 13 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 07c7b4588f..30da59750e 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 @@ -102,7 +102,7 @@ class VideoViewHolder constructor(itemView: View) : views.videoView.setOnPreparedListener { stopTimer() - countUpTimer = CountUpTimer(100).also { + countUpTimer = CountUpTimer(intervalInMs = 100).also { it.tickListener = CountUpTimer.TickListener { val duration = views.videoView.duration val progress = views.videoView.currentPosition 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 a4fd8bb4e1..297e33d9cc 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 @@ -28,10 +28,10 @@ import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.atomic.AtomicLong @OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class) -class CountUpTimer(private val intervalInMs: Long = 1_000) { +class CountUpTimer(private val initialTime: Long = 0L, private val intervalInMs: Long = 1_000) { private val coroutineScope = CoroutineScope(Dispatchers.Main) - private val elapsedTime: AtomicLong = AtomicLong() + private val elapsedTime: AtomicLong = AtomicLong(initialTime) private val resumed: AtomicBoolean = AtomicBoolean(false) init { @@ -39,13 +39,13 @@ class CountUpTimer(private val intervalInMs: Long = 1_000) { } private fun startCounter() { - tickerFlow(coroutineScope, intervalInMs / 10) + val internalDelay = if (intervalInMs > 100) intervalInMs / 10 else intervalInMs + tickerFlow(coroutineScope, internalDelay) .filter { resumed.get() } - .map { elapsedTime.addAndGet(intervalInMs / 10) } - .filter { it % intervalInMs == 0L } - .onEach { - tickListener?.onTick(it) - }.launchIn(coroutineScope) + .map { elapsedTime.addAndGet(internalDelay) } + .filter { (it - initialTime) % intervalInMs == 0L } + .onEach { tickListener?.onTick(it) } + .launchIn(coroutineScope) } var tickListener: TickListener? = null @@ -55,6 +55,7 @@ class CountUpTimer(private val intervalInMs: Long = 1_000) { } fun pause() { + tickListener?.onTick(elapsedTime()) resumed.set(false) } @@ -63,6 +64,7 @@ class CountUpTimer(private val intervalInMs: Long = 1_000) { } fun stop() { + tickListener?.onTick(elapsedTime()) coroutineScope.cancel() } diff --git a/vector/src/main/java/im/vector/app/features/call/webrtc/WebRtcCall.kt b/vector/src/main/java/im/vector/app/features/call/webrtc/WebRtcCall.kt index 0bf70690ba..9c65d94a94 100644 --- a/vector/src/main/java/im/vector/app/features/call/webrtc/WebRtcCall.kt +++ b/vector/src/main/java/im/vector/app/features/call/webrtc/WebRtcCall.kt @@ -166,7 +166,7 @@ class WebRtcCall( private var videoSender: RtpSender? = null private var screenSender: RtpSender? = null - private val timer = CountUpTimer(1000L).apply { + private val timer = CountUpTimer(intervalInMs = 1000L).apply { tickListener = CountUpTimer.TickListener { milliseconds -> val formattedDuration = formatDuration(Duration.ofMillis(milliseconds)) listeners.forEach { 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 900de041d0..1929abcc4f 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 @@ -198,7 +198,7 @@ class AudioMessageHelper @Inject constructor( private fun startRecordingAmplitudes() { amplitudeTicker?.stop() - amplitudeTicker = CountUpTimer(50).apply { + amplitudeTicker = CountUpTimer(intervalInMs = 50).apply { tickListener = CountUpTimer.TickListener { onAmplitudeTick() } resume() } 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 c108e83e76..724b2c9b6f 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 @@ -105,7 +105,7 @@ abstract class LiveLocationUserItem : VectorEpoxyModel(R.id.itemUserAvatarImageView) val itemUserDisplayNameTextView by bind(R.id.itemUserDisplayNameTextView) val itemRemainingTimeTextView by bind(R.id.itemRemainingTimeTextView) diff --git a/vector/src/main/java/im/vector/app/features/voicebroadcast/listening/VoiceBroadcastPlayerImpl.kt b/vector/src/main/java/im/vector/app/features/voicebroadcast/listening/VoiceBroadcastPlayerImpl.kt index 2559f1a7d6..c356ed5500 100644 --- a/vector/src/main/java/im/vector/app/features/voicebroadcast/listening/VoiceBroadcastPlayerImpl.kt +++ b/vector/src/main/java/im/vector/app/features/voicebroadcast/listening/VoiceBroadcastPlayerImpl.kt @@ -488,7 +488,10 @@ class VoiceBroadcastPlayerImpl @Inject constructor( fun startPlaybackTicker(id: String) { playbackTicker?.stop() - playbackTicker = CountUpTimer(50L).apply { + playbackTicker = CountUpTimer( + initialTime = playbackTracker.getPlaybackTime(id)?.toLong() ?: 0L, + intervalInMs = 50L + ).apply { tickListener = CountUpTimer.TickListener { onPlaybackTick(id) } resume() }