Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow modification of Questionnaire submit action button text via configuration #2319

Merged
merged 20 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -174,6 +174,9 @@ class DemoQuestionnaireFragment : Fragment() {
.encodeResourceToString(Patient().apply { id = "P1" })
.let { mapOf("patient" to it) },
)
.setSubmitButtonText(
getString(com.google.android.fhir.datacapture.R.string.submit_questionnaire),
)
.build(),
QUESTIONNAIRE_FRAGMENT_TAG,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,28 @@ class QuestionnaireFragment : Fragment() {
.show(requireActivity().supportFragmentManager, QuestionnaireCancelDialogFragment.TAG)
}

view.findViewById<Button>(R.id.submit_questionnaire).setOnClickListener {
viewModel.validateQuestionnaireAndUpdateUI().let { validationMap ->
if (validationMap.values.flatten().filterIsInstance<Invalid>().isEmpty()) {
setFragmentResult(SUBMIT_REQUEST_KEY, Bundle.EMPTY)
} else {
val errorViewModel: QuestionnaireValidationErrorViewModel by activityViewModels()
errorViewModel.setQuestionnaireAndValidation(viewModel.questionnaire, validationMap)
QuestionnaireValidationErrorMessageDialogFragment()
.show(
requireActivity().supportFragmentManager,
QuestionnaireValidationErrorMessageDialogFragment.TAG,
)
view
.findViewById<Button>(R.id.submit_questionnaire)
.apply {
text =
requireArguments()
.getString(EXTRA_SUBMIT_BUTTON_TEXT, getString(R.string.submit_questionnaire))
}
.setOnClickListener {
viewModel.validateQuestionnaireAndUpdateUI().let { validationMap ->
if (validationMap.values.flatten().filterIsInstance<Invalid>().isEmpty()) {
setFragmentResult(SUBMIT_REQUEST_KEY, Bundle.EMPTY)
} else {
val errorViewModel: QuestionnaireValidationErrorViewModel by activityViewModels()
errorViewModel.setQuestionnaireAndValidation(viewModel.questionnaire, validationMap)
QuestionnaireValidationErrorMessageDialogFragment()
.show(
requireActivity().supportFragmentManager,
QuestionnaireValidationErrorMessageDialogFragment.TAG,
)
}
}
}
}
val questionnaireProgressIndicator: LinearProgressIndicator =
view.findViewById(R.id.questionnaire_progress_indicator)
val questionnaireEditAdapter =
Expand Down Expand Up @@ -407,6 +414,9 @@ class QuestionnaireFragment : Fragment() {
*/
fun setShowSubmitButton(value: Boolean) = apply { args.add(EXTRA_SHOW_SUBMIT_BUTTON to value) }

/** To accept a configurable text for the submit button */
fun setSubmitButtonText(text: String) = apply { args.add(EXTRA_SUBMIT_BUTTON_TEXT to text) }

/**
* A [Boolean] extra to show or hide the Cancel button in the questionnaire. Default is true.
*/
Expand Down Expand Up @@ -509,6 +519,8 @@ class QuestionnaireFragment : Fragment() {

internal const val EXTRA_SHOW_REQUIRED_TEXT = "show-required-text"

internal const val EXTRA_SUBMIT_BUTTON_TEXT = "submit-button-text"

fun builder() = Builder()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.google.android.fhir.datacapture.QuestionnaireFragment.Companion.EXTRA
import com.google.android.fhir.datacapture.testing.DataCaptureTestApplication
import com.google.android.fhir.datacapture.views.factories.DateTimePickerViewHolderFactory
import com.google.common.truth.Truth.assertThat
import kotlin.test.assertEquals
import org.hl7.fhir.r4.model.Questionnaire
import org.junit.Before
import org.junit.Test
Expand Down Expand Up @@ -139,6 +140,37 @@ class QuestionnaireFragmentTest {
.isEqualTo(View.VISIBLE)
}

@Test
fun `questionnaire submit button text should be editable`() {
val questionnaire =
Questionnaire().apply {
id = "a-questionnaire"
addItem(
Questionnaire.QuestionnaireItemComponent().apply {
linkId = "a-link-id"
type = Questionnaire.QuestionnaireItemType.BOOLEAN
},
)
}
val questionnaireJson = parser.encodeResourceToString(questionnaire)
val customButtonText = "Apply"
val scenario =
launchFragmentInContainer<QuestionnaireFragment>(
QuestionnaireFragment.builder()
.setQuestionnaire(questionnaireJson)
.setSubmitButtonText(customButtonText)
.buildArgs(),
)

scenario.moveToState(Lifecycle.State.RESUMED)

val button =
scenario.withFragment { this.requireView().findViewById<Button>(R.id.submit_questionnaire) }
DebbieArita marked this conversation as resolved.
Show resolved Hide resolved

val buttonText = button.text.toString()
assertEquals(buttonText, customButtonText)
}

@Test
fun `should hide next button on last page`() {
val questionnaireJson =
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 @@ -88,6 +88,9 @@ class AddPatientFragment : Fragment(R.layout.add_patient_fragment) {
QuestionnaireFragment.builder()
.setQuestionnaire(viewModel.questionnaireJson)
.setShowCancelButton(true)
.setSubmitButtonText(
getString(com.google.android.fhir.datacapture.R.string.submit_questionnaire),
)
.build(),
QUESTIONNAIRE_FRAGMENT_TAG,
)
Expand Down
Loading