Add an OutputTransformation for the minutes input field

And some viewmodel optimizations
This commit is contained in:
Nishant Mishra
2025-07-06 21:27:17 +05:30
parent 6e702a6010
commit 76bc7b6da5
4 changed files with 31 additions and 12 deletions

View File

@@ -37,6 +37,7 @@ fun MinuteInputField(
state = state, state = state,
lineLimits = TextFieldLineLimits.SingleLine, lineLimits = TextFieldLineLimits.SingleLine,
inputTransformation = MinutesInputTransformation, inputTransformation = MinutesInputTransformation,
outputTransformation = MinutesOutputTransformation,
keyboardOptions = KeyboardOptions( keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.NumberPassword, keyboardType = KeyboardType.NumberPassword,
imeAction = imeAction imeAction = imeAction

View File

@@ -1,7 +1,9 @@
package org.nsh07.pomodoro.ui.settingsScreen package org.nsh07.pomodoro.ui.settingsScreen
import androidx.compose.foundation.text.input.InputTransformation import androidx.compose.foundation.text.input.InputTransformation
import androidx.compose.foundation.text.input.OutputTransformation
import androidx.compose.foundation.text.input.TextFieldBuffer import androidx.compose.foundation.text.input.TextFieldBuffer
import androidx.compose.foundation.text.input.insert
import androidx.core.text.isDigitsOnly import androidx.core.text.isDigitsOnly
object MinutesInputTransformation : InputTransformation { object MinutesInputTransformation : InputTransformation {
@@ -10,4 +12,14 @@ object MinutesInputTransformation : InputTransformation {
revertAllChanges() revertAllChanges()
} }
} }
}
object MinutesOutputTransformation : OutputTransformation {
override fun TextFieldBuffer.transformOutput() {
if (this.length == 0) {
insert(0, "00")
} else if (this.toString().toInt() < 10) {
insert(0, "0")
}
}
} }

View File

@@ -72,18 +72,18 @@ fun SettingsScreen(
) )
LazyColumn( LazyColumn(
verticalArrangement = Arrangement.spacedBy(8.dp), verticalArrangement = Arrangement.spacedBy(2.dp),
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize()
.padding(horizontal = 16.dp) .padding(horizontal = 16.dp)
) { ) {
item { item {
Text( Text(
"Durations", "Timer",
style = typography.titleSmall, style = typography.titleSmall,
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.padding(horizontal = 8.dp) .padding(horizontal = 8.dp, vertical = 14.dp)
) )
} }
item { item {
@@ -93,7 +93,10 @@ fun SettingsScreen(
.fillMaxWidth() .fillMaxWidth()
.horizontalScroll(rememberScrollState()) .horizontalScroll(rememberScrollState())
) { ) {
Column(horizontalAlignment = Alignment.CenterHorizontally) { Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(2.dp)
) {
MinuteInputField( MinuteInputField(
state = focusTimeInputFieldState, state = focusTimeInputFieldState,
shape = RoundedCornerShape( shape = RoundedCornerShape(
@@ -112,7 +115,10 @@ fun SettingsScreen(
) )
} }
Spacer(Modifier.width(2.dp)) Spacer(Modifier.width(2.dp))
Column(horizontalAlignment = Alignment.CenterHorizontally) { Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(2.dp)
) {
MinuteInputField( MinuteInputField(
state = shortBreakTimeInputFieldState, state = shortBreakTimeInputFieldState,
shape = RoundedCornerShape(4.dp), shape = RoundedCornerShape(4.dp),
@@ -126,7 +132,10 @@ fun SettingsScreen(
) )
} }
Spacer(Modifier.width(2.dp)) Spacer(Modifier.width(2.dp))
Column(horizontalAlignment = Alignment.CenterHorizontally) { Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(2.dp)
) {
MinuteInputField( MinuteInputField(
state = longBreakTimeInputFieldState, state = longBreakTimeInputFieldState,
shape = RoundedCornerShape( shape = RoundedCornerShape(

View File

@@ -231,11 +231,10 @@ class UiViewModel(
.debounce(500) .debounce(500)
.collect { .collect {
if (it.isNotEmpty()) { if (it.isNotEmpty()) {
preferenceRepository.saveIntPreference( focusTime = preferenceRepository.saveIntPreference(
"focus_time", "focus_time",
it.toString().toInt() * 60 * 1000 it.toString().toInt() * 60 * 1000
) )
updateTimerConstants(restart = false)
} }
} }
} }
@@ -244,11 +243,10 @@ class UiViewModel(
.debounce(500) .debounce(500)
.collect { .collect {
if (it.isNotEmpty()) { if (it.isNotEmpty()) {
preferenceRepository.saveIntPreference( focusTime = preferenceRepository.saveIntPreference(
"short_break_time", "short_break_time",
it.toString().toInt() * 60 * 1000 it.toString().toInt() * 60 * 1000
) )
updateTimerConstants(restart = false)
} }
} }
} }
@@ -257,11 +255,10 @@ class UiViewModel(
.debounce(500) .debounce(500)
.collect { .collect {
if (it.isNotEmpty()) { if (it.isNotEmpty()) {
preferenceRepository.saveIntPreference( focusTime = preferenceRepository.saveIntPreference(
"long_break_time", "long_break_time",
it.toString().toInt() * 60 * 1000 it.toString().toInt() * 60 * 1000
) )
updateTimerConstants(restart = false)
} }
} }
} }