From c45d70021b36926777e021b3c5f3e3812f948c52 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Tue, 29 Jan 2013 16:08:49 -0800 Subject: [PATCH] Implement join with the users table for showing user images in the task adapter --- .../com/todoroo/astrid/data/RemoteModel.java | 11 +++++++++++ .../astrid/activity/TaskListFragment.java | 6 +++++- .../todoroo/astrid/adapter/TaskAdapter.java | 19 +++++++++++++------ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/api/src/com/todoroo/astrid/data/RemoteModel.java b/api/src/com/todoroo/astrid/data/RemoteModel.java index 62e403aeb..627b2a49d 100644 --- a/api/src/com/todoroo/astrid/data/RemoteModel.java +++ b/api/src/com/todoroo/astrid/data/RemoteModel.java @@ -13,6 +13,7 @@ import android.text.TextUtils; import com.todoroo.andlib.data.AbstractModel; import com.todoroo.andlib.data.Property.LongProperty; import com.todoroo.andlib.data.Property.StringProperty; +import com.todoroo.andlib.data.TodorooCursor; /** * A model that is synchronized to a remote server and has a remote id @@ -88,4 +89,14 @@ abstract public class RemoteModel extends AbstractModel { return value; } } + + public static String getPictureUrlFromCursor(TodorooCursor cursor, StringProperty pictureProperty, String size) { + String value = cursor.get(pictureProperty); + try { + JSONObject pictureJson = new JSONObject(value); + return pictureJson.optString(size); + } catch (JSONException e) { + return value; + } + } } diff --git a/astrid/src/com/todoroo/astrid/activity/TaskListFragment.java b/astrid/src/com/todoroo/astrid/activity/TaskListFragment.java index 1de0eddbf..efba351e5 100644 --- a/astrid/src/com/todoroo/astrid/activity/TaskListFragment.java +++ b/astrid/src/com/todoroo/astrid/activity/TaskListFragment.java @@ -86,6 +86,7 @@ import com.todoroo.astrid.dao.Database; import com.todoroo.astrid.data.Metadata; import com.todoroo.astrid.data.TagData; import com.todoroo.astrid.data.Task; +import com.todoroo.astrid.data.User; import com.todoroo.astrid.helper.SyncActionHelper; import com.todoroo.astrid.helper.TaskListContextMenuExtensionLoader; import com.todoroo.astrid.helper.TaskListContextMenuExtensionLoader.ContextMenuItem; @@ -101,8 +102,8 @@ import com.todoroo.astrid.service.ThemeService; import com.todoroo.astrid.service.UpgradeService; import com.todoroo.astrid.subtasks.SubtasksListFragment; import com.todoroo.astrid.sync.SyncProviderPreferences; -import com.todoroo.astrid.tags.TaskToTagMetadata; import com.todoroo.astrid.tags.TagService; +import com.todoroo.astrid.tags.TaskToTagMetadata; import com.todoroo.astrid.taskrabbit.TaskRabbitMetadata; import com.todoroo.astrid.timers.TimerPlugin; import com.todoroo.astrid.ui.QuickAddBar; @@ -910,6 +911,8 @@ public class TaskListFragment extends ListFragment implements OnScrollListener, public static final String TAGS_METADATA_JOIN = "for_tags"; //$NON-NLS-1$ + public static final String USER_IMAGE_JOIN = "for_images"; // //$NON-NLS-1$ + /** * Fill in the Task List with current items * @@ -969,6 +972,7 @@ public class TaskListFragment extends ListFragment implements OnScrollListener, Task.ID.eq(Field.field(TR_METADATA_JOIN + "." + Metadata.TASK.name)))).toString() //$NON-NLS-1$ + Join.left(Metadata.TABLE.as(TAGS_METADATA_JOIN), tagsJoinCriterion).toString() //$NON-NLS-1$ + + Join.left(User.TABLE.as(USER_IMAGE_JOIN), Task.USER_ID.eq(User.UUID)).toString() + filter.getSqlQuery(); sqlQueryTemplate.set(SortHelper.adjustQueryForFlagsAndSort( diff --git a/astrid/src/com/todoroo/astrid/adapter/TaskAdapter.java b/astrid/src/com/todoroo/astrid/adapter/TaskAdapter.java index 4762152ee..480419af4 100644 --- a/astrid/src/com/todoroo/astrid/adapter/TaskAdapter.java +++ b/astrid/src/com/todoroo/astrid/adapter/TaskAdapter.java @@ -80,7 +80,9 @@ import com.todoroo.astrid.api.TaskDecoration; import com.todoroo.astrid.api.TaskDecorationExposer; import com.todoroo.astrid.core.LinkActionExposer; import com.todoroo.astrid.data.Metadata; +import com.todoroo.astrid.data.RemoteModel; import com.todoroo.astrid.data.Task; +import com.todoroo.astrid.data.User; import com.todoroo.astrid.files.FileMetadata; import com.todoroo.astrid.files.FilesAction; import com.todoroo.astrid.files.FilesControlSet; @@ -140,6 +142,7 @@ public class TaskAdapter extends CursorAdapter implements Filterable { Task.USER, Task.REMINDER_LAST, Task.SOCIAL_REMINDER, + User.PICTURE, TASK_RABBIT_ID, // Task rabbit metadata id (non-zero means it exists) TAGS // Concatenated list of tags }; @@ -390,6 +393,7 @@ public class TaskAdapter extends CursorAdapter implements Filterable { if (!titleOnlyLayout) { viewHolder.isTaskRabbit = (cursor.get(TASK_RABBIT_ID) > 0); viewHolder.tagsString = cursor.get(TAGS); + viewHolder.imageUrl = RemoteModel.getPictureUrlFromCursor(cursor, User.PICTURE, RemoteModel.PICTURE_THUMB); } Task task = viewHolder.task; @@ -422,6 +426,7 @@ public class TaskAdapter extends CursorAdapter implements Filterable { public ImageView taskActionIcon; public boolean isTaskRabbit; // From join query, not part of the task model public String tagsString; // From join query, not part of the task model + public String imageUrl; // From join query, not part of the task model public View[] decorations; } @@ -1145,12 +1150,14 @@ public class TaskAdapter extends CursorAdapter implements Filterable { pictureView.setDefaultImageResource(R.drawable.icn_anyone_transparent); else { pictureView.setDefaultImageResource(R.drawable.icn_default_person_image); - try { - JSONObject user = new JSONObject(task.getValue(Task.USER)); - pictureView.setUrl(user.optString("picture")); //$NON-NLS-1$ - } catch (JSONException e) { - if (Task.isRealUserId(task.getValue(Task.USER_ID))) { - // TODO: Implement a join here + if (!TextUtils.isEmpty(viewHolder.imageUrl)) { + pictureView.setUrl(viewHolder.imageUrl); + } else if (!TextUtils.isEmpty(task.getValue(Task.USER))) { + try { + JSONObject user = new JSONObject(task.getValue(Task.USER)); + pictureView.setUrl(user.optString("picture")); //$NON-NLS-1$ + } catch (JSONException e) { + // } } }