• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OpenSRP / opensrp-client-immunization / #907

pending completion
#907

Pull #194

github-actions

web-flow
Merge e31496ca4 into ce201bbb0
Pull Request #194: Remove async task

5180 of 6703 relevant lines covered (77.28%)

0.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.35
opensrp-immunization/src/main/java/org/smartregister/immunization/adapter/ServiceCardAdapter.java
1
package org.smartregister.immunization.adapter;
2

3
import static org.smartregister.immunization.util.VaccinatorUtils.generateScheduleList;
4
import static org.smartregister.immunization.util.VaccinatorUtils.nextServiceDue;
5
import static org.smartregister.util.Utils.getName;
6
import static org.smartregister.util.Utils.getValue;
7

8
import android.content.Context;
9
import android.view.View;
10
import android.view.ViewGroup;
11
import android.widget.BaseAdapter;
12

13
import org.apache.commons.lang3.StringUtils;
14
import org.joda.time.DateTime;
15
import org.smartregister.commonregistry.CommonPersonObjectClient;
16
import org.smartregister.domain.Alert;
17
import org.smartregister.domain.Photo;
18
import org.smartregister.immunization.domain.ServiceRecord;
19
import org.smartregister.immunization.domain.ServiceType;
20
import org.smartregister.immunization.domain.ServiceWrapper;
21
import org.smartregister.immunization.repository.RecurringServiceRecordRepository;
22
import org.smartregister.immunization.repository.VaccineRepository;
23
import org.smartregister.util.CallableInteractorCallBack;
24
import org.smartregister.util.GenericInteractor;
25
import org.smartregister.immunization.util.ImageUtils;
26
import org.smartregister.immunization.util.VaccinatorUtils;
27
import org.smartregister.immunization.view.ServiceCard;
28
import org.smartregister.immunization.view.ServiceGroup;
29

30
import java.util.ArrayList;
31
import java.util.Calendar;
32
import java.util.Date;
33
import java.util.HashMap;
34
import java.util.LinkedHashMap;
35
import java.util.List;
36
import java.util.Map;
37
import java.util.concurrent.Callable;
38
import java.util.concurrent.TimeUnit;
39

40
import timber.log.Timber;
41

42
import timber.log.Timber;
43

44
/**
45
 * Created by keyman on 15/05/2017.
46
 */
