Remove query and update from TaskService

pull/467/head
Alex Baker 8 years ago
parent 4beb84fbe2
commit 3fbd9b32c0

@ -2,9 +2,9 @@ package com.todoroo.astrid.subtasks;
import android.support.test.runner.AndroidJUnit4;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.data.TaskListMetadata;
import com.todoroo.astrid.service.TaskService;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -17,7 +17,7 @@ import static junit.framework.Assert.assertEquals;
@RunWith(AndroidJUnit4.class)
public class SubtasksHelperTest extends SubtasksTestCase {
@Inject TaskService taskService;
@Inject TaskDao taskDao;
@Override
public void setUp() {
@ -25,14 +25,14 @@ public class SubtasksHelperTest extends SubtasksTestCase {
createTasks();
TaskListMetadata m = new TaskListMetadata();
m.setFilter(TaskListMetadata.FILTER_ID_ALL);
updater.initializeFromSerializedTree(m, filter, SubtasksHelper.convertTreeToRemoteIds(taskService, DEFAULT_SERIALIZED_TREE));
updater.initializeFromSerializedTree(m, filter, SubtasksHelper.convertTreeToRemoteIds(taskDao, DEFAULT_SERIALIZED_TREE));
}
private void createTask(String title, String uuid) {
Task t = new Task();
t.setTitle(title);
t.setUuid(uuid);
taskService.save(t);
taskDao.save(t);
}
private void createTasks() {
@ -60,7 +60,7 @@ public class SubtasksHelperTest extends SubtasksTestCase {
private static String EXPECTED_REMOTE = "[\"-1\", [\"6\", \"4\", [\"3\", \"1\"]], \"2\", \"5\"]".replaceAll("\\s", "");
@Test
public void testLocalToRemoteIdMapping() {
String mapped = SubtasksHelper.convertTreeToRemoteIds(taskService, DEFAULT_SERIALIZED_TREE).replaceAll("\\s", "");
String mapped = SubtasksHelper.convertTreeToRemoteIds(taskDao, DEFAULT_SERIALIZED_TREE).replaceAll("\\s", "");
assertEquals(EXPECTED_REMOTE, mapped);
}

@ -2,9 +2,9 @@ package com.todoroo.astrid.subtasks;
import android.support.test.runner.AndroidJUnit4;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.data.TaskListMetadata;
import com.todoroo.astrid.service.TaskService;
import org.junit.Ignore;
import org.junit.Test;
@ -15,7 +15,7 @@ import javax.inject.Inject;
@RunWith(AndroidJUnit4.class)
public class SubtasksMovingTest extends SubtasksTestCase {
@Inject TaskService taskService;
@Inject TaskDao taskDao;
private Task A, B, C, D, E, F;
@ -25,7 +25,7 @@ public class SubtasksMovingTest extends SubtasksTestCase {
createTasks();
TaskListMetadata m = new TaskListMetadata();
m.setFilter(TaskListMetadata.FILTER_ID_ALL);
updater.initializeFromSerializedTree(m, filter, SubtasksHelper.convertTreeToRemoteIds(taskService, DEFAULT_SERIALIZED_TREE));
updater.initializeFromSerializedTree(m, filter, SubtasksHelper.convertTreeToRemoteIds(taskDao, DEFAULT_SERIALIZED_TREE));
// Assert initial state is correct
expectParentAndPosition(A, null, 0);
@ -48,7 +48,7 @@ public class SubtasksMovingTest extends SubtasksTestCase {
private Task createTask(String title) {
Task task = new Task();
task.setTitle(title);
taskService.save(task);
taskDao.save(task);
return task;
}

@ -23,6 +23,7 @@ import com.todoroo.andlib.utility.DialogUtilities;
import com.todoroo.astrid.api.AstridApiConstants;
import com.todoroo.astrid.dao.MetadataDao;
import com.todoroo.astrid.dao.TagDataDao;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.data.TagData;
import com.todoroo.astrid.data.Task;
@ -49,6 +50,7 @@ public class TasksXmlImporter {
private final MetadataDao metadataDao;
private final TaskService taskService;
private final DialogBuilder dialogBuilder;
private final TaskDao taskDao;
private Activity activity;
private Handler handler;
@ -65,11 +67,12 @@ public class TasksXmlImporter {
@Inject
public TasksXmlImporter(TagDataDao tagDataDao, MetadataDao metadataDao, TaskService taskService,
DialogBuilder dialogBuilder) {
DialogBuilder dialogBuilder, TaskDao taskDao) {
this.tagDataDao = tagDataDao;
this.metadataDao = metadataDao;
this.taskService = taskService;
this.dialogBuilder = dialogBuilder;
this.taskDao = taskDao;
}
public void importTasks(Activity activity, String input, ProgressDialog progressDialog) {
@ -195,7 +198,7 @@ public class TasksXmlImporter {
// if the task's name and creation date match an existing task, skip
long existingTask = 0;
TodorooCursor<Task> cursor = taskService.query(Query.select(Task.ID,
TodorooCursor<Task> cursor = taskDao.query(Query.select(Task.ID,
Task.COMPLETION_DATE, Task.DELETION_DATE).
where(Criterion.and(Task.TITLE.eq(title), Task.CREATION_DATE.eq(created))));
try {

@ -13,10 +13,10 @@ import com.todoroo.andlib.sql.Query;
import com.todoroo.andlib.utility.DateUtilities;
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 com.todoroo.astrid.service.TaskService;
import org.tasks.R;
import org.tasks.dialogs.DialogBuilder;
@ -30,12 +30,12 @@ import javax.inject.Inject;
public class OldTaskPreferences extends InjectingPreferenceActivity {
@Inject DialogBuilder dialogBuilder;
@Inject TaskService taskService;
@Inject GCalHelper gcalHelper;
@Inject TaskDeleter taskDeleter;
@Inject MetadataDao metadataDao;
@Inject Preferences preferences;
@Inject Database database;
@Inject TaskDao taskDao;
@Override
public void onCreate(Bundle savedInstanceState) {
@ -79,7 +79,7 @@ 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 = taskService.query(Query.select(Task.ID, Task.CALENDAR_URI).where(
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();
@ -93,7 +93,7 @@ public class OldTaskPreferences extends InjectingPreferenceActivity {
}
Task template = new Task();
template.setDeletionDate(DateUtilities.now());
return taskService.update(Task.COMPLETION_DATE.gt(0), template);
return taskDao.update(Task.COMPLETION_DATE.gt(0), template);
}
@Override
@ -110,7 +110,7 @@ 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 = taskService.query(Query.select(Task.ID, Task.TITLE, Task.CALENDAR_URI).where(
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();
@ -143,7 +143,7 @@ public class OldTaskPreferences extends InjectingPreferenceActivity {
@Override
protected Integer doInBackground(Void... params) {
int deletedEventCount = 0;
TodorooCursor<Task> cursor = taskService.query(Query.select(Task.ID, Task.CALENDAR_URI).where(
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();
@ -161,7 +161,7 @@ public class OldTaskPreferences extends InjectingPreferenceActivity {
// since the GCalHelper doesnt save it due to performance-reasons
Task template = new Task();
template.setCalendarUri(""); //$NON-NLS-1$
taskService.update(
taskDao.update(
Criterion.and(Task.COMPLETION_DATE.gt(0), Task.CALENDAR_URI.isNotNull()),
template);
return deletedEventCount;
@ -182,7 +182,7 @@ public class OldTaskPreferences extends InjectingPreferenceActivity {
@Override
protected Integer doInBackground(Void... params) {
int deletedEventCount = 0;
TodorooCursor<Task> cursor = taskService.query(Query.select(Task.ID, Task.CALENDAR_URI).where(
TodorooCursor<Task> cursor = taskDao.query(Query.select(Task.ID, Task.CALENDAR_URI).where(
Task.CALENDAR_URI.isNotNull()));
try {
int length = cursor.getCount();
@ -200,7 +200,7 @@ public class OldTaskPreferences extends InjectingPreferenceActivity {
// since the GCalHelper doesnt save it due to performance-reasons
Task template = new Task();
template.setCalendarUri(""); //$NON-NLS-1$
taskService.update(Task.CALENDAR_URI.isNotNull(), template);
taskDao.update(Task.CALENDAR_URI.isNotNull(), template);
return deletedEventCount;
}

@ -100,6 +100,19 @@ public class TaskDao {
return dao.rawQuery(selection, selectionArgs, id);
}
/**
* Update all matching a clause to have the values set on template object.
* <p>
* Example (updates "joe" => "bob" in metadata value1):
* {code}
* Metadata item = new Metadata();
* item.setVALUE1("bob");
* update(item, Metadata.VALUE1.eq("joe"));
* {code}
* @param where sql criteria
* @param template set fields on this object in order to set them in the db.
* @return # of updated items
*/
public int update(Criterion where, Task template) {
return dao.update(where, template);
}

@ -19,6 +19,7 @@ import com.todoroo.andlib.sql.Criterion;
import com.todoroo.andlib.sql.Query;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.core.SortHelper;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.dao.TaskDao.TaskCriteria;
import com.todoroo.astrid.data.TagData;
import com.todoroo.astrid.data.Task;
@ -85,6 +86,7 @@ public class Astrid2TaskProvider extends InjectingContentProvider {
@Inject Lazy<TaskService> taskService;
@Inject Lazy<TagService> tagService;
@Inject Lazy<CheckBoxes> checkBoxes;
@Inject Lazy<TaskDao> taskDao;
static {
URI_MATCHER.addURI(AUTHORITY, "tasks", URI_TASKS);
@ -172,7 +174,7 @@ public class Astrid2TaskProvider extends InjectingContentProvider {
MatrixCursor ret = new MatrixCursor(TASK_FIELD_LIST);
TodorooCursor<Task> cursor = taskService.get().query(Query.select(Task.ID, Task.TITLE,
TodorooCursor<Task> cursor = taskDao.get().query(Query.select(Task.ID, Task.TITLE,
Task.IMPORTANCE, Task.DUE_DATE).where(Criterion.and(TaskCriteria.isActive(),
TaskCriteria.isVisible())).
orderBy(SortHelper.defaultTaskOrder()).limit(MAX_NUMBER_OF_TASKS));
@ -254,7 +256,7 @@ public class Astrid2TaskProvider extends InjectingContentProvider {
replace(ID, Task.ID.name).
replace(IMPORTANCE, Task.IMPORTANCE.name);
return taskService.get().updateBySelection(criteria, selectionArgs, task);
return updateBySelection(criteria, selectionArgs, task);
case URI_TAGS:
throw new UnsupportedOperationException("tags updating: not yet");
@ -264,6 +266,22 @@ public class Astrid2TaskProvider extends InjectingContentProvider {
}
}
/**
* Update database based on selection and values
*/
private int updateBySelection(String selection, String[] selectionArgs, Task taskValues) {
TodorooCursor<Task> cursor = taskDao.get().rawQuery(selection, selectionArgs, Task.ID);
try {
for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
taskValues.setID(cursor.get(Task.ID));
taskService.get().save(taskValues);
}
return cursor.getCount();
} finally {
cursor.close();
}
}
public static void notifyDatabaseModification(Context context) {
try {
context.getContentResolver().notifyChange(CONTENT_URI, null);

@ -67,13 +67,6 @@ public class TaskService {
// --- service layer
/**
* Query underlying database
*/
public TodorooCursor<Task> query(Query query) {
return taskDao.query(query);
}
/**
* @return item, or null if it doesn't exist
*/
@ -144,40 +137,6 @@ public class TaskService {
return taskDao.query(Query.select(properties).withQueryTemplate(sql));
}
/**
* Update database based on selection and values
*/
public int updateBySelection(String selection, String[] selectionArgs,
Task taskValues) {
TodorooCursor<Task> cursor = taskDao.rawQuery(selection, selectionArgs, Task.ID);
try {
for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
taskValues.setID(cursor.get(Task.ID));
save(taskValues);
}
return cursor.getCount();
} finally {
cursor.close();
}
}
/**
* Update all matching a clause to have the values set on template object.
* <p>
* Example (updates "joe" => "bob" in metadata value1):
* {code}
* Metadata item = new Metadata();
* item.setVALUE1("bob");
* update(item, Metadata.VALUE1.eq("joe"));
* {code}
* @param where sql criteria
* @param template set fields on this object in order to set them in the db.
* @return # of updated items
*/
public int update(Criterion where, Task template) {
return taskDao.update(where, template);
}
/**
* Parse quick add markup for the given task
* @param tags an empty array to apply tags to

@ -21,6 +21,7 @@ import com.todoroo.astrid.activity.TaskListFragment;
import com.todoroo.astrid.adapter.TaskAdapter;
import com.todoroo.astrid.api.Filter;
import com.todoroo.astrid.dao.TaskAttachmentDao;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.data.RemoteModel;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.service.TaskService;
@ -53,6 +54,7 @@ public class AstridOrderedListFragmentHelper<LIST> implements OrderedListFragmen
private final TaskAttachmentDao taskAttachmentDao;
private final TaskService taskService;
private final ThemeCache themeCache;
private final TaskDao taskDao;
private DraggableTaskAdapter taskAdapter;
@ -61,7 +63,8 @@ public class AstridOrderedListFragmentHelper<LIST> implements OrderedListFragmen
public AstridOrderedListFragmentHelper(Preferences preferences, TaskAttachmentDao taskAttachmentDao,
TaskService taskService, TaskListFragment fragment,
AstridOrderedListUpdater<LIST> updater, DialogBuilder dialogBuilder,
CheckBoxes checkBoxes, TagService tagService, ThemeCache themeCache) {
CheckBoxes checkBoxes, TagService tagService,
ThemeCache themeCache, TaskDao taskDao) {
this.preferences = preferences;
this.taskAttachmentDao = taskAttachmentDao;
this.taskService = taskService;
@ -71,6 +74,7 @@ public class AstridOrderedListFragmentHelper<LIST> implements OrderedListFragmen
this.checkBoxes = checkBoxes;
this.tagService = tagService;
this.themeCache = themeCache;
this.taskDao = taskDao;
}
// --- ui component setup
@ -222,7 +226,7 @@ public class AstridOrderedListFragmentHelper<LIST> implements OrderedListFragmen
if(chained != null) {
for(String taskId : chained) {
model.setCompletionDate(completionDate);
taskService.update(Task.UUID.eq(taskId), model);
taskDao.update(Task.UUID.eq(taskId), model);
model.clear();
}
taskAdapter.notifyDataSetInvalidated();
@ -234,14 +238,14 @@ public class AstridOrderedListFragmentHelper<LIST> implements OrderedListFragmen
updater.applyToDescendants(itemId, node -> {
String uuid = node.uuid;
model.setCompletionDate(completionDate);
taskService.update(Task.UUID.eq(uuid), model);
taskDao.update(Task.UUID.eq(uuid), model);
model.clear();
chained.add(node.uuid);
});
if(chained.size() > 0) {
// move recurring items to item parent
TodorooCursor<Task> recurring = taskService.query(Query.select(Task.UUID, Task.RECURRENCE).where(
TodorooCursor<Task> recurring = taskDao.query(Query.select(Task.UUID, Task.RECURRENCE).where(
Criterion.and(Task.UUID.in(chained.toArray(new String[chained.size()])),
Task.RECURRENCE.isNotNull(), Functions.length(Task.RECURRENCE).gt(0))));
try {

@ -10,13 +10,13 @@ import com.todoroo.andlib.sql.Query;
import com.todoroo.astrid.api.Filter;
import com.todoroo.astrid.core.BuiltInFilterExposer;
import com.todoroo.astrid.dao.TagDataDao;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.dao.TaskDao.TaskCriteria;
import com.todoroo.astrid.dao.TaskListMetadataDao;
import com.todoroo.astrid.data.RemoteModel;
import com.todoroo.astrid.data.TagData;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.data.TaskListMetadata;
import com.todoroo.astrid.service.TaskService;
import com.todoroo.astrid.subtasks.AstridOrderedListUpdater.Node;
import org.tasks.R;
@ -35,15 +35,16 @@ public class SubtasksHelper {
private final Context context;
private final Preferences preferences;
private final TaskService taskService;
private final TaskDao taskDao;
private final TagDataDao tagDataDao;
private final TaskListMetadataDao taskListMetadataDao;
@Inject
public SubtasksHelper(@ForApplication Context context, Preferences preferences, TaskService taskService, TagDataDao tagDataDao, TaskListMetadataDao taskListMetadataDao) {
public SubtasksHelper(@ForApplication Context context, Preferences preferences, TaskDao taskDao,
TagDataDao tagDataDao, TaskListMetadataDao taskListMetadataDao) {
this.context = context;
this.preferences = preferences;
this.taskService = taskService;
this.taskDao = taskDao;
this.tagDataDao = tagDataDao;
this.taskListMetadataDao = taskListMetadataDao;
}
@ -86,7 +87,7 @@ public class SubtasksHelper {
if (tlm != null) {
serialized = tlm.getTaskIDs();
} else if (tagData != null) {
serialized = convertTreeToRemoteIds(taskService, tagData.getTagOrdering());
serialized = convertTreeToRemoteIds(taskDao, tagData.getTagOrdering());
} else {
serialized = "[]"; //$NON-NLS-1$
}
@ -124,9 +125,9 @@ public class SubtasksHelper {
/**
* Takes a subtasks string containing local ids and remaps it to one containing UUIDs
*/
public static String convertTreeToRemoteIds(TaskService taskService, String localTree) {
public static String convertTreeToRemoteIds(TaskDao taskDao, String localTree) {
List<Long> localIds = getIdList(localTree);
HashMap<Long, String> idMap = getIdMap(taskService, localIds, Task.ID, Task.UUID);
HashMap<Long, String> idMap = getIdMap(taskDao, localIds, Task.ID, Task.UUID);
idMap.put(-1L, "-1"); //$NON-NLS-1$
Node tree = AstridOrderedListUpdater.buildTreeModel(localTree, null);
@ -167,9 +168,9 @@ public class SubtasksHelper {
});
}
private static <A, B> HashMap<A, B> getIdMap(TaskService taskService, Iterable<A> keys, Property<A> keyProperty, Property<B> valueProperty) {
private static <A, B> HashMap<A, B> getIdMap(TaskDao taskDao, Iterable<A> keys, Property<A> keyProperty, Property<B> valueProperty) {
HashMap<A, B> map = new HashMap<>();
TodorooCursor<Task> tasks = taskService.query(Query.select(keyProperty, valueProperty).where(keyProperty.in(keys)));
TodorooCursor<Task> tasks = taskDao.query(Query.select(keyProperty, valueProperty).where(keyProperty.in(keys)));
try {
for (tasks.moveToFirst(); !tasks.isAfterLast(); tasks.moveToNext()) {
A key = tasks.get(keyProperty);

@ -18,6 +18,7 @@ import com.todoroo.astrid.adapter.TaskAdapter;
import com.todoroo.astrid.api.Filter;
import com.todoroo.astrid.core.BuiltInFilterExposer;
import com.todoroo.astrid.dao.TaskAttachmentDao;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.dao.TaskListMetadataDao;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.data.TaskListMetadata;
@ -65,6 +66,7 @@ public class SubtasksListFragment extends TaskListFragment {
@Inject TagService tagService;
@Inject ThemeCache themeCache;
@Inject Theme theme;
@Inject TaskDao taskDao;
@Override
public void onAttach(Activity activity) {
@ -75,7 +77,7 @@ public class SubtasksListFragment extends TaskListFragment {
protected OrderedListFragmentHelperInterface createFragmentHelper() {
return new AstridOrderedListFragmentHelper<>(preferences, taskAttachmentDao, taskService,
this, subtasksFilterUpdater, dialogBuilder, checkBoxes, tagService, themeCache);
this, subtasksFilterUpdater, dialogBuilder, checkBoxes, tagService, themeCache, taskDao);
}
@Override
@ -119,7 +121,7 @@ public class SubtasksListFragment extends TaskListFragment {
if (TextUtils.isEmpty(defaultOrder)) {
defaultOrder = "[]"; //$NON-NLS-1$
}
defaultOrder = SubtasksHelper.convertTreeToRemoteIds(taskService, defaultOrder);
defaultOrder = SubtasksHelper.convertTreeToRemoteIds(taskDao, defaultOrder);
taskListMetadata = new TaskListMetadata();
taskListMetadata.setFilter(filterId);
taskListMetadata.setTaskIDs(defaultOrder);

@ -16,6 +16,7 @@ import com.todoroo.astrid.activity.TaskListFragment;
import com.todoroo.astrid.adapter.TaskAdapter;
import com.todoroo.astrid.api.TagFilter;
import com.todoroo.astrid.dao.TaskAttachmentDao;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.dao.TaskListMetadataDao;
import com.todoroo.astrid.data.RemoteModel;
import com.todoroo.astrid.data.TagData;
@ -55,6 +56,7 @@ public class SubtasksTagListFragment extends TagViewFragment {
@Inject TagService tagService;
@Inject ThemeCache themeCache;
@Inject Theme theme;
@Inject TaskDao taskDao;
private AstridOrderedListFragmentHelper<TaskListMetadata> helper;
@ -65,7 +67,7 @@ public class SubtasksTagListFragment extends TagViewFragment {
super.onAttach(activity);
helper = new AstridOrderedListFragmentHelper<>(preferences, taskAttachmentDao, taskService,
this, subtasksFilterUpdater, dialogBuilder, checkBoxes, tagService, themeCache);
this, subtasksFilterUpdater, dialogBuilder, checkBoxes, tagService, themeCache, taskDao);
}
@Override

Loading…
Cancel
Save