Skip to content

Commit bbf265e

Browse files
committed
fix NumberInputBox cursor pos reset when typing
1 parent cd01ada commit bbf265e

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

app/src/main/java/spam/blocker/ui/setting/Testing.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import spam.blocker.service.SmsReceiver
2626
import spam.blocker.ui.theme.DarkOrange
2727
import spam.blocker.ui.theme.LocalPalette
2828
import spam.blocker.ui.theme.Teal200
29+
import spam.blocker.ui.widgets.AnimatedVisibleV
2930
import spam.blocker.ui.widgets.BalloonQuestionMark
3031
import spam.blocker.ui.widgets.GreyLabel
3132
import spam.blocker.ui.widgets.PopupDialog
@@ -158,7 +159,7 @@ fun PopupTesting(
158159
},
159160
)
160161
// SMS content
161-
if (vm.selectedType.intValue != Def.ForNumber) {
162+
AnimatedVisibleV(vm.selectedType.intValue != Def.ForNumber) {
162163
StrInputBox(
163164
text = vm.sms.value,
164165
label = { GreyLabel(Str(R.string.sms_content)) },

app/src/main/java/spam/blocker/ui/widgets/InputBox.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import androidx.compose.ui.layout.onGloballyPositioned
3636
import androidx.compose.ui.layout.positionOnScreen
3737
import androidx.compose.ui.platform.LocalContext
3838
import androidx.compose.ui.semantics.semantics
39+
import androidx.compose.ui.text.TextRange
3940
import androidx.compose.ui.text.TextStyle
4041
import androidx.compose.ui.text.font.FontWeight
4142
import androidx.compose.ui.text.input.KeyboardType
@@ -241,11 +242,21 @@ fun NumberInputBox(
241242
helpTooltip: @Composable (() -> Unit)? = null,
242243
) {
243244
// Code learned from the built-in BasicTextField.kt
244-
var state by remember(intValue) {
245+
var state by remember {
245246
mutableStateOf(
246247
TextFieldValue(text = intValue?.toString() ?: "")
247248
)
248249
}
250+
LaunchedEffect(intValue) {
251+
if (intValue?.toString() != state.text) {
252+
state = TextFieldValue(
253+
text = intValue?.toString() ?: "",
254+
selection = TextRange(intValue?.toString()?.length ?: 0)
255+
)
256+
}
257+
}
258+
259+
249260
// Last String value that either text field was recomposed with or updated in the onValueChange
250261
// callback. We keep track of it to prevent calling onValueChange(String) for same String when
251262
// CoreTextField's onValueChange is called multiple times without recomposition in between.

0 commit comments

Comments
 (0)