@@ -3,6 +3,7 @@ package org.kabiri.android.usbterminal.ui.setting
3
3
import androidx.compose.foundation.layout.fillMaxWidth
4
4
import androidx.compose.foundation.layout.padding
5
5
import androidx.compose.foundation.text.KeyboardActions
6
+ import androidx.compose.foundation.text.KeyboardOptions
6
7
import androidx.compose.material.icons.Icons
7
8
import androidx.compose.material.icons.outlined.Check
8
9
import androidx.compose.material3.Icon
@@ -19,22 +20,25 @@ import androidx.compose.ui.ExperimentalComposeUiApi
19
20
import androidx.compose.ui.Modifier
20
21
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
21
22
import androidx.compose.ui.res.stringResource
23
+ import androidx.compose.ui.text.input.KeyboardType
22
24
import androidx.compose.ui.tooling.preview.Preview
23
25
import androidx.compose.ui.unit.dp
24
26
import org.kabiri.android.usbterminal.R
25
27
import org.kabiri.android.usbterminal.ui.theme.UsbTerminalTheme
26
28
import org.kabiri.android.usbterminal.ui.setting.SaveState.DEFAULT
27
29
import org.kabiri.android.usbterminal.ui.setting.SaveState.UNSAVED
28
30
import org.kabiri.android.usbterminal.ui.setting.SaveState.SAVED
31
+ import org.kabiri.android.usbterminal.ui.setting.SaveState.ERROR
29
32
30
33
/* *
31
34
* Enum class to represent the save state of the setting value.
32
35
* - DEFAULT: the default state of the setting value when it has not been changed
33
36
* - UNSAVED: the setting value has been changed but not saved
34
37
* - SAVED: the setting value has been saved
38
+ * - ERROR: the setting value is invalid
35
39
*/
36
40
private enum class SaveState {
37
- DEFAULT , UNSAVED , SAVED
41
+ DEFAULT , UNSAVED , SAVED , ERROR ,
38
42
}
39
43
40
44
/* *
@@ -53,7 +57,7 @@ internal fun SettingValueItem(
53
57
var saveState by remember { mutableStateOf(DEFAULT ) }
54
58
val keyboardController = LocalSoftwareKeyboardController .current
55
59
56
- fun onClickSaveNewValue (baudRate : Int ) {
60
+ fun onClickSaveNewValue () {
57
61
inputValue.toIntOrNull()?.let { baudRate ->
58
62
onNewValue(baudRate)
59
63
saveState = SAVED
@@ -69,6 +73,11 @@ internal fun SettingValueItem(
69
73
OutlinedTextField (
70
74
value = inputValue,
71
75
onValueChange = { value: String ->
76
+ val input = value.toIntOrNull()
77
+ if (input == null ) {
78
+ saveState = ERROR
79
+ return @OutlinedTextField
80
+ }
72
81
saveState = if (value.toIntOrNull() != currentValue) UNSAVED else DEFAULT
73
82
inputValue = value
74
83
},
@@ -82,7 +91,7 @@ internal fun SettingValueItem(
82
91
DEFAULT -> {}
83
92
UNSAVED -> {
84
93
IconButton (onClick = {
85
- onClickSaveNewValue(inputValue.toInt() )
94
+ onClickSaveNewValue()
86
95
}) {
87
96
Icon (
88
97
imageVector = Icons .Outlined .Check ,
@@ -96,14 +105,21 @@ internal fun SettingValueItem(
96
105
modifier = Modifier .padding(end = 16 .dp)
97
106
)
98
107
}
108
+ ERROR -> {
109
+ Text (
110
+ text = stringResource(id = R .string.settings_label_error),
111
+ modifier = Modifier .padding(end = 16 .dp)
112
+ )
113
+ }
99
114
}
100
115
},
101
- isError = inputValue.toIntOrNull() == null ,
116
+ isError = saveState == ERROR ,
117
+ keyboardOptions = KeyboardOptions (keyboardType = KeyboardType .Number ),
102
118
keyboardActions = KeyboardActions (
103
119
onDone = {
104
- onClickSaveNewValue(inputValue.toInt() )
120
+ onClickSaveNewValue()
105
121
}
106
- )
122
+ ),
107
123
)
108
124
}
109
125
0 commit comments