Fix some timer bugs
Also remember the TextFieldStates derived from the ViewModel
This commit is contained in:
@@ -8,6 +8,7 @@ import androidx.compose.animation.scaleOut
|
|||||||
import androidx.compose.foundation.layout.calculateEndPadding
|
import androidx.compose.foundation.layout.calculateEndPadding
|
||||||
import androidx.compose.foundation.layout.calculateStartPadding
|
import androidx.compose.foundation.layout.calculateStartPadding
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.foundation.text.input.TextFieldState
|
||||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||||
import androidx.compose.material3.ExperimentalMaterial3ExpressiveApi
|
import androidx.compose.material3.ExperimentalMaterial3ExpressiveApi
|
||||||
import androidx.compose.material3.Icon
|
import androidx.compose.material3.Icon
|
||||||
@@ -24,6 +25,7 @@ import androidx.compose.runtime.getValue
|
|||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.runtime.rememberUpdatedState
|
import androidx.compose.runtime.rememberUpdatedState
|
||||||
|
import androidx.compose.runtime.saveable.rememberSaveable
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
|
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
|
||||||
@@ -55,9 +57,15 @@ fun AppScreen(
|
|||||||
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
|
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
|
||||||
val remainingTime by viewModel.time.collectAsStateWithLifecycle()
|
val remainingTime by viewModel.time.collectAsStateWithLifecycle()
|
||||||
|
|
||||||
val focusTimeInputFieldState = viewModel.focusTimeTextFieldState
|
val focusTimeInputFieldState = rememberSaveable(saver = TextFieldState.Saver) {
|
||||||
val shortBreakTimeInputFieldState = viewModel.shortBreakTimeTextFieldState
|
viewModel.focusTimeTextFieldState
|
||||||
val longBreakTimeInputFieldState = viewModel.longBreakTimeTextFieldState
|
}
|
||||||
|
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)
|
val progress by rememberUpdatedState((uiState.totalTime.toFloat() - remainingTime) / uiState.totalTime)
|
||||||
var showBrandTitle by remember { mutableStateOf(true) }
|
var showBrandTitle by remember { mutableStateOf(true) }
|
||||||
|
|||||||
@@ -147,42 +147,12 @@ class UiViewModel(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (time.value < 0) {
|
if (time.value < 0) {
|
||||||
startTime = 0L
|
skipTimer()
|
||||||
pauseTime = 0L
|
|
||||||
pauseDuration = 0L
|
|
||||||
cycles = (cycles + 1) % 8
|
|
||||||
|
|
||||||
if (cycles % 2 == 0) {
|
_uiState.update { currentState ->
|
||||||
_time.update { focusTime }
|
currentState.copy(timerRunning = false)
|
||||||
_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)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
timerJob?.cancel()
|
||||||
toggleTimer()
|
|
||||||
} else {
|
} else {
|
||||||
_uiState.update { currentState ->
|
_uiState.update { currentState ->
|
||||||
currentState.copy(
|
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) {
|
viewModelScope.launch(Dispatchers.IO) {
|
||||||
focusTime = preferenceRepository.getIntPreference("focus_time")
|
focusTime = preferenceRepository.getIntPreference("focus_time")
|
||||||
?: preferenceRepository.saveIntPreference("focus_time", focusTime)
|
?: preferenceRepository.saveIntPreference("focus_time", focusTime)
|
||||||
@@ -208,12 +178,12 @@ class UiViewModel(
|
|||||||
|
|
||||||
if (updateTextFields) {
|
if (updateTextFields) {
|
||||||
focusTimeTextFieldState.edit {
|
focusTimeTextFieldState.edit {
|
||||||
delete(0,length)
|
delete(0, length)
|
||||||
append((focusTime / 60000).toString())
|
append((focusTime / 60000).toString())
|
||||||
}
|
}
|
||||||
shortBreakTimeTextFieldState.edit {
|
shortBreakTimeTextFieldState.edit {
|
||||||
delete(0, length)
|
delete(0, length)
|
||||||
append((shortBreakTime/ 60000).toString())
|
append((shortBreakTime / 60000).toString())
|
||||||
}
|
}
|
||||||
longBreakTimeTextFieldState.edit {
|
longBreakTimeTextFieldState.edit {
|
||||||
delete(0, length)
|
delete(0, length)
|
||||||
@@ -221,7 +191,7 @@ class UiViewModel(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (restart) resetTimer()
|
resetTimer()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user