Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fd.ukkix_beta.background;
- import android.content.Context;
- import android.support.annotation.NonNull;
- import java.util.ArrayList;
- import fd.ukkix_beta.NotificationItem;
- import fd.ukkix_beta.Notifier;
- import fd.ukkix_beta.R;
- import fd.ukkix_beta.retro.ActivityData;
- import fd.ukkix_beta.retro.SubModel;
- import fd.ukkix_beta.tools.Hash;
- public class BasicAnalyzer {
- // What the hell, another class :\
- // Ok, let's remember what kinda shit we did here
- // Ah, this class was supposed to compare local cached data with new data from API
- // So, deadline notification process isn't handled here, don't ask why.
- // If you are still interested in how deadlines are detected and everything about notification stuff
- // just go to hell, I've not written any piece of code for that purpose.
- // TODO: write another class for deadline notifications
- private SubModel sm;
- private ArrayList<ActivityData> local_data;
- private ArrayList<ActivityData> new_data;
- private Context context;
- public BasicAnalyzer(@NonNull Context context, @NonNull SubModel sm, @NonNull ArrayList<ActivityData> local_data, @NonNull ArrayList<ActivityData> new_data){
- this.sm = sm;
- this.local_data = local_data;
- this.new_data = new_data;
- this.context = context;
- }
- // Here we go with analyzing process
- // If we get anything new here or detect any change, we will push new notification
- public void analyze(){
- // this class detects only new assignments and new marks
- // so, we're gonna collect them in separate variables
- ArrayList<ActivityData> new_assignments = new ArrayList<>();
- ArrayList<ActivityData> new_marks = new ArrayList<>();
- // both local and new data can't be null (@NonNull annotation), so we don't check for null
- for (int i = 0; i < new_data.size(); i++) { // for each new activity
- ActivityData newly = new_data.get(i);
- boolean is_new = true;
- for (int j = 0; j < local_data.size(); j++) { // for each local activity
- // if local data differs from new data,
- // that means we have either change or absolutely new information from API
- // first of all, we'll compare task name and deadline date
- // if they are same, it means this isn't new
- // and we'll change is_new => false
- ActivityData local = local_data.get(j);
- if (local.getTask().equals(newly.getTask()) && local.getDeadline().equals(newly.getDeadline())){
- is_new = false;
- // if data itself isn't new, there might be new mark in it
- // so, let's check for this condition
- if (!newly.getGrade().equals(local.getGrade())){ // if grade is different
- // here we are adding this activity to new_marks, 'cause we really have new mark
- new_marks.add(newly);
- }
- }
- }
- // if is_new remains true, that means, we have new assignment
- if (is_new){
- new_assignments.add(newly);
- }
- }
- // So, we've just completed most of the headache
- // Now we have two arrays: one for new assignments, another for new marks
- // We gotta notify user about this changes
- // As you noticed, we'll call another class, what the hell bro...
- // I've already written a class to handle notification process
- Notifier notifier = new Notifier(context);
- // That was a joke, we're gonna handle piece of notification process here
- // Now we'll iterate through new assignments and new marks
- // For every object, we'll create notification item object
- // and give it to our Notifier class, it'll handle the rest
- // We'll first push new assignment notifications
- if (new_assignments.size() > 0) {
- for (int i = 0; i < new_assignments.size(); i++) {
- NotificationItem ni = new NotificationItem();
- ActivityData ad = new_assignments.get(i);
- // notification_new_assignment_title: Yangi vazifa: %s (eg. Yangi vazifa: UV-2)
- ni.setTitle(context.getResources().getString(R.string.notification_new_assignment_title, ad.getTask()));
- ni.setData(sm.getSubject());
- ni.setSubdata(ad.getTeacher());
- ni.setSubject(sm.getSubject()); // we're doing duplicates
- ni.setNotifier(ad.getTeacher()); // here, again, don't ask why.
- ni.setTask(ad.getTask());
- ni.setTime((int) System.currentTimeMillis());
- // I don't care if you consider me stupid, but I'm using those 4 fields to generate ID
- // fields: title, data, notifier and the task
- // Hash class generates MD5 hash
- ni.setId(new Hash(ni.getTitle() + ni.getData() + ni.getNotifier() + ni.getTask()).getHash());
- ni.setType("assignment");
- notifier.push(ni); // here we are kicking new push notification
- }
- }
- // Now we'll notify about new marks
- if (new_marks.size() > 0){
- for (int i = 0; i < new_marks.size(); i++) {
- NotificationItem ni = new NotificationItem();
- ActivityData ad = new_marks.get(i);
- // notification_new_mark_title: %s/%s: %s (eg. 16/20: UV-2)
- ni.setTitle(context.getResources().getString(R.string.notification_new_mark_title, ad.getGrade(), ad.getMax_grade(), ad.getTask()));
- ni.setData(sm.getSubject());
- ni.setSubdata(ad.getTeacher());
- ni.setSubject(sm.getSubject()); // yeah, we're doing duplicates here again
- ni.setNotifier(ad.getTeacher());
- ni.setTask(ad.getTask());
- ni.setTime((int) System.currentTimeMillis());
- // generating ID (like I did in previous loop)
- ni.setId(new Hash(ni.getTitle() + ni.getData() + ni.getNotifier() + ni.getTask()).getHash());
- ni.setType("mark");
- notifier.push(ni); // kicking another notification
- }
- }
- // Hah! We've just finished another headache dear ;)
- // The End
- }
- }
Add Comment
Please, Sign In to add comment