• 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

91.5
opensrp-immunization/src/main/java/org/smartregister/immunization/view/ImmunizationRowCard.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 androidx.core.content.ContextCompat;
13

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

23
import java.text.SimpleDateFormat;
24
import java.util.Date;
25
import java.util.Locale;
26

27
import timber.log.Timber;
28

29
/**
30
 * Created by raihan on 13/03/2017.
31
 */
32

33
public class ImmunizationRowCard extends LinearLayout {
34
    private static SimpleDateFormat DATE_FORMAT;
35
    private Context context;
36
    private Button statusIV;
37
    private TextView nameTV;
38
    private TextView StatusTV;
39
    private Button undoB;
40
    private State state;
41
    private VaccineWrapper vaccineWrapper;
42
    private boolean editmode;
43
    private boolean statusForMoreThanThreeMonths = false;
1✔
44

45
    public ImmunizationRowCard(Context context, boolean editmode) {
46
        super(context);
1✔
47
        this.editmode = editmode;
1✔
48
        init(context);
1✔
49
    }
1✔
50

51
    private void init(Context context) {
52
        this.context = context;
1✔
53
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
1✔
54
        layoutInflater.inflate(R.layout.view_immunization_row_card, this, true).setFilterTouchesWhenObscured(true);
1✔
55
        statusIV = findViewById(R.id.status_iv);
1✔
56
        StatusTV = findViewById(R.id.status_text_tv);
1✔
57
        nameTV = findViewById(R.id.name_tv);
1✔
58
        undoB = findViewById(R.id.undo_b);
1✔
59
        DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
1✔
60
    }
1✔
61

62
    public ImmunizationRowCard(Context context) {
63
        super(context);
1✔
64
        init(context);
1✔
65
    }
1✔
66

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

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

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

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

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

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

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

126
    }
1✔
127

128
    private Date getDateDone() {
129
        if (vaccineWrapper != null) {
1✔
130
            DateTime dateDone = vaccineWrapper.getUpdatedVaccineDate();
1✔
131
            if (dateDone != null) return dateDone.toDate();
1✔
132
        }
133

134
        return null;
1✔
135
    }
136

137
    private boolean isSynced() {
138
        if (vaccineWrapper != null) {
1✔
139
            return vaccineWrapper.isSynced();
1✔
140
        }
141
        return false;
×
142
    }
143

144
    private String getStatus() {
145
        if (vaccineWrapper != null) {
1✔
146
            return vaccineWrapper.getStatus();
1✔
147
        }
148
        return null;
×
149
    }
150

151
    private Alert getAlert() {
152
        if (vaccineWrapper != null) {
1✔
153
            return vaccineWrapper.getAlert();
1✔
154
        }
155
        return null;
×
156
    }
157

