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

OpenSRP / opensrp-client-immunization / #909

pending completion
#909

push

github-actions

web-flow
Merge pull request #194 from opensrp/remove-asyncTask

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

82.24
opensrp-immunization/src/main/java/org/smartregister/immunization/adapter/ServiceRowAdapter.java
1
package org.smartregister.immunization.adapter;
2

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

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

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

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

38
import timber.log.Timber;
39

40
import timber.log.Timber;
41

42
/**
43
 * Created by keyman on 15/05/2017.
44
 */
45
public class ServiceRowAdapter extends BaseAdapter {
46
    private static final String TAG = "ServiceRowAdapter";
47
    private final Context context;
48
    private final ServiceRowGroup serviceRowGroup;
49
    public boolean editmode;
50
    private HashMap<String, ServiceRowCard> serviceRowCards;
51
    private List<ServiceType> serviceTypeList;
52
    private List<ServiceRecord> serviceRecordList;
53
    private List<Alert> alertList;
54

55
    public ServiceRowAdapter(Context context, ServiceRowGroup serviceRowGroup, boolean editmode,
56
                             List<ServiceType> serviceTypeList, List<ServiceRecord> serviceRecordList,
57
                             List<Alert> alertList) {
1✔
58
        this.context = context;
1✔
59
        this.editmode = editmode;
1✔
60
        this.serviceRowGroup = serviceRowGroup;
1✔
61
        this.serviceTypeList = serviceTypeList;
1✔
62
        this.serviceRecordList = serviceRecordList;
1✔
63
        this.alertList = alertList;
1✔
64
        serviceRowCards = new LinkedHashMap<>();
1✔
65
    }
1✔
66

67
    @Override
68
    public int getCount() {
69
        List<ServiceType> types = serviceRowGroup.getServiceTypes();
1✔
70
        if (types == null || types.isEmpty()) {
1✔
71
            return 0;
×
72
        }
73
        return types.size();
1✔
74
    }
75

76
    @Override
77
    public Object getItem(int position) {
78
        return serviceRowCards.get(position);
1✔
79
    }
80

81
    @Override
82
    public long getItemId(int position) {
83
        return 231231 + position;
1✔
84
    }
85

86
    @Override
87
    public View getView(int position, View convertView, ViewGroup parent) {
88
        try {
89
            ServiceType serviceType = serviceRowGroup.getServiceTypes().get(position);
1✔
90
            if (!serviceRowCards.containsKey(serviceType.getName())) {
1✔
91
                ServiceRowCard serviceRowCard = new ServiceRowCard(context, editmode);
1✔
92
                serviceRowCard.setId((int) getItemId(position));
1✔
93
                serviceRowCards.put(serviceType.getName(), serviceRowCard);
1✔
94

95
                ServiceRowTaskCallable callable = new ServiceRowTaskCallable(serviceRowGroup.getChildDetails(),
1✔
96
                        serviceType);
97
                ServiceRowTaskCallableInteractorCallBack callableInteractorCallBack
1✔
98
                        = getServiceRowTaskCallableInteractor(serviceRowCard);
1✔
99
                GenericInteractor interactor = getGenericInteractor();
1✔
100

101
                interactor.execute(callable, callableInteractorCallBack);
1✔
102
            }
103

104
            return serviceRowCards.get(serviceType.getName());
1✔
105
        } catch (Exception e) {
×
106
            Timber.e(e);
×
107
            return null;
×
108
        }
109
    }
110

111
    public ServiceRowTaskCallableInteractorCallBack getServiceRowTaskCallableInteractor(ServiceRowCard serviceRowCard) {
112
        return  new ServiceRowTaskCallableInteractorCallBack(serviceRowCard);
1✔
113
    }
114

115
    public void update(ArrayList<ServiceWrapper> servicesToUpdate) {
116
        if (serviceRowCards != null) {
1✔
117
            if (servicesToUpdate == null) {// Update all vaccines
1✔
118
                for (ServiceRowCard curCard : serviceRowCards.values()) {
1✔
119
                    if (curCard != null) curCard.updateState();
×
120
                }
1✔
121
            } else {// Update just the vaccines specified
122
                for (ServiceWrapper currWrapper : servicesToUpdate) {
×
123
                    if (serviceRowCards.containsKey(currWrapper.getName())) {
×
124
                        serviceRowCards.get(currWrapper.getName()).updateState();
×
125
                    }
126
                }
×
127
            }
128
        }
129
    }
1✔
130

131
    public void updateWrapperStatus(ServiceWrapper tag, CommonPersonObjectClient childDetails) {
132

133
        List<ServiceType> serviceTypes = getServiceTypes();
1✔
134

135
        List<ServiceRecord> serviceRecordList = getServiceRecordList();
1✔
136

137
        List<Alert> alertList = getAlertList();
1✔
138

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

141
        String dobString = getValue(childDetails.getColumnmaps(), "dob", false);
1✔
142
        List<Map<String, Object>> sch = generateScheduleList(serviceTypes, new DateTime(dobString), receivedServices,
1✔
143
                alertList);
144

145

146
        for (Map<String, Object> m : sch) {
1✔
147
            ServiceType serviceType = (ServiceType) m.get("service");
1✔
148
            if (tag.getName().equalsIgnoreCase(serviceType.getName())) {
1✔
149
                tag.setStatus(m.get("status").toString());
×
150
                tag.setAlert((Alert) m.get("alert"));
×
151
                tag.setServiceType(serviceType);
×
152
                tag.setVaccineDate((DateTime) m.get("date"));
×
153
            }
154
        }
1✔
155
    }
1✔
156

157
    public List<ServiceType> getServiceTypes() {
158
        return serviceTypeList;
1✔
159
    }
160

161
    public List<ServiceRecord> getServiceRecordList() {
162
        return serviceRecordList;
1✔
163
    }
164

165
    public List<Alert> getAlertList() {
166
        return alertList;
1✔
167
    }
168

169
    public void updateWrapper(ServiceWrapper tag) {
170
        List<ServiceRecord> serviceRecordList = getServiceRecordList();
1✔
171

172
        if (!serviceRecordList.isEmpty()) {
1✔
173
            for (ServiceRecord serviceRecord : serviceRecordList) {
1✔
174
                if (tag.getName().equalsIgnoreCase(serviceRecord.getName()) && serviceRecord
1✔
175
                        .getDate() != null) {
1✔
176
                    long diff = serviceRecord.getUpdatedAt() - serviceRecord.getDate().getTime();
1✔
177
                    if (diff > 0 && TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS) > 1) {
1✔
178
                        tag.setUpdatedVaccineDate(new DateTime(serviceRecord.getDate()), false);
×
179
                    } else {
180
                        tag.setUpdatedVaccineDate(new DateTime(serviceRecord.getDate()), true);
1✔
181
                    }
182
                    tag.setDbKey(serviceRecord.getId());
1✔
183
                    tag.setSynced(serviceRecord.getSyncStatus() != null && serviceRecord.getSyncStatus()
1✔
184
                            .equals(VaccineRepository.TYPE_Synced));
1✔
185
                    tag.setCreatedAt(serviceRecord.getCreatedAt());
1✔
186
                }
187
            }
1✔
188
        }
189
    }
