From 69b8ebf156c2282be64af8905d209260046fd33f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=E2=89=A1ZRS?= <12814349+LZRS@users.noreply.github.com> Date: Fri, 20 Dec 2024 18:46:02 +0300 Subject: [PATCH] Check if previous/next page is hidden to show previous/next buttons (#2569) * Only select QuestionnaireItem with 'page' extension as page * Fix hasNextPage and hasPreviousPage to check if page is hidden * Resolve NIT, renaming test linkId to page4-noExtension-hidden --- .../datacapture/QuestionnaireViewModel.kt | 4 +- .../datacapture/QuestionnaireViewModelTest.kt | 178 +++++++++++++++++- 2 files changed, 179 insertions(+), 3 deletions(-) diff --git a/datacapture/src/main/java/com/google/android/fhir/datacapture/QuestionnaireViewModel.kt b/datacapture/src/main/java/com/google/android/fhir/datacapture/QuestionnaireViewModel.kt index 056cd47aad..ecc019b0a8 100644 --- a/datacapture/src/main/java/com/google/android/fhir/datacapture/QuestionnaireViewModel.kt +++ b/datacapture/src/main/java/com/google/android/fhir/datacapture/QuestionnaireViewModel.kt @@ -1183,7 +1183,7 @@ internal data class QuestionnairePage( ) internal val QuestionnairePagination.hasPreviousPage: Boolean - get() = pages.any { it.index < currentPageIndex && it.enabled } + get() = pages.any { it.index < currentPageIndex && it.enabled && !it.hidden } internal val QuestionnairePagination.hasNextPage: Boolean - get() = pages.any { it.index > currentPageIndex && it.enabled } + get() = pages.any { it.index > currentPageIndex && it.enabled && !it.hidden } diff --git a/datacapture/src/test/java/com/google/android/fhir/datacapture/QuestionnaireViewModelTest.kt b/datacapture/src/test/java/com/google/android/fhir/datacapture/QuestionnaireViewModelTest.kt index 51e00eaa1d..659429a95a 100644 --- a/datacapture/src/test/java/com/google/android/fhir/datacapture/QuestionnaireViewModelTest.kt +++ b/datacapture/src/test/java/com/google/android/fhir/datacapture/QuestionnaireViewModelTest.kt @@ -1533,6 +1533,102 @@ class QuestionnaireViewModelTest { // Pagination // // // // ==================================================================== // + @Test + fun `should include all top level items as pages when any item has page extension`() = runTest { + val questionnaire = + Questionnaire().apply { + id = "a-questionnaire" + addItem( + QuestionnaireItemComponent().apply { + linkId = "page1-noExtension" + type = Questionnaire.QuestionnaireItemType.GROUP + addItem( + QuestionnaireItemComponent().apply { + linkId = "page1-1" + type = Questionnaire.QuestionnaireItemType.BOOLEAN + text = "Question on page 1" + }, + ) + }, + ) + addItem( + QuestionnaireItemComponent().apply { + linkId = "page2" + type = Questionnaire.QuestionnaireItemType.GROUP + addExtension(paginationExtension) + addItem( + QuestionnaireItemComponent().apply { + linkId = "page2-1" + type = Questionnaire.QuestionnaireItemType.BOOLEAN + text = "Question on page 2" + }, + ) + }, + ) + addItem( + QuestionnaireItemComponent().apply { + linkId = "page3-noExtension" + type = Questionnaire.QuestionnaireItemType.GROUP + addItem( + QuestionnaireItemComponent().apply { + linkId = "page3-1" + type = Questionnaire.QuestionnaireItemType.BOOLEAN + text = "Question on page 3" + }, + ) + }, + ) + addItem( + QuestionnaireItemComponent().apply { + linkId = "page4-noExtension-hidden" + addExtension(hiddenExtension) + type = Questionnaire.QuestionnaireItemType.GROUP + addItem( + QuestionnaireItemComponent().apply { + linkId = "page4-1" + type = Questionnaire.QuestionnaireItemType.BOOLEAN + text = "Question on page 4" + }, + ) + }, + ) + addItem( + QuestionnaireItemComponent().apply { + linkId = "page5" + type = Questionnaire.QuestionnaireItemType.GROUP + addExtension(paginationExtension) + addItem( + QuestionnaireItemComponent().apply { + linkId = "page5-1" + type = Questionnaire.QuestionnaireItemType.BOOLEAN + text = "Question on page 5" + }, + ) + }, + ) + } + + val viewModel = createQuestionnaireViewModel(questionnaire) + viewModel.runViewModelBlocking { + assertThat( + (viewModel.questionnaireStateFlow.value.displayMode as DisplayMode.EditMode).pagination, + ) + .isEqualTo( + QuestionnairePagination( + isPaginated = true, + pages = + listOf( + QuestionnairePage(0, enabled = true, hidden = false), + QuestionnairePage(1, enabled = true, hidden = false), + QuestionnairePage(2, enabled = true, hidden = false), + QuestionnairePage(3, enabled = true, hidden = true), + QuestionnairePage(4, enabled = true, hidden = false), + ), + currentPageIndex = 0, + ), + ) + } + } @Test fun `should show current page`() = runTest { @@ -1830,8 +1926,11 @@ class QuestionnaireViewModelTest { } val viewModel = createQuestionnaireViewModel(questionnaire) viewModel.runViewModelBlocking { + val questionnaireStatePagination = + (viewModel.questionnaireStateFlow.value.displayMode as DisplayMode.EditMode).pagination + assertThat( - (viewModel.questionnaireStateFlow.value.displayMode as DisplayMode.EditMode).pagination, + questionnaireStatePagination, ) .isEqualTo( QuestionnairePagination( @@ -1845,6 +1944,83 @@ class QuestionnaireViewModelTest { currentPageIndex = 1, ), ) + + assertThat(questionnaireStatePagination.hasPreviousPage).isFalse() + } + } + + @Test + fun `should skip last page if it is hidden`() = runTest { + val questionnaire = + Questionnaire().apply { + id = "a-questionnaire" + addItem( + QuestionnaireItemComponent().apply { + linkId = "page1" + type = Questionnaire.QuestionnaireItemType.GROUP + addExtension(paginationExtension) + addItem( + QuestionnaireItemComponent().apply { + linkId = "page1-1" + type = Questionnaire.QuestionnaireItemType.BOOLEAN + text = "Question on page 1" + }, + ) + }, + ) + addItem( + QuestionnaireItemComponent().apply { + linkId = "page2" + type = Questionnaire.QuestionnaireItemType.GROUP + addExtension(paginationExtension) + addItem( + QuestionnaireItemComponent().apply { + linkId = "page2-1" + type = Questionnaire.QuestionnaireItemType.BOOLEAN + text = "Question on page 2" + }, + ) + }, + ) + addItem( + QuestionnaireItemComponent().apply { + linkId = "page3" + type = Questionnaire.QuestionnaireItemType.GROUP + addExtension(paginationExtension) + addExtension(hiddenExtension) + addItem( + QuestionnaireItemComponent().apply { + linkId = "page3-1" + type = Questionnaire.QuestionnaireItemType.BOOLEAN + text = "Question on page 3" + }, + ) + }, + ) + } + val viewModel = createQuestionnaireViewModel(questionnaire) + viewModel.runViewModelBlocking { + viewModel.goToNextPage() + val questionnaireStatePagination = + (viewModel.questionnaireStateFlow.value.displayMode as DisplayMode.EditMode).pagination + + assertThat( + questionnaireStatePagination, + ) + .isEqualTo( + QuestionnairePagination( + isPaginated = true, + pages = + listOf( + QuestionnairePage(0, enabled = true, hidden = false), + QuestionnairePage(1, enabled = true, hidden = false), + QuestionnairePage(2, enabled = true, hidden = true), + ), + currentPageIndex = 1, + ), + ) + + assertThat(questionnaireStatePagination.hasNextPage).isFalse() } }