47
public class ServiceCardAdapter extends BaseAdapter {
48
    private static final String TAG = "ServiceCardAdapter";
49
    private final Context context;
50
    private final ServiceGroup serviceGroup;
51
    private HashMap<String, ServiceCard> serviceCards;
52
    private List<ServiceRecord> serviceRecordList;
53
    private List<Alert> alertList;
54
    private Map<String, List<ServiceType>> serviceTypeMap;
55

56
    private boolean isChildActive = true;
1✔
57
    private GenericInteractor mGenericInteractor;
58

59
    public ServiceCardAdapter(Context context, ServiceGroup serviceGroup, List<ServiceRecord> serviceRecordList,
60
                              List<Alert> alertList, Map<String, List<ServiceType>> serviceTypeMap) {
1✔
61
        this.context = context;
1✔
62
        this.serviceGroup = serviceGroup;
1✔
63
        this.serviceRecordList = serviceRecordList;
1✔
64
        this.alertList = alertList;
1✔
65
        this.serviceTypeMap = serviceTypeMap;
1✔
66
        serviceCards = new HashMap<>();
1✔
67
        mGenericInteractor = new GenericInteractor();
1✔
68
    }
1✔
69

70
    public void updateAll() {
71
        if (serviceCards != null) {
1✔
72
            // Update all vaccines
73
            for (ServiceCard curCard : serviceCards.values()) {
1✔
74
                if (curCard != null) curCard.updateState();
1✔
75
            }
1✔
76
        }
77

78
        visibilityCheck();
1✔
79
    }
1✔
80

81
    public void visibilityCheck() {
82
        // if all cards have been updated
83
        if (getCount() == serviceCards.size()) {
1✔
84
            if (atLeastOneVisibleCard()) {
1✔
85
                serviceGroup.post(new Runnable() {
1✔
86
                    @Override
87
                    public void run() {
88
                        serviceGroup.setVisibility(View.VISIBLE);
1✔
89
                    }
1✔
90
                });
91
            } else {
92
                serviceGroup.post(new Runnable() {
×
93
                    @Override
94
                    public void run() {
95
                        serviceGroup.setVisibility(View.VISIBLE);
×
96
                    }
×
97
                });
98
            }
99
        }
100
    }
1✔
101

102
    @Override
103
    public int getCount() {
104
        List<String> types = serviceGroup.getServiceTypeKeys();
1✔
105
        if (types == null || types.isEmpty()) {
1✔
106
            return 0;
×
107
        }
108
        return types.size();
1✔
109
    }
110

111
    @Override
112
    public Object getItem(int position) {
113
        return serviceCards.get(position);
1✔
114
    }
115

116
    @Override
117
    public long getItemId(int position) {
118
        return 231231 + position;
1✔
119
    }
120

121
    @Override
122
    public View getView(int position, View convertView, ViewGroup parent) {
123
        try {
124
            String type = serviceGroup.getServiceTypeKeys().get(position);
1✔
125
            if (!serviceCards.containsKey(type)) {
1✔
126
                ServiceCard serviceCard = new ServiceCard(context);
1✔
127
                serviceCard.setChildActive(isChildActive);
1✔
128
                serviceCard.setId((int) getItemId(position));
1✔
129
                serviceCards.put(type, serviceCard);
1✔
130

131
                ServiceCardTaskCallable callable = new ServiceCardTaskCallable(serviceGroup.getChildDetails(), type);
1✔
132
                ServiceCardTaskCallableInteractorCallable callableInteractorCallaback = getServiceCardTaskCallableInteractorCallable(serviceCard);
1✔
133
                GenericInteractor interactor = getGenericInteractor();
1✔
134

135
                interactor.execute(callable, callableInteractorCallaback);
1✔
136

137
            }
138

139
            return serviceCards.get(type);
1✔
140
        } catch (Exception e) {
×
141
            Timber.e(e);
×
142
            return null;
×
143
        }
144

145
    }
146

147
    public ServiceCardTaskCallableInteractorCallable getServiceCardTaskCallableInteractorCallable(ServiceCard serviceCard) {
148
        return new ServiceCardTaskCallableInteractorCallable(serviceCard);
1✔
149
    }
150

151
    public boolean atLeastOneVisibleCard() {
152
        if (serviceCards != null) {
1✔
153
            for (ServiceCard serviceCard : serviceCards.values()) {
1✔
154
                if (serviceCard.getVisibility() == View.VISIBLE) {
1✔
155
                    return true;
1✔
156
                }
157
            }
×
158
        }
159
        return false;
×
160
    }
161

162
    public void updateChildsActiveStatus() {
163
        if (serviceCards != null) {
1✔
164
            // Update all vaccines
165
            for (ServiceCard curCard : serviceCards.values()) {
1✔
166
                if (curCard != null) {
1✔
167
                    curCard.setChildActive(isChildActive);
1✔
168
                    curCard.updateChildsActiveStatus();
1✔
169
                }
170
            }
1✔
171
        }
172
    }
1✔
173

174
    public void setChildActive(boolean isChildActive) {
175
        this.isChildActive = isChildActive;
1✔
176
    }
1✔
177

178
    public void updateWrapperStatus(ArrayList<ServiceWrapper> tags, CommonPersonObjectClient childDetails) {
179
        if (tags == null) {
1✔
180
            return;
×
181
        }
182

183
        for (ServiceWrapper tag : tags) {
1✔
184
            updateWrapperStatus(tag.getDefaultName(), tag, childDetails);
1✔
185
        }
1✔
186
    }
1✔
187

188
    public void updateWrapperStatus(String type, ServiceWrapper tag, CommonPersonObjectClient childDetails) {
189

190
        List<ServiceType> serviceTypes = getServiceTypeMap().get(type);
1✔
191

192
        List<ServiceRecord> serviceRecordList = new ArrayList<>();
1✔
193
        for (ServiceRecord serviceRecord : getServiceRecordList()) {
1✔
194
            //if (serviceRecord.getRecurringServiceId().equals(tag.getTypeId())) {
195
            //if (serviceRecord.getName().equalsIgnoreCase(tag.getDefaultName())) {
196
            serviceRecordList.add(serviceRecord);
1✔
197
            //}
198
        }
1✔
199

200
        List<Alert> alertList = getAlertList();
1✔
201

202
        Map<String, Date> receivedServices = VaccinatorUtils.receivedServices(serviceRecordList);
1✔
203

204
        String dobString = getValue(childDetails.getColumnmaps(), "dob", false);
1✔
205
        List<Map<String, Object>> sch = generateScheduleList(serviceTypes, new DateTime(dobString), receivedServices,
1✔
206
                alertList);
207

208

209
        Map<String, Object> nv = null;
1✔
210
        if (serviceRecordList.isEmpty()) {
1✔
211
            nv = nextServiceDue(sch, serviceTypes);
1✔
212
        } else {
213
            ServiceRecord lastServiceRecord = null;
1✔
214
            for (ServiceRecord serviceRecord : serviceRecordList) {
1✔
215
                if (serviceRecord.getSyncStatus().equalsIgnoreCase(RecurringServiceRecordRepository.TYPE_Unsynced)) {
1✔
216
                    lastServiceRecord = serviceRecord;
×
217
                }
218
            }
1✔
219

220
            if (lastServiceRecord != null) {
1✔
221
                nv = nextServiceDue(sch, lastServiceRecord);
×
222
            }
223
        }
224

225
        if (nv == null) {
1✔
226
            Date lastVaccine = null;
1✔
227
            if (!serviceRecordList.isEmpty()) {
1✔
228
                ServiceRecord serviceRecord = serviceRecordList.get(serviceRecordList.size() - 1);
1✔
229
                lastVaccine = serviceRecord.getDate();
1✔
230
            }
231

232
            nv = nextServiceDue(sch, lastVaccine);
1✔
233
        }
234

235
        if (nv != null) {
1✔
236
            ServiceType nextServiceType = (ServiceType) nv.get("service");
×
237
            tag.setStatus(nv.get("status").toString());
×
238
            tag.setAlert((Alert) nv.get("alert"));
×
239
            if (nv.get("date") != null && nv.get("date") instanceof DateTime) {
×
240
                tag.setVaccineDate((DateTime) nv.get("date"));
×
241
            }
242
            tag.setServiceType(nextServiceType);
×
243
        }
244
    }
1✔
245

246
    public Map<String, List<ServiceType>> getServiceTypeMap() {
247
        if (serviceTypeMap == null) {
1✔
248
            serviceTypeMap = new LinkedHashMap<>();
×
249
        }
250
        return serviceTypeMap;
1✔
251
    }
252

253
    public List<ServiceRecord> getServiceRecordList() {
254
        if (serviceRecordList == null) {
1✔
255
            serviceRecordList = new ArrayList<>();
×
256
        }
257
        return serviceRecordList;
1✔
258
    }
259

260
    public List<Alert> getAlertList() {
261
        return alertList;
1✔
262
    }
263

264
    public void updateAllWrapperStatus(CommonPersonObjectClient childDetails) {
265

266
        List<ServiceWrapper> tags = allWrappers();
×
267
        if (tags == null) {
×
268
            return;
×
269
        }
270

271
        for (ServiceWrapper tag : tags) {
×
272
            updateWrapperStatus(tag.getDefaultName(), tag, childDetails);
×
273
        }
×
274
    }
×
275

276
    public List<ServiceWrapper> allWrappers() {
277
        List<ServiceWrapper> serviceWrappers = new ArrayList<>();
1✔
278
        if (serviceCards != null) {
1✔
279
            for (ServiceCard serviceCard : serviceCards.values()) {
1✔
280
                serviceWrappers.add(serviceCard.getServiceWrapper());
1✔
281
            }
1✔
282
        }
283
        return serviceWrappers;
1✔
284
    }
285

286
    public void updateWrapper(ServiceWrapper tag) {
287
        List<ServiceRecord> serviceRecordList = getServiceRecordList();
1✔
288

289
        if (!serviceRecordList.isEmpty()) {
1✔
290
            for (ServiceRecord serviceRecord : serviceRecordList) {
1✔
291
                if (tag.getName().toLowerCase().contains(serviceRecord.getName().toLowerCase()) && serviceRecord
1✔
292
                        .getDate() != null) {
1✔
293
                    long diff = serviceRecord.getUpdatedAt() - serviceRecord.getDate().getTime();
1✔
294
                    if (diff > 0 && TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS) > 1) {
1✔
295
                        tag.setUpdatedVaccineDate(new DateTime(serviceRecord.getDate()), false);
×
296
                    } else {
297
                        tag.setUpdatedVaccineDate(new DateTime(serviceRecord.getDate()), true);
1✔
298
                    }
299
                    tag.setDbKey(serviceRecord.getId());
1✔
300
                    tag.setSynced(serviceRecord.getSyncStatus() != null && serviceRecord.getSyncStatus()
1✔
301
                            .equals(VaccineRepository.TYPE_Synced));
1✔
302
                }
303
            }
1✔
304
        }
305

306
    }
