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
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 @@ -52,11 +52,15 @@ internal object EditTextDecimalViewHolderFactory :

val decimalStringToDisplay = questionnaireItemViewItemDecimalAnswer ?: draftAnswer
aditya-07 marked this conversation as resolved.
Show resolved Hide resolved

if (
decimalStringToDisplay?.toDoubleOrNull() !=
if (decimalStringToDisplay.isNullOrEmpty()) {
textInputEditText.setText("")
} else if (
questionnaireItemViewItemDecimalAnswer?.toDoubleOrNull() !=
textInputEditText.text.toString().toDoubleOrNull()
) {
textInputEditText.setText(decimalStringToDisplay)
} 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 @@ -70,8 +70,12 @@ internal object EditTextIntegerViewHolderFactory :
// 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())) {
if (text.isNullOrEmpty()) {
textInputEditText.setText("")
} else if (answer?.toIntOrNull() != textInputEditText.text.toString().toIntOrNull()) {
textInputEditText.setText(text)
} 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,38 @@ class EditTextDecimalViewHolderFactoryTest {
assertThat(viewHolder.itemView.findViewById<TextInputLayout>(R.id.text_input_layout).helperText)
.isNull()
}

@Test
fun `bind again should remove previous text`() {
val questionnaireViewItem =
QuestionnaireViewItem(
Questionnaire.QuestionnaireItemComponent(),
QuestionnaireResponse.QuestionnaireResponseItemComponent(),
validationResult = NotValidated,
answersChangedCallback = { _, _, _, _ -> },
)
viewHolder.bind(questionnaireViewItem)
viewHolder.itemView.findViewById<TextInputEditText>(R.id.text_input_edit_text).apply {
setText("1.1.1.1")
aditya-07 marked this conversation as resolved.
Show resolved Hide resolved
clearFocus()
}
viewHolder.itemView.clearFocus()

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,38 @@ class EditTextIntegerViewHolderFactoryTest {
assertThat(viewHolder.itemView.findViewById<TextInputLayout>(R.id.text_input_layout).helperText)
.isNull()
}

@Test
fun `bind again should remove previous text`() {
val questionnaireViewItem =
QuestionnaireViewItem(
Questionnaire.QuestionnaireItemComponent(),
QuestionnaireResponse.QuestionnaireResponseItemComponent(),
validationResult = NotValidated,
answersChangedCallback = { _, _, _, _ -> },
)
viewHolder.bind(questionnaireViewItem)
viewHolder.itemView.findViewById<TextInputEditText>(R.id.text_input_edit_text).apply {
setText("9999999999")
aditya-07 marked this conversation as resolved.
Show resolved Hide resolved
clearFocus()
}
viewHolder.itemView.clearFocus()

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