Skip to content

Commit

Permalink
fix: stopwatch time format (closes #335)
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa committed Apr 28, 2024
1 parent 2afeeb7 commit dd19e70
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private fun StopwatchController(
contentDescription = null
)
}
AnimatedVisibility(stopwatchModel.currentPosition != 0) {
AnimatedVisibility(stopwatchModel.currentPosition != 0L) {
Row {
Spacer(modifier = Modifier.width(20.dp))
if (stopwatchModel.state != WatchState.PAUSED) {
Expand Down Expand Up @@ -283,14 +283,22 @@ private fun TimeDisplay(
Row(
verticalAlignment = Alignment.Bottom
) {
val minutes = stopwatchModel.currentPosition / 60000
val hours = stopwatchModel.currentPosition.div(1000 * 60 * 60)
val minutes = stopwatchModel.currentPosition.div(1000 * 60).mod(60)
val seconds =
(stopwatchModel.currentPosition % 60000) / 1000
stopwatchModel.currentPosition.div(1000).mod(60)
val hundreds =
stopwatchModel.currentPosition % 1000 / 10
stopwatchModel.currentPosition.div(10).mod(100)

if (hours > 0) {
Text(
text = hours.toString(),
style = MaterialTheme.typography.displayLarge
)
Text(text = ":", style = MaterialTheme.typography.displayLarge)
}
Text(
text = minutes.toString(),
text = if (hours > 0) minutes.addZero() else minutes.toString(),
style = MaterialTheme.typography.displayLarge
)
Text(text = ":", style = MaterialTheme.typography.displayLarge)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.bnyro.clock.presentation.screens.stopwatch.model
import android.content.Context
import android.content.Intent
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableLongStateOf
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
Expand All @@ -18,7 +19,7 @@ class StopwatchModel : ViewModel() {
* List of laps with overall time <> lap time
*/
val rememberedTimeStamps = mutableStateListOf<Pair<TimeObject, TimeObject>>()
var currentPosition by mutableStateOf(0)
var currentPosition by mutableLongStateOf(0L)
var state: WatchState by mutableStateOf(WatchState.IDLE)

private fun startStopwatch(context: Context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import java.util.TimerTask

class StopwatchService : Service() {
private val notificationId = 1
var currentPosition = 0
var currentPosition = 0L
private set
var state = WatchState.IDLE
private set
Expand All @@ -36,7 +36,7 @@ class StopwatchService : Service() {
private lateinit var notificationManager: NotificationManagerCompat
private var notificationPermission = true

var onPositionChange: (Int) -> Unit = {}
var onPositionChange: (Long) -> Unit = {}
var onStateChange: (WatchState) -> Unit = {}

private val receiver = object : BroadcastReceiver() {
Expand Down

0 comments on commit dd19e70

Please sign in to comment.