Skip to content

Commit be50a22

Browse files
committed
better formatting for lazy login code.
1 parent 2bac009 commit be50a22

File tree

1 file changed

+47
-9
lines changed
  • mobile/src/main/java/com/windscribe/mobile/ui/preferences/account

1 file changed

+47
-9
lines changed

mobile/src/main/java/com/windscribe/mobile/ui/preferences/account/AccountScreen.kt

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import androidx.compose.foundation.layout.size
1818
import androidx.compose.foundation.layout.width
1919
import androidx.compose.foundation.rememberScrollState
2020
import androidx.compose.foundation.shape.RoundedCornerShape
21+
import androidx.compose.foundation.text.KeyboardOptions
2122
import androidx.compose.foundation.verticalScroll
2223
import androidx.compose.material3.Button
2324
import androidx.compose.material3.ButtonColors
@@ -42,11 +43,15 @@ import androidx.compose.ui.platform.LocalContext
4243
import androidx.compose.ui.platform.LocalHapticFeedback
4344
import androidx.compose.ui.res.painterResource
4445
import androidx.compose.ui.res.stringResource
46+
import androidx.compose.ui.text.TextRange
4547
import androidx.compose.ui.text.font.FontWeight
48+
import androidx.compose.ui.text.input.KeyboardCapitalization
49+
import androidx.compose.ui.text.input.TextFieldValue
4650
import androidx.compose.ui.text.style.TextAlign
4751
import androidx.compose.ui.text.style.TextOverflow
4852
import androidx.compose.ui.tooling.preview.Preview
4953
import androidx.compose.ui.unit.dp
54+
import androidx.compose.ui.unit.max
5055
import androidx.compose.ui.window.Dialog
5156
import com.windscribe.mobile.ui.AppStartActivity
5257
import com.windscribe.mobile.ui.common.PreferenceBackground
@@ -82,7 +87,9 @@ fun AccountScreen(viewModel: AccountViewModel? = null) {
8287
PreferencesNavBar(stringResource(R.string.my_account)) {
8388
navController.popBackStack()
8489
}
85-
Column(Modifier.navigationBarsPadding().verticalScroll(scrollState)) {
90+
Column(Modifier
91+
.navigationBarsPadding()
92+
.verticalScroll(scrollState)) {
8693
Spacer(modifier = Modifier.height(20.dp))
8794
AccountInfo(viewModel)
8895
Spacer(modifier = Modifier.height(14.dp))
@@ -167,19 +174,27 @@ private fun HandleAlertState(viewModel: AccountViewModel?) {
167174
viewModel?.onDialogDismiss()
168175
}, onSubmit = {
169176
viewModel?.onEnterLazyLoginCode(it)
170-
})
177+
}, true)
171178
}
172179
}
173180

174181
@Composable
175182
private fun TextFieldDialog(
176183
onDismiss: () -> Unit,
177-
onSubmit: (String) -> Unit
184+
onSubmit: (String) -> Unit,
185+
isLazyLogin: Boolean = false
178186
) {
179-
val text = remember { mutableStateOf("") }
180187
val activity = LocalContext.current as? AppStartActivity
181-
val hapticFeedbackEnabled by activity?.viewmodel?.hapticFeedback?.collectAsState() ?: remember { mutableStateOf(false) }
188+
val hapticFeedbackEnabled by activity?.viewmodel?.hapticFeedback?.collectAsState()
189+
?: remember { mutableStateOf(false) }
182190
val hapticFeedback = LocalHapticFeedback.current
191+
var textFieldValue by remember { mutableStateOf(TextFieldValue("")) }
192+
LaunchedEffect(textFieldValue.text) {
193+
if (isLazyLogin && textFieldValue.text.replace("-", "").length == 8) {
194+
onSubmit(textFieldValue.text)
195+
}
196+
}
197+
183198
Dialog(onDismissRequest = onDismiss) {
184199
Surface(
185200
shape = RoundedCornerShape(12.dp),
@@ -193,9 +208,28 @@ private fun TextFieldDialog(
193208
horizontalAlignment = Alignment.CenterHorizontally
194209
) {
195210
TextField(
196-
value = text.value,
197-
onValueChange = {
198-
text.value = it
211+
value = textFieldValue,
212+
onValueChange = { input ->
213+
if (isLazyLogin) {
214+
val clean = input.text
215+
.uppercase()
216+
.replace("-", "")
217+
.take(8)
218+
219+
val formatted = if (clean.length > 4)
220+
"${clean.substring(0, 4)}-${clean.substring(4)}"
221+
else clean
222+
textFieldValue = if (formatted != textFieldValue.text) {
223+
TextFieldValue(
224+
text = formatted,
225+
selection = TextRange(formatted.length)
226+
)
227+
} else {
228+
input
229+
}
230+
} else {
231+
textFieldValue = input
232+
}
199233
},
200234
colors = TextFieldDefaults.colors().copy(
201235
focusedContainerColor = MaterialTheme.colorScheme.primaryTextColor,
@@ -219,6 +253,10 @@ private fun TextFieldDialog(
219253
color = MaterialTheme.colorScheme.preferencesBackgroundColor,
220254
textAlign = TextAlign.Start
221255
),
256+
keyboardOptions = KeyboardOptions.Default.copy(
257+
capitalization = KeyboardCapitalization.Characters
258+
),
259+
maxLines = 1,
222260
)
223261
Spacer(modifier = Modifier.height(16.dp))
224262
Row {
@@ -253,7 +291,7 @@ private fun TextFieldDialog(
253291
hapticFeedback.performHapticFeedback(HapticFeedbackType.Confirm)
254292
}
255293
onDismiss()
256-
onSubmit(text.value)
294+
onSubmit(textFieldValue.text)
257295
},
258296
colors = ButtonColors(
259297
containerColor = Color.Transparent,

0 commit comments

Comments
 (0)