fix #33, do not stop the timer on device rotation

This commit is contained in:
tibbi 2018-04-11 17:07:41 +02:00
parent bbc3704dd7
commit 8ed6db98fc

View File

@ -24,6 +24,9 @@ import kotlinx.android.synthetic.main.fragment_timer.view.*
class TimerFragment : Fragment() { class TimerFragment : Fragment() {
private val UPDATE_INTERVAL = 1000L private val UPDATE_INTERVAL = 1000L
private val WAS_RUNNING = "was_running"
private val CURRENT_TICKS = "current_ticks"
private val TOTAL_TICKS = "total_ticks"
private var isRunning = false private var isRunning = false
private var uptimeAtStart = 0L private var uptimeAtStart = 0L
@ -105,13 +108,36 @@ class TimerFragment : Fragment() {
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
if (isRunning) { if (isRunning && activity?.isChangingConfigurations == false) {
context?.toast(R.string.timer_stopped) context?.toast(R.string.timer_stopped)
} }
isRunning = false isRunning = false
updateHandler.removeCallbacks(updateRunnable) updateHandler.removeCallbacks(updateRunnable)
} }
override fun onSaveInstanceState(outState: Bundle) {
outState.apply {
putBoolean(WAS_RUNNING, isRunning)
putInt(TOTAL_TICKS, totalTicks)
putInt(CURRENT_TICKS, currentTicks)
}
super.onSaveInstanceState(outState)
}
override fun onViewStateRestored(savedInstanceState: Bundle?) {
super.onViewStateRestored(savedInstanceState)
savedInstanceState?.apply {
isRunning = getBoolean(WAS_RUNNING, false)
totalTicks = getInt(TOTAL_TICKS, 0)
currentTicks = getInt(CURRENT_TICKS, 0)
if (isRunning) {
uptimeAtStart = SystemClock.uptimeMillis() - currentTicks * UPDATE_INTERVAL
updateTimerState(false)
}
}
}
fun updateAlarmSound(alarmSound: AlarmSound) { fun updateAlarmSound(alarmSound: AlarmSound) {
context!!.config.timerSoundTitle = alarmSound.title context!!.config.timerSoundTitle = alarmSound.title
context!!.config.timerSoundUri = alarmSound.uri context!!.config.timerSoundUri = alarmSound.uri
@ -142,13 +168,19 @@ class TimerFragment : Fragment() {
private fun togglePlayPause() { private fun togglePlayPause() {
isRunning = !isRunning isRunning = !isRunning
updateTimerState(true)
}
private fun updateTimerState(setUptimeAtStart: Boolean) {
updateIcons() updateIcons()
context!!.hideTimerNotification() context!!.hideTimerNotification()
if (isRunning) { if (isRunning) {
updateHandler.post(updateRunnable) updateHandler.post(updateRunnable)
uptimeAtStart = SystemClock.uptimeMillis()
view.timer_reset.beVisible() view.timer_reset.beVisible()
if (setUptimeAtStart) {
uptimeAtStart = SystemClock.uptimeMillis()
}
} else { } else {
updateHandler.removeCallbacksAndMessages(null) updateHandler.removeCallbacksAndMessages(null)
currentTicks = 0 currentTicks = 0