1✔
190

191
    public GenericInteractor getGenericInteractor(){
192
        return new GenericInteractor();
1✔
193
    }
194

195
    class ServiceRowTaskCallable implements Callable<ServiceWrapper> {
196

197
        private CommonPersonObjectClient childDetails;
198

199
        private ServiceType serviceType;
200

201
        ServiceRowTaskCallable(CommonPersonObjectClient childDetails, ServiceType serviceType){
1✔
202
            this.childDetails = childDetails;
1✔
203
            this.serviceType = serviceType;
1✔
204
        }
1✔
205

206
        @Override
207
        public ServiceWrapper call() throws Exception {
208
            ServiceWrapper serviceWrapper = new ServiceWrapper();
1✔
209
            serviceWrapper.setId(childDetails.entityId());
1✔
210
            serviceWrapper.setGender(childDetails.getDetails().get("gender"));
1✔
211
            serviceWrapper.setDefaultName(serviceType.getName());
1✔
212

213
            String dobString = getValue(childDetails.getColumnmaps(), "dob", false);
1✔
214
            if (StringUtils.isNotBlank(dobString)) {
1✔
215
                Calendar dobCalender = Calendar.getInstance();
1✔
216
                DateTime dateTime = new DateTime(dobString);
1✔
217
                dobCalender.setTime(dateTime.toDate());
1✔
218
                serviceWrapper.setDob(new DateTime(dobCalender.getTime()));
1✔
219
            }
220

221
            Photo photo = ImageUtils.profilePhotoByClient(childDetails);
1✔
222
            serviceWrapper.setPhoto(photo);
1✔
223

224
            String zeirId = getValue(childDetails.getColumnmaps(), "zeir_id", false);
1✔
225
            serviceWrapper.setPatientNumber(zeirId);
1✔
226

227
            String firstName = getValue(childDetails.getColumnmaps(), "first_name", true);
1✔
228
            String lastName = getValue(childDetails.getColumnmaps(), "last_name", true);
1✔
229
            String childName = getName(firstName, lastName);
1✔
230
            serviceWrapper.setPatientName(childName.trim());
1✔
231

232
            updateWrapperStatus(serviceWrapper, childDetails);
1✔
233
            updateWrapper(serviceWrapper);
1✔
234

235
            return serviceWrapper;
1✔
236
        }
237
    }
238

239
    public class ServiceRowTaskCallableInteractorCallBack implements CallableInteractorCallBack<ServiceWrapper> {
240

241
        private final ServiceRowCard serviceRowCard;
242

243
        ServiceRowTaskCallableInteractorCallBack(ServiceRowCard serviceRowCard){
1✔
244
            this.serviceRowCard = serviceRowCard;
1✔
245
        }
1✔
246
        @Override
247
        public void onResult(ServiceWrapper serviceWrapper) {
248
            serviceRowCard.setServiceWrapper(serviceWrapper);
×
249
            notifyDataSetChanged();
×
250
        }
×
251

252
        @Override
253
        public void onError(Exception ex) {
254
            Timber.e(ex);
×
255
        }
×
256
    }
257

258
}
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