Skip to content
This repository has been archived by the owner on Jun 22, 2023. It is now read-only.

Commit

Permalink
FEATURE: Implement detailed notifications
Browse files Browse the repository at this point in the history
fixes #51 #50
  • Loading branch information
d-Rickyy-b committed Jan 11, 2019
1 parent 7f65d3b commit 277c9dd
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ public int getTotalCancellations(String className) {
return cancellations;
}

public int getTotalDifferences(TimeTable savedTimeTable, String className) {
public TimeTable getTotalDifferences(TimeTable savedTimeTable, String className) {
Logger.d(TAG, "Getting differences of saved and downloaded timetable!");
int differences = 0;
TimeTable differencesTimeTable = new TimeTable();
Date currentDate = new Date();
ArrayList<TimeTableDay> savedDays = savedTimeTable.getAllDays();

Expand All @@ -108,20 +108,20 @@ public int getTotalDifferences(TimeTable savedTimeTable, String className) {
Logger.d(TAG, String.format("Dates are the same - %s | %s", ttd.getDateString(), saved_ttd.getDateString()));
Logger.d(TAG, String.format("%s", ttd.getElements(className).toString()));
Logger.d(TAG, String.format("%s", saved_ttd.getElements(className).toString()));
differences += ttd.getDifferences(saved_ttd, className);
differencesTimeTable.addDay(ttd.getDifferences(saved_ttd, className));
newDay = false;
break;
}
}

if (newDay) {
int dayDiffs = ttd.getElementsCount(className);
differences += dayDiffs;
differencesTimeTable.addDay(ttd);
Logger.d(TAG, String.format(Locale.GERMANY,"New Day found - %d cancellations for %s", dayDiffs, className));
}
}

return differences;
return differencesTimeTable;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,7 @@ public int getElementsCount(String className) {
return getElements(className).size();
}

