mirror of
https://github.com/SimpleMobileTools/Simple-Clock.git
synced 2025-02-12 09:50:40 +01:00
properly handle stopwatch pause/resume
This commit is contained in:
parent
422cbf4378
commit
caef58df54
@ -24,7 +24,8 @@ class StopwatchFragment : Fragment() {
|
|||||||
private val updateHandler = Handler()
|
private val updateHandler = Handler()
|
||||||
private val mainLooper = Looper.getMainLooper()
|
private val mainLooper = Looper.getMainLooper()
|
||||||
private var uptimeAtStart = 0L
|
private var uptimeAtStart = 0L
|
||||||
private var ticksCount = 0
|
private var totalTicks = 0
|
||||||
|
private var currentTicks = 0 // ticks that reset at pause
|
||||||
private var isRunning = false
|
private var isRunning = false
|
||||||
|
|
||||||
lateinit var view: ViewGroup
|
lateinit var view: ViewGroup
|
||||||
@ -89,26 +90,30 @@ class StopwatchFragment : Fragment() {
|
|||||||
updateHandler.post(updateRunnable)
|
updateHandler.post(updateRunnable)
|
||||||
uptimeAtStart = SystemClock.uptimeMillis()
|
uptimeAtStart = SystemClock.uptimeMillis()
|
||||||
} else {
|
} else {
|
||||||
val totalDuration = SystemClock.uptimeMillis() - uptimeAtStart
|
val prevSessionsMS = (totalTicks - currentTicks) * UPDATE_INTERVAL
|
||||||
|
val totalDuration = SystemClock.uptimeMillis() - uptimeAtStart + prevSessionsMS
|
||||||
updateHandler.removeCallbacksAndMessages(null)
|
updateHandler.removeCallbacksAndMessages(null)
|
||||||
view.stopwatch_time.text = totalDuration.formatStopwatchTime(true)
|
view.stopwatch_time.text = totalDuration.formatStopwatchTime(true)
|
||||||
|
currentTicks = 0
|
||||||
|
totalTicks--
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateDisplayedText() {
|
private fun updateDisplayedText() {
|
||||||
view.stopwatch_time.text = (ticksCount * UPDATE_INTERVAL).formatStopwatchTime(false)
|
view.stopwatch_time.text = (totalTicks * UPDATE_INTERVAL).formatStopwatchTime(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
private val updateRunnable = object : Runnable {
|
private val updateRunnable = object : Runnable {
|
||||||
override fun run() {
|
override fun run() {
|
||||||
if (isRunning) {
|
if (isRunning) {
|
||||||
ticksCount++
|
if (totalTicks % 10 == 0) {
|
||||||
updateHandler.postAtTime(this, uptimeAtStart + ticksCount * UPDATE_INTERVAL)
|
|
||||||
if (ticksCount % 10 == 0) {
|
|
||||||
mainLooper.run {
|
mainLooper.run {
|
||||||
updateDisplayedText()
|
updateDisplayedText()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
totalTicks++
|
||||||
|
currentTicks++
|
||||||
|
updateHandler.postAtTime(this, uptimeAtStart + currentTicks * UPDATE_INTERVAL)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user