• 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.11
opensrp-immunization/src/main/java/org/smartregister/immunization/view/ServiceCard.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 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.ServiceWrapper;
19
import org.smartregister.immunization.domain.State;
20
import org.smartregister.immunization.util.IMConstants;
21
import org.smartregister.immunization.util.VaccinatorUtils;
22
import org.smartregister.util.DisplayUtils;
23

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

28
import timber.log.Timber;
29

30
/**
31
 * Created by keyman on 21/02/2017.
32
 */
33

34
public class ServiceCard extends LinearLayout {
35
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd/MM/yy");
1✔
36
    private static final SimpleDateFormat SHORT_DATE_FORMAT = new SimpleDateFormat("dd/MM");
1✔
37
    private Context context;
38
    private ImageView statusIV;
39
    private TextView nameTV;
40
    private Button undoB;
41
    private State state;
42
    private ServiceWrapper serviceWrapper;
43
    private boolean isChildActive = true;
1✔
44

45
    public ServiceCard(Context context) {
46
        super(context);
1✔
47
        init(context);
1✔
48
    }
1✔
49

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

60
    public ServiceCard(Context context, AttributeSet attrs) {
61
        super(context, attrs);
×
62
        init(context);
×
63
    }
×
64

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

70
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
71
    public ServiceCard(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
72
        super(context, attrs, defStyleAttr, defStyleRes);
×
73
        init(context);
×
74
    }
×
75

76
    public ServiceWrapper getServiceWrapper() {
77
        return serviceWrapper;
1✔
78
    }
79

80
    public void setServiceWrapper(ServiceWrapper serviceWrapper) {
81
        this.serviceWrapper = serviceWrapper;
1✔
82
        updateState();
1✔
83
    }
1✔
84

85
    public void updateState() {
86
        state = State.NOT_DUE;
1✔
87
        if (serviceWrapper != null) {
1✔
88
            Date dateDone = getDateDone();
1✔
89
            boolean isSynced = isSynced();
1✔
90
            String status = getStatus();
1✔
91

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

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

139
    private Date getDateDone() {
140
        if (serviceWrapper != null) {
1✔
141
            DateTime dateDone = serviceWrapper.getUpdatedVaccineDate();
1✔
142
            if (dateDone != null) return dateDone.toDate();
1✔
143
        }
144

145
        return null;
1✔
146
    }
147

148
    private boolean isSynced() {
149
        if (serviceWrapper != null) {
1✔
150
            return serviceWrapper.isSynced();
1✔
151
        }
152
        return false;
×
153
    }
154

155
    private String getStatus() {
156
        if (serviceWrapper != null) {
1✔
157
            return serviceWrapper.getStatus();
1✔
158
        }
159
        return null;
×
160
    }
161

162
    private Alert getAlert() {
163
        if (serviceWrapper != null) {
1✔
164
            return serviceWrapper.getAlert();
1✔
165
        }
166
        return null;
×
167
    }
168

169
    private void updateStateUi() {
170
        setVisibility(VISIBLE);
1✔
171
        switch (state) {
1✔
172
            case NOT_DUE:
173
                setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
174
                statusIV.setVisibility(GONE);
1✔
175
                undoB.setVisibility(GONE);
1✔
176
                nameTV.setVisibility(VISIBLE);
1✔
177
                nameTV.setTextColor(context.getResources().getColor(R.color.silver));
1✔
178
                nameTV.setText(getServiceName());
1✔
179
                setVisibility(VISIBLE);
1✔
180
                break;
1✔
181
            case DUE:
182
                if (ImmunizationLibrary.getInstance().hideOverdueVaccineStatus()) {
1✔
183
                    setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
184
                    nameTV.setTextColor(context.getResources().getColor(R.color.silver));
1✔
185
                } else {
186
                    setBackgroundResource(R.drawable.vaccine_card_background_blue);
1✔
187
                    nameTV.setTextColor(context.getResources().getColor(android.R.color.white));
1✔
188
                }
189
                statusIV.setVisibility(GONE);
1✔
190
                undoB.setVisibility(GONE);
1✔
191
                nameTV.setVisibility(VISIBLE);
1✔
192
                nameTV.setText(String.format(Locale.getDefault(), context.getString(R.string.record_), getServiceName()));
1✔
193
                break;
1✔
194
            case DONE_CAN_BE_UNDONE:
195
                setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
196
                statusIV.setVisibility(VISIBLE);
1✔
197
                undoB.setVisibility(VISIBLE);
1✔
198
                nameTV.setVisibility(VISIBLE);
1✔
199
                nameTV.setTextColor(context.getResources().getColor(R.color.silver));
1✔
200

201
                SimpleDateFormat dateFormatToUse = SHORT_DATE_FORMAT;
1✔
202
                if (DisplayUtils.getScreenSize((Activity) context) > 7.2) {
1✔
203
                    dateFormatToUse = DATE_FORMAT;
×
204
                }
205

206
                nameTV.setText(String.format(Locale.getDefault(), context.getString(R.string.vaccine_done_can_be_undone), getServiceName(), dateFormatToUse.format(getDateDone())));
1✔
207
                break;
1✔
208
            case DONE_CAN_NOT_BE_UNDONE:
209
                setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
210
                statusIV.setVisibility(VISIBLE);
1✔
211
                undoB.setVisibility(GONE);
1✔
212
                nameTV.setVisibility(VISIBLE);
1✔
213
                nameTV.setTextColor(context.getResources().getColor(R.color.silver));
1✔
214
                nameTV.setText(String.format(Locale.getDefault(), context.getString(R.string.vaccine_done_cannot_be_undone), getServiceName(), DATE_FORMAT.format(getDateDone())));
1✔
215
                break;
1✔
216
            case OVERDUE:
217
                if (ImmunizationLibrary.getInstance().hideOverdueVaccineStatus()) {
1✔
218
                    setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
219
                    nameTV.setTextColor(context.getResources().getColor(R.color.silver));
1✔
220
                } else {
221
                    setBackgroundResource(R.drawable.vaccine_card_background_red);
1✔
222
                    nameTV.setTextColor(context.getResources().getColor(android.R.color.white));
1✔
223
                }
224
                statusIV.setVisibility(GONE);
1✔
225
                undoB.setVisibility(GONE);
1✔
226
                nameTV.setVisibility(VISIBLE);
1✔
227
                String serviceName = getServiceName();
1✔
228
                if (getDateDue() != null) {
1✔
229
                    nameTV.setText(String.format(Locale.getDefault(), context.getString(R.string.record_due_), serviceName, DATE_FORMAT.format(getDateDue())));
1✔
230
                } else {
231
                    nameTV.setText(String.format(Locale.getDefault(), context.getString(R.string.record_), serviceName));
×
232
                }
233
                break;
×
234
            case EXPIRED:
235
                setBackgroundResource(R.drawable.vaccine_card_background_white);
1✔
236
                statusIV.setVisibility(GONE);
1✔
237
                undoB.setVisibility(GONE);
1✔
238
                nameTV.setVisibility(VISIBLE);
1✔
239
                nameTV.setTextColor(context.getResources().getColor(R.color.silver));
1✔
240
                nameTV.setText(context.getResources().getString(R.string.expired_colon, getServiceName()));
1✔
241
                break;
1✔
242
            default:
243
                break;
244
        }
245
    }
1✔
246

247
    public void updateChildsActiveStatus() {
248
        if (isChildActive) {
1✔
249
            getBackground().setAlpha(IMConstants.ACTIVE_WIDGET_ALPHA);
1✔
250
        } else {
251
            getBackground().setAlpha(IMConstants.INACTIVE_WIDGET_ALPHA);
×
252
        }
253
    }
1✔
254

255
    private String getServiceName() {
256
        String name = serviceWrapper.getName();
1✔
257

258
        try {
259
            name = VaccinatorUtils.getTranslatedVaccineName(context, name);
1✔
260
        } catch (Exception e) {
×
261
            Timber.e(e);
×
262
        }
1✔
263

264
        return name;
1✔
265
    }
266

267
    private Date getDateDue() {
268
        if (serviceWrapper != null) {
1✔
269
            DateTime vaccineDate = serviceWrapper.getVaccineDate();
1✔
270
            if (vaccineDate != null) return vaccineDate.toDate();
1✔
271
        }
272
        return null;
×
273
    }
274

275
    public void setChildActive(boolean childActive) {
276
        isChildActive = childActive;
1✔
277
    }
1✔
278

279
    public State getState() {
280
        if (state == null) {
1✔
281
            updateState();
×
282
        }
283
        return state;
1✔
284
    }
285

286
    public void setState(State state) {
287
        this.state = state;
1✔
288
    }
1✔
289

290
    public Button getUndoB() {
291
        return undoB;
1✔
292
    }
293
}
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