Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinmost committed Oct 21, 2022
1 parent 501d15d commit 42ec4af
Showing 1 changed file with 37 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
* needed to avoid spewing validation errors before any questions are answered.
*/
private var isPaginationButtonPressed = false

init {
when {
state.contains(QuestionnaireFragment.EXTRA_QUESTIONNAIRE_RESPONSE_JSON_URI) -> {
Expand Down Expand Up @@ -268,7 +269,7 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
(
Questionnaire.QuestionnaireItemComponent,
QuestionnaireResponse.QuestionnaireResponseItemComponent,
List<QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent>
List<QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent>,
) -> Unit =
{ questionnaireItem, questionnaireResponseItem, answers ->
// TODO(jingtang10): update the questionnaire response item pre-order list and the parent map
Expand Down Expand Up @@ -307,7 +308,7 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
internal fun goToPreviousPage() {
when (entryMode) {
EntryMode.PRIOR_EDIT,
EntryMode.RANDOM -> {
EntryMode.RANDOM, -> {
val previousPageIndex =
pages!!.indexOfLast { it.index < currentPageIndexFlow.value!! && it.enabled }
check(previousPageIndex != -1) {
Expand All @@ -324,7 +325,7 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
internal fun goToNextPage() {
when (entryMode) {
EntryMode.PRIOR_EDIT,
EntryMode.SEQUENTIAL -> {
EntryMode.SEQUENTIAL, -> {
if (!isPaginationButtonPressed) {
// Force update validation results for all questions on the current page. This is needed
// when the user has not answered any questions so no validation has been done.
Expand Down Expand Up @@ -395,7 +396,7 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
)

fun updateDependentQuestionnaireResponseItems(
updatedQuestionnaireItem: Questionnaire.QuestionnaireItemComponent
updatedQuestionnaireItem: Questionnaire.QuestionnaireItemComponent,
) {
evaluateCalculatedExpressions(
updatedQuestionnaireItem,
Expand Down Expand Up @@ -428,7 +429,7 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat

@PublishedApi
internal suspend fun resolveAnswerValueSet(
uri: String
uri: String,
): List<Questionnaire.QuestionnaireItemAnswerOptionComponent> {
// If cache hit, return it
if (answerValueSetMap.contains(uri)) {
Expand Down Expand Up @@ -471,7 +472,7 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
// https://build.fhir.org/ig/HL7/sdc/expressions.html#x-fhir-query-enhancements
@PublishedApi
internal suspend fun resolveAnswerExpression(
item: Questionnaire.QuestionnaireItemComponent
item: Questionnaire.QuestionnaireItemComponent,
): List<Questionnaire.QuestionnaireItemAnswerOptionComponent> {
// Check cache first for database queries
val answerExpression = item.answerExpression ?: return emptyList()
Expand All @@ -489,7 +490,7 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat

private suspend fun loadAnswerExpressionOptions(
item: Questionnaire.QuestionnaireItemComponent,
expression: Expression
expression: Expression,
): List<Questionnaire.QuestionnaireItemAnswerOptionComponent> {
val data =
if (expression.isXFhirQuery) fhirEngine.search(expression.expression)
Expand All @@ -515,7 +516,7 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
questionnaireItemList: List<Questionnaire.QuestionnaireItemComponent>,
questionnaireResponseItemList: List<QuestionnaireResponse.QuestionnaireResponseItemComponent>,
currentPageIndex: Int?,
reviewMode: Boolean
reviewMode: Boolean,
): QuestionnaireState {

val showReviewButton =
Expand Down Expand Up @@ -546,7 +547,8 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
pages =
questionnaireItemList.zip(questionnaireResponseItemList).mapIndexed {
index,
(questionnaireItem, questionnaireResponseItem) ->
(questionnaireItem, questionnaireResponseItem),
->
QuestionnairePage(
index,
EnablementEvaluator.evaluate(
Expand Down Expand Up @@ -607,19 +609,20 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
*/
private fun getQuestionnaireItemViewItems(
questionnaireItem: Questionnaire.QuestionnaireItemComponent,
questionnaireResponseItem: QuestionnaireResponse.QuestionnaireResponseItemComponent
questionnaireResponseItem: QuestionnaireResponse.QuestionnaireResponseItemComponent,
): List<QuestionnaireItemViewItem> {
// Disabled/hidden questions should not get QuestionnaireItemViewItem instances
val enabled =
EnablementEvaluator.evaluate(
questionnaireItem,
questionnaireResponseItem,
questionnaireResponse
) { item, linkId -> findEnableWhenQuestionnaireResponseItem(item, linkId) }

if (!enabled || questionnaireItem.isHidden) {
return emptyList()
}

// Determine the validation result, which will be displayed on the item itself
val validationResult =
if (modifiedQuestionnaireResponseItemSet.contains(questionnaireResponseItem) ||
isPaginationButtonPressed
Expand All @@ -633,6 +636,7 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
NotValidated
}
val items = buildList {
// Add an item for the question itself
add(
QuestionnaireItemViewItem(
questionnaireItem,
Expand All @@ -643,25 +647,28 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
resolveAnswerExpression = { resolveAnswerExpression(it) }
)
)
questionnaireResponseItem.answer
.map { it.item }
.ifEmpty {
if (questionnaireItem.repeats) emptyList() else listOf(questionnaireResponseItem.item)
val nestedResponses: List<List<QuestionnaireResponse.QuestionnaireResponseItemComponent>> =
when {
// Repeated questions have one answer item per response instance, which we must display
// after the question.
questionnaireItem.repeats -> questionnaireResponseItem.answer.map { it.item }
// Non-repeated questions may have nested items, which we should display
else -> listOf(questionnaireResponseItem.item)
}
.forEach { nestedResponse ->
addAll(
getQuestionnaireItemViewItems(
// If nested display item is identified as instructions or flyover, then do not create
// questionnaire state for it.
questionnaireItemList =
questionnaireItem.item.filterNot {
it.type == Questionnaire.QuestionnaireItemType.DISPLAY &&
(it.isInstructionsCode || it.isFlyoverCode || it.isHelpCode)
},
questionnaireResponseItemList = nestedResponse,
)
nestedResponses.forEach { nestedResponse ->
addAll(
getQuestionnaireItemViewItems(
// If nested display item is identified as instructions or flyover, then do not create
// questionnaire state for it.
questionnaireItemList =
questionnaireItem.item.filterNot {
it.type == Questionnaire.QuestionnaireItemType.DISPLAY &&
(it.isInstructionsCode || it.isFlyoverCode || it.isHelpCode)
},
questionnaireResponseItemList = nestedResponse,
)
}
)
}
}
currentPageItems = items
return items
Expand Down Expand Up @@ -719,7 +726,7 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
*/
private fun createRepeatedGroupResponse(
questionnaireItem: Questionnaire.QuestionnaireItemComponent,
questionnaireResponseItem: QuestionnaireResponse.QuestionnaireResponseItemComponent
questionnaireResponseItem: QuestionnaireResponse.QuestionnaireResponseItemComponent,
): List<QuestionnaireResponse.QuestionnaireResponseItemComponent> {
val individualQuestions = questionnaireItem.item
return questionnaireResponseItem.answer.map { repeatedGroupInstance ->
Expand Down Expand Up @@ -831,7 +838,7 @@ internal data class QuestionnairePagination(
val pages: List<QuestionnairePage>,
val currentPageIndex: Int,
val showSubmitButton: Boolean = false,
val showReviewButton: Boolean = false
val showReviewButton: Boolean = false,
)

/** A single page in the questionnaire. This is used for the UI to render pagination controls. */
Expand Down

0 comments on commit 42ec4af

Please sign in to comment.