From 53d6c2387dcf591ffee29d64849325b074a7a754 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Thu, 1 Nov 2012 18:20:16 -0700 Subject: [PATCH] Refactored some of arne's code for adding assignees to a list, fixed the ui refresh issue --- .../astrid/actfm/EditPeopleControlSet.java | 51 ------------- .../astrid/activity/TaskEditFragment.java | 26 ++++++- .../astrid/activity/TaskListActivity.java | 76 +++++++++++++++++-- .../com/todoroo/astrid/ui/QuickAddBar.java | 14 +++- 4 files changed, 106 insertions(+), 61 deletions(-) diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/EditPeopleControlSet.java b/astrid/plugin-src/com/todoroo/astrid/actfm/EditPeopleControlSet.java index e7404a7dd..eafe4c02a 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/EditPeopleControlSet.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/EditPeopleControlSet.java @@ -707,57 +707,6 @@ public class EditPeopleControlSet extends PopupControlSet { if(!TextUtils.isEmpty(assignedCustom.getText())) { userJson = PeopleContainer.createUserJson(assignedCustom); assignedView = assignedCustom; - if (actFmPreferenceService.isLoggedIn()) { - // check if the custom-user is already a member of this list - TodorooCursor tags = TagService.getInstance().getTags(task.getId(), true); - Metadata metadata = new Metadata(); - for(tags.moveToFirst(); !tags.isAfterLast(); tags.moveToNext()) { - metadata.readFromCursor(tags); - final String tag = metadata.getValue(TagService.TAG); - final TagData tagData = tagDataService.getTag(tag, TagData.MEMBER_COUNT, TagData.MEMBERS, TagData.USER); - if(tagData != null) { - String customUserEmail = userJson.optString("email", ""); - final JSONArray membersFromTag = new JSONArray(tagData.getValue(TagData.MEMBERS)); - boolean customUserIsMember = false; - for(int i = 0; i < membersFromTag.length(); i++) { - JSONObject member = membersFromTag.getJSONObject(i); - String email = member.optString("mail", ""); - if (!TextUtils.isEmpty(email) && email.equals(customUserEmail)) { - customUserIsMember = true; - break; - } - } - - if (!customUserIsMember) { - // show dialog to ask if user should be added to the tag-members - final JSONObject newMember = userJson; - DialogInterface.OnClickListener okListener = new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface d, int which) { - membersFromTag.put(newMember); - tagData.setValue(TagData.MEMBERS, membersFromTag.toString()); - tagData.setValue(TagData.MEMBER_COUNT, membersFromTag.length()); - tagDataService.save(tagData); - } - }; - - DialogInterface.OnClickListener cancelListener = new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface d, int which) { - // nothing to do here - } - }; - DialogUtilities.okCancelCustomDialog(activity, - activity.getString(R.string.actfm_EPA_add_person_to_list_title), - activity.getString(R.string.actfm_EPA_add_person_to_list, customUserEmail, customUserEmail), - R.string.actfm_EPA_add_person_to_list_ok, - R.string.actfm_EPA_add_person_to_list_cancel, - android.R.drawable.ic_dialog_alert, - okListener, cancelListener); - } - } - } - } } else { if (!loadedUI || assignedList.getCheckedItemPosition() == ListView.INVALID_POSITION) return true; diff --git a/astrid/src/com/todoroo/astrid/activity/TaskEditFragment.java b/astrid/src/com/todoroo/astrid/activity/TaskEditFragment.java index 218deb94c..5feadabb4 100755 --- a/astrid/src/com/todoroo/astrid/activity/TaskEditFragment.java +++ b/astrid/src/com/todoroo/astrid/activity/TaskEditFragment.java @@ -14,6 +14,9 @@ import java.util.List; import java.util.Locale; import java.util.concurrent.atomic.AtomicReference; +import org.json.JSONException; +import org.json.JSONObject; + import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; @@ -182,7 +185,9 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener { public static final String TOKEN_TASK_WAS_ASSIGNED = "task_assigned"; //$NON-NLS-1$ - public static final String TOKEN_ASSIGNED_TO = "task_assigned_to"; //$NON-NLS-1$ + public static final String TOKEN_ASSIGNED_TO_DISPLAY = "task_assigned_to_display"; //$NON-NLS-1$ + public static final String TOKEN_ASSIGNED_TO_EMAIL = "task_assigned_to_email"; //$NON-NLS-1$ + public static final String TOKEN_ASSIGNED_TO_ID = "task_assigned_to_id"; //$NON-NLS-1$ public static final String TOKEN_TAGS_CHANGED = "tags_changed"; //$NON-NLS-1$ public static final String TOKEN_NEW_REPEATING_TASK = "new_repeating"; //$NON-NLS-1$ @@ -859,12 +864,25 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener { boolean showRepeatAlert = model.getTransitory(TaskService.TRANS_REPEAT_CHANGED) != null && !TextUtils.isEmpty(model.getValue(Task.RECURRENCE)); String assignedTo = peopleControlSet.getAssignedToString(); + String assignedEmail = ""; //$NON-NLS-1$ + long assignedId = Task.USER_ID_IGNORE; + try { + JSONObject assignedUser = new JSONObject(model.getValue(Task.USER)); + assignedEmail = assignedUser.optString("email", ""); //$NON-NLS-1$ //$NON-NLS-2$ + assignedId = assignedUser.optLong("id", Task.USER_ID_IGNORE); + } catch (JSONException e) { + // + } + if (taskEditActivity) { Intent data = new Intent(); if (!isAssignedToMe) { data.putExtra(TOKEN_TASK_WAS_ASSIGNED, true); - data.putExtra(TOKEN_ASSIGNED_TO, - assignedTo); + data.putExtra(TOKEN_ASSIGNED_TO_DISPLAY, assignedTo); + if (!TextUtils.isEmpty(assignedEmail)) + data.putExtra(TOKEN_ASSIGNED_TO_EMAIL, assignedEmail); + if (assignedId > 0) + data.putExtra(TOKEN_ASSIGNED_TO_ID, assignedId); } if (showRepeatAlert) { data.putExtra(TOKEN_NEW_REPEATING_TASK, model); @@ -877,7 +895,7 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener { // since the activity isn't actually finishing TaskListActivity tla = (TaskListActivity) getActivity(); if (!isAssignedToMe) - tla.switchToAssignedFilter(assignedTo); + tla.taskAssignedTo(assignedTo, assignedEmail, assignedId); else if (showRepeatAlert) DateChangedAlerts.showRepeatChangedDialog(tla, model); diff --git a/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java b/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java index 816b07d79..85c8e22f6 100644 --- a/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java +++ b/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java @@ -5,6 +5,10 @@ */ package com.todoroo.astrid.activity; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + import android.animation.LayoutTransition; import android.app.Activity; import android.app.SearchManager; @@ -17,6 +21,7 @@ import android.os.Bundle; import android.support.v4.app.ActionBar; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager.OnPageChangeListener; +import android.text.TextUtils; import android.util.TypedValue; import android.view.KeyEvent; import android.view.View; @@ -46,6 +51,7 @@ import com.todoroo.astrid.api.AstridApiConstants; import com.todoroo.astrid.api.Filter; import com.todoroo.astrid.api.FilterListItem; import com.todoroo.astrid.core.CustomFilterExposer; +import com.todoroo.astrid.core.PluginServices; import com.todoroo.astrid.data.TagData; import com.todoroo.astrid.data.Task; import com.todoroo.astrid.people.PeopleFilterMode; @@ -616,8 +622,10 @@ public class TaskListActivity extends AstridActivity implements MainMenuListener if (tlf != null) { if (data != null) { if (data.getBooleanExtra(TaskEditFragment.TOKEN_TASK_WAS_ASSIGNED, false)) { - String assignedTo = data.getStringExtra(TaskEditFragment.TOKEN_ASSIGNED_TO); - switchToAssignedFilter(assignedTo); + String assignedTo = data.getStringExtra(TaskEditFragment.TOKEN_ASSIGNED_TO_DISPLAY); + String assignedEmail = data.getStringExtra(TaskEditFragment.TOKEN_ASSIGNED_TO_EMAIL); + long assignedId = data.getLongExtra(TaskEditFragment.TOKEN_ASSIGNED_TO_ID, Task.USER_ID_IGNORE); + taskAssignedTo(assignedTo, assignedEmail, assignedId); } else if (data.getParcelableExtra(TaskEditFragment.TOKEN_NEW_REPEATING_TASK) != null) { Task repeating = data.getParcelableExtra(TaskEditFragment.TOKEN_NEW_REPEATING_TASK); DateChangedAlerts.showRepeatChangedDialog(this, repeating); @@ -652,8 +660,8 @@ public class TaskListActivity extends AstridActivity implements MainMenuListener tlf.refresh(); } - public void switchToAssignedFilter(final String assignedEmail) { - TaskListFragment tlf = getTaskListFragment(); + public void taskAssignedTo(final String assignedDisplay, String assignedEmail, final long assignedId) { + final TaskListFragment tlf = getTaskListFragment(); if (tlf != null && tlf.isInbox()) { DialogInterface.OnClickListener okListener = new DialogInterface.OnClickListener() { @Override @@ -664,9 +672,67 @@ public class TaskListActivity extends AstridActivity implements MainMenuListener }; DialogUtilities.okCancelCustomDialog(this, getString(R.string.actfm_view_task_title), - getString(R.string.actfm_view_task_text, assignedEmail), + getString(R.string.actfm_view_task_text, assignedDisplay), R.string.actfm_view_task_ok, R.string.actfm_view_task_cancel, 0, okListener, null); + } else if (tlf != null && (!TextUtils.isEmpty(assignedEmail) || assignedId > 0)) { + checkAddTagMember(tlf, assignedDisplay, assignedEmail, assignedId); + } + } + + private void checkAddTagMember(final TaskListFragment tlf, final String assignedDisplay, String assignedEmail, long assignedId) { + final TagData td = tlf.getActiveTagData(); + if (td != null) { + String members = td.getValue(TagData.MEMBERS); + if (members == null) + members = ""; //$NON-NLS-1$ + if (TextUtils.isEmpty(members) || TextUtils.isEmpty(assignedEmail) || !members.contains(assignedEmail)) { + // show dialog to ask if user should be added to the tag-members + JSONObject user = new JSONObject(); + JSONArray membersArray = null; + boolean memberFound = false; + try { + if (!TextUtils.isEmpty(assignedEmail)) + user.put("email", assignedEmail); //$NON-NLS-1$ + if (assignedId > 0) + user.put("id", assignedId); //$NON-NLS-1$ + membersArray = new JSONArray(members); + + for (int i = 0; i < membersArray.length(); i++) { + JSONObject member = membersArray.getJSONObject(i); + long memberId = member.optLong("id", Task.USER_ID_IGNORE); //$NON-NLS-1$ + if (memberId > 0 && memberId == assignedId) { + memberFound = true; + break; + } + } + } catch (JSONException e) { + return; + } + + if (memberFound) + return; + + membersArray.put(user); + + final JSONArray finalArray = membersArray; + DialogInterface.OnClickListener okListener = new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface d, int which) { + td.setValue(TagData.MEMBERS, finalArray.toString()); + td.setValue(TagData.MEMBER_COUNT, finalArray.length()); + PluginServices.getTagDataService().save(td); + tlf.refresh(); + } + }; + DialogUtilities.okCancelCustomDialog(this, + getString(R.string.actfm_EPA_add_person_to_list_title), + getString(R.string.actfm_EPA_add_person_to_list, assignedDisplay, assignedDisplay), + R.string.actfm_EPA_add_person_to_list_ok, + R.string.actfm_EPA_add_person_to_list_cancel, + android.R.drawable.ic_dialog_alert, + okListener, null); + } } } diff --git a/astrid/src/com/todoroo/astrid/ui/QuickAddBar.java b/astrid/src/com/todoroo/astrid/ui/QuickAddBar.java index 16aac0aea..046e94c81 100644 --- a/astrid/src/com/todoroo/astrid/ui/QuickAddBar.java +++ b/astrid/src/com/todoroo/astrid/ui/QuickAddBar.java @@ -8,6 +8,9 @@ package com.todoroo.astrid.ui; import java.util.HashSet; import java.util.concurrent.atomic.AtomicReference; +import org.json.JSONException; +import org.json.JSONObject; + import android.app.Activity; import android.content.ContentValues; import android.content.Context; @@ -309,6 +312,15 @@ public class QuickAddBar extends LinearLayout { TaskService.createWithValues(task, fragment.getFilter().valuesForNewTasks, title); String assignedTo = peopleControl.getAssignedToString(); + String assignedEmail = ""; + long assignedId = Task.USER_ID_IGNORE; + try { + JSONObject assignedUser = new JSONObject(task.getValue(Task.USER)); + assignedEmail = assignedUser.optString("email", ""); //$NON-NLS-1$ //$NON-NLS-2$ + assignedId = assignedUser.optLong("id", Task.USER_ID_IGNORE); + } catch (JSONException e) { + // + } resetControlSets(); @@ -318,7 +330,7 @@ public class QuickAddBar extends LinearLayout { fragment.showTaskEditHelpPopover(); if (activity instanceof TaskListActivity && !assignedToMe) - ((TaskListActivity) activity).switchToAssignedFilter(assignedTo); + ((TaskListActivity) activity).taskAssignedTo(assignedTo, assignedEmail, assignedId); TextView quickAdd = (TextView) findViewById(R.id.quickAddText); quickAdd.setText(""); //$NON-NLS-1$