• 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

93.42
opensrp-immunization/src/main/java/org/smartregister/immunization/view/VaccineCard.java
1
package org.smartregister.immunization.view;
2

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

14
import androidx.core.content.ContextCompat;
15

16
import org.joda.time.DateTime;
17
import org.smartregister.domain.Alert;
18
import org.smartregister.immunization.ImmunizationLibrary;
19
import org.smartregister.immunization.R;
20
import org.smartregister.immunization.domain.State;
21
import org.smartregister.immunization.domain.VaccineWrapper;
22
import org.smartregister.immunization.util.IMConstants;
23
import org.smartregister.immunization.util.VaccinatorUtils;
24
import org.smartregister.util.DisplayUtils;
25

26
import java.text.SimpleDateFormat;
27
import java.util.Date;
28
import java.util.Locale;
29

30
import timber.log.Timber;
31

32

33
/**
34
 * Created by Jason Rogena - jrogena@ona.io on 21/02/2017.
35
 */
36

37
public class VaccineCard extends LinearLayout {
38
    private static SimpleDateFormat DATE_FORMAT;
39
    private static SimpleDateFormat SHORT_DATE_FORMAT;
40
    private Context context;
41
    private ImageView statusIV;
42
    private TextView nameTV;
43
    private Button undoB;
44
    private State state;
45
    private VaccineWrapper vaccineWrapper;
46
    private boolean isChildActive = true;
1✔
47

48
    public VaccineCard(Context context) {
49
        super(context);
1✔
50
        init(context);
1✔
51
    }
1✔
52

53
    private void init(Context context) {
54
        this.context = context;
1✔
55
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
1✔
56
        layoutInflater.inflate(R.layout.view_vaccination_card, this, true).setFilterTouchesWhenObscured(true);
1✔
57
        statusIV = findViewById(R.id.status_iv);
1✔
58
        nameTV = findViewById(R.id.name_tv);
1✔
59
        undoB = findViewById(R.id.undo_b);
1✔
60

61
        //Init date formatters here to allow for dynamic language switching
62
        DATE_FORMAT = new SimpleDateFormat("dd/MM/yy", Locale.ENGLISH);
1✔
63
        SHORT_DATE_FORMAT = new SimpleDateFormat("dd/MM", Locale.ENGLISH);
1✔
64
    }
1✔
65

66
    public VaccineCard(Context context, AttributeSet attrs) {
67
        super(context, attrs);
1✔
68
        init(context);
1✔
69
    }
1✔
70

71
    public VaccineCard(Context context, AttributeSet attrs, int defStyleAttr) {
72
        super(context, attrs, defStyleAttr);
1✔
73
        init(context);
1✔
74
    }
1✔
75

76
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
77
    public VaccineCard(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
78
        super(context, attrs, defStyleAttr, defStyleRes);
1✔
79
        init(context);
1✔
80
    }
1✔
81

82
    public VaccineWrapper getVaccineWrapper() {
83
        return vaccineWrapper;
1✔
84
    }
85

86
    public void setVaccineWrapper(VaccineWrapper vaccineWrapper) {
87
        this.vaccineWrapper = vaccineWrapper;
1✔
88
        updateState();
1✔
89
    }
1✔
90

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

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

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

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

151
        return null;
1✔
152
    }
153

154
    private boolean isSynced() {
155
        if (vaccineWrapper != null) {
1✔
156
            return vaccineWrapper.isSynced();
1✔
157
        }
158
        return false;
×
159
    }
160

161
    private String getStatus() {
162
        if (vaccineWrapper != null) {
1✔
163
            return vaccineWrapper.getStatus();
1✔
164
        }
165
        return null;
×
166
    }
167

168
    private Alert getAlert() {
169
        if (vaccineWrapper != null) {
1✔
170
            return vaccineWrapper.getAlert();
1✔
171
        }
172
        return null;
×
173
    }
174

175
    private void updateStateUi() {
176
        switch (state) {
1✔
177
            case NOT_DUE:
178
                setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
179
                statusIV.setVisibility(GONE);
1✔
180
                undoB.setVisibility(GONE);
1✔
181
                nameTV.setVisibility(VISIBLE);
1✔
182
                nameTV.setTextColor(context.getResources().getColor(R.color.silver));
1✔
183
                nameTV.setText(getVaccineName());
1✔
184
                break;
1✔
185
            case DUE:
186
                if (ImmunizationLibrary.getInstance().hideOverdueVaccineStatus()) {
1✔
187
                    setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
188
                    nameTV.setTextColor(context.getResources().getColor(R.color.silver));
1✔
189
                } else {
190
                    setBackgroundResource(R.drawable.vaccine_card_background_blue);
1✔
191
                    nameTV.setTextColor(context.getResources().getColor(android.R.color.white));
1✔
192
                }
193
                statusIV.setVisibility(GONE);
1✔
194
                undoB.setVisibility(GONE);
1✔
195
                nameTV.setVisibility(VISIBLE);
1✔
196
                String vaccineName = getVaccineName();
1✔
197
                nameTV.setText(String.format(Locale.getDefault(), context.getString(R.string.record_), vaccineName));
1✔
198
                if (vaccineName.toLowerCase().contains("measles") || vaccineName.toLowerCase().contains("mr")) {
1✔
199
                    nameTV.setText(vaccineName);
1✔
200
                }
201
                break;
202
            case DONE_CAN_BE_UNDONE:
203
                setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
204
                statusIV.setVisibility(VISIBLE);
1✔
205
                undoB.setVisibility(VISIBLE);
1✔
206
                nameTV.setVisibility(VISIBLE);
1✔
207
                nameTV.setTextColor(context.getResources().getColor(R.color.silver));
1✔
208

209
                SimpleDateFormat dateFormatToUse = SHORT_DATE_FORMAT;
1✔
210
                if (DisplayUtils.getScreenSize((Activity) context) > 7.2) {
1✔
211
                    dateFormatToUse = DATE_FORMAT;
×
212
                }
213

214
                nameTV.setText(String.format(Locale.getDefault(), context.getString(R.string.vaccine_done_can_be_undone), getVaccineName(), dateFormatToUse.format(getDateDone())));
1✔
215
                statusIV.setImageDrawable(vaccineWrapper.isOutOfCatchment() ? ContextCompat.getDrawable(context, R.drawable.ic_action_check_orange) : ContextCompat.getDrawable(context, R.drawable.ic_action_check));
1✔
216

217
                break;
1✔
218
            case DONE_CAN_NOT_BE_UNDONE:
219
                setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
220
                statusIV.setVisibility(VISIBLE);
1✔
221
                undoB.setVisibility(GONE);
1✔
222
                nameTV.setVisibility(VISIBLE);
1✔
223
                nameTV.setTextColor(context.getResources().getColor(R.color.silver));
1✔
224
                nameTV.setText(String.format(Locale.getDefault(), context.getString(R.string.vaccine_done_cannot_be_undone), getVaccineName(), DATE_FORMAT.format(getDateDone())));
1✔
225
                statusIV.setImageDrawable(vaccineWrapper.isOutOfCatchment() ? ContextCompat.getDrawable(context, R.drawable.ic_action_check_orange) : ContextCompat.getDrawable(context, R.drawable.ic_action_check));
1✔
226

227
                break;
1✔
228
            case OVERDUE:
229
                if (ImmunizationLibrary.getInstance().hideOverdueVaccineStatus()) {
1✔
230
                    setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
231
                    nameTV.setTextColor(context.getResources().getColor(R.color.silver));
1✔
232
                } else {
233
                    setBackgroundResource(R.drawable.vaccine_card_background_red);
1✔
234
                    nameTV.setTextColor(context.getResources().getColor(android.R.color.white));
1✔
235
                }
236
                statusIV.setVisibility(GONE);
1✔
237
                undoB.setVisibility(GONE);
1✔
238
                nameTV.setVisibility(VISIBLE);
1✔
239
                String vName = getVaccineName();
1✔
240
                nameTV.setText(String.format(Locale.getDefault(), context.getString(R.string.record_due_), vName, DATE_FORMAT.format(getDateDue())));
1✔
241
                if (vName.toLowerCase().contains("measles") || vName.toLowerCase().contains("mr")) {
1✔
242
                    nameTV.setText(String.format(Locale.getDefault(), context.getString(R.string.mr_due_), vName, DATE_FORMAT.format(getDateDue())));
1✔
243
                }
244
                break;
245
            case EXPIRED:
246
                if (ImmunizationLibrary.getInstance().isAllowExpiredVaccineEntry() && ImmunizationLibrary.getInstance().isExpiredVaccineCardRed()) {
1✔
247
                    setBackgroundResource(R.drawable.vaccine_card_background_red);
×
248
                    nameTV.setTextColor(context.getResources().getColor(android.R.color.white));
×
249
                } else {
250
                    setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
251
                    nameTV.setTextColor(context.getResources().getColor(R.color.silver));
1✔
252
                }
253

254
                statusIV.setVisibility(GONE);
1✔
255
                undoB.setVisibility(GONE);
1✔
256
                nameTV.setVisibility(VISIBLE);
1✔
257
                nameTV.setText(String.format(Locale.getDefault(), context.getString(R.string.expired_colon), getVaccineName()));
1✔
258
                break;
1✔
259
            default:
260
                break;
261
        }
262
    }
1✔
263

264
    public void updateChildsActiveStatus() {
265
        if (isChildActive) {
1✔
266
            setBackgroundAlpha(IMConstants.ACTIVE_WIDGET_ALPHA);
1✔
267
        } else {
268
            setBackgroundAlpha(IMConstants.INACTIVE_WIDGET_ALPHA);
×
269
        }
270
    }
1✔
271

272
    private String getVaccineName() {
273
        if (vaccineWrapper != null) {
1✔
274
            String name = vaccineWrapper.getName();
1✔
275

276
            try {
277
                name = VaccinatorUtils.getTranslatedVaccineName(context, name);
1✔
278
            } catch (Exception e) {
1✔
279
                Timber.e(e);
1✔
280
            }
1✔
281

282
            return name;
1✔
283
        }
284
        return null;
×
285
    }
286

287
    private Date getDateDue() {
288
        if (vaccineWrapper != null) {
1✔
289
            DateTime vaccineDate = vaccineWrapper.getVaccineDate();
1✔
290
            if (vaccineDate != null) return vaccineDate.toDate();
1✔
291
        }
292
        return null;
×
293
    }
294

295
    private void setBackgroundAlpha(int alpha) {
296
        getBackground().setAlpha(alpha);
1✔
297
    }
1✔
298

299
    public void setChildActive(boolean childActive) {
300
        isChildActive = childActive;
1✔
301
    }
1✔
302

303
    public State getState() {
304
        if (state == null) {
1✔
305
            updateState();
×
306
        }
307
        return state;
1✔
308
    }
309

310
    public void setState(State state) {
311
        this.state = state;
1✔
312
    }
1✔
313

314
    public Button getUndoB() {
315
        return undoB;
1✔
316
    }
317
}
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