Add support for autofill in RetroAchievements login form

This commit is contained in:
Rafael Caetano 2023-02-07 18:53:38 +00:00
parent f170d39a9e
commit c26daac1d4
2 changed files with 47 additions and 1 deletions

View File

@ -0,0 +1,34 @@
package me.magnum.melonds.ui.common
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.autofill.AutofillNode
import androidx.compose.ui.autofill.AutofillType
import androidx.compose.ui.composed
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.layout.boundsInWindow
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalAutofill
import androidx.compose.ui.platform.LocalAutofillTree
@OptIn(ExperimentalComposeUiApi::class)
fun Modifier.autofill(
autofillTypes: List<AutofillType>,
onFill: ((String) -> Unit),
) = composed {
val autofill = LocalAutofill.current
val autofillNode = AutofillNode(onFill = onFill, autofillTypes = autofillTypes)
LocalAutofillTree.current += autofillNode
this.onGloballyPositioned {
autofillNode.boundingBox = it.boundsInWindow()
}.onFocusChanged { focusState ->
autofill?.run {
if (focusState.isFocused) {
requestAutofillForNode(autofillNode)
} else {
cancelAutofillForNode(autofillNode)
}
}
}
}

View File

@ -6,7 +6,9 @@ import androidx.compose.foundation.lazy.items
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.autofill.AutofillType
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.PasswordVisualTransformation
@ -17,6 +19,7 @@ import androidx.compose.ui.window.Dialog
import me.magnum.melonds.R
import me.magnum.melonds.domain.model.retroachievements.RAUserAchievement
import me.magnum.melonds.ui.common.MelonPreviewSet
import me.magnum.melonds.ui.common.autofill
import me.magnum.melonds.ui.common.melonButtonColors
import me.magnum.melonds.ui.romdetails.model.RomRetroAchievementsUiState
import me.magnum.melonds.ui.theme.MelonTheme
@ -68,7 +71,7 @@ private fun LoggedOut(
onClick = { showLoginPopup = true },
colors = melonButtonColors(),
) {
Text(text = stringResource(id = R.string.login))
Text(text = stringResource(id = R.string.login).uppercase())
}
}
}
@ -84,6 +87,7 @@ private fun LoggedOut(
}
}
@OptIn(ExperimentalComposeUiApi::class)
@Composable
private fun LoginPopup(
onDismiss: () -> Unit,
@ -122,6 +126,10 @@ private fun LoginPopup(
verticalArrangement = Arrangement.spacedBy(4.dp),
) {
OutlinedTextField(
modifier = Modifier.autofill(
autofillTypes = listOf(AutofillType.Username),
onFill = { username = it },
),
value = username,
onValueChange = { username = it },
label = {
@ -130,6 +138,10 @@ private fun LoginPopup(
)
OutlinedTextField(
modifier = Modifier.autofill(
autofillTypes = listOf(AutofillType.Password),
onFill = { password = it },
),
value = password,
onValueChange = { password = it },
visualTransformation = PasswordVisualTransformation(),