diff --git a/app/build.gradle b/app/build.gradle index 46c5e2f9..8baf614f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -41,7 +41,7 @@ android { } dependencies { - implementation 'com.simplemobiletools:commons:3.15.6' + implementation 'com.simplemobiletools:commons:3.15.7' implementation 'com.facebook.stetho:stetho:1.5.0' implementation 'com.android.support.constraint:constraint-layout:1.0.2' } diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Long.kt b/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Long.kt new file mode 100644 index 00000000..571a2b1d --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Long.kt @@ -0,0 +1,25 @@ +package com.simplemobiletools.clock.extensions + +import java.util.concurrent.TimeUnit + +fun Long.formatStopwatchTime(): String { + 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 + + return when { + hours > 0 -> { + val format = "%02d:%02d:%02d.%02d" + String.format(format, hours, minutes, seconds, ms) + } + minutes > 0 -> { + val format = "%02d:%02d.%02d" + String.format(format, minutes, seconds, ms) + } + else -> { + val format = "%02d.%02d" + 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 37fb736d..e331a253 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/StopwatchFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/StopwatchFragment.kt @@ -1,17 +1,62 @@ package com.simplemobiletools.clock.fragments +import android.graphics.Color import android.os.Bundle +import android.os.Handler 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.clock.extensions.formatStopwatchTime +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 updateHandler = Handler() + private var currMS = 0L + private var isRunning = false + lateinit var view: ViewGroup override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { - view = inflater.inflate(R.layout.fragment_stopwatch, container, false) as ViewGroup + view = (inflater.inflate(R.layout.fragment_stopwatch, container, false) as ViewGroup).apply { + stopwatch_time.setOnClickListener { togglePlayPause() } + stopwatch_play_pause.setOnClickListener { togglePlayPause() } + } return view } + + override fun onResume() { + super.onResume() + setupStopwatch() + } + + private fun setupStopwatch() { + stopwatch_time.text = currMS.formatStopwatchTime() + setupViews() + } + + private fun setupViews() { + context!!.apply { + updateTextColors(view.stopwatch_fragment) + view.stopwatch_play_pause.background = resources.getColoredDrawableWithColor(R.drawable.circle_background_filled, getAdjustedPrimaryColor()) + updatePlayPauseIcon() + } + } + + private fun updatePlayPauseIcon() { + 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.stopwatch_play_pause.setImageDrawable(resources.getColoredDrawableWithColor(drawableId, iconColor)) + } + + private fun togglePlayPause() { + isRunning = !isRunning + updatePlayPauseIcon() + } } diff --git a/app/src/main/res/layout/fragment_stopwatch.xml b/app/src/main/res/layout/fragment_stopwatch.xml index 6991f03e..c5d55e5e 100644 --- a/app/src/main/res/layout/fragment_stopwatch.xml +++ b/app/src/main/res/layout/fragment_stopwatch.xml @@ -1,8 +1,30 @@ - - + + + + +