1✔
307

308
    public GenericInteractor getGenericInteractor(){
309
        return mGenericInteractor;
1✔
310
    }
311

312
    public class ServiceCardTaskCallable implements Callable<ServiceWrapper> {
313

314
        private CommonPersonObjectClient childDetails;
315

316
        private String type;
317

318
        ServiceCardTaskCallable(CommonPersonObjectClient childDetails, String type) {
1✔
319
            this.childDetails = childDetails;
1✔
320
            this.type = type;
1✔
321
        }
1✔
322

323
        @Override
324
        public ServiceWrapper call() {
325
            ServiceWrapper serviceWrapper = new ServiceWrapper();
1✔
326
            serviceWrapper.setId(childDetails.entityId());
1✔
327
            serviceWrapper.setGender(childDetails.getDetails().get("gender"));
1✔
328
            serviceWrapper.setDefaultName(type);
1✔
329

330
            String dobString = getValue(childDetails.getColumnmaps(), "dob", false);
1✔
331
            if (StringUtils.isNotBlank(dobString)) {
1✔
332
                Calendar dobCalender = Calendar.getInstance();
1✔
333
                DateTime dateTime = new DateTime(dobString);
1✔
334
                dobCalender.setTime(dateTime.toDate());
1✔
335
                serviceWrapper.setDob(new DateTime(dobCalender.getTime()));
1✔
336
            }
337

338
            Photo photo = ImageUtils.profilePhotoByClient(childDetails);
1✔
339
            serviceWrapper.setPhoto(photo);
1✔
340

341
            String zeirId = getValue(childDetails.getColumnmaps(), "zeir_id", false);
1✔
342
            serviceWrapper.setPatientNumber(zeirId);
1✔
343

344
            String firstName = getValue(childDetails.getColumnmaps(), "first_name", true);
1✔
345
            String lastName = getValue(childDetails.getColumnmaps(), "last_name", true);
1✔
346
            String childName = getName(firstName, lastName);
1✔
347
            serviceWrapper.setPatientName(childName.trim());
1✔
348

349
            updateWrapperStatus(type, serviceWrapper, childDetails);
1✔
350
            updateWrapper(serviceWrapper);
1✔
351

352
            return serviceWrapper;
1✔
353
        }
354
    }
355

356
    public class ServiceCardTaskCallableInteractorCallable implements CallableInteractorCallBack<ServiceWrapper>{
357

358
        private ServiceCard serviceCard;
359

360
        public ServiceCardTaskCallableInteractorCallable(ServiceCard serviceCard){
1✔
361
            this.serviceCard = serviceCard;
1✔
362
        }
1✔
363

364
        @Override
365
        public void onResult(ServiceWrapper serviceWrapper) {
366
            serviceCard.setServiceWrapper(serviceWrapper);
1✔
367
            visibilityCheck();
1✔
368
            notifyDataSetChanged();
1✔
369
        }
1✔
370

371
        @Override
372
        public void onError(Exception ex) {
373
            Timber.e(ex);
×
374
        }
×
375
    }
376

377
    public void updateServiceRecordList(List<ServiceRecord> serviceRecordList) {
378
        this.serviceRecordList = serviceRecordList;
×
379
    }
×
380

381
    public void updateAlertList(List<Alert> alertList) {
382
        this.alertList = alertList;
×
383
    }
×
384
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc