Permalink
Browse files

Added an update URI for provider for PureCalendar

  • Loading branch information...
1 parent 9d21f7b commit fcb28affc68f0320db5d2747eaa05ca92839d6b9 @timsu timsu committed May 29, 2010
@@ -118,6 +118,14 @@ public Cursor getActiveTaskListCursor() {
null, null);
}
+ /** Return a list of all of the tasks matching selection */
+ public Cursor getMatchingTasksForProvider(String selection,
+ String[] selectionArgs) {
+ return database.query(tasksTable, TaskModelForProvider.FIELD_LIST,
+ selection, selectionArgs, null, null,
+ null, null);
+ }
+
/** Return a list of all tasks */
public Cursor getAllTaskListCursor() {
return database.query(tasksTable, TaskModelForList.FIELD_LIST,
@@ -593,7 +601,7 @@ public void updateAlarmForTask(TaskIdentifier taskId) throws SQLException {
public ArrayList<TaskModelForProvider> getTasksForProvider(String limit) {
- Cursor cursor = database.query(tasksTable, TaskModelForWidget.FIELD_LIST,
+ Cursor cursor = database.query(tasksTable, TaskModelForProvider.FIELD_LIST,
AbstractTaskModel.PROGRESS_PERCENTAGE + " < " +
AbstractTaskModel.COMPLETE_PERCENTAGE + " AND (" +
AbstractTaskModel.HIDDEN_UNTIL + " ISNULL OR " + AbstractTaskModel.HIDDEN_UNTIL + " < " +
@@ -21,6 +21,7 @@
import java.util.Date;
+import android.content.ContentValues;
import android.database.Cursor;
import com.timsu.astrid.data.AbstractController;
@@ -70,4 +71,8 @@ public Date getPreferredDueDate() {
public Date getDefiniteDueDate() {
return super.getDefiniteDueDate();
}
+
+ public void update(ContentValues newValues) {
+ setValues.putAll(newValues);
+ }
}
@@ -46,6 +46,9 @@
private final static String IMPORTANCE = "importance";
private final static String ID = "id";
+ // fake property for updatu=ing that completes a task
+ private final static String COMPLETED = "completed";
+
private final static String TAGS_ID = "tags_id";
static String[] TASK_FIELD_LIST = new String[] { NAME, IMPORTANCE_COLOR, PREFERRED_DUE_DATE, DEFINITE_DUE_DATE,
@@ -198,7 +201,37 @@ public int update(Uri uri, ContentValues values, String selection, String[] sele
if (LOGD)
Log.d(TAG, "update");
- return 0;
+ switch (URI_MATCHER.match(uri)) {
+
+ case URI_TASKS:
+ int updated = 0;
+
+ // handle the "completed" value separately
+ if(values.containsKey(COMPLETED)) {
+ boolean completed = values.getAsBoolean(COMPLETED);
+ values.remove(COMPLETED);
+ values.put(TaskModelForProvider.PROGRESS_PERCENTAGE,
+ completed ? TaskModelForProvider.COMPLETE_PERCENTAGE : 0);
+ }
+
+ TaskController taskController = new TaskController(ctx);
+ taskController.open();
+ Cursor c = taskController.getMatchingTasksForProvider(selection, selectionArgs);
+ for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
+ TaskModelForProvider model = new TaskModelForProvider(c);
+ model.update(values);
+ taskController.saveTask(model, false);
+ updated++;
+ }
+ taskController.close();
+ return updated;
+
+ case URI_TAGS:
+ throw new UnsupportedOperationException("tags updating: not yet");
+
+ default:
+ throw new IllegalStateException("Unrecognized URI:" + uri);
+ }
}
public static void notifyDatabaseModification() {
@@ -207,7 +240,6 @@ public static void notifyDatabaseModification() {
Log.d(TAG, "notifyDatabaseModification");
ctx.getContentResolver().notifyChange(CONTENT_URI, null);
-
}
}
@@ -1,21 +1,98 @@
package com.todoroo.astrid.provider;
import android.content.ContentResolver;
+import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
+import com.timsu.astrid.data.task.TaskController;
+import com.timsu.astrid.data.task.TaskModelForEdit;
+import com.timsu.astrid.data.task.TaskModelForList;
import com.timsu.astrid.provider.TasksProvider;
-import com.todoroo.andlib.test.TodorooTestCase;
+import com.todoroo.astrid.test.DatabaseTestCase;
-public class TaskProviderTests extends TodorooTestCase {
+public class TaskProviderTests extends DatabaseTestCase {
/** test that we can start things up and query for tasks */
public void testSimpleTaskQuery() {
+ // create some stuff
+ TaskController tc = new TaskController(getContext());
+ tc.open();
+ TaskModelForEdit task = new TaskModelForEdit();
+ task.setName("abc");
+ tc.saveTask(task, false);
+ task = new TaskModelForEdit();
+ task.setName("def");
+ tc.saveTask(task, false);
+ assertEquals(2, tc.getAllTaskIdentifiers().size());
+
+ // query it
+ Uri tasks = Uri.withAppendedPath(TasksProvider.CONTENT_URI, "tasks");
ContentResolver resolver = getContext().getContentResolver();
+ Cursor c = resolver.query(tasks, null, null, null, null);
+ assertEquals(2, c.getCount());
+ c.moveToFirst();
+ assertEquals("abc", c.getString(0));
+ c.moveToNext();
+ assertEquals("def", c.getString(0));
+
+ // complete one
+ Cursor c2 = tc.getAllTaskListCursor();
+ c2.moveToFirst();
+ TaskModelForList listTask = new TaskModelForList(c2);
+ listTask.setProgressPercentage(TaskModelForList.COMPLETE_PERCENTAGE);
+ tc.saveTask(listTask, false);
+ tc.close();
+
+ // should be gone
+ c = resolver.query(tasks, new String[] { "name" }, null, null, null);
+ assertEquals(1, c.getCount());
+ }
+
+ /** test that we can update a task */
+ public void testSimpleTaskUpdate() {
+ // create some stuff
+ TaskController tc = new TaskController(getContext());
+ tc.open();
+ TaskModelForEdit task1 = new TaskModelForEdit();
+ task1.setName("solve world peace");
+ tc.saveTask(task1, false);
+ TaskModelForEdit task2 = new TaskModelForEdit();
+ task2.setName("solve P = NP");
+ tc.saveTask(task2, false);
+ assertEquals(2, tc.getAllTaskIdentifiers().size());
+ tc.close();
+ // query provider
Uri tasks = Uri.withAppendedPath(TasksProvider.CONTENT_URI, "tasks");
- Cursor c = resolver.query(tasks, new String[] { "name" }, null, null, null);
- assertTrue(c.getCount() >= 0);
+ ContentResolver resolver = getContext().getContentResolver();
+ Cursor c = resolver.query(tasks, null, null, null, null);
+ assertEquals(2, c.getCount());
+ c.moveToFirst();
+ assertEquals(task1.getName(), c.getString(0));
+ long id = c.getLong(5);
+ assertEquals(task1.getTaskIdentifier().getId(), id);
+ c.moveToNext();
+ assertEquals(task2.getName(), c.getString(0));
+
+ // complete first task
+ ContentValues values = new ContentValues();
+ values.put("completed", true);
+ int result = resolver.update(tasks, values, "_id = " + id, null);
+
+ // query
+ c = resolver.query(tasks, null, null, null, null);
+ assertEquals(1, c.getCount());
+ c.moveToFirst();
+ assertEquals(task2.getName(), c.getString(0));
+
+ // uncomplete
+ values.put("completed", false);
+ resolver.update(tasks, values, "_id = ?", new String[] { Long.toString(id) });
+ assertEquals(1, result);
+ c = resolver.query(tasks, null, null, null, null);
+ assertEquals(2, c.getCount());
+
}
}

0 comments on commit fcb28af

Please sign in to comment.