mirror of https://github.com/tasks/tasks
got saved filter saving working. also updated the icon on the task list activity
parent
23df93f19c
commit
7cbc7478c0
@ -0,0 +1,61 @@
|
||||
/**
|
||||
* See the file "LICENSE" for the full license governing this code.
|
||||
*/
|
||||
package com.todoroo.astrid.api;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import com.todoroo.andlib.utility.DateUtilities;
|
||||
|
||||
/**
|
||||
* PermaSql allows for creating SQL statements that can be saved and used
|
||||
* later without dates getting stale. It also allows these values to be
|
||||
* used in
|
||||
*
|
||||
* @author Tim Su <tim@todoroo.com>
|
||||
*
|
||||
*/
|
||||
public final class PermaSql {
|
||||
|
||||
// --- placeholder strings
|
||||
|
||||
/** value to be replaced with the current time as long */
|
||||
public static final String VALUE_NOW = "NOW()"; //$NON-NLS-1$
|
||||
|
||||
/** value to be replaced by end of day as long */
|
||||
public static final String VALUE_EOD = "EOD()"; //$NON-NLS-1$
|
||||
|
||||
/** value to be replaced by end of day yesterday as long */
|
||||
public static final String VALUE_EOD_YESTERDAY = "EODY()"; //$NON-NLS-1$
|
||||
|
||||
/** value to be replaced by end of day tomorrow as long */
|
||||
public static final String VALUE_EOD_TOMORROW = "EODT()"; //$NON-NLS-1$
|
||||
|
||||
/** value to be replaced by end of day day after tomorrow as long */
|
||||
public static final String VALUE_EOD_DAY_AFTER = "EODTT()"; //$NON-NLS-1$
|
||||
|
||||
/** value to be replaced by end of day next week as long */
|
||||
public static final String VALUE_EOD_NEXT_WEEK = "EODW()"; //$NON-NLS-1$
|
||||
|
||||
/** Replace placeholder strings with actual */
|
||||
public static String replacePlaceholders(String value) {
|
||||
if(value.contains(VALUE_NOW))
|
||||
value = value.replace(VALUE_NOW, Long.toString(DateUtilities.now()));
|
||||
if(value.contains(VALUE_EOD) || value.contains(VALUE_EOD_DAY_AFTER) ||
|
||||
value.contains(VALUE_EOD_NEXT_WEEK) || value.contains(VALUE_EOD_TOMORROW) ||
|
||||
value.contains(VALUE_EOD_YESTERDAY)) {
|
||||
Date date = new Date();
|
||||
date.setHours(23);
|
||||
date.setMinutes(59);
|
||||
date.setSeconds(59);
|
||||
long time = date.getTime();
|
||||
value = value.replace(VALUE_EOD_YESTERDAY, Long.toString(time - DateUtilities.ONE_DAY));
|
||||
value = value.replace(VALUE_EOD, Long.toString(time));
|
||||
value = value.replace(VALUE_EOD_TOMORROW, Long.toString(time + DateUtilities.ONE_DAY));
|
||||
value = value.replace(VALUE_EOD_DAY_AFTER, Long.toString(time + 2 * DateUtilities.ONE_DAY));
|
||||
value = value.replace(VALUE_EOD_NEXT_WEEK, Long.toString(time + 7 * DateUtilities.ONE_DAY));
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
/**
|
||||
* See the file "LICENSE" for the full license governing this code.
|
||||
*/
|
||||
package com.todoroo.astrid.core;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
|
||||
import com.timsu.astrid.R;
|
||||
import com.todoroo.andlib.data.TodorooCursor;
|
||||
import com.todoroo.andlib.sql.Order;
|
||||
import com.todoroo.andlib.sql.Query;
|
||||
import com.todoroo.astrid.activity.FilterListActivity;
|
||||
import com.todoroo.astrid.api.AstridApiConstants;
|
||||
import com.todoroo.astrid.api.Filter;
|
||||
import com.todoroo.astrid.api.FilterListHeader;
|
||||
import com.todoroo.astrid.api.FilterListItem;
|
||||
import com.todoroo.astrid.dao.StoreObjectDao;
|
||||
import com.todoroo.astrid.model.StoreObject;
|
||||
|
||||
/**
|
||||
* Exposes Astrid's built in filters to the {@link FilterListActivity}
|
||||
*
|
||||
* @author Tim Su <tim@todoroo.com>
|
||||
*
|
||||
*/
|
||||
public final class CustomFilterExposer extends BroadcastReceiver {
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
Resources r = context.getResources();
|
||||
|
||||
PendingIntent customFilterIntent = PendingIntent.getActivity(context, 0,
|
||||
new Intent(context, CustomFilterActivity.class), 0);
|
||||
IntentFilter customFilter = new IntentFilter(r.getString(R.string.BFE_Custom),
|
||||
customFilterIntent);
|
||||
customFilter.listingIcon = ((BitmapDrawable)r.getDrawable(R.drawable.gnome_filter)).getBitmap();
|
||||
|
||||
Filter[] customFilters = buildSavedFilters();
|
||||
|
||||
FilterListItem[] list;
|
||||
if(customFilters.length == 0) {
|
||||
list = new FilterListItem[1];
|
||||
} else {
|
||||
list = new FilterListItem[customFilters.length + 2];
|
||||
list[1] = new FilterListHeader(r.getString(R.string.BFE_Saved));
|
||||
for(int i = 0; i < customFilters.length; i++)
|
||||
list[i + 2] = customFilters[i];
|
||||
}
|
||||
|
||||
list[0] = customFilter;
|
||||
|
||||
// transmit filter list
|
||||
Intent broadcastIntent = new Intent(AstridApiConstants.BROADCAST_SEND_FILTERS);
|
||||
broadcastIntent.putExtra(AstridApiConstants.EXTRAS_RESPONSE, list);
|
||||
context.sendBroadcast(broadcastIntent, AstridApiConstants.PERMISSION_READ);
|
||||
}
|
||||
|
||||
private Filter[] buildSavedFilters() {
|
||||
StoreObjectDao dao = PluginServices.getStoreObjectDao();
|
||||
TodorooCursor<StoreObject> cursor = dao.query(Query.select(StoreObject.PROPERTIES).where(
|
||||
StoreObject.TYPE.eq(SavedFilter.TYPE)).orderBy(Order.asc(SavedFilter.NAME)));
|
||||
try {
|
||||
Filter[] list = new Filter[cursor.getCount()];
|
||||
|
||||
StoreObject savedFilter = new StoreObject();
|
||||
for(int i = 0; i < list.length; i++) {
|
||||
cursor.moveToNext();
|
||||
savedFilter.readFromCursor(cursor);
|
||||
list[i] = SavedFilter.load(savedFilter);
|
||||
}
|
||||
|
||||
return list;
|
||||
} finally {
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,132 @@
|
||||
package com.todoroo.astrid.core;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.todoroo.andlib.data.TodorooCursor;
|
||||
import com.todoroo.andlib.data.Property.StringProperty;
|
||||
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;
|
||||
import com.todoroo.astrid.dao.StoreObjectDao;
|
||||
import com.todoroo.astrid.model.StoreObject;
|
||||
|
||||
/**
|
||||
* {@link StoreObject} entries for a saved custom filter
|
||||
*
|
||||
* @author Tim Su <tim@todoroo.com>
|
||||
*
|
||||
*/
|
||||
public class SavedFilter {
|
||||
|
||||
/** type */
|
||||
public static final String TYPE = "filter"; //$NON-NLS-1$
|
||||
|
||||
/** saved filter name */
|
||||
public static final StringProperty NAME = new StringProperty(StoreObject.TABLE,
|
||||
StoreObject.ITEM.name);
|
||||
|
||||
/** perma-sql */
|
||||
public static final StringProperty SQL = new StringProperty(StoreObject.TABLE,
|
||||
StoreObject.VALUE1.name);
|
||||
|
||||
/** serialized new task content values */
|
||||
public static final StringProperty VALUES = new StringProperty(StoreObject.TABLE,
|
||||
StoreObject.VALUE2.name);
|
||||
|
||||
/** serialized list of filters applied */
|
||||
public static final StringProperty FILTERS = new StringProperty(StoreObject.TABLE,
|
||||
StoreObject.VALUE3.name);
|
||||
|
||||
// --- data storage and retrieval methods
|
||||
|
||||
/**
|
||||
* Save a filter
|
||||
*
|
||||
* @param adapter
|
||||
* @param title
|
||||
* @param sql2
|
||||
* @param values2
|
||||
*/
|
||||
public static void persist(CustomFilterAdapter adapter, String title,
|
||||
String sql, ContentValues values) {
|
||||
|
||||
if(title == null || title.length() == 0)
|
||||
return;
|
||||
|
||||
// if filter of this name exists, edit it
|
||||
StoreObjectDao dao = PluginServices.getStoreObjectDao();
|
||||
StoreObject storeObject = new StoreObject();
|
||||
TodorooCursor<StoreObject> cursor = dao.query(Query.select(StoreObject.ID).where(NAME.eq(title)));
|
||||
try {
|
||||
if(!cursor.isAfterLast()) {
|
||||
cursor.moveToNext();
|
||||
storeObject.readFromCursor(cursor);
|
||||
}
|
||||
} finally {
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
// populate saved filter properties
|
||||
storeObject.setValue(StoreObject.TYPE, TYPE);
|
||||
storeObject.setValue(NAME, title);
|
||||
storeObject.setValue(SQL, sql);
|
||||
|
||||
if(values == null)
|
||||
storeObject.setValue(VALUES, ""); //$NON-NLS-1$
|
||||
else
|
||||
storeObject.setValue(VALUES, AndroidUtilities.contentValuesToSerializedString(values));
|
||||
|
||||
String filters = serializeFilters(adapter);
|
||||
storeObject.setValue(FILTERS, filters);
|
||||
|
||||
dao.persist(storeObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn a series of CriterionInstance objects into a string
|
||||
* @param adapter
|
||||
* @return
|
||||
*/
|
||||
private static String serializeFilters(CustomFilterAdapter adapter) {
|
||||
StringBuilder values = new StringBuilder();
|
||||
for(int i = 0; i < adapter.getCount(); i++) {
|
||||
CriterionInstance item = adapter.getItem(i);
|
||||
|
||||
// criterion|entry|text|type|sql
|
||||
values.append(item.criterion.identifier.replace(AndroidUtilities.SERIALIZATION_SEPARATOR,
|
||||
AndroidUtilities.SEPARATOR_ESCAPE)).append(AndroidUtilities.SERIALIZATION_SEPARATOR);
|
||||
if(item.selectedIndex > -1 && item.criterion.entryValues != null)
|
||||
values.append(item.criterion.entryValues[item.selectedIndex].replace(AndroidUtilities.SERIALIZATION_SEPARATOR,
|
||||
AndroidUtilities.SEPARATOR_ESCAPE));
|
||||
values.append(AndroidUtilities.SERIALIZATION_SEPARATOR);
|
||||
values.append(item.criterion.text.replace(AndroidUtilities.SERIALIZATION_SEPARATOR,
|
||||
AndroidUtilities.SEPARATOR_ESCAPE)).append(AndroidUtilities.SERIALIZATION_SEPARATOR);
|
||||
values.append(item.type).append(AndroidUtilities.SERIALIZATION_SEPARATOR);
|
||||
if(item.criterion.sql != null)
|
||||
values.append(item.criterion.sql);
|
||||
values.append('\n');
|
||||
}
|
||||
|
||||
return values.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Read filter from store
|
||||
* @param savedFilter
|
||||
* @return
|
||||
*/
|
||||
public static Filter load(StoreObject savedFilter) {
|
||||
String title = savedFilter.getValue(NAME);
|
||||
String sql = savedFilter.getValue(SQL);
|
||||
String values = savedFilter.getValue(VALUES);
|
||||
|
||||
ContentValues contentValues = null;
|
||||
if(!TextUtils.isEmpty(values))
|
||||
contentValues = AndroidUtilities.contentValuesFromSerializedString(values);
|
||||
|
||||
return new Filter(title, title, sql, contentValues);
|
||||
}
|
||||
|
||||
}
|
Binary file not shown.
After Width: | Height: | Size: 930 B |
Binary file not shown.
Before Width: | Height: | Size: 1.2 KiB |
Binary file not shown.
Before Width: | Height: | Size: 1.2 KiB |
Binary file not shown.
After Width: | Height: | Size: 552 B |
Binary file not shown.
After Width: | Height: | Size: 432 B |
Loading…
Reference in New Issue