Register/deregister geofences on completion change

pull/281/head
Alex Baker 10 years ago
parent 747236e21e
commit ea47169f96

@ -26,6 +26,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.tasks.Broadcaster; import org.tasks.Broadcaster;
import org.tasks.R; import org.tasks.R;
import org.tasks.location.GeofenceService;
import org.tasks.notifications.NotificationManager; import org.tasks.notifications.NotificationManager;
import org.tasks.preferences.Preferences; import org.tasks.preferences.Preferences;
@ -52,11 +53,13 @@ public class TaskDao {
private final ReminderService reminderService; private final ReminderService reminderService;
private final NotificationManager notificationManager; private final NotificationManager notificationManager;
private final Preferences preferences; private final Preferences preferences;
private GeofenceService geofenceService;
@Inject @Inject
public TaskDao(Database database, MetadataDao metadataDao, Broadcaster broadcaster, public TaskDao(Database database, MetadataDao metadataDao, Broadcaster broadcaster,
ReminderService reminderService, NotificationManager notificationManager, ReminderService reminderService, NotificationManager notificationManager,
Preferences preferences) { Preferences preferences, GeofenceService geofenceService) {
this.geofenceService = geofenceService;
dao = new RemoteModelDao<>(database, Task.class); dao = new RemoteModelDao<>(database, Task.class);
this.preferences = preferences; this.preferences = preferences;
this.metadataDao = metadataDao; this.metadataDao = metadataDao;
@ -355,9 +358,13 @@ public class TaskDao {
} }
task.markSaved(); task.markSaved();
if(values.containsKey(Task.COMPLETION_DATE.name) && task.isCompleted()) { boolean completionDateModified = values.containsKey(Task.COMPLETION_DATE.name);
if(completionDateModified && task.isCompleted()) {
afterComplete(task); afterComplete(task);
} else { } else {
if (completionDateModified) {
geofenceService.setupGeofences(task.getId());
}
if(values.containsKey(Task.DUE_DATE.name) || if(values.containsKey(Task.DUE_DATE.name) ||
values.containsKey(Task.REMINDER_FLAGS.name) || values.containsKey(Task.REMINDER_FLAGS.name) ||
values.containsKey(Task.REMINDER_PERIOD.name) || values.containsKey(Task.REMINDER_PERIOD.name) ||
@ -399,7 +406,9 @@ public class TaskDao {
* Called after the task was just completed * Called after the task was just completed
*/ */
private void afterComplete(Task task) { private void afterComplete(Task task) {
notificationManager.cancel((int) task.getId()); long taskId = task.getId();
notificationManager.cancel(taskId);
geofenceService.cancelGeofences(taskId);
} }
} }

@ -53,6 +53,16 @@ public class GeofenceService {
geofenceApi.register(getActiveGeofences()); geofenceApi.register(getActiveGeofences());
} }
public void setupGeofences(long taskId) {
geofenceApi.register(getGeofencesForTask(taskId));
}
public void cancelGeofences(long taskId) {
for (Geofence geofence : getGeofences(taskId)) {
geofenceApi.cancel(geofence);
}
}
public boolean synchronizeGeofences(final long taskId, Set<Geofence> geofences) { public boolean synchronizeGeofences(final long taskId, Set<Geofence> geofences) {
List<Metadata> metadata = newArrayList(transform(geofences, new Function<Geofence, Metadata>() { List<Metadata> metadata = newArrayList(transform(geofences, new Function<Geofence, Metadata>() {
@Override @Override
@ -89,10 +99,6 @@ public class GeofenceService {
})); }));
} }
private void setupGeofences(long taskId) {
geofenceApi.register(getGeofencesForTask(taskId));
}
private List<Geofence> getActiveGeofences() { private List<Geofence> getActiveGeofences() {
return toGeofences(metadataDao.toList(Query.select(Metadata.ID, Metadata.TASK, GeofenceFields.PLACE, GeofenceFields.LATITUDE, GeofenceFields.LONGITUDE, GeofenceFields.RADIUS). return toGeofences(metadataDao.toList(Query.select(Metadata.ID, Metadata.TASK, GeofenceFields.PLACE, GeofenceFields.LATITUDE, GeofenceFields.LONGITUDE, GeofenceFields.RADIUS).
join(Join.inner(Task.TABLE, Metadata.TASK.eq(Task.ID))). join(Join.inner(Task.TABLE, Metadata.TASK.eq(Task.ID))).

Loading…
Cancel
Save