From 300622a8f7e0cc44e264af452bec51316d757b9b Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Tue, 15 Jul 2014 14:06:48 -0500 Subject: [PATCH] Hide UserActivity cursors --- .../andlib/utility/AndroidUtilities.java | 24 --- .../com/todoroo/astrid/data/UserActivity.java | 5 - .../astrid/actfm/sync/messages/NameMaps.java | 173 ------------------ .../todoroo/astrid/dao/UserActivityDao.java | 14 +- .../astrid/notes/EditNoteActivity.java | 67 +------ 5 files changed, 22 insertions(+), 261 deletions(-) delete mode 100644 astrid/src/main/java/com/todoroo/astrid/actfm/sync/messages/NameMaps.java diff --git a/api/src/main/java/com/todoroo/andlib/utility/AndroidUtilities.java b/api/src/main/java/com/todoroo/andlib/utility/AndroidUtilities.java index a18d62073..1c821a4fe 100644 --- a/api/src/main/java/com/todoroo/andlib/utility/AndroidUtilities.java +++ b/api/src/main/java/com/todoroo/andlib/utility/AndroidUtilities.java @@ -404,30 +404,6 @@ public class AndroidUtilities { activity.overridePendingTransition(enterAnim, exitAnim); } - /** - * Join items to a list - */ - public static T[] addToArray(Class type, T[] list, T... newItems) { - int originalListLength = 0; - int length = 0; - if (list != null) { - originalListLength = list.length; - length += list.length; - } - if (newItems != null) { - length += newItems.length; - } - - T[] newList = (T[]) Array.newInstance(type, length); - if (list != null) { - System.arraycopy(list, 0, newList, 0, list.length); - } - if (newItems != null) { - System.arraycopy(newItems, 0, newList, originalListLength, newItems.length); - } - return newList; - } - /** * Capitalize the first character */ diff --git a/api/src/main/java/com/todoroo/astrid/data/UserActivity.java b/api/src/main/java/com/todoroo/astrid/data/UserActivity.java index edcd2643f..426746ef4 100644 --- a/api/src/main/java/com/todoroo/astrid/data/UserActivity.java +++ b/api/src/main/java/com/todoroo/astrid/data/UserActivity.java @@ -7,7 +7,6 @@ import com.todoroo.andlib.data.Property; import com.todoroo.andlib.data.Property.LongProperty; import com.todoroo.andlib.data.Property.StringProperty; import com.todoroo.andlib.data.Table; -import com.todoroo.andlib.data.TodorooCursor; import com.todoroo.astrid.api.AstridApiConstants; public class UserActivity extends RemoteModel { @@ -62,10 +61,6 @@ public class UserActivity extends RemoteModel { // --- Action codes public static final String ACTION_TASK_COMMENT = "task_comment"; - public UserActivity(TodorooCursor cursor) { - super(cursor); - } - // --- helpers /** Default values container */ diff --git a/astrid/src/main/java/com/todoroo/astrid/actfm/sync/messages/NameMaps.java b/astrid/src/main/java/com/todoroo/astrid/actfm/sync/messages/NameMaps.java deleted file mode 100644 index deecdec01..000000000 --- a/astrid/src/main/java/com/todoroo/astrid/actfm/sync/messages/NameMaps.java +++ /dev/null @@ -1,173 +0,0 @@ -package com.todoroo.astrid.actfm.sync.messages; - -import com.todoroo.andlib.data.Property; -import com.todoroo.astrid.data.TagData; -import com.todoroo.astrid.data.Task; -import com.todoroo.astrid.data.TaskAttachment; -import com.todoroo.astrid.data.TaskListMetadata; -import com.todoroo.astrid.data.UserActivity; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -public class NameMaps { - - // -------------------------------- - // ---- Table name mappings ------- - // -------------------------------- - - // Universal table identifiers - public static final String TABLE_ID_USER_ACTIVITY = "user_activities"; - - // -------------------------------- - // ---- Column name mappings ------- - // -------------------------------- - private static void putPropertyToServerName(Property property, String serverName, - Map, String> propertyMap, Map> localNameMap, Map serverNameMap, - Set excludedFromOutstandingSet, boolean writeable) { - propertyMap.put(property, serverName); - localNameMap.put(property.name, property); - serverNameMap.put(property.name, serverName); - if (!writeable && excludedFromOutstandingSet != null) { - excludedFromOutstandingSet.add(property.name); - } - } - - // ---------- - // Tasks - // ---------- - - private static final Map, String> TASK_PROPERTIES_LOCAL_TO_SERVER; - private static final Map> TASK_COLUMN_NAMES_TO_PROPERTIES; - private static final Map TASK_COLUMNS_LOCAL_TO_SERVER; - private static final Set TASK_PROPERTIES_EXCLUDED; - - - private static void putTaskPropertyToServerName(Property property, String serverName, boolean writeable) { - putPropertyToServerName(property, serverName, TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMN_NAMES_TO_PROPERTIES, - TASK_COLUMNS_LOCAL_TO_SERVER, TASK_PROPERTIES_EXCLUDED, writeable); - } - - static { - // Hardcoded local columns mapped to corresponding server names - TASK_PROPERTIES_LOCAL_TO_SERVER = new HashMap<>(); - TASK_COLUMNS_LOCAL_TO_SERVER = new HashMap<>(); - TASK_COLUMN_NAMES_TO_PROPERTIES = new HashMap<>(); - TASK_PROPERTIES_EXCLUDED = new HashSet<>(); - - putTaskPropertyToServerName(Task.TITLE, "title", true); - putTaskPropertyToServerName(Task.IMPORTANCE, "importance", true); - putTaskPropertyToServerName(Task.DUE_DATE, "due", true); - putTaskPropertyToServerName(Task.HIDE_UNTIL, "hide_until", true); - putTaskPropertyToServerName(Task.CREATION_DATE, "created_at", true); - putTaskPropertyToServerName(Task.COMPLETION_DATE, "completed_at", true); - putTaskPropertyToServerName(Task.RECURRENCE, "repeat", true); - putTaskPropertyToServerName(Task.DELETION_DATE, "deleted_at", true); - putTaskPropertyToServerName(Task.NOTES, "notes", true); - putTaskPropertyToServerName(Task.RECURRENCE, "repeat", true); - putTaskPropertyToServerName(Task.UUID, "uuid", false); - } - - // ---------- - // TagData - // ---------- - - private static final Map, String> TAG_DATA_PROPERTIES_LOCAL_TO_SERVER; - private static final Map> TAG_DATA_COLUMN_NAMES_TO_PROPERTIES; - private static final Map TAG_DATA_COLUMNS_LOCAL_TO_SERVER; - private static final Set TAG_PROPERTIES_EXCLUDED; - - private static void putTagPropertyToServerName(Property property, String serverName, boolean writeable) { - putPropertyToServerName(property, serverName, TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMN_NAMES_TO_PROPERTIES, - TAG_DATA_COLUMNS_LOCAL_TO_SERVER, TAG_PROPERTIES_EXCLUDED, writeable); - } - static { - // Hardcoded local columns mapped to corresponding server names - TAG_DATA_PROPERTIES_LOCAL_TO_SERVER = new HashMap<>(); - TAG_DATA_COLUMNS_LOCAL_TO_SERVER = new HashMap<>(); - TAG_DATA_COLUMN_NAMES_TO_PROPERTIES = new HashMap<>(); - TAG_PROPERTIES_EXCLUDED = new HashSet<>(); - - putTagPropertyToServerName(TagData.NAME, "name", true); - putTagPropertyToServerName(TagData.UUID, "uuid", false); - } - - // ---------- - // User Activity - // ---------- - private static final Map, String> USER_ACTIVITY_PROPERTIES_LOCAL_TO_SERVER; - private static final Map> USER_ACTIVITY_COLUMN_NAMES_TO_PROPERTIES; - private static final Map USER_ACTIVITY_COLUMNS_LOCAL_TO_SERVER; - private static final Set USER_ACTIVITY_PROPERTIES_EXCLUDED; - - private static void putUserActivityPropertyToServerName(Property property, String serverName, boolean writeable) { - putPropertyToServerName(property, serverName, USER_ACTIVITY_PROPERTIES_LOCAL_TO_SERVER, USER_ACTIVITY_COLUMN_NAMES_TO_PROPERTIES, - USER_ACTIVITY_COLUMNS_LOCAL_TO_SERVER, USER_ACTIVITY_PROPERTIES_EXCLUDED, writeable); - } - - static { - USER_ACTIVITY_PROPERTIES_LOCAL_TO_SERVER = new HashMap<>(); - USER_ACTIVITY_COLUMN_NAMES_TO_PROPERTIES = new HashMap<>(); - USER_ACTIVITY_COLUMNS_LOCAL_TO_SERVER = new HashMap<>(); - USER_ACTIVITY_PROPERTIES_EXCLUDED = new HashSet<>(); - - putUserActivityPropertyToServerName(UserActivity.ACTION, "action", true); - putUserActivityPropertyToServerName(UserActivity.MESSAGE, "message", true); - putUserActivityPropertyToServerName(UserActivity.PICTURE, "picture", true); - putUserActivityPropertyToServerName(UserActivity.TARGET_ID, "target_id", true); - putUserActivityPropertyToServerName(UserActivity.CREATED_AT, "created_at", true); - putUserActivityPropertyToServerName(UserActivity.DELETED_AT, "deleted_at", true); - } - - // ---------- - // TaskAttachment - // ---------- - private static final Map, String> TASK_ATTACHMENT_PROPERTIES_LOCAL_TO_SERVER; - private static final Map> TASK_ATTACHMENT_COLUMN_NAMES_TO_PROPERTIES; - private static final Map TASK_ATTACHMENT_COLUMNS_LOCAL_TO_SERVER; - private static final Set TASK_ATTACHMENT_PROPERTIES_EXCLUDED; - - private static void putTaskAttachmentPropertyToServerName(Property property, String serverName, boolean writeable) { - putPropertyToServerName(property, serverName, TASK_ATTACHMENT_PROPERTIES_LOCAL_TO_SERVER, TASK_ATTACHMENT_COLUMN_NAMES_TO_PROPERTIES, - TASK_ATTACHMENT_COLUMNS_LOCAL_TO_SERVER, TASK_ATTACHMENT_PROPERTIES_EXCLUDED, writeable); - } - - static { - TASK_ATTACHMENT_PROPERTIES_LOCAL_TO_SERVER = new HashMap<>(); - TASK_ATTACHMENT_COLUMN_NAMES_TO_PROPERTIES = new HashMap<>(); - TASK_ATTACHMENT_COLUMNS_LOCAL_TO_SERVER = new HashMap<>(); - TASK_ATTACHMENT_PROPERTIES_EXCLUDED = new HashSet<>(); - - putTaskAttachmentPropertyToServerName(TaskAttachment.UUID, "uuid", false); - putTaskAttachmentPropertyToServerName(TaskAttachment.TASK_UUID, "task_id", true); - putTaskAttachmentPropertyToServerName(TaskAttachment.NAME, "name", false); - putTaskAttachmentPropertyToServerName(TaskAttachment.CONTENT_TYPE, "content_type", false); - putTaskAttachmentPropertyToServerName(TaskAttachment.DELETED_AT, "deleted_at", true); - } - - // ---------- - // TaskListMetadata - // ---------- - private static final Map, String> TASK_LIST_METADATA_PROPERTIES_LOCAL_TO_SERVER; - private static final Map> TASK_LIST_METADATA_COLUMN_NAMES_TO_PROPERTIES; - private static final Map TASK_LIST_METADATA_COLUMNS_LOCAL_TO_SERVER; - private static final Set TASK_LIST_METADATA_PROPERTIES_EXCLUDED; - - private static void putTaskListMetadataPropertyToServerName(Property property, String serverName, boolean writeable) { - putPropertyToServerName(property, serverName, TASK_LIST_METADATA_PROPERTIES_LOCAL_TO_SERVER, TASK_LIST_METADATA_COLUMN_NAMES_TO_PROPERTIES, - TASK_LIST_METADATA_COLUMNS_LOCAL_TO_SERVER, TASK_LIST_METADATA_PROPERTIES_EXCLUDED, writeable); - } - - static { - TASK_LIST_METADATA_PROPERTIES_LOCAL_TO_SERVER = new HashMap<>(); - TASK_LIST_METADATA_COLUMN_NAMES_TO_PROPERTIES = new HashMap<>(); - TASK_LIST_METADATA_COLUMNS_LOCAL_TO_SERVER = new HashMap<>(); - TASK_LIST_METADATA_PROPERTIES_EXCLUDED = new HashSet<>(); - - putTaskListMetadataPropertyToServerName(TaskListMetadata.TAG_UUID, "tag_id", true); - putTaskListMetadataPropertyToServerName(TaskListMetadata.FILTER, "filter", true); - putTaskListMetadataPropertyToServerName(TaskListMetadata.TASK_IDS, "task_ids", true); - } -} diff --git a/astrid/src/main/java/com/todoroo/astrid/dao/UserActivityDao.java b/astrid/src/main/java/com/todoroo/astrid/dao/UserActivityDao.java index 8d8813db2..480d63ae6 100644 --- a/astrid/src/main/java/com/todoroo/astrid/dao/UserActivityDao.java +++ b/astrid/src/main/java/com/todoroo/astrid/dao/UserActivityDao.java @@ -2,6 +2,10 @@ package com.todoroo.astrid.dao; import android.content.ContentValues; +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.utility.DateUtilities; import com.todoroo.astrid.data.UserActivity; @@ -28,9 +32,17 @@ public class UserActivityDao extends RemoteModelDao { @Override public boolean saveExisting(UserActivity item) { ContentValues values = item.getSetValues(); - if(values == null || values.size() == 0) { + if (values == null || values.size() == 0) { return false; } return super.saveExisting(item); } + + public void getCommentsForTask(String taskUuid, Callback callback) { + query(callback, Query.select(UserActivity.PROPERTIES).where( + Criterion.and(UserActivity.ACTION.eq(UserActivity.ACTION_TASK_COMMENT), + UserActivity.TARGET_ID.eq(taskUuid), + UserActivity.DELETED_AT.eq(0)) + ).orderBy(Order.desc("1"))); + } } diff --git a/astrid/src/main/java/com/todoroo/astrid/notes/EditNoteActivity.java b/astrid/src/main/java/com/todoroo/astrid/notes/EditNoteActivity.java index 39aca2fc1..400586c71 100644 --- a/astrid/src/main/java/com/todoroo/astrid/notes/EditNoteActivity.java +++ b/astrid/src/main/java/com/todoroo/astrid/notes/EditNoteActivity.java @@ -31,17 +31,13 @@ import android.widget.LinearLayout; import android.widget.TextView; import android.widget.TextView.OnEditorActionListener; -import com.todoroo.andlib.data.Property; +import com.todoroo.andlib.data.Callback; import com.todoroo.andlib.data.TodorooCursor; -import com.todoroo.andlib.sql.Criterion; -import com.todoroo.andlib.sql.Order; import com.todoroo.andlib.sql.Query; -import com.todoroo.andlib.utility.AndroidUtilities; import com.todoroo.andlib.utility.DateUtilities; import com.todoroo.astrid.actfm.ActFmCameraModule; import com.todoroo.astrid.actfm.ActFmCameraModule.CameraResultCallback; import com.todoroo.astrid.actfm.ActFmCameraModule.ClearImageCallback; -import com.todoroo.astrid.actfm.sync.messages.NameMaps; import com.todoroo.astrid.activity.AstridActivity; import com.todoroo.astrid.activity.TaskEditFragment; import com.todoroo.astrid.dao.MetadataDao.MetadataCriteria; @@ -71,21 +67,6 @@ import static org.tasks.files.ImageHelper.sampleBitmap; public class EditNoteActivity extends LinearLayout implements TimerActionListener { - private static final Property.StringProperty ACTIVITY_TYPE_PROPERTY = new Property.StringProperty(null, "'" + NameMaps.TABLE_ID_USER_ACTIVITY + "' as type"); //$NON-NLS-1$//$NON-NLS-2$ - - private static final Property[] USER_ACTIVITY_PROPERTIES = { - UserActivity.CREATED_AT, - UserActivity.UUID, - UserActivity.ACTION, - UserActivity.MESSAGE, - UserActivity.TARGET_ID, - UserActivity.PICTURE, - UserActivity.ID, - ACTIVITY_TYPE_PROPERTY, - }; - - private static final int TYPE_PROPERTY_INDEX = USER_ACTIVITY_PROPERTIES.length - 1; - private Task task; private final Preferences preferences; @@ -215,6 +196,7 @@ public class EditNoteActivity extends LinearLayout implements TimerActionListene public void beforeTextChanged(CharSequence s, int start, int count, int after) { // } + @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // @@ -224,7 +206,7 @@ public class EditNoteActivity extends LinearLayout implements TimerActionListene commentField.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView view, int actionId, KeyEvent event) { - if(actionId == EditorInfo.IME_NULL && commentField.getText().length() > 0) { + if (actionId == EditorInfo.IME_NULL && commentField.getText().length() > 0) { addComment(); return true; } @@ -291,30 +273,17 @@ public class EditNoteActivity extends LinearLayout implements TimerActionListene notes.close(); } - TodorooCursor updates = getActivityForTask(task); - try { - UserActivity update = new UserActivity(); - for(updates.moveToFirst(); !updates.isAfterLast(); updates.moveToNext()) { - update.clear(); - - String type = updates.getString(TYPE_PROPERTY_INDEX); - NoteOrUpdate noa = null; - if (NameMaps.TABLE_ID_USER_ACTIVITY.equals(type)) { - readUserActivityProperties(updates, update); - noa = NoteOrUpdate.fromUpdate(update); - } - if(noa != null) { - items.add(noa); - } + userActivityDao.getCommentsForTask(task.getUuid(), new Callback() { + @Override + public void apply(UserActivity update) { + items.add(NoteOrUpdate.fromUpdate(update)); } - } finally { - updates.close(); - } + }); Collections.sort(items, new Comparator() { @Override public int compare(NoteOrUpdate a, NoteOrUpdate b) { - if(a.createdAt < b.createdAt) { + if (a.createdAt < b.createdAt) { return 1; } else if (a.createdAt == b.createdAt) { return 0; @@ -350,24 +319,6 @@ public class EditNoteActivity extends LinearLayout implements TimerActionListene } } - public TodorooCursor getActivityForTask(Task task) { - Query taskQuery = Query.select(AndroidUtilities.addToArray(Property.class, USER_ACTIVITY_PROPERTIES)) - .where(Criterion.and(UserActivity.ACTION.eq(UserActivity.ACTION_TASK_COMMENT), UserActivity.TARGET_ID.eq(task.getUuid()), UserActivity.DELETED_AT.eq(0))); - - Query resultQuery = taskQuery.orderBy(Order.desc("1")); //$NON-NLS-1$ - - return userActivityDao.query(resultQuery); - } - - private static void readUserActivityProperties(TodorooCursor unionCursor, UserActivity activity) { - activity.setCreatedAt(unionCursor.getLong(0)); - activity.setUUID(unionCursor.getString(1)); - activity.setAction(unionCursor.getString(2)); - activity.setMessage(unionCursor.getString(3)); - activity.setTargetId(unionCursor.getString(4)); - activity.setPicture(unionCursor.getString(5)); - } - public View getUpdateNotes(NoteOrUpdate note, ViewGroup parent) { View convertView = ((Activity)getContext()).getLayoutInflater().inflate( R.layout.update_adapter_row, parent, false);