Skip to content

Commit

Permalink
Add example timers (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
Myzel394 authored Jul 17, 2023
1 parent deb0b78 commit 20fb7a7
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
35 changes: 35 additions & 0 deletions app/src/main/java/com/bnyro/clock/ui/common/ExampleTimers.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.bnyro.clock.ui.common

data class ExampleTimer(
val seconds: Int,
) {
// Write a getter for formattedTime that returns a String in the format of "HH:MM:SS"
val formattedTime: String
get() {
val hours = seconds / 3600;
val minutes = (seconds % 3600) / 60;
val seconds = seconds % 60;

return if (hours == 0) {
String.format("%02d:%02d", minutes, seconds);
} else {
String.format("%02d:%02d:%02d", hours, minutes, seconds);
}
}

companion object {
val exampleTimers = listOf<Int>(
60,
60 * 2,
60 * 5,
60 * 10,
60 * 13,
60 * 15,
60 * 20,
60 * 30,
60 * 60,
60 * 90,
60 * 120,
).map { ExampleTimer(it) }
}
}
14 changes: 14 additions & 0 deletions app/src/main/java/com/bnyro/clock/ui/model/TimerModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,18 @@ class TimerModel : ViewModel() {
fun clear() {
timePickerSecondsState = INITIAL_SECONDS_STATE
}

fun setSeconds(seconds: Int) {
val remainingSeconds = seconds % 60
val minutes = (seconds - remainingSeconds) / 60
val remainingMinutes = minutes % 60
val hours = (minutes - remainingMinutes) / 60
val remainingHours = hours % 24

timePickerSecondsState = INITIAL_SECONDS_STATE

addSeconds(remainingSeconds)
addMinutes(remainingMinutes)
addHours(remainingHours)
}
}
28 changes: 28 additions & 0 deletions app/src/main/java/com/bnyro/clock/ui/screens/TimerScreen.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.bnyro.clock.ui.screens

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
Expand Down Expand Up @@ -43,6 +46,10 @@ import com.bnyro.clock.R
import com.bnyro.clock.extensions.addZero
import com.bnyro.clock.obj.NumberKeypadOperation
import com.bnyro.clock.obj.WatchState
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import com.bnyro.clock.ui.common.ExampleTimer
import com.bnyro.clock.ui.components.ClickableIcon
import com.bnyro.clock.ui.components.DialogButton
import com.bnyro.clock.ui.components.FormattedTimerTime
Expand Down Expand Up @@ -128,6 +135,7 @@ fun TimerScreen(timerModel: TimerModel) {
)
}
}
Spacer(modifier = Modifier.height(16.dp))
Row(
modifier = Modifier.padding(bottom = 16.dp)
) {
Expand All @@ -149,6 +157,26 @@ fun TimerScreen(timerModel: TimerModel) {
Icon(imageVector = Icons.Default.PlayArrow, contentDescription = null)
}
}
LazyRow(
contentPadding = PaddingValues(horizontal = 16.dp),
horizontalArrangement = Arrangement.spacedBy(16.dp),
) {
items(ExampleTimer.exampleTimers) { timer ->
Button(
onClick = {
timerModel.setSeconds(timer.seconds)
createNew = false
timerModel.startTimer(context)
},
colors = ButtonDefaults.filledTonalButtonColors(),
) {
Text(
timer.formattedTime,
)
}
}
}
Spacer(modifier = Modifier.height(32.dp))
}
} else {
LazyColumn(
Expand Down

0 comments on commit 20fb7a7

Please sign in to comment.