Skip to content

Commit

Permalink
Save the help card visibility in a Set (google#2357)
Browse files Browse the repository at this point in the history
* Save the help card visibility in a state flow

* spotless

* Use Set to avoid duplication

* Use stateFlow.update{ }

* Use boolean instead of int and remove dependency to the View class

* Add default value for helpCardStateChangedCallback

* Address review

* Fix test

* Unwrap set from state flow

* Remove .invoke from callback

* Change openedHelpCardSet type from QuestionnaireItem to QrItem

* spotlessApply
  • Loading branch information
FikriMilano authored Feb 1, 2024
1 parent 4d675da commit ab3b5ec
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 8 deletions.
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 @@ -38,6 +38,7 @@ import com.google.android.fhir.datacapture.extensions.flattened
import com.google.android.fhir.datacapture.extensions.hasDifferentAnswerSet
import com.google.android.fhir.datacapture.extensions.isDisplayItem
import com.google.android.fhir.datacapture.extensions.isFhirPath
import com.google.android.fhir.datacapture.extensions.isHelpCode
import com.google.android.fhir.datacapture.extensions.isHidden
import com.google.android.fhir.datacapture.extensions.isPaginated
import com.google.android.fhir.datacapture.extensions.localizedTextSpanned
Expand Down Expand Up @@ -251,6 +252,19 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
/** Toggles review mode. */
private val isInReviewModeFlow = MutableStateFlow(shouldShowReviewPageFirst)

/** Tracks which help card has been opened. */
private val openedHelpCardSet: MutableSet<QuestionnaireResponseItemComponent> = mutableSetOf()

/** Callback to save the help card state. */
private val helpCardStateChangedCallback: (Boolean, QuestionnaireResponseItemComponent) -> Unit =
{ shouldBeVisible, questionnaireResponseItem ->
if (shouldBeVisible) {
openedHelpCardSet.add(questionnaireResponseItem)
} else {
openedHelpCardSet.remove(questionnaireResponseItem)
}
}

/**
* Contains [QuestionnaireResponseItemComponent]s that have been modified by the user.
* [QuestionnaireResponseItemComponent]s that have not been modified by the user will not be
Expand Down Expand Up @@ -752,6 +766,9 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
}

val items = buildList {
val itemHelpCard = questionnaireItem.item.firstOrNull { it.isHelpCode }
val isHelpCard = itemHelpCard != null
val isHelpCardOpen = openedHelpCardSet.contains(questionnaireResponseItem)
// Add an item for the question itself
add(
QuestionnaireAdapterItem.Question(
Expand All @@ -776,6 +793,8 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
showRequiredText = showRequiredText,
showOptionalText = showOptionalText,
),
isHelpCardOpen = isHelpCard && isHelpCardOpen,
helpCardStateChangedCallback = helpCardStateChangedCallback,
),
),
)
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 @@ -27,6 +27,7 @@ import com.google.android.fhir.datacapture.R
import com.google.android.fhir.datacapture.views.QuestionnaireViewItem
import com.google.android.material.card.MaterialCardView
import org.hl7.fhir.r4.model.Questionnaire
import org.hl7.fhir.r4.model.QuestionnaireResponse.QuestionnaireResponseItemComponent

/** Displays `localizedText` if it is not null or empty, or hides the [TextView]. */
fun TextView.updateTextAndVisibility(localizedText: Spanned? = null) {
Expand Down Expand Up @@ -57,8 +58,11 @@ fun initHelpViews(
helpCardView: MaterialCardView,
helpTextView: TextView,
questionnaireItem: Questionnaire.QuestionnaireItemComponent,
questionnaireResponseItem: QuestionnaireResponseItemComponent,
isHelpCardInitiallyVisible: Boolean,
helpCardStateChangedCallback: (Boolean, QuestionnaireResponseItemComponent) -> Unit,
) {
helpCardView.visibility = GONE
helpCardView.visibility = if (isHelpCardInitiallyVisible) VISIBLE else GONE
helpButton.visibility =
if (questionnaireItem.hasHelpButton) {
VISIBLE
Expand All @@ -68,8 +72,14 @@ fun initHelpViews(
helpButton.setOnClickListener {
helpCardView.visibility =
when (helpCardView.visibility) {
VISIBLE -> GONE
else -> VISIBLE
VISIBLE -> {
helpCardStateChangedCallback(false, questionnaireResponseItem)
GONE
}
else -> {
helpCardStateChangedCallback(true, questionnaireResponseItem)
VISIBLE
}
}
}
helpTextView.updateTextAndVisibility(questionnaireItem.localizedHelpSpanned)
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 @@ -49,6 +49,9 @@ class GroupHeaderView(context: Context, attrs: AttributeSet?) : LinearLayout(con
helpCardView = findViewById(R.id.helpCardView),
helpTextView = findViewById(R.id.helpText),
questionnaireItem = questionnaireViewItem.questionnaireItem,
questionnaireResponseItem = questionnaireViewItem.getQuestionnaireResponseItem(),
isHelpCardInitiallyVisible = questionnaireViewItem.isHelpCardOpen,
helpCardStateChangedCallback = questionnaireViewItem.helpCardStateChangedCallback,
)
prefix.updateTextAndVisibility(questionnaireViewItem.questionnaireItem.localizedPrefixSpanned)
// CQF expression takes precedence over static question text
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 @@ -49,6 +49,9 @@ class HeaderView(context: Context, attrs: AttributeSet?) : LinearLayout(context,
helpCardView = findViewById(R.id.helpCardView),
helpTextView = findViewById(R.id.helpText),
questionnaireItem = questionnaireViewItem.questionnaireItem,
questionnaireResponseItem = questionnaireViewItem.getQuestionnaireResponseItem(),
isHelpCardInitiallyVisible = questionnaireViewItem.isHelpCardOpen,
helpCardStateChangedCallback = questionnaireViewItem.helpCardStateChangedCallback,
)
prefix.updateTextAndVisibility(questionnaireViewItem.questionnaireItem.localizedPrefixSpanned)
// CQF expression takes precedence over static question text
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 All @@ -21,13 +21,16 @@ import android.text.Spanned
import androidx.recyclerview.widget.RecyclerView
import com.google.android.fhir.datacapture.R
import com.google.android.fhir.datacapture.extensions.displayString
import com.google.android.fhir.datacapture.extensions.isHelpCode
import com.google.android.fhir.datacapture.extensions.localizedTextSpanned
import com.google.android.fhir.datacapture.extensions.toSpanned
import com.google.android.fhir.datacapture.validation.NotValidated
import com.google.android.fhir.datacapture.validation.Valid
import com.google.android.fhir.datacapture.validation.ValidationResult
import org.hl7.fhir.r4.model.Questionnaire
import org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemComponent
import org.hl7.fhir.r4.model.QuestionnaireResponse
import org.hl7.fhir.r4.model.QuestionnaireResponse.QuestionnaireResponseItemComponent

/**
* Data item for [QuestionnaireItemViewHolder] in [RecyclerView].
Expand Down Expand Up @@ -77,8 +80,14 @@ data class QuestionnaireViewItem(
val draftAnswer: Any? = null,
val enabledDisplayItems: List<Questionnaire.QuestionnaireItemComponent> = emptyList(),
val questionViewTextConfiguration: QuestionTextConfiguration = QuestionTextConfiguration(),
val isHelpCardOpen: Boolean = questionnaireItem.isHelpCode,
val helpCardStateChangedCallback: (Boolean, QuestionnaireResponseItemComponent) -> Unit =
{ _, _ ->
},
) {

fun getQuestionnaireResponseItem(): QuestionnaireResponseItemComponent = questionnaireResponseItem

/**
* A read-only list of answers to be rendered in the view.
*
Expand Down

0 comments on commit ab3b5ec

Please sign in to comment.