Skip to content

Commit

Permalink
Set the draft value and clear the text when answer values are not there
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya-07 committed Jan 8, 2024
1 parent 2df5e02 commit dcf4d23
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 7 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 @@ -52,11 +52,15 @@ internal object EditTextDecimalViewHolderFactory :

val decimalStringToDisplay = questionnaireItemViewItemDecimalAnswer ?: draftAnswer

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")
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")
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("")
}
}

0 comments on commit dcf4d23

Please sign in to comment.