mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2025-03-18 20:29:10 +03:00
Reset elapsed time on stop action and add a start method to the CountUpTimer
This commit is contained in:
parent
9383319b37
commit
912c37e3ff
8 changed files with 24 additions and 27 deletions
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -79,10 +79,12 @@ abstract class LiveLocationUserItem : VectorEpoxyModel<LiveLocationUserItem.Hold
|
|||
}
|
||||
}
|
||||
|
||||
holder.timer.tickListener = CountUpTimer.TickListener {
|
||||
holder.itemLastUpdatedAtTextView.text = getFormattedLastUpdatedAt(locationUpdateTimeMillis)
|
||||
holder.timer.apply {
|
||||
tickListener = CountUpTimer.TickListener {
|
||||
holder.itemLastUpdatedAtTextView.text = getFormattedLastUpdatedAt(locationUpdateTimeMillis)
|
||||
}
|
||||
start()
|
||||
}
|
||||
holder.timer.resume()
|
||||
|
||||
holder.view.setOnClickListener { callback?.onUserSelected(matrixItem.id) }
|
||||
}
|
||||
|
|
|
@ -492,12 +492,9 @@ class VoiceBroadcastPlayerImpl @Inject constructor(
|
|||
|
||||
fun startPlaybackTicker(id: String) {
|
||||
playbackTicker?.stop()
|
||||
playbackTicker = CountUpTimer(
|
||||
initialTime = playbackTracker.getPlaybackTime(id)?.toLong() ?: 0L,
|
||||
intervalInMs = 50L
|
||||
).apply {
|
||||
playbackTicker = CountUpTimer(intervalInMs = 50L).apply {
|
||||
tickListener = CountUpTimer.TickListener { onPlaybackTick(id, it.toInt()) }
|
||||
resume()
|
||||
start(initialTime = playbackTracker.getPlaybackTime(id)?.toLong() ?: 0L)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -245,9 +245,9 @@ class VoiceBroadcastRecorderQ(
|
|||
) {
|
||||
fun start() {
|
||||
recordingTicker?.stop()
|
||||
recordingTicker = CountUpTimer().apply {
|
||||
tickListener = CountUpTimer.TickListener { onTick(elapsedTime()) }
|
||||
resume()
|
||||
recordingTicker = CountUpTimer().also {
|
||||
it.tickListener = CountUpTimer.TickListener { tick -> onTick(tick) }
|
||||
it.start()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue