-
Notifications
You must be signed in to change notification settings - Fork 25
/
AddPatientViewModel.kt
98 lines (89 loc) · 3.37 KB
/
AddPatientViewModel.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
/*
* 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.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope
import ca.uhn.fhir.context.FhirContext
import ca.uhn.fhir.context.FhirVersionEnum
import com.google.android.fhir.FhirEngine
import com.google.android.fhir.datacapture.mapping.ResourceMapper
import com.google.android.fhir.datacapture.validation.Invalid
import com.google.android.fhir.datacapture.validation.QuestionnaireResponseValidator
import java.util.UUID
import kotlinx.coroutines.launch
import org.hl7.fhir.r4.model.Patient
import org.hl7.fhir.r4.model.Questionnaire
import org.hl7.fhir.r4.model.QuestionnaireResponse
/** ViewModel for patient registration screen {@link AddPatientFragment}. */
class AddPatientViewModel(application: Application, private val state: SavedStateHandle) :
AndroidViewModel(application) {
val questionnaire: String
get() = getQuestionnaireJson()
val isPatientSaved = MutableLiveData<Boolean>()
private val questionnaireResource: Questionnaire
get() =
FhirContext.forCached(FhirVersionEnum.R4).newJsonParser().parseResource(questionnaire)
as Questionnaire
private var fhirEngine: FhirEngine = FhirApplication.fhirEngine(application.applicationContext)
private var questionnaireJson: String? = null
/**
* Saves patient registration questionnaire response into the application database.
*
* @param questionnaireResponse patient registration questionnaire response
*/
fun savePatient(questionnaireResponse: QuestionnaireResponse) {
viewModelScope.launch {
if (QuestionnaireResponseValidator.validateQuestionnaireResponse(
questionnaireResource,
questionnaireResponse,
getApplication()
)
.values
.flatten()
.any { it is Invalid }
) {
isPatientSaved.value = false
return@launch
}
val entry = ResourceMapper.extract(questionnaireResource, questionnaireResponse).entryFirstRep
if (entry.resource !is Patient) {
return@launch
}
val patient = entry.resource as Patient
patient.id = generateUuid()
fhirEngine.create(patient)
isPatientSaved.value = true
}
}
private fun getQuestionnaireJson(): String {
questionnaireJson?.let {
return it
}
questionnaireJson = readFileFromAssets(state[AddPatientFragment.QUESTIONNAIRE_FILE_PATH_KEY]!!)
return questionnaireJson!!
}
private fun readFileFromAssets(filename: String): String {
return getApplication<Application>().assets.open(filename).bufferedReader().use {
it.readText()
}
}
private fun generateUuid(): String {
return UUID.randomUUID().toString()
}
}