BIT-1570 Make the name value appear in the dialog (#907)

This commit is contained in:
Oleg Semenenko 2024-01-31 14:26:59 -06:00 committed by Álison Fernandes
parent 0f72413379
commit 2127dcbb1d
3 changed files with 36 additions and 3 deletions

View file

@ -4,10 +4,15 @@ import androidx.compose.material3.AlertDialog
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.res.stringResource
import com.x8bit.bitwarden.R
@ -18,6 +23,8 @@ import com.x8bit.bitwarden.R
* @param textFieldLabel Label for the text field.
* @param onConfirmClick Called when the confirm button is clicked.
* @param onDismissRequest Called when the user attempts to dismiss the dialog.
* @param autoFocus When set to true, the view will request focus after the first recomposition.
* @param initialText The text that will be visible at the start of text entry.
*/
@Composable
fun BitwardenTextEntryDialog(
@ -25,8 +32,12 @@ fun BitwardenTextEntryDialog(
textFieldLabel: String,
onConfirmClick: (String) -> Unit,
onDismissRequest: () -> Unit,
autoFocus: Boolean = false,
initialText: String? = null,
) {
var text by remember { mutableStateOf("") }
var text by remember { mutableStateOf(initialText.orEmpty()) }
val focusRequester = remember { FocusRequester() }
var shouldRequestFocus by remember { mutableStateOf(false) }
AlertDialog(
onDismissRequest = onDismissRequest,
@ -55,8 +66,19 @@ fun BitwardenTextEntryDialog(
label = textFieldLabel,
value = text,
onValueChange = { text = it },
modifier = Modifier
.focusRequester(focusRequester)
.onGloballyPositioned {
shouldRequestFocus = true
},
)
},
containerColor = MaterialTheme.colorScheme.surfaceContainerHigh,
)
if (autoFocus && shouldRequestFocus) {
LaunchedEffect(Unit) {
focusRequester.requestFocus()
}
}
}

View file

@ -69,6 +69,8 @@ fun VaultAddEditCustomField(
title = stringResource(id = R.string.custom_field_name),
textFieldLabel = stringResource(id = R.string.name),
onDismissRequest = { shouldShowEditDialog = false },
autoFocus = true,
initialText = customField.name,
onConfirmClick = { name ->
onCustomFieldValueChange(customField.updateName(name))
shouldShowEditDialog = false

View file

@ -2310,9 +2310,18 @@ class VaultAddEditScreenTest : BaseComposeTest() {
.onNodeWithText("Edit")
.performClick()
composeTestRule
.onAllNodesWithText("TestBoolean")
.filterToOne(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
composeTestRule
.onNodeWithText("Name")
.performTextInput("NewTestBooleanName")
.performTextClearance()
composeTestRule
.onNodeWithText("Name")
.performTextInput("Boolean")
composeTestRule
.onNodeWithText("Ok")
@ -2321,7 +2330,7 @@ class VaultAddEditScreenTest : BaseComposeTest() {
verify {
viewModel.trySendAction(
VaultAddEditAction.Common.CustomFieldValueChange(
VaultAddEditState.Custom.BooleanField("Test ID 1", "NewTestBooleanName", false),
VaultAddEditState.Custom.BooleanField("Test ID 1", "Boolean", false),
),
)
}