SelectPatientsFragment.java
6.47 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.sw.laryngoscope.FHIR.fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.sw.laryngoscope.R;
import com.sw.laryngoscope.FHIR.adapter.SelectPatientsAdapter;
import com.sw.laryngoscope.databinding.FgPatientFragmentBinding;
import com.sw.laryngoscope.FHIR.model.PatientModel;
import com.sw.laryngoscope.FHIR.viewModel.SharedViewModel;
import java.util.ArrayList;
import java.util.HashMap;
public class SelectPatientsFragment extends Fragment implements SelectPatientsAdapter.OnPatientClickListener{
private String practitionerID;
private SelectPatientsFragment thisFrag; // a reference to this fragment
// private Toolbar toolbar;
private SharedViewModel sharedViewModel;
// private RecyclerView recyclerView;
private ArrayList<PatientModel> selectedPatients = new ArrayList<>();
// private TextView title;
// private TextView loadingTextView;
// private ImageButton backButton;
// private ProgressBar loadingSpinner;
private FgPatientFragmentBinding binding;
/**
* Constructor
* @param practitionerID the Health Practitioner's ID
*/
public SelectPatientsFragment(String practitionerID) {
this.practitionerID = practitionerID;
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
binding = FgPatientFragmentBinding.inflate(inflater, container, false);
return binding.getRoot();
}
public void onViewCreatred(@NonNull View view,@Nullable Bundle savedInstanceState){
sharedViewModel = new ViewModelProvider(requireActivity()).get(SharedViewModel.class);
// find all the graphical components
// recyclerView = root.findViewById(R.id.select_patients_recycler_view);
// backButton = root.findViewById(R.id.btn_back);
// toolbar = root.findViewById(R.id.select_patients_toolbar);
// title = toolbar.findViewById(R.id.toolbar_title);
// loadingTextView = root.findViewById(R.id.select_patients_txt_loading);
// loadingSpinner = root.findViewById(R.id.select_patients_progressBar);
binding.selectPatientsTxtLoading.setVisibility(View.VISIBLE);
binding.selectPatientsProgressBar.setVisibility(View.VISIBLE);
sharedViewModel.setPractitionerID(practitionerID);
setUpToolBar();
//update UI when there's new patient under Health Practitioner
sharedViewModel.getAllPatients().observe(getViewLifecycleOwner(),patientUpdatedObserver);
thisFrag = this;
}
/**
* This observer observes for changes in patient list
* ( The list that contains all patients under Health Practitioner )
* It displays a loading screen when the app is still fetching data from the server,
* It immediately updates the UI to display the patients when the system completes
* the data fetching process.
*/
private Observer<HashMap<String,PatientModel>> patientUpdatedObserver = new Observer<HashMap<String,PatientModel >>() {
@Override
public void onChanged( HashMap < String, PatientModel > patientHashMap) {
if (patientHashMap.size() == 0) {
binding.selectPatientsTxtLoading.setText(R.string.zero_patients_message);
}
else{
binding.selectPatientsProgressBar.setVisibility(View.GONE);
binding.selectPatientsTxtLoading.setVisibility(View.GONE);
binding.selectPatientsRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
binding.selectPatientsRecyclerView.setAdapter( new SelectPatientsAdapter(patientHashMap,thisFrag, selectedPatients));
}
}
};
private void setUpToolBar() {
binding.selectPatientsToolbar.toolbarTitle.setText(R.string.title_patient_selection);
binding.selectPatientsToolbar.getRoot().inflateMenu(R.menu.select_patients_menu);
Toolbar.OnMenuItemClickListener menuItemClickListener = item -> {
if (selectedPatients.size() > 0) {
// save list into view model
sharedViewModel.setSelectedPatients(selectedPatients);
// MainActivity main = (MainActivity) getActivity();
// Objects.requireNonNull(main).findFragment(MainActivity.home_fragment);
} else {
String message = "Please select AT LEAST 1 patient";
Context context = getContext();
Toast toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
toast.show();
}
return true;
};
binding.selectPatientsToolbar.getRoot().setOnMenuItemClickListener(menuItemClickListener);
binding.selectPatientsToolbar.btnBack.setOnClickListener(v -> {
// MainActivity main = (MainActivity) getActivity();
// Objects.requireNonNull(main).findFragment(MainActivity.login_fragment);
});
}
public void onPatientClick(boolean checked, PatientModel patient) {
PatientModel patientWithSameID = null;
boolean found = false;
for (PatientModel p : selectedPatients){
if(p.getPatientID().equals(patient.getPatientID())){
found = true;
patientWithSameID = p;
break;
}
}
if (checked) {
if (!found) {
selectedPatients.add(patient);
}
} else if (!checked) {
if(found){
selectedPatients.remove(patientWithSameID);
}else{
selectedPatients.remove(patient);
}
}
updateToolbar();
}
/**
* This method updates the tool bar to display the total number of patients that is currently
* being selected
*/
private void updateToolbar() {
String text = selectedPatients.size() + " Patients Selected";
// Log.d("Select_Patient_Fragment","current sellected patient : " + selected_patients.toString());
binding.selectPatientsToolbar.toolbarTitle.setText(text);
}
}