Skip to content

Commit

Permalink
Add argument to disable Submit anyway button in the questionnaire con…
Browse files Browse the repository at this point in the history
…firmation popup (#2673)

* Add argument to disable Submit anyway button in the questionnaire confirmation popup

* Incorporated review feedback.

* Incorporated additional review feedback.
  • Loading branch information
rahulmalhotra authored Oct 1, 2024
1 parent 7fd7d56 commit 8d3dbc8
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,23 @@ class QuestionnaireFragment : Fragment() {
} else {
val errorViewModel: QuestionnaireValidationErrorViewModel by activityViewModels()
errorViewModel.setQuestionnaireAndValidation(viewModel.questionnaire, validationMap)
QuestionnaireValidationErrorMessageDialogFragment()
.show(
requireActivity().supportFragmentManager,
QuestionnaireValidationErrorMessageDialogFragment.TAG,
)
val validationErrorMessageDialog = QuestionnaireValidationErrorMessageDialogFragment()
if (requireArguments().containsKey(EXTRA_SHOW_SUBMIT_ANYWAY_BUTTON)) {
validationErrorMessageDialog.arguments =
Bundle().apply {
putBoolean(
EXTRA_SHOW_SUBMIT_ANYWAY_BUTTON,
requireArguments()
.getBoolean(
EXTRA_SHOW_SUBMIT_ANYWAY_BUTTON,
),
)
}
}
validationErrorMessageDialog.show(
requireActivity().supportFragmentManager,
QuestionnaireValidationErrorMessageDialogFragment.TAG,
)
}
}
}
Expand Down Expand Up @@ -407,6 +419,11 @@ class QuestionnaireFragment : Fragment() {
args.add(EXTRA_SHOW_NAVIGATION_IN_DEFAULT_LONG_SCROLL to value)
}

/** Setter to show/hide the Submit anyway button. This button is visible by default. */
fun setShowSubmitAnywayButton(value: Boolean) = apply {
args.add(EXTRA_SHOW_SUBMIT_ANYWAY_BUTTON to value)
}

@VisibleForTesting fun buildArgs() = bundleOf(*args.toTypedArray())

