add some Timer fragment functionality

This commit is contained in:
tibbi 2018-03-08 21:02:19 +01:00
parent a7c915b4fd
commit 7ef63b76e4
3 changed files with 114 additions and 17 deletions

View File

@ -5,7 +5,6 @@ import android.graphics.Color
import android.graphics.Matrix
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
@ -28,7 +27,6 @@ class StopwatchFragment : Fragment() {
private val UPDATE_INTERVAL = 10L
private val updateHandler = Handler()
private val mainLooper = Looper.getMainLooper()
private var uptimeAtStart = 0L
private var totalTicks = 0
private var currentTicks = 0 // ticks that reset at pause
@ -97,7 +95,7 @@ class StopwatchFragment : Fragment() {
override fun onResume() {
super.onResume()
setupStopwatch()
setupViews()
}
override fun onDestroy() {
@ -106,10 +104,6 @@ class StopwatchFragment : Fragment() {
updateHandler.removeCallbacks(updateRunnable)
}
private fun setupStopwatch() {
setupViews()
}
private fun setupViews() {
val adjustedPrimaryColor = context!!.getAdjustedPrimaryColor()
view.apply {
@ -221,10 +215,8 @@ class StopwatchFragment : Fragment() {
override fun run() {
if (isRunning) {
if (totalTicks % 10 == 0) {
mainLooper.run {
updateDisplayedText()
}
}
totalTicks++
currentTicks++
lapTicks++

View File

@ -1,24 +1,47 @@
package com.simplemobiletools.clock.fragments
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.os.SystemClock
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.simplemobiletools.clock.R
import com.simplemobiletools.clock.extensions.config
import com.simplemobiletools.commons.extensions.getFormattedDuration
import com.simplemobiletools.commons.extensions.updateTextColors
import com.simplemobiletools.commons.extensions.*
import kotlinx.android.synthetic.main.fragment_timer.view.*
class TimerFragment : Fragment() {
private val UPDATE_INTERVAL = 1000L
private var isRunning = false
private var uptimeAtStart = 0L
private var initialSecs = 0
private var totalTicks = 0
private var currentTicks = 0
private var updateHandler = Handler()
lateinit var view: ViewGroup
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
view = (inflater.inflate(R.layout.fragment_timer, container, false) as ViewGroup).apply {
timer_time.text = context!!.config.lastTimerSeconds.getFormattedDuration()
timer_time.setOnClickListener {
togglePlayPause()
}
timer_play_pause.setOnClickListener {
togglePlayPause()
}
timer_reset.setOnClickListener {
resetTimer()
}
}
initialSecs = context!!.config.lastTimerSeconds
updateDisplayedText()
return view
}
@ -27,14 +50,67 @@ class TimerFragment : Fragment() {
setupViews()
}
override fun onDestroy() {
super.onDestroy()
isRunning = false
updateHandler.removeCallbacks(updateRunnable)
}
private fun setupViews() {
view.apply {
context!!.updateTextColors(timer_fragment)
}
setupTimer()
timer_play_pause.background = resources.getColoredDrawableWithColor(R.drawable.circle_background_filled, context!!.getAdjustedPrimaryColor())
timer_reset.applyColorFilter(context!!.config.textColor)
}
private fun setupTimer() {
updateIcons()
updateDisplayedText()
}
private fun togglePlayPause() {
isRunning = !isRunning
updateIcons()
if (isRunning) {
updateHandler.post(updateRunnable)
uptimeAtStart = SystemClock.uptimeMillis()
view.timer_reset.beVisible()
} else {
updateHandler.removeCallbacksAndMessages(null)
currentTicks = 0
totalTicks--
}
}
private fun updateIcons() {
val drawableId = if (isRunning) R.drawable.ic_pause else R.drawable.ic_play
val iconColor = if (context!!.getAdjustedPrimaryColor() == Color.WHITE) Color.BLACK else context!!.config.textColor
view.timer_play_pause.setImageDrawable(resources.getColoredDrawableWithColor(drawableId, iconColor))
}
private fun resetTimer() {
updateHandler.removeCallbacksAndMessages(null)
isRunning = false
currentTicks = 0
totalTicks = 0
initialSecs = context!!.config.lastTimerSeconds
updateDisplayedText()
updateIcons()
view.timer_reset.beGone()
}
private fun updateDisplayedText() {
view.timer_time.text = (initialSecs - totalTicks).getFormattedDuration()
}
private val updateRunnable = object : Runnable {
override fun run() {
if (isRunning) {
updateDisplayedText()
currentTicks++
totalTicks++
updateHandler.postAtTime(this, uptimeAtStart + currentTicks * UPDATE_INTERVAL)
}
}
}
}

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
@ -21,4 +21,33 @@
app:layout_constraintTop_toTopOf="parent"
tools:text="00:00"/>
</android.support.design.widget.CoordinatorLayout>
<ImageView
android:id="@+id/timer_reset"
style="@style/MyBorderlessBackgroundStyle"
android:layout_width="@dimen/stopwatch_button_small_size"
android:layout_height="@dimen/stopwatch_button_small_size"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/timer_play_pause"
android:padding="@dimen/normal_margin"
android:src="@drawable/ic_reset"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="@+id/timer_play_pause"
app:layout_constraintEnd_toStartOf="@+id/timer_play_pause"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/timer_play_pause"/>
<ImageView
android:id="@+id/timer_play_pause"
android:layout_width="@dimen/stopwatch_button_size"
android:layout_height="@dimen/stopwatch_button_size"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="@dimen/big_margin"
android:padding="@dimen/normal_margin"
android:src="@drawable/ic_play"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"/>
</android.support.constraint.ConstraintLayout>