Move broadcast of task list changes into TaskDao, since otherwise TaskApiDao updates trigger multiple change broadcasts

pull/14/head
Tim Su 12 years ago
parent 8a77fb843d
commit 0f9e77d69c

@ -7,17 +7,14 @@ package com.todoroo.astrid.data;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.SQLException;
import com.todoroo.andlib.data.ContentResolverDao;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.sql.Criterion;
import com.todoroo.andlib.sql.Field;
import com.todoroo.andlib.sql.Functions;
import com.todoroo.andlib.sql.Query;
import com.todoroo.astrid.api.AstridApiConstants;
import com.todoroo.astrid.api.PermaSql;
/**
@ -148,23 +145,15 @@ public class TaskApiDao extends ContentResolverDao<Task> {
@Override
public boolean save(Task model) {
ContentValues setValues = model.getSetValues();
try {
if(super.save(model)) {
afterSave(model, setValues);
return true;
}
return super.save(model);
} catch (SQLException e) {
if (model.containsNonNullValue(Task.REMOTE_ID)) {
TodorooCursor<Task> cursor = query(Query.select(Task.ID).where(Task.REMOTE_ID.eq(model.getValue(Task.REMOTE_ID))));
if (cursor.getCount() > 0) {
cursor.moveToFirst();
model.setId(cursor.get(Task.ID));
setValues = model.getSetValues();
if (super.save(model)) {
afterSave(model, setValues);
return true;
}
return super.save(model);
}
}
}
@ -195,37 +184,4 @@ public class TaskApiDao extends ContentResolverDao<Task> {
return false;
}
/**
* Send broadcasts on task change (triggers things like task repeats)
* @param task task that was saved
* @param values values that were updated
*/
public static void afterSave(Task task, ContentValues values) {
if(insignificantChange(values))
return;
if(values.containsKey(Task.COMPLETION_DATE.name) && task.isCompleted()) {
Context context = ContextManager.getContext();
if(context != null) {
Intent broadcastIntent;
broadcastIntent = new Intent(AstridApiConstants.BROADCAST_EVENT_TASK_COMPLETED);
broadcastIntent.putExtra(AstridApiConstants.EXTRAS_TASK_ID, task.getId());
context.sendOrderedBroadcast(broadcastIntent, null);
}
}
afterTaskListChanged();
}
/**
* Send broadcast when task list changes. Widgets should update.
*/
public static void afterTaskListChanged() {
Context context = ContextManager.getContext();
if(context != null) {
Intent broadcastIntent = new Intent(AstridApiConstants.BROADCAST_EVENT_TASK_LIST_UPDATED);
context.sendOrderedBroadcast(broadcastIntent, null);
}
}
}

@ -6,13 +6,17 @@
package com.todoroo.astrid.dao;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteConstraintException;
import android.util.Log;
import com.timsu.astrid.R;
import com.todoroo.andlib.data.DatabaseDao;
import com.todoroo.andlib.data.Property;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.service.DependencyInjectionService;
import com.todoroo.andlib.sql.Criterion;
import com.todoroo.andlib.sql.Field;
@ -20,6 +24,7 @@ import com.todoroo.andlib.sql.Functions;
import com.todoroo.andlib.sql.Query;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.api.AstridApiConstants;
import com.todoroo.astrid.dao.MetadataDao.MetadataCriteria;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.data.TaskApiDao;
@ -164,7 +169,7 @@ public class TaskDao extends DatabaseDao<Task> {
// delete all metadata
metadataDao.deleteWhere(MetadataCriteria.byTask(id));
TaskApiDao.afterTaskListChanged();
broadcastTaskChanged();
return true;
}
@ -390,7 +395,40 @@ public class TaskDao extends DatabaseDao<Task> {
}
// run api save hooks
TaskApiDao.afterSave(task, values);
broadcastTaskSave(task, values);
}
/**
* Send broadcasts on task change (triggers things like task repeats)
* @param task task that was saved
* @param values values that were updated
*/
public static void broadcastTaskSave(Task task, ContentValues values) {
if(TaskApiDao.insignificantChange(values))
return;
if(values.containsKey(Task.COMPLETION_DATE.name) && task.isCompleted()) {
Context context = ContextManager.getContext();
if(context != null) {
Intent broadcastIntent;
broadcastIntent = new Intent(AstridApiConstants.BROADCAST_EVENT_TASK_COMPLETED);
broadcastIntent.putExtra(AstridApiConstants.EXTRAS_TASK_ID, task.getId());
context.sendOrderedBroadcast(broadcastIntent, null);
}
}
broadcastTaskChanged();
}
/**
* Send broadcast when task list changes. Widgets should update.
*/
public static void broadcastTaskChanged() {
Context context = ContextManager.getContext();
if(context != null) {
Intent broadcastIntent = new Intent(AstridApiConstants.BROADCAST_EVENT_TASK_LIST_UPDATED);
context.sendOrderedBroadcast(broadcastIntent, null);
}
}
/**

Loading…
Cancel
Save