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 {
stopTimer()
countUpTimer = CountUpTimer(100).also {
countUpTimer = CountUpTimer(intervalInMs = 100).also {
it.tickListener = CountUpTimer.TickListener {
val duration = views.videoView.duration
val progress = views.videoView.currentPosition

View file

@ -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()
}

View file

@ -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 {

View file

@ -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()
}

View file

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

View file

@ -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()
}