Move event deletion to CalendarEventProvider

pull/467/head
Alex Baker 8 years ago
parent 6377cf1e3b
commit 4137abd55b

@ -7,7 +7,6 @@ package com.todoroo.astrid.core;
import android.os.Bundle;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.sql.Criterion;
import com.todoroo.andlib.sql.Query;
import com.todoroo.andlib.utility.DateUtilities;
@ -15,27 +14,29 @@ import com.todoroo.astrid.dao.Database;
import com.todoroo.astrid.dao.MetadataDao;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.gcal.GCalHelper;
import com.todoroo.astrid.service.TaskDeleter;
import org.tasks.R;
import org.tasks.calendars.CalendarEventProvider;
import org.tasks.dialogs.DialogBuilder;
import org.tasks.injection.ActivityComponent;
import org.tasks.injection.InjectingPreferenceActivity;
import org.tasks.preferences.Preferences;
import org.tasks.ui.ProgressDialogAsyncTask;
import java.util.List;
import javax.inject.Inject;
public class OldTaskPreferences extends InjectingPreferenceActivity {
@Inject DialogBuilder dialogBuilder;
@Inject GCalHelper gcalHelper;
@Inject TaskDeleter taskDeleter;
@Inject MetadataDao metadataDao;
@Inject Preferences preferences;
@Inject Database database;
@Inject TaskDao taskDao;
@Inject CalendarEventProvider calendarEventProvider;
@Override
public void onCreate(Bundle savedInstanceState) {
@ -79,20 +80,12 @@ public class OldTaskPreferences extends InjectingPreferenceActivity {
.setPositiveButton(android.R.string.ok, (dialog, which) -> new ProgressDialogAsyncTask(OldTaskPreferences.this, dialogBuilder) {
@Override
protected Integer doInBackground(Void... params) {
TodorooCursor<Task> cursor = taskDao.query(Query.select(Task.ID, Task.CALENDAR_URI).where(
Criterion.and(Task.COMPLETION_DATE.gt(0), Task.CALENDAR_URI.isNotNull())));
try {
int length = cursor.getCount();
for (int i = 0; i < length; i++) {
cursor.moveToNext();
Task task = new Task(cursor);
gcalHelper.deleteTaskEvent(task);
}
} finally {
cursor.close();
}
Query query = Query.select(Task.ID, Task.CALENDAR_URI)
.where(Criterion.and(Task.COMPLETION_DATE.gt(0), Task.CALENDAR_URI.isNotNull()));
taskDao.forEach(query, calendarEventProvider::deleteEvent);
Task template = new Task();
template.setDeletionDate(DateUtilities.now());
template.setCalendarUri("");
return taskDao.update(Task.COMPLETION_DATE.gt(0), template);
}
@ -110,18 +103,9 @@ public class OldTaskPreferences extends InjectingPreferenceActivity {
.setPositiveButton(android.R.string.ok, (dialog, which) -> new ProgressDialogAsyncTask(OldTaskPreferences.this, dialogBuilder) {
@Override
protected Integer doInBackground(Void... params) {
TodorooCursor<Task> cursor = taskDao.query(Query.select(Task.ID, Task.TITLE, Task.CALENDAR_URI).where(
Criterion.and(Task.DELETION_DATE.gt(0), Task.CALENDAR_URI.isNotNull())));
try {
int length = cursor.getCount();
for (int i = 0; i < length; i++) {
cursor.moveToNext();
Task task = new Task(cursor);
gcalHelper.deleteTaskEvent(task);
}
} finally {
cursor.close();
}
Query query = Query.select(Task.ID, Task.CALENDAR_URI)
.where(Criterion.and(Task.DELETION_DATE.gt(0), Task.CALENDAR_URI.isNotNull()));
taskDao.forEach(query, calendarEventProvider::deleteEvent);
int result = taskDeleter.purgeDeletedTasks();
metadataDao.removeDanglingMetadata();
return result;
@ -142,29 +126,7 @@ public class OldTaskPreferences extends InjectingPreferenceActivity {
@Override
protected Integer doInBackground(Void... params) {
int deletedEventCount = 0;
TodorooCursor<Task> cursor = taskDao.query(Query.select(Task.ID, Task.CALENDAR_URI).where(
Criterion.and(Task.COMPLETION_DATE.gt(0), Task.CALENDAR_URI.isNotNull())));
try {
int length = cursor.getCount();
for (int i = 0; i < length; i++) {
cursor.moveToNext();
Task task = new Task(cursor);
if (gcalHelper.deleteTaskEvent(task)) {
deletedEventCount++;
}
}
} finally {
cursor.close();
}
// mass update the CALENDAR_URI here,
// since the GCalHelper doesnt save it due to performance-reasons
Task template = new Task();
template.setCalendarUri(""); //$NON-NLS-1$
taskDao.update(
Criterion.and(Task.COMPLETION_DATE.gt(0), Task.CALENDAR_URI.isNotNull()),
template);
return deletedEventCount;
return deleteCalendarEvents(Criterion.and(Task.COMPLETION_DATE.gt(0), Task.CALENDAR_URI.isNotNull()));
}
@Override
@ -181,27 +143,7 @@ public class OldTaskPreferences extends InjectingPreferenceActivity {
.setPositiveButton(android.R.string.ok, (dialog, which) -> new ProgressDialogAsyncTask(OldTaskPreferences.this, dialogBuilder) {
@Override
protected Integer doInBackground(Void... params) {
int deletedEventCount = 0;
TodorooCursor<Task> cursor = taskDao.query(Query.select(Task.ID, Task.CALENDAR_URI).where(
Task.CALENDAR_URI.isNotNull()));
try {
int length = cursor.getCount();
for (int i = 0; i < length; i++) {
cursor.moveToNext();
Task task = new Task(cursor);
if (gcalHelper.deleteTaskEvent(task)) {
deletedEventCount++;
}
}
} finally {
cursor.close();
}
// mass update the CALENDAR_URI here,
// since the GCalHelper doesnt save it due to performance-reasons
Task template = new Task();
template.setCalendarUri(""); //$NON-NLS-1$
taskDao.update(Task.CALENDAR_URI.isNotNull(), template);
return deletedEventCount;
return deleteCalendarEvents(Task.CALENDAR_URI.isNotNull());
}
@Override
@ -213,6 +155,22 @@ public class OldTaskPreferences extends InjectingPreferenceActivity {
.show();
}
private int deleteCalendarEvents(Criterion criterion) {
int deletedEventCount = 0;
List<Task> tasks = taskDao.toList(Query.select(Task.ID, Task.CALENDAR_URI).where(criterion));
for (Task task : tasks) {
if (calendarEventProvider.deleteEvent(task)) {
deletedEventCount++;
}
}
// mass update the CALENDAR_URI here,
// since the GCalHelper doesnt save it due to performance-reasons
Task template = new Task();
template.setCalendarUri(""); //$NON-NLS-1$
taskDao.update(criterion, template);
return deletedEventCount;
}
private void resetPreferences() {
dialogBuilder.newMessageDialog(R.string.EPr_reset_preferences_warning)
.setPositiveButton(R.string.EPr_reset_preferences, (dialog, which) -> {

@ -94,7 +94,7 @@ public class GCalHelper {
String eventuri = getTaskEventUri(task);
if(!TextUtils.isEmpty(eventuri) && deleteEventIfExists) {
deleteTaskEvent(task);
calendarEventProvider.deleteEvent(task);
}
try {
@ -145,35 +145,6 @@ public class GCalHelper {
task.setCalendarUri(uri.toString());
}
public boolean deleteTaskEvent(Task task) {
boolean eventDeleted = false;
String uri;
if(task.containsNonNullValue(Task.CALENDAR_URI)) {
uri = task.getCalendarURI();
} else {
task = taskDao.fetch(task.getId(), Task.CALENDAR_URI);
if(task == null) {
return false;
}
uri = task.getCalendarURI();
}
if(!TextUtils.isEmpty(uri)) {
try {
Uri calendarUri = Uri.parse(uri);
if (calendarEventProvider.hasEvent(calendarUri)) {
cr.delete(calendarUri, null, null);
eventDeleted = true;
}
task.setCalendarUri("");
} catch (Exception e) {
Timber.e(e, e.getMessage());
}
}
return eventDeleted;
}
public void createStartAndEndDate(Task task, ContentValues values) {
long dueDate = task.getDueDate();
long tzCorrectedDueDate = dueDate + TimeZone.getDefault().getOffset(dueDate);

@ -5,19 +5,20 @@ import com.todoroo.andlib.sql.Query;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.gcal.GCalHelper;
import org.tasks.calendars.CalendarEventProvider;
import javax.inject.Inject;
public class TaskDeleter {
private final GCalHelper gcalHelper;
private final TaskDao taskDao;
private final CalendarEventProvider calendarEventProvider;
@Inject
public TaskDeleter(GCalHelper gcalHelper, TaskDao taskDao) {
this.gcalHelper = gcalHelper;
public TaskDeleter(TaskDao taskDao, CalendarEventProvider calendarEventProvider) {
this.taskDao = taskDao;
this.calendarEventProvider = calendarEventProvider;
}
/**
@ -58,7 +59,7 @@ public class TaskDeleter {
long id = item.getId();
item.clear();
item.setId(id);
gcalHelper.deleteTaskEvent(item);
calendarEventProvider.deleteEvent(item);
item.setDeletionDate(DateUtilities.now());
taskDao.save(item);
}

@ -6,6 +6,10 @@ import android.database.Cursor;
import android.net.Uri;
import android.provider.CalendarContract;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.data.Task;
import org.tasks.injection.ForApplication;
import org.tasks.preferences.PermissionChecker;
@ -31,14 +35,17 @@ public class CalendarEventProvider {
};
private final ContentResolver contentResolver;
private final TaskDao taskDao;
private final PermissionChecker permissionChecker;
private final CalendarEventAttendeeProvider calendarEventAttendeeProvider;
@Inject
public CalendarEventProvider(@ForApplication Context context, PermissionChecker permissionChecker,
CalendarEventAttendeeProvider calendarEventAttendeeProvider) {
CalendarEventAttendeeProvider calendarEventAttendeeProvider,
TaskDao taskDao) {
this.permissionChecker = permissionChecker;
this.calendarEventAttendeeProvider = calendarEventAttendeeProvider;
this.taskDao = taskDao;
contentResolver = context.getContentResolver();
}
@ -55,8 +62,24 @@ public class CalendarEventProvider {
return events.isEmpty() ? null : events.get(0);
}
public boolean hasEvent(Uri eventUri) {
return getEvent(eventUri) != null;
public boolean deleteEvent(Task task) {
if (!task.containsNonNullValue(Task.CALENDAR_URI)) {
task = taskDao.fetch(task.getId(), Task.CALENDAR_URI);
if(task == null) {
return false;
}
}
String uri = task.getCalendarURI();
task.setCalendarUri("");
return deleteEvent(uri);
}
private boolean deleteEvent(String eventUri) {
return !TextUtils.isEmpty(eventUri) && deleteEvent(Uri.parse(eventUri));
}
private boolean deleteEvent(Uri eventUri) {
return getEvent(eventUri) != null && contentResolver.delete(eventUri, null, null) > 0;
}
public List<AndroidCalendarEvent> getEventsBetween(long start, long end) {

@ -26,6 +26,7 @@ import org.tasks.R;
import org.tasks.activities.CalendarSelectionActivity;
import org.tasks.analytics.Tracker;
import org.tasks.calendars.AndroidCalendar;
import org.tasks.calendars.CalendarEventProvider;
import org.tasks.calendars.CalendarProvider;
import org.tasks.dialogs.DialogBuilder;
import org.tasks.injection.ForActivity;
@ -68,6 +69,7 @@ public class CalendarControlSet extends TaskEditControlFragment {
@Inject Tracker tracker;
@Inject DialogBuilder dialogBuilder;
@Inject ThemeBase themeBase;
@Inject CalendarEventProvider calendarEventProvider;
private String calendarId;
private String calendarName;
@ -153,7 +155,7 @@ public class CalendarControlSet extends TaskEditControlFragment {
if (!isNullOrEmpty(task.getCalendarURI())) {
if (eventUri == null) {
gcalHelper.deleteTaskEvent(task);
calendarEventProvider.deleteEvent(task);
} else if (!calendarEntryExists(task.getCalendarURI())) {
task.setCalendarUri("");
}

Loading…
Cancel
Save