Be more precise if the timer is paused and resume.

This commit is contained in:
Benoit Marty 2021-07-13 14:12:58 +02:00 committed by Benoit Marty
parent 83bf48dfff
commit c69bc12637

View file

@ -17,6 +17,7 @@
package im.vector.app.core.utils package im.vector.app.core.utils
import io.reactivex.Observable import io.reactivex.Observable
import timber.log.Timber
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicLong import java.util.concurrent.atomic.AtomicLong
@ -26,11 +27,12 @@ class CountUpTimer(private val intervalInMs: Long) {
private val elapsedTime: AtomicLong = AtomicLong() private val elapsedTime: AtomicLong = AtomicLong()
private val resumed: AtomicBoolean = AtomicBoolean(false) private val resumed: AtomicBoolean = AtomicBoolean(false)
private val disposable = Observable.interval(intervalInMs, TimeUnit.MILLISECONDS) private val disposable = Observable.interval(intervalInMs / 10, TimeUnit.MILLISECONDS)
.filter { resumed.get() } .filter { resumed.get() }
.doOnNext { elapsedTime.addAndGet(intervalInMs) } .map { elapsedTime.addAndGet(intervalInMs / 10) }
.filter { it % intervalInMs == 0L }
.subscribe { .subscribe {
tickListener?.onTick(elapsedTime.get()) tickListener?.onTick(it)
} }
var tickListener: TickListener? = null var tickListener: TickListener? = null