Skip to content

Commit

Permalink
Move more value out of ValueInput
Browse files Browse the repository at this point in the history
  • Loading branch information
YuanLiou committed Jul 15, 2023
1 parent ef18b79 commit dd9b243
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fun AddDistanceRecordScreen(
modifier = modifier.fillMaxSize()
) {
val step = 1
val minimumValue = 0
val defaultText = "0"
var userInput by remember { mutableStateOf(defaultText) }

Expand All @@ -41,7 +42,8 @@ fun AddDistanceRecordScreen(
ValueInput(
currentInput = userInput,
focusRequester = focusRequester,
onUserInputText = { result, minimumValue ->
isMinusValueEnabled = userInput.toInt() > minimumValue,
onUserInputText = { result ->
val newInput = result?.toString()?.trim() ?: defaultText
if (InputUtils.isNumeric(newInput) && newInput.toInt() >= minimumValue) {
userInput = newInput
Expand All @@ -53,7 +55,10 @@ fun AddDistanceRecordScreen(
userInput = (it.toInt() + step).toString()
},
onUserClickMinorButton = {
userInput = (it.toInt() - step).toString()
val value = it.toInt()
if (value > minimumValue) {
userInput = (value - step).toString()
}
}
)
}
Expand Down
22 changes: 11 additions & 11 deletions wear/src/main/java/com/rayliu/gymnote/wearos/ui/WearInputUI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import kotlin.math.roundToInt
@Composable
fun ValueInput(
focusRequester: FocusRequester,
onUserInputText: (CharSequence?, minimumValue: Int) -> Unit,
currentInput: String,
isMinusValueEnabled: Boolean,
onUserInputText: (CharSequence?) -> Unit,
onUserClickPlusButton: (String) -> Unit,
onUserClickMinorButton: (String) -> Unit,
modifier: Modifier = Modifier,
currentInput: String = "0",
minimumValue: Int = 0
modifier: Modifier = Modifier
) {
Column(
verticalArrangement = Arrangement.Center,
Expand All @@ -43,7 +43,7 @@ fun ValueInput(
val accumulatedValue = it.roundToInt()
if (accumulatedValue > 0) {
onUserClickPlusButton(currentInput)
} else if (accumulatedValue < 0 && currentInput.toInt() > minimumValue) {
} else if (accumulatedValue < 0) {
onUserClickMinorButton(currentInput)
}
}
Expand All @@ -60,15 +60,13 @@ fun ValueInput(

WearEditText(
text = currentInput,
onUserInputText = {
onUserInputText(it, minimumValue)
},
onUserInputText = onUserInputText,
fontSize = 20.sp
)

CompactButton(
onClick = { onUserClickMinorButton(currentInput) },
enabled = currentInput.toInt() > minimumValue,
enabled = isMinusValueEnabled,
modifier = Modifier.padding(top = 6.dp)
) {
Image(
Expand All @@ -90,9 +88,11 @@ fun ValueInputPreview() {
GymNoteTheme {
ValueInput(
focusRequester = FocusRequester(),
onUserInputText = { _, _ -> },
currentInput = "0",
isMinusValueEnabled = true,
onUserInputText = {},
onUserClickPlusButton = {},
onUserClickMinorButton = {},
onUserClickMinorButton = {}
)
}
}

0 comments on commit dd9b243

Please sign in to comment.