add stopwatch timer updating
This commit is contained in:
parent
2ef7285764
commit
422cbf4378
|
@ -2,23 +2,27 @@ package com.simplemobiletools.clock.extensions
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit
|
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 hours = TimeUnit.MILLISECONDS.toHours(this)
|
||||||
val minutes = TimeUnit.MILLISECONDS.toMinutes(this) - TimeUnit.HOURS.toMinutes(hours)
|
val minutes = TimeUnit.MILLISECONDS.toMinutes(this) - TimeUnit.HOURS.toMinutes(hours)
|
||||||
val seconds = TimeUnit.MILLISECONDS.toSeconds(this) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(this))
|
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 {
|
return when {
|
||||||
hours > 0 -> {
|
hours > 0 -> {
|
||||||
val format = "%02d:%02d:%02d.%02d"
|
val format = "%02d:%02d:%d.$MSFormat"
|
||||||
String.format(format, hours, minutes, seconds, ms)
|
String.format(format, hours, minutes, seconds, ms)
|
||||||
}
|
}
|
||||||
minutes > 0 -> {
|
minutes > 0 -> {
|
||||||
val format = "%02d:%02d.%02d"
|
val format = "%02d:%d.$MSFormat"
|
||||||
String.format(format, minutes, seconds, ms)
|
String.format(format, minutes, seconds, ms)
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
val format = "%02d.%02d"
|
val format = "%d.$MSFormat"
|
||||||
String.format(format, seconds, ms)
|
String.format(format, seconds, ms)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,8 @@ package com.simplemobiletools.clock.fragments
|
||||||
import android.graphics.Color
|
import android.graphics.Color
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
|
import android.os.Looper
|
||||||
|
import android.os.SystemClock
|
||||||
import android.support.v4.app.Fragment
|
import android.support.v4.app.Fragment
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
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.getAdjustedPrimaryColor
|
||||||
import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor
|
import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor
|
||||||
import com.simplemobiletools.commons.extensions.updateTextColors
|
import com.simplemobiletools.commons.extensions.updateTextColors
|
||||||
import kotlinx.android.synthetic.main.fragment_stopwatch.*
|
|
||||||
import kotlinx.android.synthetic.main.fragment_stopwatch.view.*
|
import kotlinx.android.synthetic.main.fragment_stopwatch.view.*
|
||||||
|
|
||||||
class StopwatchFragment : Fragment() {
|
class StopwatchFragment : Fragment() {
|
||||||
|
private val UPDATE_INTERVAL = 10L
|
||||||
|
|
||||||
private val updateHandler = Handler()
|
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
|
private var isRunning = false
|
||||||
|
|
||||||
lateinit var view: ViewGroup
|
lateinit var view: ViewGroup
|
||||||
|
@ -52,11 +57,11 @@ class StopwatchFragment : Fragment() {
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
updateHandler.removeCallbacksAndMessages(null)
|
isRunning = false
|
||||||
|
updateHandler.removeCallbacks(updateRunnable)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setupStopwatch() {
|
private fun setupStopwatch() {
|
||||||
stopwatch_time.text = currMS.formatStopwatchTime()
|
|
||||||
setupViews()
|
setupViews()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,6 +71,7 @@ class StopwatchFragment : Fragment() {
|
||||||
view.stopwatch_play_pause.background = resources.getColoredDrawableWithColor(R.drawable.circle_background_filled, getAdjustedPrimaryColor())
|
view.stopwatch_play_pause.background = resources.getColoredDrawableWithColor(R.drawable.circle_background_filled, getAdjustedPrimaryColor())
|
||||||
}
|
}
|
||||||
updatePlayPauseIcon()
|
updatePlayPauseIcon()
|
||||||
|
updateDisplayedText()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updatePlayPauseIcon() {
|
private fun updatePlayPauseIcon() {
|
||||||
|
@ -78,5 +84,32 @@ class StopwatchFragment : Fragment() {
|
||||||
isRunning = !isRunning
|
isRunning = !isRunning
|
||||||
updatePlayPauseIcon()
|
updatePlayPauseIcon()
|
||||||
view.stopwatch_lap.beVisibleIf(isRunning)
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue