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

OpenSRP / opensrp-client-immunization / #898

pending completion
#898

push

github-actions

web-flow
Merge pull request #195 from opensrp/check-vaccine-duplicates

Add Duplicate Vaccines & Recurring Service Record Checks

5076 of 6673 relevant lines covered (76.07%)

0.76 hits per line

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

86.39
opensrp-immunization/src/main/java/org/smartregister/immunization/view/ServiceRowCard.java
1
package org.smartregister.immunization.view;
2

3
import android.annotation.TargetApi;
4
import android.content.Context;
5
import android.os.Build;
6
import android.util.AttributeSet;
7
import android.view.LayoutInflater;
8
import android.widget.Button;
9
import android.widget.LinearLayout;
10
import android.widget.TextView;
11

12
import org.joda.time.DateTime;
13
import org.smartregister.domain.Alert;
14
import org.smartregister.immunization.ImmunizationLibrary;
15
import org.smartregister.immunization.R;
16
import org.smartregister.immunization.domain.ServiceWrapper;
17
import org.smartregister.immunization.domain.State;
18
import org.smartregister.immunization.util.VaccinateActionUtils;
19
import org.smartregister.immunization.util.VaccinatorUtils;
20

21
import java.text.SimpleDateFormat;
22
import java.util.Date;
23
import java.util.Locale;
24

25
import timber.log.Timber;
26

27
/**
28
 * Created by raihan on 13/03/2017.
29
 */
30