// Returns the number of differences between two lists
public int getDifferences(TimeTableDay ttd, String className) {
TimeTableDay getDifferences(TimeTableDay ttd, String className) {
ArrayList<TimeTableElement> savedElements = ttd.getElements(className);
ArrayList<TimeTableElement> newElements = this.getElements(className);

Expand Down Expand Up @@ -228,10 +227,10 @@ public int getDifferences(TimeTableDay ttd, String className) {

// savedElements now contains only those elements which are no longer in the TimeTable
// newElements now only contains those elements which are new (not saved yet) or have changed in a single part
int changesToOldCancellations = savedElements.size();
int newCancellations = newElements.size();
savedElements.addAll(newElements);
ArrayList<TimeTableElement> tmp = savedElements;

return changesToOldCancellations + newCancellations;
return new TimeTableDay(date, week, tmp);
}

// Checks if this and the given day are at the same date
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public TimeTable parse(ArrayList<String> websites) {
timeTable1.addDay(ttd1);
timeTable2.addDay(ttd2);

int diffs = timeTable1.getTotalDifferences(timeTable2, "K1");
int diffs = timeTable1.getTotalDifferences(timeTable2, "K1").getDaysCount();
Logger.d("MockParser", String.format("Diffs: %s", diffs));

timeTableReturn.addDay(ttd1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import androidx.work.WorkerParameters;
import de.aurora.mggvertretungsplan.R;
import de.aurora.mggvertretungsplan.datamodel.TimeTable;
import de.aurora.mggvertretungsplan.datamodel.TimeTableDay;
import de.aurora.mggvertretungsplan.parsing.BaseParser;
import de.aurora.mggvertretungsplan.parsing.BaseParser.ParsingCompleteListener;
import de.aurora.mggvertretungsplan.parsing.MGGParser;
Expand Down Expand Up @@ -90,39 +91,29 @@ public void onParsingComplete(TimeTable timeTable) {
}
}

// Compare new data with old data
int totalDiffs = timeTable.getTotalDifferences(timeTable_saved, class_name);

// Get new cancellations
// new_cancellations = ...

// Get removed cancellations
// removed_cancellations = ...

// Get changed cancellations
// changed_cancellations = ...

// "x neue Ausfälle"
// "x Änderung/en am Vertretugnsplan"
// "..."

NotificationHelper notificationHelper = new NotificationHelper(getApplicationContext());

TimeTable diffTimeTable = timeTable.getTotalDifferences(timeTable_saved, class_name);
int totalDiffs = diffTimeTable.getTotalCancellations(class_name);
Logger.d(TAG, String.format(Locale.GERMANY, "Total differences: %d", totalDiffs));

Context context = getApplicationContext();
String ticker = context.getResources().getString(R.string.notification_cancellations_ticker);
String title = context.getResources().getString(R.string.notification_cancellations_title);
String info = context.getResources().getQuantityString(R.plurals.notification_cancellations_info, totalDiffs, totalDiffs);

NotificationHelper notificationHelper = new NotificationHelper(getApplicationContext());

if (totalDiffs <= 0) {
Logger.d(TAG, "Not sending a notification!");
return;
}

Logger.d(TAG, "Notifying user");
notificationHelper.notifyChanges(ticker, title, info);
if (sp.getBoolean("show_detailed_notifications", true)) {
for (TimeTableDay ttd : diffTimeTable.getAllDays()) {
Logger.d(TAG, "Notifying user");
notificationHelper.notifyChange(ttd);
}
} else {
Context context = getApplicationContext();
String ticker = context.getResources().getString(R.string.notification_cancellations_ticker);
String title = context.getResources().getString(R.string.notification_cancellations_title);
String info = context.getResources().getQuantityString(R.plurals.notification_cancellations_info, totalDiffs, totalDiffs);
notificationHelper.notifyChanges(ticker, title, info);
}
saveData(timeTable);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,14 +316,13 @@ public void getDifferences() {
TimeTableDay day7 = new TimeTableDay("01.01.2018", WEEK_A, day7List);


assertEquals(0, day1.getDifferences(day1, className));
assertEquals(1, day1.getDifferences(day2, className));
assertEquals(1, day1.getDifferences(day3, className));
assertEquals(1, day1.getDifferences(day4, className));
assertEquals(2, day1.getDifferences(day5, className));
assertEquals(2, day1.getDifferences(day6, className));
assertEquals(4, day1.getDifferences(day7, className));

assertEquals(0, day1.getDifferences(day1, className).getElementsCount(className));
assertEquals(1, day1.getDifferences(day2, className).getElementsCount(className));
assertEquals(1, day1.getDifferences(day3, className).getElementsCount(className));
assertEquals(1, day1.getDifferences(day4, className).getElementsCount(className));
assertEquals(2, day1.getDifferences(day5, className).getElementsCount(className));
assertEquals(2, day1.getDifferences(day6, className).getElementsCount(className));
assertEquals(4, day1.getDifferences(day7, className).getElementsCount(className));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,71 @@ public void testGetTotalCancellations() {
}

public void testGetTotalDifferences() {
ArrayList<ArrayList<String>> dayList = new ArrayList<>();
dayList.add(new ArrayList<>(Arrays.asList("1", "K1", "D", "---", "H202", "---", "")));
dayList.add(new ArrayList<>(Arrays.asList("2", "7a", "E", "---", "H105", "---", "")));
dayList.add(new ArrayList<>(Arrays.asList("3", "7a", "BIO", "---", "S320", "---", "")));
dayList.add(new ArrayList<>(Arrays.asList("3", "5c", "G", "---", "M315", "---", "Test")));
TimeTableDay ttd = new TimeTableDay("31.12.", WEEK_A, dayList);
timeTable.addDay(ttd);

ArrayList<ArrayList<String>> dayList2 = new ArrayList<>();
dayList2.add(new ArrayList<>(Arrays.asList("1", "K2", "D", "---", "H202", "---", "")));
//dayList2.add(new ArrayList<>(Arrays.asList("1", "K2", "D", "---", "H202", "---", ""))); //Duplicate - should not count
dayList2.add(new ArrayList<>(Arrays.asList("2", "5b", "E", "---", "H105", "---", "")));
dayList2.add(new ArrayList<>(Arrays.asList("3", "7a", "BIO", "---", "S320", "---", "")));
dayList2.add(new ArrayList<>(Arrays.asList("4", "7a", "BIO", "---", "S320", "---", ""))); // Getting merged with 3. lesson
dayList2.add(new ArrayList<>(Arrays.asList("3-4", "9c", "G", "---", "M315", "---", "Test")));
dayList2.add(new ArrayList<>(Arrays.asList("5", "9c", "D", "---", "M315", "---", "Test")));
dayList2.add(new ArrayList<>(Arrays.asList("8-9", "9c", "Sp", "---", "M315", "---", "Test")));
TimeTableDay ttd2 = new TimeTableDay("30.12.", WEEK_A, dayList2);
timeTable.addDay(ttd2);

// Second tt
TimeTable timeTable1 = new TimeTable();
ArrayList<ArrayList<String>> dayList3 = new ArrayList<>();
dayList3.add(new ArrayList<>(Arrays.asList("1", "K1", "D", "---", "H202", "---", "")));
dayList3.add(new ArrayList<>(Arrays.asList("2", "7a", "E", "---", "H105", "---", "")));
dayList3.add(new ArrayList<>(Arrays.asList("3", "7a", "BIO", "---", "S320", "---", "")));
dayList3.add(new ArrayList<>(Arrays.asList("3", "5c", "G", "---", "M315", "---", "Test")));
TimeTableDay ttd3 = new TimeTableDay("31.12.", WEEK_A, dayList3);
timeTable1.addDay(ttd3);

ArrayList<ArrayList<String>> dayList4 = new ArrayList<>();
dayList4.add(new ArrayList<>(Arrays.asList("1", "K2", "D", "---", "H202", "---", "")));
dayList4.add(new ArrayList<>(Arrays.asList("2", "5b", "E", "---", "H105", "---", "")));
dayList4.add(new ArrayList<>(Arrays.asList("3", "7a", "BIO", "---", "S320", "---", "")));
dayList4.add(new ArrayList<>(Arrays.asList("4", "7a", "BIO", "---", "S320", "---", ""))); // Getting merged with 3. lesson
dayList4.add(new ArrayList<>(Arrays.asList("3-4", "9c", "G", "---", "M315", "---", "Test")));
dayList4.add(new ArrayList<>(Arrays.asList("5", "9c", "D", "---", "M315", "---", "Test")));
dayList4.add(new ArrayList<>(Arrays.asList("8-9", "9c", "Sp", "---", "M315", "---", "Test")));
TimeTableDay ttd4 = new TimeTableDay("30.12.", WEEK_A, dayList4);
timeTable1.addDay(ttd4);

assertEquals(0, timeTable.getTotalDifferences(timeTable1, "7a").getTotalCancellations("7a"));
assertEquals(0, timeTable.getTotalDifferences(timeTable1, "K2").getTotalCancellations("K2"));
assertEquals(0, timeTable.getTotalDifferences(timeTable1, "8f").getTotalCancellations("8f"));

// The other way around
assertEquals(0, timeTable1.getTotalDifferences(timeTable, "7a").getTotalCancellations("7a"));
assertEquals(0, timeTable1.getTotalDifferences(timeTable, "K2").getTotalCancellations("K2"));
assertEquals(0, timeTable1.getTotalDifferences(timeTable, "8f").getTotalCancellations("8f"));

// Another check
assertEquals(timeTable.getTotalDifferences(timeTable1, "7a").getTotalCancellations("7a"), timeTable1.getTotalDifferences(timeTable, "7a").getTotalCancellations("7a"));
assertEquals(timeTable.getTotalDifferences(timeTable1, "K2").getTotalCancellations("K2"), timeTable1.getTotalDifferences(timeTable, "K2").getTotalCancellations("K2"));
assertEquals(timeTable.getTotalDifferences(timeTable1, "8f").getTotalCancellations("8f"), timeTable1.getTotalDifferences(timeTable, "8f").getTotalCancellations("8f"));

TimeTable timeTable2 = new TimeTable();
timeTable2.addDay(ttd3);

dayList4.add(new ArrayList<>(Arrays.asList("1-2", "K1", "D", "---", "H001", "---", "Test")));
ttd4 = new TimeTableDay("30.12.", WEEK_A, dayList4);
timeTable2.addDay(ttd4);

assertEquals(0, timeTable.getTotalDifferences(timeTable2, "7a").getTotalCancellations("7a"));
assertEquals(0, timeTable.getTotalDifferences(timeTable2, "K2").getTotalCancellations("K2"));
assertEquals(0, timeTable.getTotalDifferences(timeTable2, "8f").getTotalCancellations("8f"));
}

public void testToString() {
Expand Down

0 comments on commit 277c9dd

Please sign in to comment.