Move fetchFiltered to TaskAdapter

pull/467/head
Alex Baker 8 years ago
parent a44368123d
commit 10f5f3c1f4

@ -36,9 +36,13 @@ import com.todoroo.andlib.data.Property.IntegerProperty;
import com.todoroo.andlib.data.Property.LongProperty;
import com.todoroo.andlib.data.Property.StringProperty;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.sql.Criterion;
import com.todoroo.andlib.sql.Functions;
import com.todoroo.andlib.sql.Query;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.Pair;
import com.todoroo.astrid.activity.TaskListFragment;
import com.todoroo.astrid.api.PermaSql;
import com.todoroo.astrid.api.TaskAction;
import com.todoroo.astrid.core.LinkActionExposer;
import com.todoroo.astrid.dao.TaskAttachmentDao;
@ -203,7 +207,43 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
return getFilterQueryProvider().runQuery(constraint);
}
return taskDao.fetchFiltered(query.get(), constraint, fragment.taskProperties());
return fetchFiltered(query.get(), constraint, fragment.taskProperties());
}
/**
* Fetch tasks for the given filter
* @param constraint text constraint, or null
*/
private TodorooCursor<Task> fetchFiltered(String queryTemplate, CharSequence constraint,
Property<?>... properties) {
Criterion whereConstraint = null;
if(constraint != null) {
whereConstraint = Functions.upper(Task.TITLE).like("%" +
constraint.toString().toUpperCase() + "%");
}
if(queryTemplate == null) {
if(whereConstraint == null) {
return taskDao.query(Query.selectDistinct(properties));
} else {
return taskDao.query(Query.selectDistinct(properties).where(whereConstraint));
}
}
String sql;
if(whereConstraint != null) {
if(!queryTemplate.toUpperCase().contains("WHERE")) {
sql = queryTemplate + " WHERE " + whereConstraint;
} else {
sql = queryTemplate.replace("WHERE ", "WHERE " + whereConstraint + " AND ");
}
} else {
sql = queryTemplate;
}
sql = PermaSql.replacePlaceholders(sql);
return taskDao.query(Query.select(properties).withQueryTemplate(sql));
}
/* ======================================================================

@ -455,38 +455,12 @@ public class TaskDao {
save(item);
}
/**
* Fetch tasks for the given filter
* @param constraint text constraint, or null
*/
public TodorooCursor<Task> fetchFiltered(String queryTemplate, CharSequence constraint,
Property<?>... properties) {
Criterion whereConstraint = null;
if(constraint != null) {
whereConstraint = Functions.upper(Task.TITLE).like("%" +
constraint.toString().toUpperCase() + "%");
}
public TodorooCursor<Task> fetchFiltered(String queryTemplate, Property<?>... properties) {
if(queryTemplate == null) {
if(whereConstraint == null) {
return query(Query.selectDistinct(properties));
} else {
return query(Query.selectDistinct(properties).where(whereConstraint));
}
}
String sql;
if(whereConstraint != null) {
if(!queryTemplate.toUpperCase().contains("WHERE")) {
sql = queryTemplate + " WHERE " + whereConstraint;
} else {
sql = queryTemplate.replace("WHERE ", "WHERE " + whereConstraint + " AND ");
}
} else {
sql = queryTemplate;
return query(Query.selectDistinct(properties));
}
sql = PermaSql.replacePlaceholders(sql);
String sql = PermaSql.replacePlaceholders(queryTemplate);
return query(Query.select(properties).withQueryTemplate(sql));
}

@ -79,7 +79,7 @@ public abstract class AstridOrderedListUpdater<LIST> {
Set<String> idsInQuery = new HashSet<>();
String sql = filter.getSqlQuery().replaceAll("ORDER BY .*", ""); //$NON-NLS-1$//$NON-NLS-2$
sql = sql + String.format(" ORDER BY %s", Task.CREATION_DATE); //$NON-NLS-1$
TodorooCursor<Task> tasks = taskDao.fetchFiltered(sql, null, Task.UUID);
TodorooCursor<Task> tasks = taskDao.fetchFiltered(sql, Task.UUID);
try {
for (tasks.moveToFirst(); !tasks.isAfterLast(); tasks.moveToNext()) {
String id = tasks.getString(0);

@ -147,7 +147,7 @@ public class Notifier {
TodorooCursor<Task> taskTodorooCursor = null;
int count;
try {
taskTodorooCursor = taskDao.fetchFiltered(query, null, Task.ID);
taskTodorooCursor = taskDao.fetchFiltered(query, Task.ID);
if (taskTodorooCursor == null) {
return;
}

@ -78,7 +78,7 @@ public class TaskListDataProvider {
// Peform query
try {
return taskDao.fetchFiltered(sqlQueryTemplate.get(), null, properties);
return taskDao.fetchFiltered(sqlQueryTemplate.get(), properties);
} catch (SQLiteException e) {
// We don't show this error anymore--seems like this can get triggered
// by a strange bug, but there seems to not be any negative side effect.

@ -200,7 +200,7 @@ class ScrollableViewsFactory implements RemoteViewsService.RemoteViewsFactory {
private TodorooCursor<Task> getCursor() {
String query = getQuery();
return taskDao.fetchFiltered(query, null, Task.ID, Task.TITLE, Task.DUE_DATE, Task.COMPLETION_DATE, Task.IMPORTANCE, Task.RECURRENCE);
return taskDao.fetchFiltered(query, Task.ID, Task.TITLE, Task.DUE_DATE, Task.COMPLETION_DATE, Task.IMPORTANCE, Task.RECURRENCE);
}
private Task getTask(int position) {

Loading…
Cancel
Save