adding some stopwatch UI

This commit is contained in:
tibbi 2018-03-07 18:41:27 +01:00
parent 928177e8be
commit 75277a1fc4
4 changed files with 96 additions and 4 deletions

View File

@ -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'
}

View File

@ -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)
}
}
}

View File

@ -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()
}
}

View File

@ -1,8 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/stopwatch_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.design.widget.CoordinatorLayout>
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/stopwatch_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/normal_margin"
android:background="?attr/selectableItemBackground"
android:padding="@dimen/activity_margin"
android:textSize="@dimen/alarm_text_size"
tools:text="00:00"/>
<ImageView
android:id="@+id/stopwatch_play_pause"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="@dimen/big_margin"
android:padding="@dimen/normal_margin"
android:src="@drawable/ic_play"/>
</RelativeLayout>