From ccfbd8f79b33add86f7f2c0e1a5e7a2a9bceb36a Mon Sep 17 00:00:00 2001 From: Arne Jans Date: Thu, 25 Aug 2011 11:15:40 +0200 Subject: [PATCH] Extracted deleteTaskEvent to its own Helper-class to keep UI-classes clean. --- .../astrid/core/OldTaskPreferences.java | 6 +-- .../todoroo/astrid/gcal/GCalControlSet.java | 30 -------------- .../com/todoroo/astrid/gcal/GCalHelper.java | 40 +++++++++++++++++++ .../todoroo/astrid/service/TaskService.java | 6 +-- 4 files changed, 46 insertions(+), 36 deletions(-) create mode 100644 astrid/plugin-src/com/todoroo/astrid/gcal/GCalHelper.java diff --git a/astrid/plugin-src/com/todoroo/astrid/core/OldTaskPreferences.java b/astrid/plugin-src/com/todoroo/astrid/core/OldTaskPreferences.java index 48d92c52b..ae861984d 100644 --- a/astrid/plugin-src/com/todoroo/astrid/core/OldTaskPreferences.java +++ b/astrid/plugin-src/com/todoroo/astrid/core/OldTaskPreferences.java @@ -21,7 +21,7 @@ import com.todoroo.andlib.utility.DateUtilities; import com.todoroo.andlib.utility.DialogUtilities; import com.todoroo.andlib.utility.TodorooPreferenceActivity; import com.todoroo.astrid.data.Task; -import com.todoroo.astrid.gcal.GCalControlSet; +import com.todoroo.astrid.gcal.GCalHelper; import com.todoroo.astrid.service.MetadataService; import com.todoroo.astrid.service.TaskService; @@ -85,7 +85,7 @@ public class OldTaskPreferences extends TodorooPreferenceActivity { for(int i = 0; i < length; i++) { cursor.moveToNext(); task.readFromCursor(cursor); - GCalControlSet.deleteTaskEvent(task); + GCalHelper.deleteTaskEvent(task); } } finally { cursor.close(); @@ -124,7 +124,7 @@ public class OldTaskPreferences extends TodorooPreferenceActivity { for(int i = 0; i < length; i++) { cursor.moveToNext(); task.readFromCursor(cursor); - GCalControlSet.deleteTaskEvent(task); + GCalHelper.deleteTaskEvent(task); } } finally { cursor.close(); diff --git a/astrid/plugin-src/com/todoroo/astrid/gcal/GCalControlSet.java b/astrid/plugin-src/com/todoroo/astrid/gcal/GCalControlSet.java index e7cbe0ec3..8f510283f 100644 --- a/astrid/plugin-src/com/todoroo/astrid/gcal/GCalControlSet.java +++ b/astrid/plugin-src/com/todoroo/astrid/gcal/GCalControlSet.java @@ -22,13 +22,11 @@ import android.widget.Toast; import com.timsu.astrid.R; import com.todoroo.andlib.service.Autowired; -import com.todoroo.andlib.service.ContextManager; import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.andlib.service.ExceptionService; import com.todoroo.andlib.utility.DateUtilities; import com.todoroo.andlib.utility.Preferences; import com.todoroo.astrid.activity.TaskEditActivity.TaskEditControlSet; -import com.todoroo.astrid.core.PluginServices; import com.todoroo.astrid.data.Task; import com.todoroo.astrid.gcal.Calendars.CalendarResult; import com.todoroo.astrid.service.StatisticsService; @@ -118,34 +116,6 @@ public class GCalControlSet implements TaskEditControlSet { }); } - public static void deleteTaskEvent(Task task) { - String uri = task.getValue(Task.CALENDAR_URI); - Uri calendarUri = null; - - if (TextUtils.isEmpty(uri)) { - task = PluginServices.getTaskService().fetchById(task.getId(), Task.ID, Task.CALENDAR_URI); - uri = task.getValue(Task.CALENDAR_URI); - } - - if(!TextUtils.isEmpty(uri)) { - try { - calendarUri = Uri.parse(uri); - - // try to load calendar - ContentResolver cr = ContextManager.getContext().getContentResolver(); - Cursor cursor = cr.query(calendarUri, new String[] { "dtstart" }, null, null, null); //$NON-NLS-1$ - boolean deleted = cursor.getCount() == 0; - cursor.close(); - - if (!deleted) { - cr.delete(calendarUri, null, null); - } - } catch (Exception e) { - e.printStackTrace(); - } - } - } - @Override public void readFromTask(Task task) { this.myTask = task; diff --git a/astrid/plugin-src/com/todoroo/astrid/gcal/GCalHelper.java b/astrid/plugin-src/com/todoroo/astrid/gcal/GCalHelper.java new file mode 100644 index 000000000..04f663abb --- /dev/null +++ b/astrid/plugin-src/com/todoroo/astrid/gcal/GCalHelper.java @@ -0,0 +1,40 @@ +package com.todoroo.astrid.gcal; + +import android.content.ContentResolver; +import android.database.Cursor; +import android.net.Uri; +import android.text.TextUtils; + +import com.todoroo.andlib.service.ContextManager; +import com.todoroo.astrid.core.PluginServices; +import com.todoroo.astrid.data.Task; + +public class GCalHelper { + public static void deleteTaskEvent(Task task) { + String uri = task.getValue(Task.CALENDAR_URI); + Uri calendarUri = null; + + if (TextUtils.isEmpty(uri)) { + task = PluginServices.getTaskService().fetchById(task.getId(), Task.ID, Task.CALENDAR_URI); + uri = task.getValue(Task.CALENDAR_URI); + } + + if(!TextUtils.isEmpty(uri)) { + try { + calendarUri = Uri.parse(uri); + + // try to load calendar + ContentResolver cr = ContextManager.getContext().getContentResolver(); + Cursor cursor = cr.query(calendarUri, new String[] { "dtstart" }, null, null, null); //$NON-NLS-1$ + boolean deleted = cursor.getCount() == 0; + cursor.close(); + + if (!deleted) { + cr.delete(calendarUri, null, null); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } +} diff --git a/astrid/src/com/todoroo/astrid/service/TaskService.java b/astrid/src/com/todoroo/astrid/service/TaskService.java index 4ea562124..dd0e0e648 100644 --- a/astrid/src/com/todoroo/astrid/service/TaskService.java +++ b/astrid/src/com/todoroo/astrid/service/TaskService.java @@ -24,7 +24,7 @@ import com.todoroo.astrid.dao.TaskDao; import com.todoroo.astrid.dao.TaskDao.TaskCriteria; import com.todoroo.astrid.data.Metadata; import com.todoroo.astrid.data.Task; -import com.todoroo.astrid.gcal.GCalControlSet; +import com.todoroo.astrid.gcal.GCalHelper; import com.todoroo.astrid.gtasks.GtasksMetadata; import com.todoroo.astrid.opencrx.OpencrxCoreUtils; import com.todoroo.astrid.producteev.ProducteevUtilities; @@ -151,14 +151,14 @@ public class TaskService { if(!item.isSaved()) return; else if(item.containsValue(Task.TITLE) && item.getValue(Task.TITLE).length() == 0) { - GCalControlSet.deleteTaskEvent(item); + GCalHelper.deleteTaskEvent(item); taskDao.delete(item.getId()); item.setId(Task.NO_ID); } else { long id = item.getId(); item.clear(); item.setId(id); - GCalControlSet.deleteTaskEvent(item); + GCalHelper.deleteTaskEvent(item); item.setValue(Task.DELETION_DATE, DateUtilities.now()); taskDao.save(item); }