Hide StoreObject cursors

pull/189/head
Alex Baker 10 years ago
parent bb8e746ff7
commit 0cdff3a9bb

@ -175,7 +175,7 @@ public abstract class AbstractModel implements Parcelable, Cloneable {
/**
* Reads all properties from the supplied cursor and store
*/
private void readPropertiesFromCursor(TodorooCursor<? extends AbstractModel> cursor) {
void readPropertiesFromCursor(TodorooCursor<? extends AbstractModel> cursor) {
if (values == null) {
values = new ContentValues();
}

@ -102,15 +102,33 @@ public class DatabaseDao<TYPE extends AbstractModel> {
TodorooCursor<TYPE> cursor = query(query);
try {
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
callback.apply(cursorConstructor.newInstance(cursor));
callback.apply(fromCursor(cursor));
}
} catch (InvocationTargetException | InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
} finally {
cursor.close();
}
}
public TYPE getFirst(Query query) {
TodorooCursor<TYPE> cursor = query(query);
try {
return cursor.moveToFirst() ? fromCursor(cursor) : null;
} finally {
cursor.close();
}
}
private TYPE fromCursor(TodorooCursor<TYPE> cursor) {
TYPE instance;
try {
instance = modelClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
instance.readPropertiesFromCursor(cursor);
return instance;
}
/**
* Construct a query with SQL DSL objects
*/

@ -79,6 +79,7 @@ public class StoreObject extends AbstractModel {
super();
}
@Deprecated
public StoreObject(TodorooCursor<StoreObject> cursor) {
super(cursor);
}

@ -11,10 +11,9 @@ import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.data.Callback;
import com.todoroo.andlib.sql.Criterion;
import com.todoroo.andlib.sql.Order;
import com.todoroo.andlib.sql.Query;
import com.todoroo.andlib.sql.QueryTemplate;
import com.todoroo.andlib.utility.DialogUtilities;
import com.todoroo.astrid.activity.FilterListFragment;
@ -34,6 +33,7 @@ import org.tasks.injection.Injector;
import org.tasks.preferences.Preferences;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
@ -70,43 +70,36 @@ public final class CustomFilterExposer extends InjectingBroadcastReceiver implem
return buildSavedFilters(context, r);
}
private Filter[] buildSavedFilters(Context context, Resources r) {
TodorooCursor<StoreObject> cursor = storeObjectDao.query(Query.select(StoreObject.PROPERTIES).where(
StoreObject.TYPE.eq(SavedFilter.TYPE)).orderBy(Order.asc(SavedFilter.NAME)));
try {
ArrayList<Filter> list = new ArrayList<>();
// stock filters
if (preferences.getBoolean(R.string.p_show_recently_modified_filter, true)) {
Filter recent = new Filter(r.getString(R.string.BFE_Recent),
r.getString(R.string.BFE_Recent),
new QueryTemplate().where(
Criterion.all).orderBy(
Order.desc(Task.MODIFICATION_DATE)).limit(15),
null);
list.add(recent);
}
private Filter[] buildSavedFilters(final Context context, Resources r) {
final List<Filter> list = new ArrayList<>();
// stock filters
if (preferences.getBoolean(R.string.p_show_recently_modified_filter, true)) {
Filter recent = new Filter(r.getString(R.string.BFE_Recent),
r.getString(R.string.BFE_Recent),
new QueryTemplate().where(
Criterion.all).orderBy(
Order.desc(Task.MODIFICATION_DATE)).limit(15),
null);
list.add(recent);
}
if (cursor != null) {
for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
StoreObject savedFilter = new StoreObject(cursor);
Filter f = SavedFilter.load(savedFilter);
Intent deleteIntent = new Intent(context, DeleteActivity.class);
deleteIntent.putExtra(TOKEN_FILTER_ID, savedFilter.getId());
deleteIntent.putExtra(TOKEN_FILTER_NAME, f.title);
f.contextMenuLabels = new String[] { context.getString(R.string.BFE_Saved_delete) };
f.contextMenuIntents = new Intent[] { deleteIntent };
list.add(f);
}
}
storeObjectDao.getSavedFilters(new Callback<StoreObject>() {
@Override
public void apply(StoreObject savedFilter) {
Filter f = SavedFilter.load(savedFilter);
Intent deleteIntent = new Intent(context, DeleteActivity.class);
deleteIntent.putExtra(TOKEN_FILTER_ID, savedFilter.getId());
deleteIntent.putExtra(TOKEN_FILTER_NAME, f.title);
f.contextMenuLabels = new String[] { context.getString(R.string.BFE_Saved_delete) };
f.contextMenuIntents = new Intent[] { deleteIntent };
list.add(f);
return list.toArray(new Filter[list.size()]);
} finally {
if (cursor != null) {
cursor.close();
}
}
});
return list.toArray(new Filter[list.size()]);
}
/**

@ -9,8 +9,6 @@ import android.content.ContentValues;
import android.text.TextUtils;
import com.todoroo.andlib.data.Property.StringProperty;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.sql.Query;
import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.astrid.api.Filter;
import com.todoroo.astrid.core.CustomFilterActivity.CriterionInstance;
@ -57,17 +55,9 @@ public class SavedFilter {
}
// if filter of this name exists, edit it
StoreObject storeObject;
TodorooCursor<StoreObject> cursor = dao.query(Query.select(StoreObject.ID).where(NAME.eq(title)));
try {
if (cursor.isAfterLast()) {
storeObject = new StoreObject();
} else {
cursor.moveToNext();
storeObject = new StoreObject(cursor);
}
} finally {
cursor.close();
StoreObject storeObject = dao.getSavedFilterByName(title);
if (storeObject == null) {
storeObject = new StoreObject();
}
// populate saved filter properties

@ -5,40 +5,46 @@
*/
package com.todoroo.astrid.dao;
import com.todoroo.andlib.data.Callback;
import com.todoroo.andlib.data.DatabaseDao;
import com.todoroo.andlib.sql.Criterion;
import com.todoroo.andlib.sql.Order;
import com.todoroo.astrid.core.SavedFilter;
import com.todoroo.astrid.data.StoreObject;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Singleton;
/**
* Data Access layer for {@link StoreObject}-related operations.
*
* @author Tim Su <tim@todoroo.com>
*
*/
import static com.todoroo.andlib.sql.Criterion.and;
import static com.todoroo.andlib.sql.Query.select;
@Singleton
public class StoreObjectDao extends DatabaseDao<StoreObject> {
private static Criterion isSavedFilter = StoreObject.TYPE.eq(SavedFilter.TYPE);
@Inject
public StoreObjectDao(Database database) {
public StoreObjectDao(Database database) {
super(StoreObject.class);
setDatabase(database);
}
// --- SQL clause generators
/**
* Generates SQL clauses
*/
public static class StoreObjectCriteria {
public void getSavedFilters(Callback<StoreObject> callback) {
query(callback, select(StoreObject.PROPERTIES)
.where(isSavedFilter)
.orderBy(Order.asc(SavedFilter.NAME)));
}
/** Returns all store objects with given type */
public static Criterion byType(String type) {
return StoreObject.TYPE.eq(type);
}
public List<StoreObject> getByType(String type) {
return toList(select(StoreObject.PROPERTIES)
.where(StoreObject.TYPE.eq(type)));
}
public StoreObject getSavedFilterByName(String title) {
return getFirst(select(StoreObject.ID)
.where(and(isSavedFilter, SavedFilter.NAME.eq(title))));
}
}

@ -28,6 +28,8 @@ import com.todoroo.astrid.data.Task;
import org.tasks.R;
import org.tasks.injection.InjectingBroadcastReceiver;
import java.util.List;
import javax.inject.Inject;
public class GtasksCustomFilterCriteriaExposer extends InjectingBroadcastReceiver {
@ -51,14 +53,14 @@ public class GtasksCustomFilterCriteriaExposer extends InjectingBroadcastReceive
Resources r = context.getResources();
StoreObject[] lists = gtasksListService.getLists();
List<StoreObject> lists = gtasksListService.getLists();
CustomFilterCriterion[] result = new CustomFilterCriterion[1];
String[] listNames = new String[lists.length];
String[] listIds = new String[lists.length];
for (int i = 0; i < lists.length; i++) {
listNames[i] = lists[i].getValue(GtasksList.NAME);
listIds[i] = lists[i].getValue(GtasksList.REMOTE_ID);
String[] listNames = new String[lists.size()];
String[] listIds = new String[lists.size()];
for (int i = 0; i < lists.size(); i++) {
listNames[i] = lists.get(i).getValue(GtasksList.NAME);
listIds[i] = lists.get(i).getValue(GtasksList.REMOTE_ID);
}
ContentValues values = new ContentValues();

@ -35,6 +35,8 @@ import org.tasks.injection.ForApplication;
import org.tasks.injection.InjectingBroadcastReceiver;
import org.tasks.injection.Injector;
import java.util.List;
import javax.inject.Inject;
/**
@ -50,7 +52,7 @@ public class GtasksFilterExposer extends InjectingBroadcastReceiver implements A
@Inject @ForApplication Context context;
@Inject GtasksMetadata gtasksMetadata;
private StoreObject[] lists;
private List<StoreObject> lists;
public static Filter filterFromList(GtasksMetadata gtasksMetadata, Context context, StoreObject list) {
String listName = list.getValue(GtasksList.NAME);
@ -99,16 +101,16 @@ public class GtasksFilterExposer extends InjectingBroadcastReceiver implements A
return null;
}
Filter[] listFilters = new Filter[lists.length];
for(int i = 0; i < lists.length; i++) {
listFilters[i] = filterFromList(gtasksMetadata, context, lists[i]);
Filter[] listFilters = new Filter[lists.size()];
for(int i = 0; i < lists.size(); i++) {
listFilters[i] = filterFromList(gtasksMetadata, context, lists.get(i));
}
return listFilters;
}
private boolean noListsToShow() {
return lists.length == 0;
return lists.isEmpty();
}
@Override
@ -117,5 +119,4 @@ public class GtasksFilterExposer extends InjectingBroadcastReceiver implements A
return prepareFilters();
}
}

@ -7,14 +7,13 @@ package com.todoroo.astrid.gtasks;
import com.google.api.services.tasks.model.TaskList;
import com.google.api.services.tasks.model.TaskLists;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.sql.Query;
import com.todoroo.astrid.dao.StoreObjectDao;
import com.todoroo.astrid.dao.StoreObjectDao.StoreObjectCriteria;
import com.todoroo.astrid.data.StoreObject;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Singleton;
@ -26,7 +25,7 @@ public class GtasksListService {
private final StoreObjectDao storeObjectDao;
private StoreObject[] lists = null;
private List<StoreObject> lists;
@Inject
public GtasksListService(StoreObjectDao storeObjectDao) {
@ -38,21 +37,10 @@ public class GtasksListService {
return;
}
TodorooCursor<StoreObject> cursor = storeObjectDao.query(Query.select(StoreObject.PROPERTIES).
where(StoreObjectCriteria.byType(GtasksList.TYPE)));
try {
lists = new StoreObject[cursor.getCount()];
for(int i = 0; i < lists.length; i++) {
cursor.moveToNext();
StoreObject dashboard = new StoreObject(cursor);
lists[i] = dashboard;
}
} finally {
cursor.close();
}
lists = storeObjectDao.getByType(GtasksList.TYPE);
}
public StoreObject[] getLists() {
public List<StoreObject> getLists() {
readLists();
return lists;
}
@ -65,13 +53,13 @@ public class GtasksListService {
public synchronized void updateLists(TaskLists remoteLists) {
readLists();
HashSet<Long> previousLists = new HashSet<>(lists.length);
Set<Long> previousLists = new HashSet<>();
for(StoreObject list : lists) {
previousLists.add(list.getId());
}
List<TaskList> items = remoteLists.getItems();
StoreObject[] newLists = new StoreObject[items.size()];
List<StoreObject> newLists = new ArrayList<>();
for(int i = 0; i < items.size(); i++) {
com.google.api.services.tasks.model.TaskList remote = items.get(i);
@ -94,7 +82,7 @@ public class GtasksListService {
local.setValue(GtasksList.ORDER, i);
storeObjectDao.persist(local);
previousLists.remove(local.getId());
newLists[i] = local;
newLists.add(local);
}
lists = newLists;

@ -126,13 +126,13 @@ public class GtasksSyncV2Provider extends SyncV2Provider {
handler.handleException("gtasks-sync=io", e); //$NON-NLS-1$
}
StoreObject[] lists = gtasksListService.getLists();
if (lists.length == 0) {
List<StoreObject> lists = gtasksListService.getLists();
if (lists.isEmpty()) {
finishSync(callback);
return;
}
final AtomicInteger finisher = new AtomicInteger(lists.length);
final AtomicInteger finisher = new AtomicInteger(lists.size());
// TODO: Check timestamps from invoker.allGtaskLists and pare down lists to sync

Loading…
Cancel
Save