Fix some timer bugs

Also remember the TextFieldStates derived from the ViewModel
This commit is contained in:
Nishant Mishra
2025-07-07 09:56:15 +05:30
parent 76bc7b6da5
commit 4141cdab85
2 changed files with 19 additions and 41 deletions

View File

@@ -8,6 +8,7 @@ import androidx.compose.animation.scaleOut
import androidx.compose.foundation.layout.calculateEndPadding
import androidx.compose.foundation.layout.calculateStartPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ExperimentalMaterial3ExpressiveApi
import androidx.compose.material3.Icon
@@ -24,6 +25,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
@@ -55,9 +57,15 @@ fun AppScreen(
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
val remainingTime by viewModel.time.collectAsStateWithLifecycle()
val focusTimeInputFieldState = viewModel.focusTimeTextFieldState
val shortBreakTimeInputFieldState = viewModel.shortBreakTimeTextFieldState
val longBreakTimeInputFieldState = viewModel.longBreakTimeTextFieldState
val focusTimeInputFieldState = rememberSaveable(saver = TextFieldState.Saver) {
viewModel.focusTimeTextFieldState
}
val shortBreakTimeInputFieldState = rememberSaveable(saver = TextFieldState.Saver) {
viewModel.shortBreakTimeTextFieldState
}
val longBreakTimeInputFieldState = rememberSaveable(saver = TextFieldState.Saver) {
viewModel.longBreakTimeTextFieldState
}
val progress by rememberUpdatedState((uiState.totalTime.toFloat() - remainingTime) / uiState.totalTime)
var showBrandTitle by remember { mutableStateOf(true) }

View File

@@ -147,42 +147,12 @@ class UiViewModel(
}
if (time.value < 0) {
startTime = 0L
pauseTime = 0L
pauseDuration = 0L
cycles = (cycles + 1) % 8
skipTimer()
if (cycles % 2 == 0) {
_time.update { focusTime }
_uiState.update { currentState ->
currentState.copy(
timerMode = TimerMode.FOCUS,
timeStr = millisecondsToStr(time.value),
totalTime = time.value,
nextTimerMode = if (cycles == 6) TimerMode.LONG_BREAK else TimerMode.SHORT_BREAK,
nextTimeStr = if (cycles == 6) millisecondsToStr(
longBreakTime
) else millisecondsToStr(
shortBreakTime
)
)
}
} else {
val long = cycles == 7
_time.update { if (long) longBreakTime else shortBreakTime }
_uiState.update { currentState ->
currentState.copy(
timerMode = if (long) TimerMode.LONG_BREAK else TimerMode.SHORT_BREAK,
timeStr = millisecondsToStr(time.value),
totalTime = time.value,
nextTimerMode = TimerMode.FOCUS,
nextTimeStr = millisecondsToStr(focusTime)
)
}
_uiState.update { currentState ->
currentState.copy(timerRunning = false)
}
toggleTimer()
timerJob?.cancel()
} else {
_uiState.update { currentState ->
currentState.copy(
@@ -197,7 +167,7 @@ class UiViewModel(
}
}
fun updateTimerConstants(updateTextFields: Boolean = false, restart: Boolean = true) {
fun updateTimerConstants(updateTextFields: Boolean = false) {
viewModelScope.launch(Dispatchers.IO) {
focusTime = preferenceRepository.getIntPreference("focus_time")
?: preferenceRepository.saveIntPreference("focus_time", focusTime)
@@ -208,12 +178,12 @@ class UiViewModel(
if (updateTextFields) {
focusTimeTextFieldState.edit {
delete(0,length)
delete(0, length)
append((focusTime / 60000).toString())
}
shortBreakTimeTextFieldState.edit {
delete(0, length)
append((shortBreakTime/ 60000).toString())
append((shortBreakTime / 60000).toString())
}
longBreakTimeTextFieldState.edit {
delete(0, length)
@@ -221,7 +191,7 @@ class UiViewModel(
}
}
if (restart) resetTimer()
resetTimer()
}
}