-
Notifications
You must be signed in to change notification settings - Fork 25
/
AddPatientFragment.kt
108 lines (96 loc) · 3.55 KB
/
AddPatientFragment.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* Copyright 2022-2023 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://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.fhir.examples.cloudfunction
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.NavHostFragment
import com.google.android.fhir.datacapture.QuestionnaireFragment
import org.hl7.fhir.r4.model.QuestionnaireResponse
/** A fragment class to show patient registration screen. */
class AddPatientFragment : Fragment(R.layout.add_patient_fragment) {
private val viewModel: AddPatientViewModel by viewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setUpActionBar()
setHasOptionsMenu(true)
updateArguments()
if (savedInstanceState == null) {
addQuestionnaireFragment()
}
observePatientSaveAction()
childFragmentManager.setFragmentResultListener(
QuestionnaireFragment.SUBMIT_REQUEST_KEY,
viewLifecycleOwner
) { _, _ -> onSubmitAction() }
(activity as MainActivity).setDrawerEnabled(false)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
android.R.id.home -> {
NavHostFragment.findNavController(this).navigateUp()
true
}
else -> super.onOptionsItemSelected(item)
}
}
private fun setUpActionBar() {
(requireActivity() as AppCompatActivity).supportActionBar?.apply {
title = requireContext().getString(R.string.add_patient)
setDisplayHomeAsUpEnabled(true)
}
}
private fun updateArguments() {
requireArguments()
.putString(QUESTIONNAIRE_FILE_PATH_KEY, "new-patient-registration-paginated.json")
}
private fun addQuestionnaireFragment() {
childFragmentManager.commit {
add(
R.id.add_patient_container,
QuestionnaireFragment.builder().setQuestionnaire(viewModel.questionnaire).build(),
QUESTIONNAIRE_FRAGMENT_TAG
)
}
}
private fun onSubmitAction() {
val questionnaireFragment =
childFragmentManager.findFragmentByTag(QUESTIONNAIRE_FRAGMENT_TAG) as QuestionnaireFragment
savePatient(questionnaireFragment.getQuestionnaireResponse())
}
private fun savePatient(questionnaireResponse: QuestionnaireResponse) {
viewModel.savePatient(questionnaireResponse)
}
private fun observePatientSaveAction() {
viewModel.isPatientSaved.observe(viewLifecycleOwner) {
if (!it) {
Toast.makeText(requireContext(), "Inputs are missing.", Toast.LENGTH_SHORT).show()
return@observe
}
Toast.makeText(requireContext(), "Patient is saved.", Toast.LENGTH_SHORT).show()
NavHostFragment.findNavController(this).navigateUp()
}
}
companion object {
const val QUESTIONNAIRE_FILE_PATH_KEY = "questionnaire-file-path-key"
const val QUESTIONNAIRE_FRAGMENT_TAG = "questionnaire-fragment-tag"
}
}