Skip to content

Commit

Permalink
Allow modification of Questionnaire submit action button text via con…
Browse files Browse the repository at this point in the history
…figuration (google#2319)

* Allow modification of Questionnaire submit action button text via configuration

* Run spotlessApply

* Update test to check that button text is editable

* Update test to check that button text is editable

* 🎨 Apply Spotless formatting

* Empty commit

* Resolve merge conflicts

---------

Co-authored-by: Allan Onchuru <[email protected]>
Co-authored-by: Francis Odhiambo <[email protected]>
Co-authored-by: Benjamin Mwalimu <[email protected]>
Co-authored-by: Allan Onchuru <[email protected]>
  • Loading branch information
5 people authored Jan 31, 2024
1 parent d97031d commit 4d675da
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 15 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 @@ -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) }

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

0 comments on commit 4d675da

Please sign in to comment.