Ver código fonte

Worked on OTP copy/paste code, now working

Khubaib 4 meses atrás
pai
commit
3bb1014f8c

+ 26 - 19
app/src/main/java/com/fastest/pass/forgotpassword/presentation/ui/components/ForgotPasswordVerifyScreen.kt

@@ -193,16 +193,22 @@ fun ColumnScope.OTPFields() {
             TextField(
                 value = otpValues[index],
                 onValueChange = { newValue ->
-                    if (newValue.length <= 1) {
-                        otpValues[index] = newValue
-                        if (newValue.isNotEmpty() && index < otpLength - 1) {
-                            Log.d("test_otp_event", "newValue = ${otpValues[index]}")
-                            focusManager.moveFocus(FocusDirection.Next)
+                    if (newValue.length <= otpLength) {
+                        if (newValue.length == 1) {
+                            otpValues[index] = newValue
+                            if (index != otpLength -1) {
+                                focusManager.moveFocus(FocusDirection.Next)
+                            } else {
+                                focusManager.clearFocus()
+                                keyboardController?.hide()
+                            }
+                        } else if (newValue.length == otpLength) {
+                            newValue.forEachIndexed { i, c ->
+                                if (i < otpLength) otpValues[i] = c.toString()
+                            }
+                            focusManager.clearFocus()
+                            keyboardController?.hide()
                         }
-//                        else if (index == otpLength -1) {
-//                            focusManager.clearFocus()
-//                            keyboardController?.hide()
-//                        }
                     }
                 },
                 keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
@@ -228,16 +234,17 @@ fun ColumnScope.OTPFields() {
                         if (event.key == Key.Backspace) {
                             if (index > 0) {
                                 Log.d("test_otp_event", "Inside otp event")
-                                if (index < otpLength) {
-                                    focusManager.moveFocus(FocusDirection.Previous)
-                                    otpValues[index - 1] = ""
-                                }
-//                                else if (otpValues[index].isNotEmpty()) {
-//                                    otpValues[index] = ""
-//                                } else {
-//                                    focusManager.moveFocus(FocusDirection.Previous)
-//                                    otpValues[index - 1] = ""
-//                                }
+                                    if (index == otpLength -1) {
+                                        if (otpValues[index].isNotEmpty()) {
+                                            otpValues[index] = ""
+                                        } else {
+                                            focusManager.moveFocus(FocusDirection.Previous)
+                                            otpValues[index - 1] = ""
+                                        }
+                                    } else {
+                                        focusManager.moveFocus(FocusDirection.Previous)
+                                        otpValues[index - 1] = ""
+                                    }
                             }
                             true
                         }