From 66290ca0daa085be93f567a540a83f68b5b9601f Mon Sep 17 00:00:00 2001 From: aditya-07 Date: Mon, 23 Jan 2023 23:43:10 +0530 Subject: [PATCH] Fixed the state when view holder is recycled (#1819) --- ...uestionnaireItemSliderViewHolderFactory.kt | 5 +- ...ionnaireItemSliderViewHolderFactoryTest.kt | 53 +++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/datacapture/src/main/java/com/google/android/fhir/datacapture/views/QuestionnaireItemSliderViewHolderFactory.kt b/datacapture/src/main/java/com/google/android/fhir/datacapture/views/QuestionnaireItemSliderViewHolderFactory.kt index fe5c8a6c61..b6230ce684 100644 --- a/datacapture/src/main/java/com/google/android/fhir/datacapture/views/QuestionnaireItemSliderViewHolderFactory.kt +++ b/datacapture/src/main/java/com/google/android/fhir/datacapture/views/QuestionnaireItemSliderViewHolderFactory.kt @@ -57,13 +57,14 @@ internal object QuestionnaireItemSliderViewHolderFactory : } with(slider) { + clearOnChangeListeners() valueFrom = minValue valueTo = maxValue stepSize = - (questionnaireItemViewItem.questionnaireItem?.sliderStepValue + (questionnaireItemViewItem.questionnaireItem.sliderStepValue ?: SLIDER_DEFAULT_STEP_SIZE) .toFloat() - value = answer?.valueIntegerType?.value?.toFloat() ?: slider.valueFrom + value = answer?.valueIntegerType?.value?.toFloat() ?: valueFrom addOnChangeListener { _, newValue, _ -> // Responds to when slider's value is changed diff --git a/datacapture/src/test/java/com/google/android/fhir/datacapture/views/QuestionnaireItemSliderViewHolderFactoryTest.kt b/datacapture/src/test/java/com/google/android/fhir/datacapture/views/QuestionnaireItemSliderViewHolderFactoryTest.kt index 15f16b7214..fae93081ac 100644 --- a/datacapture/src/test/java/com/google/android/fhir/datacapture/views/QuestionnaireItemSliderViewHolderFactoryTest.kt +++ b/datacapture/src/test/java/com/google/android/fhir/datacapture/views/QuestionnaireItemSliderViewHolderFactoryTest.kt @@ -327,4 +327,57 @@ class QuestionnaireItemSliderViewHolderFactoryTest { assertThat(viewHolder.itemView.findViewById(R.id.slider).isEnabled).isFalse() } + + @Test + fun `bind multiple times with different QuestionnaireItemViewItem should show proper slider value`() { + viewHolder.bind( + QuestionnaireItemViewItem( + Questionnaire.QuestionnaireItemComponent(), + QuestionnaireResponse.QuestionnaireResponseItemComponent().apply { + addAnswer( + QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent().apply { + value = IntegerType(10) + } + ) + }, + validationResult = NotValidated, + answersChangedCallback = { _, _, _ -> }, + ) + ) + + assertThat(viewHolder.itemView.findViewById(R.id.slider).value).isEqualTo(10) + + viewHolder.bind( + QuestionnaireItemViewItem( + Questionnaire.QuestionnaireItemComponent(), + QuestionnaireResponse.QuestionnaireResponseItemComponent().apply { + addAnswer( + QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent().apply { + value = IntegerType(12) + } + ) + }, + validationResult = NotValidated, + answersChangedCallback = { _, _, _ -> }, + ) + ) + + assertThat(viewHolder.itemView.findViewById(R.id.slider).value).isEqualTo(12) + + viewHolder.bind( + QuestionnaireItemViewItem( + Questionnaire.QuestionnaireItemComponent().apply { + addExtension().apply { + url = "http://hl7.org/fhir/StructureDefinition/minValue" + setValue(IntegerType("50")) + } + }, + QuestionnaireResponse.QuestionnaireResponseItemComponent(), + validationResult = NotValidated, + answersChangedCallback = { _, _, _ -> }, + ) + ) + + assertThat(viewHolder.itemView.findViewById(R.id.slider).value).isEqualTo(50) + } }