Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup the text during the re-bind of the Decimal and Integer view holders. #2403

Merged
merged 3 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022-2023 Google LLC
* Copyright 2022-2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -47,16 +47,17 @@ internal object EditTextDecimalViewHolderFactory :
) {
val questionnaireItemViewItemDecimalAnswer =
questionnaireViewItem.answers.singleOrNull()?.valueDecimalType?.value?.toString()

val draftAnswer = questionnaireViewItem.draftAnswer?.toString()

val decimalStringToDisplay = questionnaireItemViewItemDecimalAnswer ?: draftAnswer

if (
decimalStringToDisplay?.toDoubleOrNull() !=
if (questionnaireItemViewItemDecimalAnswer.isNullOrEmpty() && draftAnswer.isNullOrEmpty()) {
textInputEditText.setText("")
} else if (
questionnaireItemViewItemDecimalAnswer?.toDoubleOrNull() !=
textInputEditText.text.toString().toDoubleOrNull()
) {
textInputEditText.setText(decimalStringToDisplay)
textInputEditText.setText(questionnaireItemViewItemDecimalAnswer)
} else if (draftAnswer != null && draftAnswer != textInputEditText.text.toString()) {
textInputEditText.setText(draftAnswer)
}
// Update error message if draft answer present
if (draftAnswer != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022-2023 Google LLC
* Copyright 2022-2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -64,14 +64,16 @@ internal object EditTextIntegerViewHolderFactory :
questionnaireViewItem.answers.singleOrNull()?.valueIntegerType?.value?.toString()
val draftAnswer = questionnaireViewItem.draftAnswer?.toString()

val text = answer ?: draftAnswer

// Update the text on the UI only if the value of the saved answer or draft answer
// is different from what the user is typing. We compare the two fields as integers to
// avoid shifting focus if the text values are different, but their integer representation
// is the same (e.g. "001" compared to "1")
if ((text?.toIntOrNull() != textInputEditText.text.toString().toIntOrNull())) {
textInputEditText.setText(text)
if (answer.isNullOrEmpty() && draftAnswer.isNullOrEmpty()) {
textInputEditText.setText("")
} else if (answer?.toIntOrNull() != textInputEditText.text.toString().toIntOrNull()) {
textInputEditText.setText(answer)
} else if (draftAnswer != null && draftAnswer != textInputEditText.text.toString()) {
textInputEditText.setText(draftAnswer)
}

// Update error message if draft answer present
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Google LLC
* Copyright 2023-2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -447,4 +447,42 @@ class EditTextDecimalViewHolderFactoryTest {
assertThat(viewHolder.itemView.findViewById<TextInputLayout>(R.id.text_input_layout).helperText)
.isNull()
}

@Test
fun `bind again should remove previous text`() {
viewHolder.bind(
QuestionnaireViewItem(
Questionnaire.QuestionnaireItemComponent(),
QuestionnaireResponse.QuestionnaireResponseItemComponent(),
validationResult = NotValidated,
answersChangedCallback = { _, _, _, _ -> },
draftAnswer = "1.1.1.1",
),
)

assertThat(
viewHolder.itemView
.findViewById<TextInputEditText>(R.id.text_input_edit_text)
.text
.toString(),
)
.isEqualTo("1.1.1.1")

viewHolder.bind(
QuestionnaireViewItem(
Questionnaire.QuestionnaireItemComponent(),
QuestionnaireResponse.QuestionnaireResponseItemComponent(),
validationResult = NotValidated,
answersChangedCallback = { _, _, _, _ -> },
),
)

assertThat(
viewHolder.itemView
.findViewById<TextInputEditText>(R.id.text_input_edit_text)
.text
.toString(),
)
.isEqualTo("")
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Google LLC
* Copyright 2023-2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -369,4 +369,42 @@ class EditTextIntegerViewHolderFactoryTest {
assertThat(viewHolder.itemView.findViewById<TextInputLayout>(R.id.text_input_layout).helperText)
.isNull()
}

@Test
fun `bind again should remove previous text`() {
viewHolder.bind(
QuestionnaireViewItem(
Questionnaire.QuestionnaireItemComponent(),
QuestionnaireResponse.QuestionnaireResponseItemComponent(),
validationResult = NotValidated,
answersChangedCallback = { _, _, _, _ -> },
draftAnswer = "9999999999",
),
)

assertThat(
viewHolder.itemView
.findViewById<TextInputEditText>(R.id.text_input_edit_text)
.text
.toString(),
)
.isEqualTo("9999999999")

viewHolder.bind(
QuestionnaireViewItem(
Questionnaire.QuestionnaireItemComponent(),
QuestionnaireResponse.QuestionnaireResponseItemComponent(),
validationResult = NotValidated,
answersChangedCallback = { _, _, _, _ -> },
),
)

assertThat(
viewHolder.itemView
.findViewById<TextInputEditText>(R.id.text_input_edit_text)
.text
.toString(),
)
.isEqualTo("")
}
}
Loading