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 1 commit
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
Prev Previous commit
Next Next commit
Review comments: Refactored code and tests
  • Loading branch information
aditya-07 committed Jan 8, 2024
commit c46aa8771efd17bdf86f3670463a07a2b3f815d5
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,15 @@ internal object EditTextDecimalViewHolderFactory :
) {
val questionnaireItemViewItemDecimalAnswer =
questionnaireViewItem.answers.singleOrNull()?.valueDecimalType?.value?.toString()

val draftAnswer = questionnaireViewItem.draftAnswer?.toString()

val decimalStringToDisplay = questionnaireItemViewItemDecimalAnswer ?: draftAnswer

if (decimalStringToDisplay.isNullOrEmpty()) {
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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,14 @@ 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.isNullOrEmpty()) {
if (answer.isNullOrEmpty() && draftAnswer.isNullOrEmpty()) {
textInputEditText.setText("")
} else if (answer?.toIntOrNull() != textInputEditText.text.toString().toIntOrNull()) {
textInputEditText.setText(text)
textInputEditText.setText(answer)
} else if (draftAnswer != null && draftAnswer != textInputEditText.text.toString()) {
textInputEditText.setText(draftAnswer)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,19 +450,23 @@ class EditTextDecimalViewHolderFactoryTest {

@Test
fun `bind again should remove previous text`() {
val questionnaireViewItem =
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(),
)
viewHolder.bind(questionnaireViewItem)
viewHolder.itemView.findViewById<TextInputEditText>(R.id.text_input_edit_text).apply {
setText("1.1.1.1")
clearFocus()
}
viewHolder.itemView.clearFocus()
.isEqualTo("1.1.1.1")

viewHolder.bind(
QuestionnaireViewItem(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,19 +372,23 @@ class EditTextIntegerViewHolderFactoryTest {

@Test
fun `bind again should remove previous text`() {
val questionnaireViewItem =
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(),
)
viewHolder.bind(questionnaireViewItem)
viewHolder.itemView.findViewById<TextInputEditText>(R.id.text_input_edit_text).apply {
setText("9999999999")
clearFocus()
}
viewHolder.itemView.clearFocus()
.isEqualTo("9999999999")

viewHolder.bind(
QuestionnaireViewItem(
Expand Down
Loading