SharedViewModel.java
5.5 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.sw.laryngoscope.FHIR.viewModel;
import android.util.Log;
import android.os.Handler;
import android.os.HandlerThread;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.LiveData;
import com.sw.laryngoscope.FHIR.model.PatientModel;
import com.sw.laryngoscope.FHIR.model.observation.ObservationModel;
import com.sw.laryngoscope.FHIR.model.observation.ObservationType;
import com.sw.laryngoscope.FHIR.service.repository.ObservationRepositoryFactory;
import com.sw.laryngoscope.FHIR.service.repository.PatientRepository;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Objects;
public class SharedViewModel extends ViewModel implements Poll {
// for polling
private MutableLiveData<HashMap<String, PatientModel>> patientObservations = new MutableLiveData<>();
// to get all the patients under practitioner
private MutableLiveData<HashMap<String, PatientModel>> allPatients = new MutableLiveData<>();
private String practitionerID = "";
private PatientRepository patientRepository;
private ObservationRepositoryFactory observationRepositoryFactory;
private MutableLiveData<String> selectedFrequency = new MutableLiveData<>() ;
private MutableLiveData<ArrayList<PatientModel>> selectedPatients = new MutableLiveData<>();
private void initShareViewModel() {
patientRepository = new PatientRepository(practitionerID);
observationRepositoryFactory = new ObservationRepositoryFactory();
fetchAllPatients();
setSelectedPatients(new ArrayList<>());
selectedFrequency.setValue("10");
}
public void setPractitionerID(String practitionerID) {
this.practitionerID = practitionerID;
initShareViewModel();
}
public LiveData<String> getSelectedFrequency() {
return selectedFrequency;
}
public void updateCurrentSelected(String currentSelected) {
this.selectedFrequency.setValue(currentSelected);
}
public LiveData<HashMap<String, PatientModel>> getAllPatientObservations() {
return patientObservations;
}
public void setSelectedPatients(ArrayList<PatientModel> selectedPatientsArray) {
selectedPatients.setValue(selectedPatientsArray);
}
public LiveData<HashMap<String, PatientModel>> getAllPatients() {
return allPatients;
}
private ObservationModel getObservation(String id, ObservationType observationType) {
return observationRepositoryFactory.getObservationModel(id, observationType);
}
private void fetchAllPatients() {
// run asynchronous tasks on background thread to prevent network on main exception
HandlerThread backgroundThread = new HandlerThread("Background Thread");
backgroundThread.start();
Handler timer = new Handler(backgroundThread.getLooper());
timer.post(() -> {
HashMap < String, PatientModel > patientHashMap = new HashMap<>();
// loop through all patients
for (PatientModel patient : patientRepository.getAllPatients()) {
try {
// only show patients with cholesterol and bp values
for (ObservationType type: ObservationType.values()) {
patient.setObservation(type, getObservation(patient.getPatientID(), type));
patientHashMap.put(patient.getPatientID(), patient);
}
patient.addLatestBPReadings(observationRepositoryFactory.getLatestBloodPressureReadings(patient.getPatientID(),5));
}
// patient does not have the observation type
catch (Exception e) {
Log.e("Patient ", "No observation type");
}
}
// update LiveData and notify observers - used by select patient
allPatients.postValue(patientHashMap);
});
polling();
}
public void polling() {
// run asynchronous tasks on background thread to prevent network on main exception
HandlerThread backgroundThread = new HandlerThread("Background Thread");
backgroundThread.start();
Handler timer = new Handler(backgroundThread.getLooper());
timer.post(new Runnable() {
@Override
public void run() {
HashMap<String, PatientModel> poHashMap = new HashMap<>();
// loop through all patients. update observations if observation is selected to be monitored
for (PatientModel patientModel: Objects.requireNonNull(selectedPatients.getValue())) {
for (ObservationType type: ObservationType.values()) {
if (patientModel.isObservationMonitored(type)) {
patientModel.setObservation(type, getObservation(patientModel.getPatientID(), type));
poHashMap.put(patientModel.getPatientID(), patientModel);
if (type == ObservationType.BLOOD_PRESSURE) {
patientModel.addLatestBPReadings(observationRepositoryFactory.getLatestBloodPressureReadings(patientModel.getPatientID(),5));
}
}
}
}
// update LiveData and notify observers
patientObservations.postValue(poHashMap);
timer.postDelayed(this, Integer.parseInt(Objects.requireNonNull(getSelectedFrequency().getValue()))*1000);
}});
}
}