Skip to content

Commit

Permalink
Merge pull request #313 from SuhasDissa/lap-times
Browse files Browse the repository at this point in the history
feat: show the time between laps
  • Loading branch information
SuhasDissa authored Mar 15, 2024
2 parents 584eba5 + eec27b8 commit fc1a560
Show file tree
Hide file tree
Showing 5 changed files with 343 additions and 147 deletions.
25 changes: 25 additions & 0 deletions app/src/main/java/com/bnyro/clock/obj/TimeObject.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,29 @@ data class TimeObject(
return "${hours.addZero()}:${minutes.addZero()}:${seconds.addZero()}.${(milliseconds / 10).addZero()}"
.replace("^(00:)*".toRegex(), "")
}

fun toFullString(): String {
return String.format("%02d:%02d.%02d", minutes + hours * 60, seconds, milliseconds / 10)
}

operator fun minus(value: TimeObject): TimeObject {
var hours =
(this.hours - value.hours)
var minutes =
(this.minutes - value.minutes).also { if (it < 0) hours -= 1 }
.let { if (it < 0) 60 + it else it }
var seconds =
(this.seconds - value.seconds).also { if (it < 0) minutes -= 1 }
.let { if (it < 0) 60 + it else it }
val milliseconds =
(this.milliseconds - value.milliseconds).also { if (it < 0) seconds -= 1 }
.let { if (it < 0) 1000 + it else it }

return TimeObject(
hours,
minutes,
seconds,
milliseconds
)
}
}
18 changes: 17 additions & 1 deletion app/src/main/java/com/bnyro/clock/ui/model/StopwatchModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.core.content.ContextCompat
import androidx.lifecycle.ViewModel
import com.bnyro.clock.obj.TimeObject
import com.bnyro.clock.obj.WatchState
import com.bnyro.clock.services.StopwatchService
import com.bnyro.clock.util.TimeHelper

class StopwatchModel : ViewModel() {
val rememberedTimeStamps = mutableStateListOf<Int>()
/**
* List of laps with overall time <> lap time
*/
val rememberedTimeStamps = mutableStateListOf<Pair<TimeObject, TimeObject>>()
var currentPosition by mutableStateOf(0)
var state: WatchState by mutableStateOf(WatchState.IDLE)

Expand All @@ -28,6 +33,17 @@ class StopwatchModel : ViewModel() {
context.sendBroadcast(startIntent)
}

fun onLapClicked() {
val overallTime = TimeHelper.millisToTime(currentPosition.toLong())
if (rememberedTimeStamps.isNotEmpty()) {
val lastLap = rememberedTimeStamps.last()
rememberedTimeStamps.add(Pair(overallTime, overallTime - lastLap.first))
} else {
rememberedTimeStamps.add(Pair(overallTime, overallTime))
}

}

fun pauseResumeStopwatch(context: Context) {
when (state) {
WatchState.IDLE -> startStopwatch(context)
Expand Down
76 changes: 51 additions & 25 deletions app/src/main/java/com/bnyro/clock/ui/nav/NavContainer.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.bnyro.clock.ui.nav

import android.util.Log
import android.content.res.Configuration
import androidx.activity.addCallback
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.NavigationRail
import androidx.compose.material3.NavigationRailItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
Expand All @@ -18,6 +21,7 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
Expand All @@ -27,7 +31,6 @@ import androidx.navigation.compose.rememberNavController
import com.bnyro.clock.ui.MainActivity
import com.bnyro.clock.ui.model.ClockModel
import com.bnyro.clock.ui.model.SettingsModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

val bottomNavItems = listOf(
Expand Down Expand Up @@ -82,36 +85,59 @@ fun NavContainer(
}
}

val orientation = LocalConfiguration.current.orientation
Scaffold(
bottomBar = {
NavigationBar(
tonalElevation = 5.dp
) {
bottomNavItems.forEach {
NavigationBarItem(
label = {
Text(stringResource(it.stringRes))
},
icon = {
Icon(it.icon, null)
},
selected = it == selectedRoute,
onClick = {
selectedRoute = it
navController.navigate(it.route)
}
)
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
NavigationBar(
tonalElevation = 5.dp
) {
bottomNavItems.forEach {
NavigationBarItem(
label = {
Text(stringResource(it.stringRes))
},
icon = {
Icon(it.icon, null)
},
selected = it == selectedRoute,
onClick = {
selectedRoute = it
navController.navigate(it.route)
}
)
}
}
}
}
) { pV ->
AppNavHost(
navController,
settingsModel,
clockModel,
modifier = Modifier
Row(
Modifier
.fillMaxSize()
.padding(pV)
)
) {
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
NavigationRail {
bottomNavItems.forEach {
NavigationRailItem(selected = it == selectedRoute,
onClick = {
selectedRoute = it
navController.navigate(it.route)
},
icon = { Icon(it.icon, null) },
label = {
Text(stringResource(it.stringRes))
})
}
}
}
AppNavHost(
navController,
settingsModel,
clockModel,
modifier = Modifier
.fillMaxSize()
)
}
}
}
Loading

0 comments on commit fc1a560

Please sign in to comment.