FutureDreams

SOURCE: Ukki X beta [BasicAnalyzer]

Nov 14th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.40 KB | None | 0 0
  1. package fd.ukkix_beta.background;
  2.  
  3. import android.content.Context;
  4. import android.support.annotation.NonNull;
  5. import java.util.ArrayList;
  6. import fd.ukkix_beta.NotificationItem;
  7. import fd.ukkix_beta.Notifier;
  8. import fd.ukkix_beta.R;
  9. import fd.ukkix_beta.retro.ActivityData;
  10. import fd.ukkix_beta.retro.SubModel;
  11. import fd.ukkix_beta.tools.Hash;
  12.  
  13. public class BasicAnalyzer {
  14.  
  15.     // What the hell, another class :\
  16.     // Ok, let's remember what kinda shit we did here
  17.     // Ah, this class was supposed to compare local cached data with new data from API
  18.     // So, deadline notification process isn't handled here, don't ask why.
  19.     // If you are still interested in how deadlines are detected and everything about notification stuff
  20.     // just go to hell, I've not written any piece of code for that purpose.
  21.     // TODO: write another class for deadline notifications
  22.  
  23.     private SubModel sm;
  24.     private ArrayList<ActivityData> local_data;
  25.     private ArrayList<ActivityData> new_data;
  26.     private Context context;
  27.  
  28.     public BasicAnalyzer(@NonNull Context context, @NonNull SubModel sm, @NonNull ArrayList<ActivityData> local_data, @NonNull ArrayList<ActivityData> new_data){
  29.         this.sm = sm;
  30.         this.local_data = local_data;
  31.         this.new_data = new_data;
  32.         this.context = context;
  33.     }
  34.  
  35.     // Here we go with analyzing process
  36.     // If we get anything new here or detect any change, we will push new notification
  37.     public void analyze(){
  38.         // this class detects only new assignments and new marks
  39.         // so, we're gonna collect them in separate variables
  40.         ArrayList<ActivityData> new_assignments = new ArrayList<>();
  41.         ArrayList<ActivityData> new_marks = new ArrayList<>();
  42.  
  43.         // both local and new data can't be null (@NonNull annotation), so we don't check for null
  44.         for (int i = 0; i < new_data.size(); i++) { // for each new activity
  45.             ActivityData newly = new_data.get(i);
  46.             boolean is_new = true;
  47.             for (int j = 0; j < local_data.size(); j++) { // for each local activity
  48.                 // if local data differs from new data,
  49.                 // that means we have either change or absolutely new information from API
  50.                 // first of all, we'll compare task name and deadline date
  51.                 // if they are same, it means this isn't new
  52.                 // and we'll change is_new => false
  53.                 ActivityData local = local_data.get(j);
  54.                 if (local.getTask().equals(newly.getTask()) && local.getDeadline().equals(newly.getDeadline())){
  55.                     is_new = false;
  56.                     // if data itself isn't new, there might be new mark in it
  57.                     // so, let's check for this condition
  58.                     if (!newly.getGrade().equals(local.getGrade())){ // if grade is different
  59.                         // here we are adding this activity to new_marks, 'cause we really have new mark
  60.                         new_marks.add(newly);
  61.                     }
  62.                 }
  63.             }
  64.             // if is_new remains true, that means, we have new assignment
  65.             if (is_new){
  66.                 new_assignments.add(newly);
  67.             }
  68.         }
  69.  
  70.         // So, we've just completed most of the headache
  71.         // Now we have two arrays: one for new assignments, another for new marks
  72.         // We gotta notify user about this changes
  73.         // As you noticed, we'll call another class, what the hell bro...
  74.         // I've already written a class to handle notification process
  75.         Notifier notifier = new Notifier(context);
  76.  
  77.         // That was a joke, we're gonna handle piece of notification process here
  78.         // Now we'll iterate through new assignments and new marks
  79.         // For every object, we'll create notification item object
  80.         // and give it to our Notifier class, it'll handle the rest
  81.  
  82.         // We'll first push new assignment notifications
  83.  
  84.         if (new_assignments.size() > 0) {
  85.             for (int i = 0; i < new_assignments.size(); i++) {
  86.                 NotificationItem ni = new NotificationItem();
  87.                 ActivityData ad = new_assignments.get(i);
  88.                 // notification_new_assignment_title: Yangi vazifa: %s (eg. Yangi vazifa: UV-2)
  89.                 ni.setTitle(context.getResources().getString(R.string.notification_new_assignment_title, ad.getTask()));
  90.                 ni.setData(sm.getSubject());
  91.                 ni.setSubdata(ad.getTeacher());
  92.                 ni.setSubject(sm.getSubject()); // we're doing duplicates
  93.                 ni.setNotifier(ad.getTeacher()); // here, again, don't ask why.
  94.                 ni.setTask(ad.getTask());
  95.                 ni.setTime((int) System.currentTimeMillis());
  96.                 // I don't care if you consider me stupid, but I'm using those 4 fields to generate ID
  97.                 // fields: title, data, notifier and the task
  98.                 // Hash class generates MD5 hash
  99.                 ni.setId(new Hash(ni.getTitle() + ni.getData() + ni.getNotifier() + ni.getTask()).getHash());
  100.                 ni.setType("assignment");
  101.  
  102.                 notifier.push(ni); // here we are kicking new push notification
  103.             }
  104.         }
  105.  
  106.  
  107.         // Now we'll notify about new marks
  108.         if (new_marks.size() > 0){
  109.             for (int i = 0; i < new_marks.size(); i++) {
  110.                 NotificationItem ni = new NotificationItem();
  111.                 ActivityData ad = new_marks.get(i);
  112.                 // notification_new_mark_title: %s/%s: %s (eg. 16/20: UV-2)
  113.                 ni.setTitle(context.getResources().getString(R.string.notification_new_mark_title, ad.getGrade(), ad.getMax_grade(), ad.getTask()));
  114.                 ni.setData(sm.getSubject());
  115.                 ni.setSubdata(ad.getTeacher());
  116.                 ni.setSubject(sm.getSubject()); // yeah, we're doing duplicates here again
  117.                 ni.setNotifier(ad.getTeacher());
  118.                 ni.setTask(ad.getTask());
  119.                 ni.setTime((int) System.currentTimeMillis());
  120.                 // generating ID (like I did in previous loop)
  121.                 ni.setId(new Hash(ni.getTitle() + ni.getData() + ni.getNotifier() + ni.getTask()).getHash());
  122.                 ni.setType("mark");
  123.  
  124.                 notifier.push(ni); // kicking another notification
  125.             }
  126.         }
  127.  
  128.         // Hah! We've just finished another headache dear ;)
  129.         // The End
  130.     }
  131. }
Add Comment
Please, Sign In to add comment