Skip to content

Commit

Permalink
Cleanup the text during the re-bind of the Decimal and Integer view h…
Browse files Browse the repository at this point in the history
…olders. (#2403)

* Set the draft value and clear the text when answer values are not there

* Review comments: Refactored code and tests
  • Loading branch information
aditya-07 committed Jan 9, 2024
1 parent 4e11525 commit 3260a1e
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 14 deletions.
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("")
}
}

0 comments on commit 3260a1e

Please sign in to comment.