/** @return A [QuestionnaireFragment] with provided [Bundle] arguments. */
Expand Down Expand Up @@ -509,6 +526,12 @@ class QuestionnaireFragment : Fragment() {
internal const val EXTRA_SHOW_NAVIGATION_IN_DEFAULT_LONG_SCROLL =
"show-navigation-in-default-long-scroll"

/**
* A [Boolean] extra to show or hide the Submit anyway button in the questionnaire. Default is
* true.
*/
internal const val EXTRA_SHOW_SUBMIT_ANYWAY_BUTTON = "show-submit-anyway-button"

fun builder() = Builder()
}

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 @@ -51,17 +51,23 @@ internal class QuestionnaireValidationErrorMessageDialogFragment(

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
isCancelable = false
return MaterialAlertDialogBuilder(requireContext())
.setView(onCreateCustomView())
.setPositiveButton(R.string.questionnaire_validation_error_fix_button_text) { dialog, _ ->
val currentDialog =
MaterialAlertDialogBuilder(requireContext()).setView(onCreateCustomView()).setPositiveButton(
R.string.questionnaire_validation_error_fix_button_text,
) { dialog, _ ->
setFragmentResult(RESULT_CALLBACK, bundleOf(RESULT_KEY to RESULT_VALUE_FIX))
dialog?.dismiss()
}
.setNegativeButton(R.string.questionnaire_validation_error_submit_button_text) { dialog, _ ->
if (arguments == null || requireArguments().getBoolean(EXTRA_SHOW_SUBMIT_ANYWAY_BUTTON, true)) {
currentDialog.setNegativeButton(R.string.questionnaire_validation_error_submit_button_text) {
dialog,
_,
->
setFragmentResult(RESULT_CALLBACK, bundleOf(RESULT_KEY to RESULT_VALUE_SUBMIT))
dialog?.dismiss()
}
.create()
}
return currentDialog.create()
}

@VisibleForTesting
Expand Down Expand Up @@ -97,6 +103,12 @@ internal class QuestionnaireValidationErrorMessageDialogFragment(
const val RESULT_KEY = "result"
const val RESULT_VALUE_FIX = "result_fix"
const val RESULT_VALUE_SUBMIT = "result_submit"

/**
* A [Boolean] extra to show or hide the Submit anyway button in the questionnaire. Default is
* true.
*/
internal const val EXTRA_SHOW_SUBMIT_ANYWAY_BUTTON = "show-submit-anyway-button"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@

package com.google.android.fhir.datacapture

import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.FragmentFactory
import androidx.fragment.app.testing.launchFragment
import androidx.fragment.app.testing.launchFragmentInContainer
import androidx.fragment.app.testing.withFragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.test.platform.app.InstrumentationRegistry
import com.google.android.fhir.datacapture.validation.QuestionnaireResponseValidator
import com.google.common.truth.Truth.assertThat
import kotlin.test.assertEquals
import kotlinx.coroutines.test.runTest
import org.hl7.fhir.r4.model.Questionnaire
import org.hl7.fhir.r4.model.QuestionnaireResponse
Expand Down Expand Up @@ -75,6 +80,93 @@ class QuestionnaireValidationErrorMessageDialogFragmentTest {
}
}

@Test
fun `check alertDialog when submit anyway button argument is true should show Submit anyway button`() {
runTest {
val questionnaireValidationErrorMessageDialogArguments = Bundle()
questionnaireValidationErrorMessageDialogArguments.putBoolean(
QuestionnaireValidationErrorMessageDialogFragment.EXTRA_SHOW_SUBMIT_ANYWAY_BUTTON,
true,
)
with(
launchFragment<QuestionnaireValidationErrorMessageDialogFragment>(
themeResId = R.style.Theme_Questionnaire,
fragmentArgs = questionnaireValidationErrorMessageDialogArguments,
),
) {
onFragment { fragment ->
assertThat(fragment.dialog).isNotNull()
assertThat(fragment.requireDialog().isShowing).isTrue()
val alertDialog = fragment.dialog as? AlertDialog
val context = InstrumentationRegistry.getInstrumentation().targetContext
val positiveButtonText =
context.getString(R.string.questionnaire_validation_error_fix_button_text)
val negativeButtonText =
context.getString(R.string.questionnaire_validation_error_submit_button_text)
assertThat(alertDialog?.getButton(AlertDialog.BUTTON_POSITIVE)?.text)
.isEqualTo(positiveButtonText)
assertThat(alertDialog?.getButton(AlertDialog.BUTTON_NEGATIVE)?.text)
.isEqualTo(negativeButtonText)
}
}
}
}

@Test
fun `check alertDialog when no arguments are passed should show Submit anyway button`() {
runTest {
with(
launchFragment<QuestionnaireValidationErrorMessageDialogFragment>(
themeResId = R.style.Theme_Questionnaire,
),
) {
onFragment { fragment ->
assertThat(fragment.dialog).isNotNull()
assertThat(fragment.requireDialog().isShowing).isTrue()
val alertDialog = fragment.dialog as? AlertDialog
val context = InstrumentationRegistry.getInstrumentation().targetContext
val positiveButtonText =
context.getString(R.string.questionnaire_validation_error_fix_button_text)
val negativeButtonText =
context.getString(R.string.questionnaire_validation_error_submit_button_text)
assertThat(alertDialog?.getButton(AlertDialog.BUTTON_POSITIVE)?.text)
.isEqualTo(positiveButtonText)
assertThat(alertDialog?.getButton(AlertDialog.BUTTON_NEGATIVE)?.text)
.isEqualTo(negativeButtonText)
}
}
}
}

@Test
fun `check alertDialog when submit anyway button argument is false should hide Submit anyway button`() {
runTest {
val validationErrorBundle = Bundle()
validationErrorBundle.putBoolean(
QuestionnaireValidationErrorMessageDialogFragment.EXTRA_SHOW_SUBMIT_ANYWAY_BUTTON,
false,
)
with(
launchFragment<QuestionnaireValidationErrorMessageDialogFragment>(
themeResId = R.style.Theme_Questionnaire,
fragmentArgs = validationErrorBundle,
),
) {
onFragment { fragment ->
assertThat(fragment.dialog).isNotNull()
assertThat(fragment.requireDialog().isShowing).isTrue()
val alertDialog = fragment.dialog as? AlertDialog
val context = InstrumentationRegistry.getInstrumentation().targetContext
val positiveButtonText =
context.getString(R.string.questionnaire_validation_error_fix_button_text)
assertThat(alertDialog?.getButton(AlertDialog.BUTTON_POSITIVE)?.text)
.isEqualTo(positiveButtonText)
assertEquals(alertDialog?.getButton(AlertDialog.BUTTON_NEGATIVE)?.text, "")
}
}
}
}

private suspend fun createTestValidationErrorViewModel(
questionnaire: Questionnaire,
questionnaireResponse: QuestionnaireResponse,
Expand Down

0 comments on commit 8d3dbc8

Please sign in to comment.