Add reminder jobs in batches

pull/795/head
Alex Baker 7 years ago
parent 1580b12f95
commit a01fd12df6

@ -6,11 +6,14 @@
package com.todoroo.astrid.reminders;
import static com.google.common.collect.Lists.transform;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.data.Task;
import java.util.List;
import javax.inject.Inject;
import org.jetbrains.annotations.Nullable;
import org.tasks.injection.ApplicationScope;
import org.tasks.jobs.NotificationQueue;
import org.tasks.jobs.ReminderEntry;
@ -49,24 +52,24 @@ public final class ReminderService {
}
public void scheduleAllAlarms(List<Long> taskIds) {
for (Task task : taskDao.fetch(taskIds)) {
scheduleAlarm(task);
}
jobs.add(transform(taskDao.fetch(taskIds), this::getReminderEntry));
}
public void scheduleAllAlarms() {
for (Task task : taskDao.getTasksWithReminders()) {
scheduleAlarm(task);
}
jobs.add(transform(taskDao.getTasksWithReminders(), this::getReminderEntry));
}
public void scheduleAlarm(Task task) {
jobs.add(getReminderEntry(task));
}
public void cancelReminder(long taskId) {
jobs.cancelReminder(taskId);
}
public void scheduleAlarm(Task task) {
private @Nullable ReminderEntry getReminderEntry(Task task) {
if (task == null || !task.isSaved()) {
return;
return null;
}
long taskId = task.getId();
@ -76,7 +79,7 @@ public final class ReminderService {
cancelReminder(taskId);
if (task.isCompleted() || task.isDeleted()) {
return;
return null;
}
// snooze reminder
@ -98,14 +101,16 @@ public final class ReminderService {
// snooze trumps all
if (whenSnooze != NO_ALARM) {
jobs.add(new ReminderEntry(taskId, whenSnooze, TYPE_SNOOZE));
return new ReminderEntry(taskId, whenSnooze, TYPE_SNOOZE);
} else if (whenRandom < whenDueDate && whenRandom < whenOverdue) {
jobs.add(new ReminderEntry(taskId, whenRandom, TYPE_RANDOM));
return new ReminderEntry(taskId, whenRandom, TYPE_RANDOM);
} else if (whenDueDate < whenOverdue) {
jobs.add(new ReminderEntry(taskId, whenDueDate, TYPE_DUE));
return new ReminderEntry(taskId, whenDueDate, TYPE_DUE);
} else if (whenOverdue != NO_ALARM) {
jobs.add(new ReminderEntry(taskId, whenOverdue, TYPE_OVERDUE));
return new ReminderEntry(taskId, whenOverdue, TYPE_OVERDUE);
}
return null;
}
private long calculateNextSnoozeReminder(Task task) {

@ -1,5 +1,6 @@
package org.tasks.jobs;
import static com.google.common.base.Predicates.notNull;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Lists.newArrayList;
@ -7,8 +8,11 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.Ordering;
import com.google.common.collect.TreeMultimap;
import com.google.common.primitives.Ints;
import java.util.Collections;
import java.util.List;
import javax.inject.Inject;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.tasks.injection.ApplicationScope;
import org.tasks.preferences.Preferences;
import org.tasks.time.DateTime;
@ -28,8 +32,15 @@ public class NotificationQueue {
}
public synchronized <T extends NotificationQueueEntry> void add(T entry) {
boolean reschedule = jobs.isEmpty() || entry.getTime() < firstTime();
jobs.put(entry.getTime(), entry);
add(Collections.singletonList(entry));
}
public synchronized <T extends NotificationQueueEntry> void add(Iterable<T> entries) {
boolean reschedule = false;
for (T entry : filter(entries, notNull())) {
reschedule |= jobs.isEmpty() || entry.getTime() < firstTime();
jobs.put(entry.getTime(), entry);
}
if (reschedule) {
scheduleNext(true);
}

Loading…
Cancel
Save