158
    private void updateStateUi() {
159
        if (getDbKey() != null) {
1✔
160
            statusForMoreThanThreeMonths = VaccinateActionUtils.moreThanThreeMonths(getCreatedAt());
1✔
161
        }
162

163
        statusIV.setVisibility(VISIBLE);
1✔
164
        switch (state) {
1✔
165
            case NOT_DUE:
166
                setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
167
                statusIV.setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
168
                undoB.setVisibility(INVISIBLE);
1✔
169
                nameTV.setVisibility(VISIBLE);
1✔
170
                nameTV.setTextColor(context.getResources().getColor(R.color.silver));
1✔
171
                nameTV.setText(getVaccineName());
1✔
172
                StatusTV.setText(DATE_FORMAT.format(getDateDue()));
1✔
173
                break;
1✔
174

175
            case DUE:
176
                setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
177
                if (ImmunizationLibrary.getInstance().hideOverdueVaccineStatus()) {
1✔
178
                    statusIV.setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
179
                } else {
180
                    statusIV.setBackgroundResource(R.drawable.vaccine_card_background_blue);
1✔
181
                }
182
                undoB.setVisibility(INVISIBLE);
1✔
183
                nameTV.setVisibility(VISIBLE);
1✔
184
                nameTV.setText(getVaccineName());
1✔
185
                StatusTV.setText(DATE_FORMAT.format(getDateDue()));
1✔
186
                break;
1✔
187

188
            case DONE_CAN_BE_UNDONE:
189
                setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
190
                statusIV.setBackgroundResource(R.drawable.vaccine_card_background_green);
1✔
191
                if (editmode && !statusForMoreThanThreeMonths && !vaccineWrapper.isSynced()) {
1✔
192
                    undoB.setVisibility(VISIBLE);
×
193
                } else {
194
                    undoB.setVisibility(INVISIBLE);
1✔
195
                }
196
                nameTV.setVisibility(VISIBLE);
1✔
197
                nameTV.setText(getVaccineName());
1✔
198
                StatusTV.setText(DATE_FORMAT.format(getDateDone()));
1✔
199
                statusIV.setBackground(vaccineWrapper.isOutOfCatchment() ? ContextCompat.getDrawable(context, R.drawable.ic_action_check_orange) : ContextCompat.getDrawable(context, R.drawable.ic_action_check));
1✔
200

201
                break;
1✔
202

203
            case DONE_CAN_NOT_BE_UNDONE:
204
                setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
205
                statusIV.setBackgroundResource(R.drawable.vaccine_card_background_green);
1✔
206
                if (editmode && !statusForMoreThanThreeMonths && !vaccineWrapper.isSynced()) {
1✔
207
                    undoB.setVisibility(VISIBLE);
×
208
                } else {
209
                    undoB.setVisibility(INVISIBLE);
1✔
210
                }
211
                nameTV.setVisibility(VISIBLE);
1✔
212
                nameTV.setText(getVaccineName());
1✔
213
                StatusTV.setText(DATE_FORMAT.format(getDateDone()));
1✔
214
                statusIV.setBackground(vaccineWrapper.isOutOfCatchment() ? ContextCompat.getDrawable(context, R.drawable.ic_action_check_orange) : ContextCompat.getDrawable(context, R.drawable.ic_action_check));
1✔
215

216
                break;
1✔
217

218
            case OVERDUE:
219
                setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
220
                if (ImmunizationLibrary.getInstance().hideOverdueVaccineStatus()) {
1✔
221
                    statusIV.setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
222
                } else {
223
                    statusIV.setBackgroundResource(R.drawable.vaccine_card_background_red);
1✔
224
                }
225
                undoB.setVisibility(INVISIBLE);
1✔
226
                nameTV.setVisibility(VISIBLE);
1✔
227
                nameTV.setText(getVaccineName());
1✔
228
                StatusTV.setText(DATE_FORMAT.format(getDateDue()));
1✔
229
                break;
1✔
230

231
            case EXPIRED:
232
                if (ImmunizationLibrary.getInstance().isAllowExpiredVaccineEntry() && ImmunizationLibrary.getInstance().isExpiredVaccineCardRed()) {
1✔
233
                    statusIV.setBackgroundResource(R.drawable.vaccine_card_background_red);
×
234
                    StatusTV.setTextColor(context.getResources().getColor(R.color.white));
×
235
                } else {
236
                    statusIV.setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
237
                    StatusTV.setTextColor(context.getResources().getColor(R.color.silver));
1✔
238
                }
239

240
                setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
241
                undoB.setVisibility(INVISIBLE);
1✔
242
                nameTV.setText(getVaccineName());
1✔
243
                StatusTV.setText(context.getResources().getString(R.string.expired));
1✔
244
                break;
1✔
245

246
            default:
247
                break;
248
        }
249
    }
1✔
250

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

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

265
    private String getVaccineName() {
266
        if (vaccineWrapper != null) {
1✔
267
            String name = vaccineWrapper.getName();
1✔
268

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

275
            return name;
1✔
276
        }
277
        return null;
×
278
    }
279

280
    private Date getDateDue() {
281
        if (vaccineWrapper != null) {
1✔
282
            DateTime vaccineDate = vaccineWrapper.getVaccineDate();
1✔
283
            if (vaccineDate != null) return vaccineDate.toDate();
1✔
284
        }
285
        return null;
×
286
    }
287

288
    public State getState() {
289
        if (state == null) {
1✔
290
            updateState();
×
291
        }
292
        return state;
1✔
293
    }
294

295
    public void setState(State state) {
296
        this.state = state;
1✔
297
    }
1✔
298

299
    public Button getUndoB() {
300
        return undoB;
×
301
    }
302

303
    public boolean isEditmode() {
304
        return editmode;
1✔
305
    }
306

307
    public boolean isStatusForMoreThanThreeMonths() {
308
        return statusForMoreThanThreeMonths;
1✔
309
    }
310
}
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