use of app.abhijit.iter.models.Subject in project bunk by abhijitparida.
the class ResponseParserTest method parse_ValidAttendanceJson_ParsesCorrectly.
@Test
public void parse_ValidAttendanceJson_ParsesCorrectly() {
ResponseParser responseParser = new ResponseParser();
Student student = responseParser.parse("{\"name\":\"name\",\"status\":\"success\"}", "{\"griddata\":[{\"Latt\":\"10 / 10\",\"Patt\":\"Not Applicable\",\"subject\":\"Subject I\",\"subjectcode\":\"SUB001\"},{\"Latt\":\"Not Applicable\",\"Patt\":\"20 / 20\",\"subject\":\"Subject II\",\"subjectcode\":\"SUB002\"}]}");
HashMap<String, Subject> subjects = student.subjects;
assertEquals(2, subjects.size());
assertEquals("Subject I", subjects.get("SUB001").name);
assertEquals("SUB001", subjects.get("SUB001").code);
assertEquals(10, subjects.get("SUB001").theoryPresent);
assertEquals(10, subjects.get("SUB001").theoryTotal);
assertEquals(0, subjects.get("SUB001").labPresent);
assertEquals(0, subjects.get("SUB001").labTotal);
assertEquals("Subject II", subjects.get("SUB002").name);
assertEquals("SUB002", subjects.get("SUB002").code);
assertEquals(0, subjects.get("SUB002").theoryPresent);
assertEquals(0, subjects.get("SUB002").theoryTotal);
assertEquals(20, subjects.get("SUB002").labPresent);
assertEquals(20, subjects.get("SUB002").labTotal);
}
use of app.abhijit.iter.models.Subject in project bunk by abhijitparida.
the class AttendanceActivity method processAndDisplayAttendance.
private void processAndDisplayAttendance() {
ArrayList<SubjectView> subjectViews = new ArrayList<>();
Boolean updated = false;
for (Subject subject : mNewStudent.subjects.values()) {
SubjectView subjectView = new SubjectView();
subjectView.avatar = subjectAvatar(subject.code);
subjectView.name = subject.name;
subjectView.attendance = String.format(Locale.US, "%.2f%%", subject.attendance());
subjectView.theory = String.format(Locale.US, "%d/%d classes", subject.theoryPresent, subject.theoryTotal);
subjectView.lab = String.format(Locale.US, "%d/%d classes", subject.labPresent, subject.labTotal);
subjectView.absent = String.format(Locale.US, "%d classes", subject.absent());
if (mOldStudent.subjects.containsKey(subject.code)) {
Subject oldSubject = mOldStudent.subjects.get(subject.code);
if (subject.theoryPresent != oldSubject.theoryPresent || subject.theoryTotal != oldSubject.theoryTotal || subject.labPresent != oldSubject.labPresent || subject.labTotal != oldSubject.labTotal) {
updated = true;
subjectView.oldAttendance = String.format(Locale.US, "%.2f%%", oldSubject.attendance());
subjectView.oldTheory = String.format(Locale.US, "%d/%d classes", oldSubject.theoryPresent, oldSubject.theoryTotal);
subjectView.oldLab = String.format(Locale.US, "%d/%d classes", oldSubject.labPresent, oldSubject.labTotal);
subjectView.oldAbsent = String.format(Locale.US, "%d classes", oldSubject.absent());
if (subject.attendance() >= oldSubject.attendance()) {
subjectView.status = R.drawable.ic_status_up;
} else {
subjectView.status = R.drawable.ic_status_down;
}
} else {
subject.lastUpdated = oldSubject.lastUpdated;
if (subject.attendance() > 85.0) {
subjectView.status = R.drawable.ic_status_ok;
} else if (subject.attendance() > 75.0) {
subjectView.status = R.drawable.ic_status_warning;
} else {
subjectView.status = R.drawable.ic_status_critical;
}
}
}
subjectView.bunkStats = subject.bunkStats(mPrefMinimumAttendance, mPrefExtendedStats);
subjectView.lastUpdated = DateUtils.getRelativeTimeSpanString(subject.lastUpdated, new Date().getTime(), 0).toString();
subjectViews.add(subjectView);
}
mCache.setStudent(mNewStudent.username, mNewStudent);
if (updated) {
Toast.makeText(mContext, "Attendance updated", Toast.LENGTH_SHORT).show();
Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(500);
}
findViewById(R.id.no_attendance).setVisibility(subjectViews.isEmpty() ? View.VISIBLE : View.GONE);
findViewById(R.id.subjects).setVisibility(subjectViews.isEmpty() ? View.GONE : View.VISIBLE);
mSubjectViews.clear();
mSubjectViews.addAll(subjectViews);
mSubjectAdapter.notifyDataSetChanged();
}
use of app.abhijit.iter.models.Subject in project bunk by abhijitparida.
the class ResponseParser method processAttendance.
private HashMap<String, Subject> processAttendance(String attendanceJson) {
JsonArray attendance;
try {
attendance = this.jsonParser.parse(attendanceJson).getAsJsonObject().get("griddata").getAsJsonArray();
} catch (Exception e) {
throw new InvalidResponseException();
}
HashMap<String, Subject> subjects = new HashMap<>();
Pattern classesPattern = Pattern.compile("^(\\d+) / (\\d+)$");
for (int i = 0; i < attendance.size(); i++) {
JsonObject s = attendance.get(i).getAsJsonObject();
if (!s.has("Latt") || !s.has("Patt") || !s.has("subject") || !s.has("subjectcode")) {
throw new InvalidResponseException();
}
Subject subject = new Subject();
subject.name = s.get("subject").getAsString();
subject.code = s.get("subjectcode").getAsString();
subject.lastUpdated = new Date().getTime();
Matcher theoryClassesMatcher = classesPattern.matcher(s.get("Latt").getAsString());
if (theoryClassesMatcher.find()) {
subject.theoryPresent = Integer.parseInt(theoryClassesMatcher.group(1));
subject.theoryTotal = Integer.parseInt(theoryClassesMatcher.group(2));
}
Matcher labClassesMatcher = classesPattern.matcher(s.get("Patt").getAsString());
if (labClassesMatcher.find()) {
subject.labPresent = Integer.parseInt(labClassesMatcher.group(1));
subject.labTotal = Integer.parseInt(labClassesMatcher.group(2));
}
subjects.put(subject.code, subject);
}
return subjects;
}
Aggregations