-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Highlight missing fields and Show validation popup. (#1671)
* Show validation popup for missing fields * Fixed formatting * Review comments fix * Added companion object to ErrorMessage dialog fragment * Review changes * Review Comments: Removed recycler view from popup. * Updated margin for errors * Added scrollview for the dialog body. * Update text Co-authored-by: Omar Ismail <[email protected]> Co-authored-by: Jing Tang <[email protected]>
- Loading branch information
1 parent
cb13ade
commit 9f3fc87
Showing
10 changed files
with
370 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
{ | ||
"linkId": "2", | ||
"type": "string", | ||
"required": true, | ||
"item": [ | ||
{ | ||
"linkId": "2.1", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
.../com/google/android/fhir/datacapture/QuestionnaireValidationErrorMessageDialogFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright 2022 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http:https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.android.fhir.datacapture | ||
|
||
import android.app.Dialog | ||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.widget.TextView | ||
import androidx.annotation.VisibleForTesting | ||
import androidx.appcompat.view.ContextThemeWrapper | ||
import androidx.core.content.res.use | ||
import androidx.fragment.app.DialogFragment | ||
import androidx.fragment.app.activityViewModels | ||
import androidx.fragment.app.setFragmentResult | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import com.google.android.fhir.datacapture.validation.Invalid | ||
import com.google.android.fhir.datacapture.validation.ValidationResult | ||
import com.google.android.material.dialog.MaterialAlertDialogBuilder | ||
import org.hl7.fhir.r4.model.Questionnaire | ||
|
||
/** | ||
* [Dialog] to highlight the required fields that need to be filled by the user before submitting | ||
* the [Questionnaire]. This is shown when user has pressed internal submit button in the | ||
* [QuestionnaireFragment] and there are some validation errors. | ||
*/ | ||
internal class QuestionnaireValidationErrorMessageDialogFragment( | ||
/** | ||
* Factory helps with testing and should not be set to anything in the regular production flow. | ||
*/ | ||
private val factoryProducer: (() -> ViewModelProvider.Factory)? = null | ||
) : DialogFragment() { | ||
|
||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { | ||
isCancelable = false | ||
return MaterialAlertDialogBuilder(requireContext()) | ||
.setView(onCreateCustomView()) | ||
.setPositiveButton(R.string.questionnaire_validation_error_fix_button_text) { dialog, _ -> | ||
dialog?.dismiss() | ||
} | ||
.setNegativeButton(R.string.questionnaire_validation_error_submit_button_text) { dialog, _ -> | ||
setFragmentResult(RESULT_CALLBACK, Bundle.EMPTY) | ||
dialog?.dismiss() | ||
} | ||
.create() | ||
} | ||
|
||
@VisibleForTesting | ||
fun onCreateCustomView(layoutInflater: LayoutInflater = getLayoutInflater()): View { | ||
val themeId = | ||
layoutInflater.context.obtainStyledAttributes(R.styleable.QuestionnaireTheme).use { | ||
it.getResourceId( | ||
// Use the custom questionnaire theme if it is specified | ||
R.styleable.QuestionnaireTheme_questionnaire_theme, | ||
// Otherwise, use the default questionnaire theme | ||
R.style.Theme_Questionnaire | ||
) | ||
} | ||
|
||
return layoutInflater | ||
.cloneInContext(ContextThemeWrapper(layoutInflater.context, themeId)) | ||
.inflate(R.layout.questionnaire_validation_dialog, null) | ||
.apply { | ||
findViewById<TextView>(R.id.body).apply { | ||
val viewModel: QuestionnaireValidationErrorViewModel by | ||
activityViewModels(factoryProducer) | ||
text = | ||
viewModel.getItemsTextWithValidationErrors().joinToString(separator = "\n") { | ||
context.getString(R.string.questionnaire_validation_error_item_text_with_bullet, it) | ||
} | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
const val TAG = "QuestionnaireValidationErrorMessageDialogFragment" | ||
const val RESULT_CALLBACK = "QuestionnaireValidationResultCallback" | ||
} | ||
} | ||
|
||
internal class QuestionnaireValidationErrorViewModel : ViewModel() { | ||
private var questionnaire: Questionnaire? = null | ||
private var validation: Map<String, List<ValidationResult>>? = null | ||
|
||
fun setQuestionnaireAndValidation( | ||
questionnaire: Questionnaire, | ||
validation: Map<String, List<ValidationResult>> | ||
) { | ||
this.questionnaire = questionnaire | ||
this.validation = validation | ||
} | ||
|
||
/** @return Texts associated with the failing [Questionnaire.QuestionnaireItemComponent]s. */ | ||
fun getItemsTextWithValidationErrors(): List<String> { | ||
val invalidFields = | ||
validation?.filterValues { it.filterIsInstance<Invalid>().isNotEmpty() } ?: emptyMap() | ||
return questionnaire | ||
?.item | ||
?.flattened() | ||
?.filter { invalidFields.contains(it.linkId) } | ||
?.map { if (it.text.isNullOrEmpty()) it.localizedFlyoverSpanned.toString() else it.text } | ||
?: emptyList() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
datacapture/src/main/res/layout/questionnaire_validation_dialog.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<!-- | ||
Copyright 2021 Google LLC | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http:https://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<LinearLayout | ||
xmlns:android="http:https://schemas.android.com/apk/res/android" | ||
xmlns:tools="http:https://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:orientation="vertical" | ||
android:padding="@dimen/padding_modal_card" | ||
> | ||
|
||
<TextView | ||
android:id="@+id/dialog_title" | ||
style="?attr/questionnaireValidationDialogTitleStyle" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
tools:text="Complete all required fields" | ||
android:text="@string/questionnaire_validation_error_headline" | ||
/> | ||
|
||
<TextView | ||
android:layout_marginTop="@dimen/padding_default" | ||
android:id="@+id/dialog_subtitle" | ||
style="?attr/questionnaireValidationDialogBodyStyle" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
tools:text="Fields that need to be completed:" | ||
android:text="@string/questionnaire_validation_error_supporting_text" | ||
tools:visibility="visible" | ||
/> | ||
|
||
<ScrollView | ||
android:layout_marginVertical="@dimen/item_margin_vertical" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
> | ||
<TextView | ||
android:id="@+id/body" | ||
style="?attr/questionnaireValidationDialogBodyStyle" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
tools:text="Missing field." | ||
tools:visibility="visible" | ||
/> | ||
</ScrollView> | ||
|
||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.