Skip to content

Commit

Permalink
fix: app crash when clicking next on create account screen (#2183) (#…
Browse files Browse the repository at this point in the history
…2186)

(cherry picked from commit 46a57b7)
  • Loading branch information
MohamadJaara committed Sep 13, 2023
1 parent b2d241a commit f4f8ef7
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.autofill.AutofillType
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
Expand All @@ -35,9 +34,8 @@ import androidx.compose.ui.text.input.TextFieldValue
import com.wire.android.R
import com.wire.android.ui.common.ShakeAnimation
import com.wire.android.ui.common.error.CoreFailureErrorDialog
import com.wire.android.ui.common.textfield.AutoFillTextField
import com.wire.android.ui.common.textfield.WireTextField
import com.wire.android.ui.common.textfield.WireTextFieldState
import com.wire.android.ui.common.textfield.clearAutofillTree
import com.wire.android.ui.theme.wireDimensions

@OptIn(ExperimentalComposeUiApi::class)
Expand All @@ -50,7 +48,6 @@ fun UsernameTextField(
onUsernameChange: (TextFieldValue) -> Unit,
onUsernameErrorAnimated: () -> Unit
) {
clearAutofillTree()
if (errorState is HandleUpdateErrorState.DialogError.GenericError) {
CoreFailureErrorDialog(errorState.coreFailure, onErrorDismiss)
}
Expand All @@ -61,8 +58,7 @@ fun UsernameTextField(
animate()
onUsernameErrorAnimated()
}
AutoFillTextField(
autofillTypes = listOf(AutofillType.Username),
WireTextField(
value = username,
onValueChange = onUsernameChange,
placeholderText = stringResource(R.string.create_account_username_placeholder),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import androidx.compose.runtime.remember
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.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.platform.LocalContext
Expand Down Expand Up @@ -77,9 +76,8 @@ import com.wire.android.ui.common.button.WireButtonState
import com.wire.android.ui.common.button.WirePrimaryButton
import com.wire.android.ui.common.button.WireSecondaryButton
import com.wire.android.ui.common.error.CoreFailureErrorDialog
import com.wire.android.ui.common.textfield.AutoFillTextField
import com.wire.android.ui.common.textfield.WireTextField
import com.wire.android.ui.common.textfield.WireTextFieldState
import com.wire.android.ui.common.textfield.clearAutofillTree
import com.wire.android.ui.common.topappbar.WireCenterAlignedTopAppBar
import com.wire.android.ui.destinations.CreateAccountDetailsScreenDestination
import com.wire.android.ui.destinations.LoginScreenDestination
Expand Down Expand Up @@ -139,7 +137,6 @@ private fun EmailContent(
tosUrl: String,
serverConfig: ServerConfig.Links
) {
clearAutofillTree()
val focusRequester = remember { FocusRequester() }

Scaffold(topBar = {
Expand Down Expand Up @@ -174,8 +171,7 @@ private fun EmailContent(
)
.testTag("createTeamText")
)
AutoFillTextField(
autofillTypes = listOf(AutofillType.EmailAddress),
WireTextField(
value = state.email,
onValueChange = onEmailChange,
placeholderText = stringResource(R.string.create_account_email_placeholder),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ private fun PasswordTextField(state: RegisterDeviceState, onPasswordChange: (Tex
keyboardActions = KeyboardActions(onDone = { keyboardController?.hide() }),
modifier = Modifier
.padding(horizontal = MaterialTheme.wireDimensions.spacing16x)
.testTag("password field")
.testTag("password field"),
autofill = true
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ fun RemoveDeviceDialog(
modifier = Modifier
.focusRequester(focusRequester)
.padding(bottom = MaterialTheme.wireDimensions.spacing8x)
.testTag("remove device password field")
.testTag("remove device password field"),
autofill = true
)
LaunchedEffect(Unit) { // executed only once when showing the dialog
focusRequester.requestFocus()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ private fun PasswordInput(modifier: Modifier, password: TextFieldValue, onPasswo
imeAction = ImeAction.Done,
keyboardActions = KeyboardActions(onDone = { keyboardController?.hide() }),
modifier = modifier.testTag("passwordField"),
autofill = true,
testTag = "PasswordInput"
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private fun ProxyPasswordInput(modifier: Modifier, proxyPassword: TextFieldValue
labelText = stringResource(R.string.label_proxy_password),
keyboardActions = KeyboardActions(onDone = { keyboardController?.hide() }),
modifier = modifier.testTag("passwordField"),
autofillTypes = listOf()
autofill = false
)
}

Expand Down
8 changes: 6 additions & 2 deletions app/src/main/kotlin/com/wire/android/ui/common/WireDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,9 @@ fun PreviewWireDialog() {
) {
WirePasswordTextField(
value = password,
onValueChange = { password = it })
onValueChange = { password = it },
autofill = false
)
}
}
}
Expand Down Expand Up @@ -338,7 +340,9 @@ fun PreviewWireDialogWith2OptionButtons() {
) {
WirePasswordTextField(
value = password,
onValueChange = { password = it })
onValueChange = { password = it },
autofill = true
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.autofill.Autofill
import androidx.compose.ui.autofill.AutofillNode
import androidx.compose.ui.autofill.AutofillType
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.layout.boundsInWindow
import androidx.compose.ui.layout.onGloballyPositioned
Expand Down Expand Up @@ -72,6 +74,7 @@ internal fun AutoFillTextField(
shape: Shape = RoundedCornerShape(MaterialTheme.wireDimensions.textFieldCornerSize),
colors: WireTextFieldColors = wireTextFieldColors(),
modifier: Modifier = Modifier,
onTap: (Offset) -> Unit = { },
testTag: String = String.EMPTY
) {
val autofillNode = AutofillNode(
Expand Down Expand Up @@ -105,20 +108,28 @@ internal fun AutoFillTextField(
shape = shape,
colors = colors,
modifier = modifier
.onGloballyPositioned { autofillNode.boundingBox = it.boundsInWindow() }
.onFocusChanged { focusState ->
autofill?.run {
if (focusState.isFocused) {
requestAutofillForNode(autofillNode)
} else {
cancelAutofillForNode(autofillNode)
}
}
},
.fillBounds(autofillNode)
.defaultOnFocusAutoFill(autofill, autofillNode),
onTap = onTap,
testTag = testTag
)
}

@OptIn(ExperimentalComposeUiApi::class)
fun Modifier.fillBounds(autofillNode: AutofillNode) = this.then(
Modifier.onGloballyPositioned { autofillNode.boundingBox = it.boundsInWindow() }
)

@OptIn(ExperimentalComposeUiApi::class)
fun Modifier.defaultOnFocusAutoFill(autofill: Autofill?, autofillNode: AutofillNode): Modifier =
then(Modifier.onFocusChanged { focusState ->
if (focusState.isFocused) {
autofill?.requestAutofillForNode(autofillNode)
} else {
autofill?.cancelAutofillForNode(autofillNode)
}
})

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun clearAutofillTree() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.autofill.AutofillType
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
Expand Down Expand Up @@ -77,58 +78,102 @@ fun WirePasswordTextField(
shape: Shape = RoundedCornerShape(16.dp),
colors: WireTextFieldColors = wireTextFieldColors(),
modifier: Modifier = Modifier,
autofill: Boolean,
onTap: (Offset) -> Unit = { },
testTag: String = String.EMPTY,
autofillTypes: List<AutofillType> = listOf(AutofillType.Password)
) {
var passwordVisibility by remember { mutableStateOf(false) }
AutoFillTextField(
autofillTypes = autofillTypes,
value = value,
onValueChange = onValueChange,
readOnly = readOnly,
singleLine = true,
maxLines = 1,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password, autoCorrect = false, imeAction = imeAction),
keyboardActions = keyboardActions,
placeholderText = placeholderText,
labelText = labelText,
labelMandatoryIcon = labelMandatoryIcon,
descriptionText = descriptionText,
state = state,
interactionSource = interactionSource,
textStyle = textStyle,
placeholderTextStyle = placeHolderTextStyle,
inputMinHeight = inputMinHeight,
shape = shape,
colors = colors,
modifier = modifier,
visualTransformation = if (passwordVisibility) VisualTransformation.None else PasswordVisualTransformation(),
trailingIcon = {
val image = if (passwordVisibility) Icons.Filled.Visibility else Icons.Filled.VisibilityOff
IconButton(onClick = { passwordVisibility = !passwordVisibility }) {
Icon(
imageVector = image,
contentDescription = stringResource(
if (!passwordVisibility) R.string.content_description_reveal_password
else R.string.content_description_hide_password
),
modifier = Modifier
.size(20.dp)
.testTag("hidePassword")
)
}
},
testTag = testTag,
)

val keyBoardOption = remember {
KeyboardOptions(keyboardType = KeyboardType.Password, autoCorrect = false, imeAction = imeAction)
}

val visualTransformation = remember(passwordVisibility) {
if (passwordVisibility) VisualTransformation.None else PasswordVisualTransformation()
}

val icon = remember(passwordVisibility) {
if (passwordVisibility) Icons.Filled.Visibility else Icons.Filled.VisibilityOff
}

val iconButton = @Composable {
IconButton(onClick = { passwordVisibility = !passwordVisibility }) {
Icon(
imageVector = icon,
contentDescription = stringResource(
if (!passwordVisibility) R.string.content_description_reveal_password
else R.string.content_description_hide_password
),
modifier = Modifier
.size(20.dp)
.testTag("hidePassword")
)
}
}

if (autofill) {
AutoFillTextField(
value = value,
onValueChange = onValueChange,
readOnly = readOnly,
singleLine = true,
maxLines = 1,
keyboardOptions = keyBoardOption,
keyboardActions = keyboardActions,
placeholderText = placeholderText,
labelText = labelText,
labelMandatoryIcon = labelMandatoryIcon,
descriptionText = descriptionText,
state = state,
interactionSource = interactionSource,
textStyle = textStyle,
placeholderTextStyle = placeHolderTextStyle,
inputMinHeight = inputMinHeight,
shape = shape,
colors = colors,
modifier = modifier,
visualTransformation = visualTransformation,
trailingIcon = iconButton,
autofillTypes = listOf(AutofillType.Password),
onTap = onTap,
testTag = testTag
)
} else {
WireTextField(
value = value,
onValueChange = onValueChange,
readOnly = readOnly,
singleLine = true,
maxLines = 1,
keyboardOptions = keyBoardOption,
keyboardActions = keyboardActions,
placeholderText = placeholderText,
labelText = labelText,
labelMandatoryIcon = labelMandatoryIcon,
descriptionText = descriptionText,
state = state,
interactionSource = interactionSource,
textStyle = textStyle,
placeholderTextStyle = placeHolderTextStyle,
inputMinHeight = inputMinHeight,
shape = shape,
colors = colors,
modifier = modifier,
visualTransformation = visualTransformation,
trailingIcon = iconButton,
onTap = onTap,
testTag = testTag
)
}
}

@OptIn(ExperimentalComposeUiApi::class)
@Preview(name = "Default WirePasswordTextField")
@Composable
fun PreviewWirePasswordTextField() {
WirePasswordTextField(
value = TextFieldValue(""),
onValueChange = {},
modifier = Modifier.padding(16.dp)
modifier = Modifier.padding(16.dp),
autofill = false
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.platform.LocalContext
Expand All @@ -60,7 +59,6 @@ import com.wire.android.ui.theme.wireColorScheme
import com.wire.android.ui.theme.wireDimensions
import com.wire.android.ui.theme.wireTypography

@OptIn(ExperimentalComposeUiApi::class)
@RootNavGraph
@Destination(
navArgsDelegate = CreatePasswordGuestLinkNavArgs::class
Expand Down Expand Up @@ -161,7 +159,7 @@ fun CreatePasswordProtectedGuestLinkScreen(
id = R.string.conversation_options_create_password_protected_guest_link_button_placeholder_text
),
onValueChange = viewModel::onPasswordUpdated,
autofillTypes = emptyList()
autofill = false
)
Spacer(modifier = Modifier.height(dimensions().spacing8x))
}
Expand All @@ -185,7 +183,7 @@ fun CreatePasswordProtectedGuestLinkScreen(
),
value = viewModel.state.passwordConfirm,
onValueChange = viewModel::onPasswordConfirmUpdated,
autofillTypes = emptyList()
autofill = false
)
Spacer(modifier = Modifier.height(dimensions().spacing24x))
}
Expand Down
Loading

0 comments on commit f4f8ef7

Please sign in to comment.