Skip to content

Commit

Permalink
WIP Commit - Change NumberInput state value type to string
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasemde authored and mipro98 committed Jul 14, 2024
1 parent af046f7 commit dc027a4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.BaselineShift
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.unit.TextUnit
Expand Down Expand Up @@ -163,7 +164,10 @@ class NumberInputState (
require(initialValue in minValue..maxValue) { "initialValue not in allowed range" }
}
}
var currentValue = mutableStateOf(initialValue)
val currentValue = mutableStateOf(TextFieldValue(initialValue?.toString() ?: ""))

val numericValue: Int
get() = currentValue.value.text.toIntOrNull() ?: minValue

companion object {
/**
Expand All @@ -179,7 +183,7 @@ class NumberInputState (
},
restore = {
NumberInputState(
initialValue = it[0] as Int?,
initialValue = (it[0] as TextFieldValue).text.toIntOrNull(),
minValue = it[1] as Int,
maxValue = it[2] as Int
)
Expand Down Expand Up @@ -227,11 +231,6 @@ fun NumberInput(
MaterialTheme.colorScheme.primaryContainer
}
val maxLength = state.maxValue.toString().length
val displayValue = if (focused || !padStart) {
state.currentValue.value?.toString() ?: ""
} else {
(state.currentValue.value?.toString() ?: "").padStart(maxLength, '0')
}

Row {
BasicTextField(
Expand All @@ -241,13 +240,35 @@ fun NumberInput(
.height(IntrinsicSize.Min)
.padding(MaterialTheme.spacing.extraSmall)
.focusRequester(focusRequester)
.onFocusChanged { focused = it.isFocused },
value = displayValue,
.onFocusChanged {
if(it.isFocused) {
state.currentValue.value = state.currentValue.value.copy(
text = state.currentValue.value.text.trimStart('0')
)
} else {
if (state.currentValue.value.text.isEmpty())
state.currentValue.value = TextFieldValue(state.minValue.toString())
if(padStart)
state.currentValue.value = state.currentValue.value.copy(
state.currentValue.value.text.padStart(maxLength, '0')
)
}
focused = it.isFocused
},
value = state.currentValue.value,
textStyle = textStyle.copy(color = MaterialTheme.colorScheme.onSurface),
onValueChange = { newValue ->
val number = newValue.toIntOrNull()
if (number in state.minValue..state.maxValue || number == null) {
state.currentValue.value = number
var newValueOnlyNumbers = newValue.text.filter { it.isDigit() }
if (newValueOnlyNumbers.length > maxLength) {
if(newValue.selection.collapsed && newValue.selection.start == 0) {
newValueOnlyNumbers = newValueOnlyNumbers.take(1)
} else {
return@BasicTextField
}
}
val number = newValueOnlyNumbers.toIntOrNull() ?: state.minValue
if (number in state.minValue..state.maxValue) {
state.currentValue.value = newValue.copy(newValueOnlyNumbers)
onValueChanged(number)
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ sealed class GoalsUiEvent {
}

sealed class GoalDialogUiEvent {
data class HourTargetChanged(val hours: Int?) : GoalDialogUiEvent()
data class MinuteTargetChanged(val minutes: Int?) : GoalDialogUiEvent()
data class HourTargetChanged(val hours: Int) : GoalDialogUiEvent()
data class MinuteTargetChanged(val minutes: Int) : GoalDialogUiEvent()
data class PeriodChanged(val period: Int) : GoalDialogUiEvent()
data class PeriodUnitChanged(val periodUnit: GoalPeriodUnit) : GoalDialogUiEvent()
data class GoalTypeChanged(val goalType: GoalType) : GoalDialogUiEvent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ import kotlin.time.Duration.Companion.seconds


data class GoalDialogData(
val targetHours: Int? = null,
val targetMinutes: Int? = null,
val periodInPeriodUnits: Int? = null,
val targetHours: Int = 0,
val targetMinutes: Int = 0,
val periodInPeriodUnits: Int = 0,
val target: Duration = 0.seconds,
val periodUnit: GoalPeriodUnit = GoalPeriodUnit.DEFAULT,
val goalType: GoalType = GoalType.DEFAULT,
val oneShot: Boolean = false,
Expand Down Expand Up @@ -412,11 +413,11 @@ class GoalsViewModel @Inject constructor(
}
}

private fun onHoursChanged(hours: Int?) {
private fun onHoursChanged(hours: Int) {
_dialogData.update { it?.copy(targetHours = hours) }
}

private fun onMinutesChanged(minutes: Int?) {
private fun onMinutesChanged(minutes: Int) {
_dialogData.update { it?.copy(targetMinutes = minutes) }
}

Expand Down

0 comments on commit dc027a4

Please sign in to comment.