Skip to content

Commit

Permalink
Update weight record screen
Browse files Browse the repository at this point in the history
  • Loading branch information
YuanLiou committed Jul 15, 2023
1 parent fc65eda commit 3cf1062
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 3 deletions.
4 changes: 3 additions & 1 deletion wear/src/main/java/com/rayliu/gymnote/wearos/MainNavGraph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,13 @@ fun NavGraphBuilder.mainNavGraph(
AddRecordScreen(
recordTypes = recordTypes,
focusRequester = focusRequester,
onRequestFocus = {
RequestFocusOnResume(focusRequester)
},
onCancelButtonClicked = {
navController.popBackStack()
}
)
RequestFocusOnResume(focusRequester)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ fun AddRecordScreen(
recordTypes: ImmutableSet<RecordType>,
focusRequester: FocusRequester,
onCancelButtonClicked: () -> Unit,
onRequestFocus: @Composable (FocusRequester) -> Unit,
modifier: Modifier = Modifier
) {
Box(
Expand All @@ -56,6 +57,7 @@ fun AddRecordScreen(
for (recordType in recordTypes) {
if (page == recordTypes.indexOf(recordType)) {
ShowScreenByRecordType(focusRequester, recordType)
onRequestFocus(focusRequester)
}
}

Expand Down Expand Up @@ -91,7 +93,14 @@ private fun ShowScreenByRecordType(
) {
when (recordType) {
RecordType.WEIGHT -> {
val defaultText = "0.0"
var userInput by remember { mutableStateOf(defaultText) }
AddWeightRecordScreen(
focusRequester = focusRequester,
valueUnit = "kg",
defaultText = defaultText,
userInput = userInput,
onInputChanged = { userInput = it },
modifier = Modifier
)
}
Expand Down Expand Up @@ -154,6 +163,7 @@ private fun AddRecordScreenPreview() {
AddRecordScreen(
persistentSetOf(),
focusRequester = FocusRequester(),
onRequestFocus = {},
onCancelButtonClicked = {}
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,114 @@
package com.rayliu.gymnote.wearos.addrecord.recordinput

import android.widget.Toast
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
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.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.wear.compose.material.CompactButton
import androidx.wear.compose.material.Icon
import androidx.wear.compose.material.Text
import com.rayliu.gymnote.R
import com.rayliu.gymnote.wearos.theme.GymNoteTheme
import com.rayliu.gymnote.wearos.theme.PreviewConstants
import com.rayliu.gymnote.wearos.ui.ValueInput
import com.rayliu.gymnote.wearos.utils.InputUtils

@Composable
fun AddWeightRecordScreen(
focusRequester: FocusRequester,
valueUnit: String,
defaultText: String,
userInput: String,
onInputChanged: (String) -> Unit,
modifier: Modifier = Modifier
) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Center,
modifier = modifier.fillMaxSize()
) {
Text("Add Weight Records")
val step = 0.5f
val minimumValue = 0.0f

val context = LocalContext.current
val waringText = stringResource(id = R.string.warning_input_type_is_not_number)

val undoButtonVisibility = if (userInput != defaultText) {
1f
} else {
0f
}

Column(
verticalArrangement = Arrangement.Center
) {
CompactButton(
onClick = { onInputChanged(defaultText) },
modifier = Modifier.alpha(undoButtonVisibility)
) {
Icon(
imageVector = Icons.Rounded.Undo,
contentDescription = "undo"
)
}
}

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

Column(
verticalArrangement = Arrangement.Center
) {
Box {
// For layout only
CompactButton(
modifier = Modifier.alpha(0f),
enabled = false,
onClick = {}
) {}

Text(
valueUnit,
textAlign = TextAlign.Center,
modifier = Modifier.align(Alignment.Center)
)
}
}
}
}

Expand All @@ -33,6 +121,12 @@ fun AddWeightRecordScreen(
@Composable
private fun AddWeightRecordPreview() {
GymNoteTheme {
AddWeightRecordScreen()
AddWeightRecordScreen(
focusRequester = FocusRequester(),
valueUnit = "kg",
defaultText = "0.0",
userInput = "0.0",
onInputChanged = {}
)
}
}

0 comments on commit 3cf1062

Please sign in to comment.