Skip to content

Commit

Permalink
Finish add distance record input screen
Browse files Browse the repository at this point in the history
  • Loading branch information
YuanLiou committed Jul 15, 2023
1 parent 4ffd3d6 commit 6417fdc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
Expand Down Expand Up @@ -106,8 +109,14 @@ private fun ShowScreenByRecordType(
}

RecordType.DISTANCE -> {
val defaultText = "0.0"
var userInput by remember { mutableStateOf(defaultText) }
AddDistanceRecordScreen(
focusRequester = focusRequester,
valueUnit = "km",
defaultText = defaultText,
userInput = userInput,
onInputChanged = { userInput = it },
modifier = Modifier
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Undo
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
Expand All @@ -33,17 +29,19 @@ import com.rayliu.gymnote.wearos.utils.InputUtils
@Composable
fun AddDistanceRecordScreen(
focusRequester: FocusRequester,
valueUnit: String,
defaultText: String,
userInput: String,
onInputChanged: (String) -> Unit,
modifier: Modifier = Modifier
) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Center,
modifier = modifier.fillMaxSize()
) {
val step = 1
val minimumValue = 0
val defaultText = "0"
var userInput by remember { mutableStateOf(defaultText) }
val step = 0.5f
val minimumValue = 0.0f

val context = LocalContext.current
val waringText = stringResource(id = R.string.warning_input_type_is_not_number)
Expand All @@ -58,7 +56,7 @@ fun AddDistanceRecordScreen(
verticalArrangement = Arrangement.Center
) {
CompactButton(
onClick = { userInput = defaultText },
onClick = { onInputChanged(defaultText) },
modifier = Modifier.alpha(undoButtonVisibility)
) {
Icon(
Expand All @@ -68,29 +66,29 @@ fun AddDistanceRecordScreen(
}
}


ValueInput(
currentInput = userInput,
focusRequester = focusRequester,
isMinusValueEnabled = userInput.toInt() > minimumValue,
isMinusValueEnabled = userInput.toFloat() > minimumValue,
onUserInputText = { result ->
val newInput = result?.toString()?.trim() ?: defaultText
if (InputUtils.isNumeric(newInput) && newInput.toInt() >= minimumValue) {
userInput = newInput
if (InputUtils.isNumeric(newInput) && newInput.toFloat() >= minimumValue) {
onInputChanged(newInput)
} else {
Toast.makeText(context, waringText, Toast.LENGTH_SHORT).show()
}
},
onUserClickPlusButton = {
userInput = (it.toInt() + step).toString()
val newInput = (it.toFloat() + step).toString()
onInputChanged(newInput)
},
onUserClickMinorButton = {
val value = it.toInt()
val value = it.toFloat()
if (value > minimumValue) {
userInput = (value - step).toString()
val newInput = (value - step).toString()
onInputChanged(newInput)
}
},
modifier = Modifier
}
)

Column(
Expand All @@ -105,7 +103,7 @@ fun AddDistanceRecordScreen(
) {}

Text(
"m",
valueUnit,
textAlign = TextAlign.Center,
modifier = Modifier.align(Alignment.Center)
)
Expand All @@ -124,7 +122,11 @@ fun AddDistanceRecordScreen(
private fun AddDistanceRecordPreview() {
GymNoteTheme {
AddDistanceRecordScreen(
FocusRequester()
FocusRequester(),
valueUnit = "km",
defaultText = "0",
userInput = "0",
onInputChanged = {}
)
}
}

0 comments on commit 6417fdc

Please sign in to comment.