-
Notifications
You must be signed in to change notification settings - Fork 3
/
GrowthDialogFragment.java
302 lines (255 loc) · 11.8 KB
/
GrowthDialogFragment.java
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package org.smartregister.growthmonitoring.fragment;
import android.annotation.SuppressLint;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.VisibleForTesting;
import android.support.design.widget.TabLayout;
import android.support.v4.app.DialogFragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.joda.time.DateTime;
import org.opensrp.api.constants.Gender;
import org.smartregister.commonregistry.CommonPersonObjectClient;
import org.smartregister.growthmonitoring.GrowthMonitoringLibrary;
import org.smartregister.growthmonitoring.R;
import org.smartregister.growthmonitoring.adapter.GrowthMonitoringTabsAdapter;
import org.smartregister.growthmonitoring.domain.Height;
import org.smartregister.growthmonitoring.domain.Weight;
import org.smartregister.growthmonitoring.util.AppProperties;
import org.smartregister.growthmonitoring.util.GrowthMonitoringConstants;
import org.smartregister.growthmonitoring.util.GrowthMonitoringUtils;
import org.smartregister.growthmonitoring.util.ImageUtils;
import org.smartregister.util.DateUtil;
import org.smartregister.util.OpenSRPImageLoader;
import org.smartregister.util.Utils;
import org.smartregister.view.activity.DrishtiApplication;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
@SuppressLint("ValidFragment")
public class GrowthDialogFragment extends DialogFragment {
private CommonPersonObjectClient personDetails;
private List<Weight> weights;
private List<Height> heights;
private TabLayout tabLayout;
private ViewPager viewPager;
private TextView nameView;
private TextView numberView;
private ImageView profilePic;
private TextView ageView;
private TextView pmtctStatus;
private Button done;
private static Boolean hasProperty;
private static Boolean monitorGrowth = false;
public static GrowthDialogFragment newInstance(CommonPersonObjectClient personDetails, List<Weight> weights,
List<Height> heights) {
hasProperty = GrowthMonitoringLibrary.getInstance().getAppProperties().hasProperty(AppProperties.KEY.MONITOR_GROWTH);
if (hasProperty) {
monitorGrowth = GrowthMonitoringLibrary.getInstance().getAppProperties().getPropertyBoolean(AppProperties.KEY.MONITOR_GROWTH);
}
GrowthDialogFragment growthDialogFragment = new GrowthDialogFragment();
growthDialogFragment.setPersonDetails(personDetails);
growthDialogFragment.setWeights(weights);
if (hasProperty && monitorGrowth) {
growthDialogFragment.setHeights(heights);
}
return growthDialogFragment;
}
public void setPersonDetails(CommonPersonObjectClient personDetails) {
this.personDetails = personDetails;
}
@VisibleForTesting
protected void sortWeights() {
HashMap<Long, Weight> weightHashMap = new HashMap<>();
for (Weight curWeight : getWeights()) {
if (curWeight.getDate() != null && curWeight.getBaseEntityId() != null) {
Calendar curCalendar = Calendar.getInstance();
curCalendar.setTime(curWeight.getDate());
GrowthMonitoringUtils.standardiseCalendarDate(curCalendar);
if (!weightHashMap.containsKey(curCalendar.getTimeInMillis())) {
weightHashMap.put(curCalendar.getTimeInMillis(), curWeight);
} else if (curWeight.getUpdatedAt() > weightHashMap.get(curCalendar.getTimeInMillis()).getUpdatedAt()) {
weightHashMap.put(curCalendar.getTimeInMillis(), curWeight);
}
}
}
List<Long> keys = new ArrayList<>(weightHashMap.keySet());
Collections.sort(keys, Collections.<Long>reverseOrder());
List<Weight> weightList = new ArrayList<>();
for (Long curKey : keys) {
weightList.add(weightHashMap.get(curKey));
}
setWeights(weightList);
}
@VisibleForTesting
protected void sortHeights() {
HashMap<Long, Height> heightHashMap = new HashMap<>();
for (Height curHeight : getHeights()) {
if (curHeight.getDate() != null && curHeight.getBaseEntityId() != null) {
Calendar curCalendar = Calendar.getInstance();
curCalendar.setTime(curHeight.getDate());
GrowthMonitoringUtils.standardiseCalendarDate(curCalendar);
if (!heightHashMap.containsKey(curCalendar.getTimeInMillis())) {
heightHashMap.put(curCalendar.getTimeInMillis(), curHeight);
} else if (curHeight.getUpdatedAt() > heightHashMap.get(curCalendar.getTimeInMillis()).getUpdatedAt()) {
heightHashMap.put(curCalendar.getTimeInMillis(), curHeight);
}
}
}
List<Long> keys = new ArrayList<>(heightHashMap.keySet());
Collections.sort(keys, Collections.<Long>reverseOrder());
List<Height> heightList = new ArrayList<>();
for (Long curKey : keys) {
heightList.add(heightHashMap.get(curKey));
}
setHeights(heightList);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo_Light_Dialog);
}
@Override
public void onStart() {
super.onStart();
// without a handler, the window sizes itself correctly
// but the keyboard does not show up
new Handler().post(new Runnable() {
@Override
public void run() {
Window window = null;
if (getDialog() != null) {
window = getDialog().getWindow();
}
if (window == null) {
return;
}
Point size = new Point();
Display display = window.getWindowManager().getDefaultDisplay();
display.getSize(size);
window.setLayout(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
window.setGravity(Gravity.CENTER);
}
});
}
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
String firstName = Utils.getValue(personDetails.getColumnmaps(), GrowthMonitoringConstants.FIRST_NAME, true);
String lastName = Utils.getValue(personDetails.getColumnmaps(), GrowthMonitoringConstants.LAST_NAME, true);
final ViewGroup dialogView = setUpViews(inflater, container);
nameView.setText(Utils.getName(firstName, lastName));
String personId = Utils.getValue(personDetails.getColumnmaps(), GrowthMonitoringConstants.ZEIR_ID, false);
if (StringUtils.isNotBlank(personId)) {
numberView.setText(String.format("%s: %s", getString(R.string.label_zeir), personId));
} else {
numberView.setText("");
}
String genderString = Utils.getValue(personDetails, GrowthMonitoringConstants.GENDER, false);
String baseEntityId = personDetails.entityId();
profilePic.setTag(R.id.entity_id, baseEntityId);
DrishtiApplication.getCachedImageLoaderInstance().getImageByClientId(baseEntityId, OpenSRPImageLoader
.getStaticImageListener(profilePic, ImageUtils.profileImageResourceByGender(genderString),
ImageUtils.profileImageResourceByGender(genderString)));
String formattedAge = "";
String dobString = Utils.getValue(personDetails.getColumnmaps(), GrowthMonitoringConstants.DOB, false);
if (!TextUtils.isEmpty(dobString)) {
DateTime dateTime = new DateTime(dobString);
Date dob = dateTime.toDate();
long timeDiff = Calendar.getInstance().getTimeInMillis() - dob.getTime();
if (timeDiff >= 0) {
formattedAge = DateUtil.getDuration(timeDiff);
}
}
if (StringUtils.isNotBlank(formattedAge)) {
ageView.setText(String.format("%s: %s", getString(R.string.age), formattedAge));
} else {
ageView.setText("");
}
String pmtctStatusString =
Utils.getValue(personDetails.getColumnmaps(), GrowthMonitoringConstants.PMTCT_STATUS, true);
if (!TextUtils.isEmpty(pmtctStatusString)) {
pmtctStatus.setText(pmtctStatusString);
} else {
pmtctStatus.setText("");
}
Gender gender = getGender(genderString);
int genderStringRes = R.string.boys;
if (gender == Gender.FEMALE) {
genderStringRes = R.string.girls;
}
GrowthMonitoringTabsAdapter adapter = new GrowthMonitoringTabsAdapter(getChildFragmentManager());
sortWeights();
adapter.addFragment(String.format(getString(R.string.weight_for_age), getString(genderStringRes).toUpperCase()),
WeightMonitoringFragment.createInstance(dobString, gender, getWeights()));
if (hasProperty && monitorGrowth) {
sortHeights();
adapter.addFragment(String.format(getString(R.string.height_for_age), getString(genderStringRes).toUpperCase()),
HeightMonitoringFragment.createInstance(dobString, gender, getHeights()));
}
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
doneAction();
return dialogView;
}
@NotNull
private ViewGroup setUpViews(LayoutInflater inflater, ViewGroup container) {
final ViewGroup dialogView = (ViewGroup) inflater.inflate(R.layout.growth_dialog_view, container, false);
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
tabLayout = dialogView.findViewById(R.id.growth_tab_layout);
viewPager = dialogView.findViewById(R.id.growth_view_pager);
nameView = dialogView.findViewById(R.id.child_name);
numberView = dialogView.findViewById(R.id.child_zeir_id);
profilePic = dialogView.findViewById(R.id.child_profilepic);
ageView = dialogView.findViewById(R.id.child_age);
pmtctStatus = dialogView.findViewById(R.id.pmtct_status);
done = dialogView.findViewById(R.id.done);
return dialogView;
}
@NotNull
private Gender getGender(String genderString) {
Gender gender = Gender.UNKNOWN;
if (genderString != null && genderString.equalsIgnoreCase(GrowthMonitoringConstants.FEMALE)) {
gender = Gender.FEMALE;
} else if (genderString != null && genderString.equalsIgnoreCase(GrowthMonitoringConstants.MALE)) {
gender = Gender.MALE;
}
return gender;
}
private void doneAction() {
done.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GrowthDialogFragment.this.dismiss();
}
});
}
public List<Weight> getWeights() {
return weights;
}
public void setWeights(List<Weight> weights) {
this.weights = weights;
}
public List<Height> getHeights() {
return heights;
}
public void setHeights(List<Height> heights) {
this.heights = heights;
}
}