From 422cbf43786b8b133ed39c6835ddde444f7034bf Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 7 Mar 2018 21:41:39 +0100 Subject: [PATCH] add stopwatch timer updating --- .../clock/extensions/Long.kt | 14 ++++--- .../clock/fragments/StopwatchFragment.kt | 41 +++++++++++++++++-- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Long.kt b/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Long.kt index 571a2b1d..4e670890 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Long.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Long.kt @@ -2,23 +2,27 @@ package com.simplemobiletools.clock.extensions import java.util.concurrent.TimeUnit -fun Long.formatStopwatchTime(): String { +fun Long.formatStopwatchTime(useLongerMSFormat: Boolean): String { + val MSFormat = if (useLongerMSFormat) "%03d" else "%01d" val hours = TimeUnit.MILLISECONDS.toHours(this) val minutes = TimeUnit.MILLISECONDS.toMinutes(this) - TimeUnit.HOURS.toMinutes(hours) val seconds = TimeUnit.MILLISECONDS.toSeconds(this) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(this)) - val ms = (this % 1000) / 10 + var ms = this % 1000 + if (!useLongerMSFormat) { + ms /= 100 + } return when { hours > 0 -> { - val format = "%02d:%02d:%02d.%02d" + val format = "%02d:%02d:%d.$MSFormat" String.format(format, hours, minutes, seconds, ms) } minutes > 0 -> { - val format = "%02d:%02d.%02d" + val format = "%02d:%d.$MSFormat" String.format(format, minutes, seconds, ms) } else -> { - val format = "%02d.%02d" + val format = "%d.$MSFormat" String.format(format, seconds, ms) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/StopwatchFragment.kt b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/StopwatchFragment.kt index 49a81914..a9b14690 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/StopwatchFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/StopwatchFragment.kt @@ -3,6 +3,8 @@ package com.simplemobiletools.clock.fragments import android.graphics.Color import android.os.Bundle import android.os.Handler +import android.os.Looper +import android.os.SystemClock import android.support.v4.app.Fragment import android.view.LayoutInflater import android.view.View @@ -14,12 +16,15 @@ import com.simplemobiletools.commons.extensions.beVisibleIf import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor import com.simplemobiletools.commons.extensions.updateTextColors -import kotlinx.android.synthetic.main.fragment_stopwatch.* import kotlinx.android.synthetic.main.fragment_stopwatch.view.* class StopwatchFragment : Fragment() { + private val UPDATE_INTERVAL = 10L + private val updateHandler = Handler() - private var currMS = 0L + private val mainLooper = Looper.getMainLooper() + private var uptimeAtStart = 0L + private var ticksCount = 0 private var isRunning = false lateinit var view: ViewGroup @@ -52,11 +57,11 @@ class StopwatchFragment : Fragment() { override fun onDestroy() { super.onDestroy() - updateHandler.removeCallbacksAndMessages(null) + isRunning = false + updateHandler.removeCallbacks(updateRunnable) } private fun setupStopwatch() { - stopwatch_time.text = currMS.formatStopwatchTime() setupViews() } @@ -66,6 +71,7 @@ class StopwatchFragment : Fragment() { view.stopwatch_play_pause.background = resources.getColoredDrawableWithColor(R.drawable.circle_background_filled, getAdjustedPrimaryColor()) } updatePlayPauseIcon() + updateDisplayedText() } private fun updatePlayPauseIcon() { @@ -78,5 +84,32 @@ class StopwatchFragment : Fragment() { isRunning = !isRunning updatePlayPauseIcon() view.stopwatch_lap.beVisibleIf(isRunning) + + if (isRunning) { + updateHandler.post(updateRunnable) + uptimeAtStart = SystemClock.uptimeMillis() + } else { + val totalDuration = SystemClock.uptimeMillis() - uptimeAtStart + updateHandler.removeCallbacksAndMessages(null) + view.stopwatch_time.text = totalDuration.formatStopwatchTime(true) + } + } + + private fun updateDisplayedText() { + view.stopwatch_time.text = (ticksCount * UPDATE_INTERVAL).formatStopwatchTime(false) + } + + private val updateRunnable = object : Runnable { + override fun run() { + if (isRunning) { + ticksCount++ + updateHandler.postAtTime(this, uptimeAtStart + ticksCount * UPDATE_INTERVAL) + if (ticksCount % 10 == 0) { + mainLooper.run { + updateDisplayedText() + } + } + } + } } }