31
public class ServiceRowCard extends LinearLayout {
32
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
1✔
33
    private Context context;
34
    private Button statusIV;
35
    private TextView nameTV;
36
    private TextView StatusTV;
37
    private Button undoB;
38
    private State state;
39
    private ServiceWrapper serviceWrapper;
40
    private boolean editmode;
41
    private boolean statusForMoreThanThreeMonths = false;
1✔
42

43
    public ServiceRowCard(Context context, boolean editmode) {
44
        super(context);
1✔
45
        this.editmode = editmode;
1✔
46
        init(context);
1✔
47
    }
1✔
48

49
    private void init(Context context) {
50
        this.context = context;
1✔
51
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
1✔
52
        layoutInflater.inflate(R.layout.view_immunization_row_card, this, true).setFilterTouchesWhenObscured(true);
1✔
53
        statusIV = findViewById(R.id.status_iv);
1✔
54
        // statusIV.setBackground(ContextCompat.getDrawable(context, R.drawable.ic_action_check_orange));
55
        StatusTV = findViewById(R.id.status_text_tv);
1✔
56
        nameTV = findViewById(R.id.name_tv);
1✔
57
        undoB = findViewById(R.id.undo_b);
1✔
58
    }
1✔
59

60
    public ServiceRowCard(Context context) {
61
        super(context);
1✔
62
        init(context);
1✔
63
    }
1✔
64

65
    public ServiceRowCard(Context context, AttributeSet attrs) {
66
        super(context, attrs);
×
67
        init(context);
×
68
    }
×
69

70
    public ServiceRowCard(Context context, AttributeSet attrs, int defStyleAttr) {
71
        super(context, attrs, defStyleAttr);
×
72
        init(context);
×
73
    }
×
74

75
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
76
    public ServiceRowCard(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
77
        super(context, attrs, defStyleAttr, defStyleRes);
×
78
        init(context);
×
79
    }
×
80

81
    public ServiceWrapper getServiceWrapper() {
82
        return serviceWrapper;
1✔
83
    }
84

85
    public void setServiceWrapper(ServiceWrapper serviceWrapper) {
86
        this.serviceWrapper = serviceWrapper;
1✔
87
        updateState();
1✔
88
    }
1✔
89

90
    public void updateState() {
91
        state = State.NOT_DUE;
1✔
92
        if (serviceWrapper != null) {
1✔
93
            Date dateDone = getDateDone();
1✔
94
            boolean isSynced = isSynced();
1✔
95
            String status = getStatus();
1✔
96

97
            if (dateDone != null) {// Vaccination was done
1✔
98
                if (isSynced) {
1✔
99
                    state = State.DONE_CAN_NOT_BE_UNDONE;
1✔
100
                } else {
101
                    state = State.DONE_CAN_BE_UNDONE;
1✔
102
                }
103
            } else {// Vaccination has not been done
104
                if (status != null) {
1✔
105
                    if (status.equalsIgnoreCase("due")) {
1✔
106
                        Alert alert = getAlert();
1✔
107
                        if (alert == null) {
1✔
108
                            //state = State.NO_ALERT;
109
                        } else if (alert.status().value().equalsIgnoreCase("normal")) {
1✔
110
                            state = State.DUE;
1✔
111
                        } else if (alert.status().value().equalsIgnoreCase("upcoming")) {
1✔
112
                            //state = State.UPCOMING;
113
                        } else if (alert.status().value().equalsIgnoreCase("urgent")) {
1✔
114
                            state = State.OVERDUE;
1✔
115
                        } else if (alert.status().value().equalsIgnoreCase("expired")) {
1✔
116
                            state = State.EXPIRED;
1✔
117
                        }
118
                    } else if ("expired".equalsIgnoreCase(getStatus())) {
1✔
119
                        state = State.EXPIRED;
1✔
120
                    }
121
                }
122

123
                //                Calendar today = Calendar.getInstance();
124
                //                today.set(Calendar.HOUR_OF_DAY, 0);
125
                //                today.set(Calendar.MINUTE, 0);
126
                //                today.set(Calendar.SECOND, 0);
127
                //                today.set(Calendar.MILLISECOND, 0);
128
                //                if (getDateDue().getTime() > (today.getTimeInMillis() + TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS))) {
129
                //                    // Vaccination due more than one day from today
130
                //                    this.state = State.NOT_DUE;
131
                //                } else if (getDateDue().getTime() < (today.getTimeInMillis() - TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS))) {
132
                //                    // Vaccination overdue
133
                //                    this.state = State.OVERDUE;
134
                //                } else {
135
                //                    this.state = State.DUE;
136
                //                }
137
            }
138
            updateStateUi();
1✔
139
        }
140

141
    }
1✔
142

143
    private Date getDateDone() {
144
        if (serviceWrapper != null) {
1✔
145
            DateTime dateDone = serviceWrapper.getUpdatedVaccineDate();
1✔
146
            if (dateDone != null) return dateDone.toDate();
1✔
147
        }
148

149
        return null;
1✔
150
    }
151

152
    private boolean isSynced() {
153
        if (serviceWrapper != null) {
1✔
154
            return serviceWrapper.isSynced();
1✔
155
        }
156
        return false;
×
157
    }
158

159
    private String getStatus() {
160
        if (serviceWrapper != null) {
1✔
161
            return serviceWrapper.getStatus();
1✔
162
        }
163
        return null;
×
164
    }
165

166
    private Alert getAlert() {
167
        if (serviceWrapper != null) {
1✔
168
            return serviceWrapper.getAlert();
1✔
169
        }
170
        return null;
×
171
    }
172

173
    private void updateStateUi() {
174
        if (getDbKey() != null) {
1✔
175
            statusForMoreThanThreeMonths = VaccinateActionUtils.moreThanThreeMonths(getCreatedAt());
1✔
176
        }
177

178
        statusIV.setVisibility(VISIBLE);
1✔
179
        switch (state) {
1✔
180
            case NOT_DUE:
181
                setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
182
                statusIV.setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
183
                undoB.setVisibility(INVISIBLE);
1✔
184
                nameTV.setVisibility(VISIBLE);
1✔
185
                nameTV.setTextColor(context.getResources().getColor(R.color.silver));
1✔
186
                nameTV.setText(getVaccineName());
1✔
187
                StatusTV.setText(getDateDue() == null ? "" : DATE_FORMAT.format(getDateDue()));
1✔
188
                break;
1✔
189
            case DUE:
190
                setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
191
                if (ImmunizationLibrary.getInstance().hideOverdueVaccineStatus()) {
1✔
192
                    statusIV.setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
193
                } else {
194
                    statusIV.setBackgroundResource(R.drawable.vaccine_card_background_blue);
1✔
195
                }
196
                undoB.setVisibility(INVISIBLE);
1✔
197
                nameTV.setVisibility(VISIBLE);
1✔
198
                nameTV.setText(getVaccineName());
1✔
199
                StatusTV.setText(getDateDue() == null ? "" : DATE_FORMAT.format(getDateDue()));
1✔
200
                break;
1✔
201
            case DONE_CAN_BE_UNDONE:
202
                setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
203
                statusIV.setBackgroundResource(R.drawable.vaccine_card_background_green);
1✔
204
                if (editmode && !statusForMoreThanThreeMonths) {
1✔
205
                    undoB.setVisibility(VISIBLE);
×
206
                } else {
207
                    undoB.setVisibility(INVISIBLE);
1✔
208
                }
209
                nameTV.setVisibility(VISIBLE);
1✔
210
                nameTV.setText(getVaccineName());
1✔
211
                StatusTV.setText(DATE_FORMAT.format(getDateDone()));
1✔
212
                break;
1✔
213
            case DONE_CAN_NOT_BE_UNDONE:
214
                setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
215
                statusIV.setBackgroundResource(R.drawable.vaccine_card_background_green);
1✔
216
                if (editmode && !statusForMoreThanThreeMonths) {
1✔
217
                    undoB.setVisibility(VISIBLE);
×
218
                } else {
219
                    undoB.setVisibility(INVISIBLE);
1✔
220
                }
221
                nameTV.setVisibility(VISIBLE);
1✔
222
                nameTV.setText(getVaccineName());
1✔
223
                StatusTV.setText(DATE_FORMAT.format(getDateDone()));
1✔
224
                break;
1✔
225
            case OVERDUE:
226
                setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
227
                if (ImmunizationLibrary.getInstance().hideOverdueVaccineStatus()) {
1✔
228
                    statusIV.setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
229
                } else {
230
                    statusIV.setBackgroundResource(R.drawable.vaccine_card_background_red);
1✔
231
                }
232
                undoB.setVisibility(INVISIBLE);
1✔
233
                nameTV.setVisibility(VISIBLE);
1✔
234
                nameTV.setText(getVaccineName());
1✔
235
                StatusTV.setText(getDateDue() == null ? "" : DATE_FORMAT.format(getDateDue()));
1✔
236
                break;
1✔
237
            case EXPIRED:
238
                setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
239
                statusIV.setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
240
                undoB.setVisibility(INVISIBLE);
1✔
241
                nameTV.setText(getVaccineName());
1✔
242
                StatusTV.setText(context.getResources().getString(R.string.expired));
1✔
243
                StatusTV.setTextColor(context.getResources().getColor(R.color.silver));
1✔
244
                break;
1✔
245
            default:
246
                break;
247
        }
248
    }
1✔
249

250
    private Long getDbKey() {
251
        if (serviceWrapper != null) {
1✔
252
            return serviceWrapper.getDbKey();
1✔
253
        }
254
        return null;
×
255
    }
256

257
    private Date getCreatedAt() {
258
        if (serviceWrapper != null) {
1✔
259
            return serviceWrapper.getCreatedAt();
1✔
260
        }
261
        return null;
×
262
    }
263

264
    private String getVaccineName() {
265
        String name = serviceWrapper.getName();
1✔
266

267
        try {
268
            name = VaccinatorUtils.getTranslatedVaccineName(context, name);
1✔
269
        } catch (Exception e) {
×
270
            Timber.e(e);
×
271
        }
1✔
272

273
        return name;
1✔
274
    }
275

276
    private Date getDateDue() {
277
        if (serviceWrapper != null) {
1✔
278
            DateTime vaccineDate = serviceWrapper.getVaccineDate();
1✔
279
            if (vaccineDate != null) return vaccineDate.toDate();
1✔
280
        }
281
        return null;
1✔
282
    }
283

284
    public State getState() {
285
        if (state == null) {
1✔
286
            updateState();
×
287
        }
288
        return state;
1✔
289
    }
290

291
    public void setState(State state) {
292
        this.state = state;
1✔
293
    }
1✔
294

295
    public Button getUndoB() {
296
        return undoB;
×
297
    }
298

299
    public boolean isEditmode() {
300
        return editmode;
1✔
301
    }
302

303
    public boolean isStatusForMoreThanThreeMonths() {
304
        return statusForMoreThanThreeMonths;
1✔
305
    }
306
}
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