jump-sdk / jetpack_compose_country_code_picker_emoji

Jetpack Compose Country Code Picker with Emoji Flags
https://jitpack.io/#jump-sdk/jetpack_compose_country_code_picker_emoji
Apache License 2.0
48 stars 17 forks source link

Crash on 2nd Autofill #42

Closed ankur2136 closed 11 months ago

ankur2136 commented 11 months ago

Describe the bug Crash on autofill phone number

To Reproduce Steps to reproduce the behavior: I can consistently repro on v2.2.2 However this doesn't crash on the first autofill.

  1. Autofill on a composable screen1
  2. Navigate to screen 2
  3. Navigate back to screen1
  4. Autofill again --> Crash

Note that if I manually type in the phone number and press done on the keyboard, it doesn't crash. both code paths do the same thing focusRequester.freeFocus()

Desktop (please complete the following information):

Smartphone (please complete the following information):

Additional context

FATAL EXCEPTION: main
Process: com.solanamobile.solid, PID: 6684
java.lang.IllegalStateException: 
   FocusRequester is not initialized. Here are some possible fixes:

   1. Remember the FocusRequester: val focusRequester = remember { FocusRequester() }
   2. Did you forget to add a Modifier.focusRequester() ?
   3. Are you attempting to request focus during composition? Focus requests should be made in
   response to some event. Eg Modifier.clickable { focusRequester.requestFocus() }

    at androidx.compose.ui.focus.FocusRequester.freeFocus(FocusRequester.kt:121)
    at com.togitech.ccp.component.TogiCountryCodePickerKt$TogiCountryCodePicker$2.invoke(TogiCountryCodePicker.kt:184)
    at com.togitech.ccp.component.TogiCountryCodePickerKt$TogiCountryCodePicker$2.invoke(TogiCountryCodePicker.kt:170)
    at androidx.compose.ui.autofill.AutofillTree.performAutofill(AutofillTree.kt:50)
    at androidx.compose.ui.autofill.AndroidAutofill_androidKt.performAutofill(AndroidAutofill.android.kt:132)
    at androidx.compose.ui.platform.AndroidComposeView.autofill(AndroidComposeView.android.kt:1297)
    at android.view.autofill.AutofillManager.autofill(AutofillManager.java:2605)
    at android.view.autofill.AutofillManager.-$$Nest$mautofill(Unknown Source:0)
    at android.view.autofill.AutofillManager$AutofillManagerClient.lambda$autofill$1(AutofillManager.java:3804)
    at android.view.autofill.AutofillManager$AutofillManagerClient$$ExternalSyntheticLambda2.run(Unknown Source:10)
    at android.os.Handler.handleCallback(Handler.java:942)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:201)
    at android.os.Looper.loop(Looper.java:288)
    at android.app.ActivityThread.main(ActivityThread.java:7866)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
hbmartin commented 11 months ago

Were you able to repro using LaunchedEffect per https://stackoverflow.com/questions/74184416/search-focusrequester-is-not-initialized/77250333 ?

ankur2136 commented 11 months ago
    val coroutineScope = rememberCoroutineScope()

    OutlinedTextField(
        ...
        modifier = modifier
            .....
            .autofill(
                autofillTypes = listOf(AutofillType.PhoneNumberNational),
                onFill = { filledPhoneNumber ->
                   ...
                    keyboardController?.hide()
                    // https://github.com/jump-sdk/jetpack_compose_country_code_picker_emoji/issues/42
                    coroutineScope.launch {
                        focusRequester.freeFocus()
                    }
                },
                focusRequester = focusRequester,
            )
            .focusRequester(focusRequester = focusRequester),
            ...
        },

Tested a fix where wrapping the freeFocus inside a coroutineScope fixes this issue.