Skip to content

Instantly share code, notes, and snippets.

@rahulsainani
Last active September 14, 2021 14:23
Show Gist options
  • Save rahulsainani/7fb27aadabbcd222243e1fc0fec183d1 to your computer and use it in GitHub Desktop.
Save rahulsainani/7fb27aadabbcd222243e1fc0fec183d1 to your computer and use it in GitHub Desktop.
@Composable
fun PhoneNumberConsent(
onPhoneNumberFetchedFromDevice: (phoneNumber: String) -> Unit,
) {
val context = LocalContext.current
val getPhoneNumberConsent =
rememberLauncherForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result: ActivityResult? ->
if (result?.resultCode == Activity.RESULT_OK && result.data != null) {
val credential = result.data!!.getParcelableExtra<Credential>(Credential.EXTRA_KEY)
if (credential != null) {
Timber.d("Phone number fetched from auto fill")
onPhoneNumberFetchedFromDevice(credential.id)
}
} else {
Timber.d("No number selected or unavailable. User can type manually.")
}
}
LaunchedEffect(Unit) {
val credentialsClient = Credentials.getClient(context)
val hintRequest = HintRequest.Builder()
.setPhoneNumberIdentifierSupported(true)
.build()
val intent = credentialsClient.getHintPickerIntent(hintRequest)
getPhoneNumberConsent.launch(IntentSenderRequest.Builder(intent.intentSender).build())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment