Add optional initial time parameter in CountUpTimer

This commit is contained in:
Florian Renaud 2023-01-26 18:26:34 +01:00
parent 3ab465ea93
commit af67705778
6 changed files with 18 additions and 13 deletions

View File

@ -102,7 +102,7 @@ class VideoViewHolder constructor(itemView: View) :
views.videoView.setOnPreparedListener { views.videoView.setOnPreparedListener {
stopTimer() stopTimer()
countUpTimer = CountUpTimer(100).also { countUpTimer = CountUpTimer(intervalInMs = 100).also {
it.tickListener = CountUpTimer.TickListener { it.tickListener = CountUpTimer.TickListener {
val duration = views.videoView.duration val duration = views.videoView.duration
val progress = views.videoView.currentPosition val progress = views.videoView.currentPosition

View File

@ -28,10 +28,10 @@ import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicLong import java.util.concurrent.atomic.AtomicLong
@OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class) @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 coroutineScope = CoroutineScope(Dispatchers.Main)
private val elapsedTime: AtomicLong = AtomicLong() private val elapsedTime: AtomicLong = AtomicLong(initialTime)
private val resumed: AtomicBoolean = AtomicBoolean(false) private val resumed: AtomicBoolean = AtomicBoolean(false)
init { init {
@ -39,13 +39,13 @@ class CountUpTimer(private val intervalInMs: Long = 1_000) {
} }
private fun startCounter() { private fun startCounter() {
tickerFlow(coroutineScope, intervalInMs / 10) val internalDelay = if (intervalInMs > 100) intervalInMs / 10 else intervalInMs
tickerFlow(coroutineScope, internalDelay)
.filter { resumed.get() } .filter { resumed.get() }
.map { elapsedTime.addAndGet(intervalInMs / 10) } .map { elapsedTime.addAndGet(internalDelay) }
.filter { it % intervalInMs == 0L } .filter { (it - initialTime) % intervalInMs == 0L }
.onEach { .onEach { tickListener?.onTick(it) }
tickListener?.onTick(it) .launchIn(coroutineScope)
}.launchIn(coroutineScope)
} }
var tickListener: TickListener? = null var tickListener: TickListener? = null
@ -55,6 +55,7 @@ class CountUpTimer(private val intervalInMs: Long = 1_000) {
} }
fun pause() { fun pause() {
tickListener?.onTick(elapsedTime())
resumed.set(false) resumed.set(false)
} }
@ -63,6 +64,7 @@ class CountUpTimer(private val intervalInMs: Long = 1_000) {
} }
fun stop() { fun stop() {
tickListener?.onTick(elapsedTime())
coroutineScope.cancel() coroutineScope.cancel()
} }

View File

@ -166,7 +166,7 @@ class WebRtcCall(
private var videoSender: RtpSender? = null private var videoSender: RtpSender? = null
private var screenSender: RtpSender? = null private var screenSender: RtpSender? = null
private val timer = CountUpTimer(1000L).apply { private val timer = CountUpTimer(intervalInMs = 1000L).apply {
tickListener = CountUpTimer.TickListener { milliseconds -> tickListener = CountUpTimer.TickListener { milliseconds ->
val formattedDuration = formatDuration(Duration.ofMillis(milliseconds)) val formattedDuration = formatDuration(Duration.ofMillis(milliseconds))
listeners.forEach { listeners.forEach {

View File

@ -198,7 +198,7 @@ class AudioMessageHelper @Inject constructor(
private fun startRecordingAmplitudes() { private fun startRecordingAmplitudes() {
amplitudeTicker?.stop() amplitudeTicker?.stop()
amplitudeTicker = CountUpTimer(50).apply { amplitudeTicker = CountUpTimer(intervalInMs = 50).apply {
tickListener = CountUpTimer.TickListener { onAmplitudeTick() } tickListener = CountUpTimer.TickListener { onAmplitudeTick() }
resume() resume()
} }

View File

@ -105,7 +105,7 @@ abstract class LiveLocationUserItem : VectorEpoxyModel<LiveLocationUserItem.Hold
} }
class Holder : VectorEpoxyHolder() { class Holder : VectorEpoxyHolder() {
val timer: CountUpTimer = CountUpTimer(1000) val timer: CountUpTimer = CountUpTimer(intervalInMs = 1000)
val itemUserAvatarImageView by bind<ImageView>(R.id.itemUserAvatarImageView) val itemUserAvatarImageView by bind<ImageView>(R.id.itemUserAvatarImageView)
val itemUserDisplayNameTextView by bind<TextView>(R.id.itemUserDisplayNameTextView) val itemUserDisplayNameTextView by bind<TextView>(R.id.itemUserDisplayNameTextView)
val itemRemainingTimeTextView by bind<TextView>(R.id.itemRemainingTimeTextView) val itemRemainingTimeTextView by bind<TextView>(R.id.itemRemainingTimeTextView)

View File

@ -488,7 +488,10 @@ class VoiceBroadcastPlayerImpl @Inject constructor(
fun startPlaybackTicker(id: String) { fun startPlaybackTicker(id: String) {
playbackTicker?.stop() playbackTicker?.stop()
playbackTicker = CountUpTimer(50L).apply { playbackTicker = CountUpTimer(
initialTime = playbackTracker.getPlaybackTime(id)?.toLong() ?: 0L,
intervalInMs = 50L
).apply {
tickListener = CountUpTimer.TickListener { onPlaybackTick(id) } tickListener = CountUpTimer.TickListener { onPlaybackTick(id) }
resume() resume()
} }