mirror of
https://github.com/SimpleMobileTools/Simple-Clock.git
synced 2025-02-02 11:36:52 +01:00
do not reset Stopwatch at device rotation
This commit is contained in:
parent
597f2ebc17
commit
51513e625d
@ -25,6 +25,10 @@ import kotlinx.android.synthetic.main.fragment_stopwatch.view.*
|
|||||||
|
|
||||||
class StopwatchFragment : Fragment() {
|
class StopwatchFragment : Fragment() {
|
||||||
private val UPDATE_INTERVAL = 10L
|
private val UPDATE_INTERVAL = 10L
|
||||||
|
private val WAS_RUNNING = "was_running"
|
||||||
|
private val TOTAL_TICKS = "total_ticks"
|
||||||
|
private val CURRENT_TICKS = "current_ticks"
|
||||||
|
private val LAP_TICKS = "lap_ticks"
|
||||||
|
|
||||||
private val updateHandler = Handler()
|
private val updateHandler = Handler()
|
||||||
private var uptimeAtStart = 0L
|
private var uptimeAtStart = 0L
|
||||||
@ -124,7 +128,7 @@ class StopwatchFragment : Fragment() {
|
|||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
if (isRunning) {
|
if (isRunning && activity?.isChangingConfigurations == false) {
|
||||||
context?.toast(R.string.stopwatch_stopped)
|
context?.toast(R.string.stopwatch_stopped)
|
||||||
}
|
}
|
||||||
isRunning = false
|
isRunning = false
|
||||||
@ -135,6 +139,28 @@ class StopwatchFragment : Fragment() {
|
|||||||
storedTextColor = context!!.config.textColor
|
storedTextColor = context!!.config.textColor
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onSaveInstanceState(outState: Bundle) {
|
||||||
|
outState.putBoolean(WAS_RUNNING, isRunning)
|
||||||
|
outState.putInt(TOTAL_TICKS, totalTicks)
|
||||||
|
outState.putInt(CURRENT_TICKS, currentTicks)
|
||||||
|
outState.putInt(LAP_TICKS, lapTicks)
|
||||||
|
super.onSaveInstanceState(outState)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onViewStateRestored(savedInstanceState: Bundle?) {
|
||||||
|
super.onViewStateRestored(savedInstanceState)
|
||||||
|
if (savedInstanceState != null) {
|
||||||
|
isRunning = savedInstanceState.getBoolean(WAS_RUNNING, false)
|
||||||
|
totalTicks = savedInstanceState.getInt(TOTAL_TICKS, 0)
|
||||||
|
currentTicks = savedInstanceState.getInt(CURRENT_TICKS, 0)
|
||||||
|
lapTicks = savedInstanceState.getInt(LAP_TICKS, 0)
|
||||||
|
if (isRunning) {
|
||||||
|
uptimeAtStart = SystemClock.uptimeMillis() - currentTicks * UPDATE_INTERVAL
|
||||||
|
updateStopwatchState(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun setupViews() {
|
private fun setupViews() {
|
||||||
val adjustedPrimaryColor = context!!.getAdjustedPrimaryColor()
|
val adjustedPrimaryColor = context!!.getAdjustedPrimaryColor()
|
||||||
view.apply {
|
view.apply {
|
||||||
@ -155,13 +181,19 @@ class StopwatchFragment : Fragment() {
|
|||||||
|
|
||||||
private fun togglePlayPause() {
|
private fun togglePlayPause() {
|
||||||
isRunning = !isRunning
|
isRunning = !isRunning
|
||||||
|
updateStopwatchState(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun updateStopwatchState(setUptimeAtStart: Boolean) {
|
||||||
updateIcons()
|
updateIcons()
|
||||||
view.stopwatch_lap.beVisibleIf(isRunning)
|
view.stopwatch_lap.beVisibleIf(isRunning)
|
||||||
|
|
||||||
if (isRunning) {
|
if (isRunning) {
|
||||||
updateHandler.post(updateRunnable)
|
updateHandler.post(updateRunnable)
|
||||||
uptimeAtStart = SystemClock.uptimeMillis()
|
|
||||||
view.stopwatch_reset.beVisible()
|
view.stopwatch_reset.beVisible()
|
||||||
|
if (setUptimeAtStart) {
|
||||||
|
uptimeAtStart = SystemClock.uptimeMillis()
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
val prevSessionsMS = (totalTicks - currentTicks) * UPDATE_INTERVAL
|
val prevSessionsMS = (totalTicks - currentTicks) * UPDATE_INTERVAL
|
||||||
val totalDuration = SystemClock.uptimeMillis() - uptimeAtStart + prevSessionsMS
|
val totalDuration = SystemClock.uptimeMillis() - uptimeAtStart + prevSessionsMS
|
||||||
|
Loading…
x
Reference in New Issue
Block a user