Skip to content

Commit

Permalink
Change the logic of checking changes in service
Browse files Browse the repository at this point in the history
Now service tracks changes not by comparing old and new update time,
but by total sum of all scores.
  • Loading branch information
vladholubiev committed Feb 9, 2015
1 parent 5963ba5 commit ff982c7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;
Expand All @@ -23,7 +22,7 @@ public class ScoreCheckerService extends Service {
public static int pendingModulesCount = NearbyModules.getCount();

private Timer timer = new Timer("timer");
private TimerTask updateTimeTask = new UpdateTimeTask();
private TimerTask scoresChangedTask = new ScoresChangedTask();
private TimerTask moduleDatesUpdateTask = new ModuleDatesUpdateTask();

public ScoreCheckerService() {
Expand Down Expand Up @@ -70,22 +69,28 @@ public void onEvent(InternetConnectionAbsent event) {
}

private void startServiceTimer() {
scheduleModuleDatesUpdateTask();
scheduleScoresChangedTask();
}

private void scheduleModuleDatesUpdateTask() {
moduleDatesUpdateTask = new ModuleDatesUpdateTask();
timer.schedule(moduleDatesUpdateTask, 200, TimeUnit.HOURS.toMillis(12));
}

updateTimeTask = new UpdateTimeTask();
private void scheduleScoresChangedTask() {
scoresChangedTask = new ScoresChangedTask();
ModuleDatesUpdateTask.updatePendingModulesCount();
if (pendingModulesCount > 0) {
timer.schedule(updateTimeTask, 0, TimeUnit.HOURS.toMillis(2));

Log.d(TAG, "" + pendingModulesCount + " modules in the near 2 days");
timer.schedule(scoresChangedTask, 0, TimeUnit.HOURS.toMillis(2));
} else {
Log.d(TAG, "No modules dates in the near 2 days");
}
}

private void stopServiceTimer() {
moduleDatesUpdateTask.cancel();
updateTimeTask.cancel();
scoresChangedTask.cancel();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package ua.samosfator.moduleok.notification;

import java.util.List;
import java.util.TimerTask;

import java8.util.stream.StreamSupport;
import ua.samosfator.moduleok.SessionIdExpiredException;
import ua.samosfator.moduleok.StudentKeeper;
import ua.samosfator.moduleok.parser.Subject;

public class ScoresChangedTask extends TimerTask {
@Override
public void run() {
if (areScoresChanged()) {
ScoreCheckerNotification.sendNotification();
}
}

private boolean areScoresChanged() {
List<Subject> oldSubjects = getSubjects();
try {
StudentKeeper.refreshStudent();
} catch (SessionIdExpiredException ignored) {
}
List<Subject> newSubjects = getSubjects();

return getTotalSumAllScores(oldSubjects) != getTotalSumAllScores(newSubjects);
}

private int getTotalSumAllScores(List<Subject> subjects) {
final int[] oldTotalScore = {0};
StreamSupport.parallelStream(subjects)
.map(subject -> StreamSupport.parallelStream(subject.getModules()))
.map(modulesStream -> modulesStream.map(module -> oldTotalScore[0] += module.getScore()));
return oldTotalScore[0];
}

private List<Subject> getSubjects() {
return StudentKeeper.getCurrentStudent()
.getSemesters()
.get(StudentKeeper.getCurrentSemesterIndex())
.getSubjects();
}
}

This file was deleted.

0 comments on commit ff982c7

Please sign in to comment.