Refactored some of arne's code for adding assignees to a list, fixed the ui refresh issue

pull/14/head
Sam Bosley 12 years ago
parent e349349e7e
commit 53d6c2387d

@ -707,57 +707,6 @@ public class EditPeopleControlSet extends PopupControlSet {
if(!TextUtils.isEmpty(assignedCustom.getText())) { if(!TextUtils.isEmpty(assignedCustom.getText())) {
userJson = PeopleContainer.createUserJson(assignedCustom); userJson = PeopleContainer.createUserJson(assignedCustom);
assignedView = assignedCustom; assignedView = assignedCustom;
if (actFmPreferenceService.isLoggedIn()) {
// check if the custom-user is already a member of this list
TodorooCursor<Metadata> 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 { } else {
if (!loadedUI || assignedList.getCheckedItemPosition() == ListView.INVALID_POSITION) if (!loadedUI || assignedList.getCheckedItemPosition() == ListView.INVALID_POSITION)
return true; return true;

@ -14,6 +14,9 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Dialog; 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_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_TAGS_CHANGED = "tags_changed"; //$NON-NLS-1$
public static final String TOKEN_NEW_REPEATING_TASK = "new_repeating"; //$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 boolean showRepeatAlert = model.getTransitory(TaskService.TRANS_REPEAT_CHANGED) != null
&& !TextUtils.isEmpty(model.getValue(Task.RECURRENCE)); && !TextUtils.isEmpty(model.getValue(Task.RECURRENCE));
String assignedTo = peopleControlSet.getAssignedToString(); 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) { if (taskEditActivity) {
Intent data = new Intent(); Intent data = new Intent();
if (!isAssignedToMe) { if (!isAssignedToMe) {
data.putExtra(TOKEN_TASK_WAS_ASSIGNED, true); data.putExtra(TOKEN_TASK_WAS_ASSIGNED, true);
data.putExtra(TOKEN_ASSIGNED_TO, data.putExtra(TOKEN_ASSIGNED_TO_DISPLAY, assignedTo);
assignedTo); if (!TextUtils.isEmpty(assignedEmail))
data.putExtra(TOKEN_ASSIGNED_TO_EMAIL, assignedEmail);
if (assignedId > 0)
data.putExtra(TOKEN_ASSIGNED_TO_ID, assignedId);
} }
if (showRepeatAlert) { if (showRepeatAlert) {
data.putExtra(TOKEN_NEW_REPEATING_TASK, model); data.putExtra(TOKEN_NEW_REPEATING_TASK, model);
@ -877,7 +895,7 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener {
// since the activity isn't actually finishing // since the activity isn't actually finishing
TaskListActivity tla = (TaskListActivity) getActivity(); TaskListActivity tla = (TaskListActivity) getActivity();
if (!isAssignedToMe) if (!isAssignedToMe)
tla.switchToAssignedFilter(assignedTo); tla.taskAssignedTo(assignedTo, assignedEmail, assignedId);
else if (showRepeatAlert) else if (showRepeatAlert)
DateChangedAlerts.showRepeatChangedDialog(tla, model); DateChangedAlerts.showRepeatChangedDialog(tla, model);

@ -5,6 +5,10 @@
*/ */
package com.todoroo.astrid.activity; package com.todoroo.astrid.activity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.animation.LayoutTransition; import android.animation.LayoutTransition;
import android.app.Activity; import android.app.Activity;
import android.app.SearchManager; import android.app.SearchManager;
@ -17,6 +21,7 @@ import android.os.Bundle;
import android.support.v4.app.ActionBar; import android.support.v4.app.ActionBar;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager.OnPageChangeListener; import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.View; 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.Filter;
import com.todoroo.astrid.api.FilterListItem; import com.todoroo.astrid.api.FilterListItem;
import com.todoroo.astrid.core.CustomFilterExposer; import com.todoroo.astrid.core.CustomFilterExposer;
import com.todoroo.astrid.core.PluginServices;
import com.todoroo.astrid.data.TagData; import com.todoroo.astrid.data.TagData;
import com.todoroo.astrid.data.Task; import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.people.PeopleFilterMode; import com.todoroo.astrid.people.PeopleFilterMode;
@ -616,8 +622,10 @@ public class TaskListActivity extends AstridActivity implements MainMenuListener
if (tlf != null) { if (tlf != null) {
if (data != null) { if (data != null) {
if (data.getBooleanExtra(TaskEditFragment.TOKEN_TASK_WAS_ASSIGNED, false)) { if (data.getBooleanExtra(TaskEditFragment.TOKEN_TASK_WAS_ASSIGNED, false)) {
String assignedTo = data.getStringExtra(TaskEditFragment.TOKEN_ASSIGNED_TO); String assignedTo = data.getStringExtra(TaskEditFragment.TOKEN_ASSIGNED_TO_DISPLAY);
switchToAssignedFilter(assignedTo); 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) { } else if (data.getParcelableExtra(TaskEditFragment.TOKEN_NEW_REPEATING_TASK) != null) {
Task repeating = data.getParcelableExtra(TaskEditFragment.TOKEN_NEW_REPEATING_TASK); Task repeating = data.getParcelableExtra(TaskEditFragment.TOKEN_NEW_REPEATING_TASK);
DateChangedAlerts.showRepeatChangedDialog(this, repeating); DateChangedAlerts.showRepeatChangedDialog(this, repeating);
@ -652,8 +660,8 @@ public class TaskListActivity extends AstridActivity implements MainMenuListener
tlf.refresh(); tlf.refresh();
} }
public void switchToAssignedFilter(final String assignedEmail) { public void taskAssignedTo(final String assignedDisplay, String assignedEmail, final long assignedId) {
TaskListFragment tlf = getTaskListFragment(); final TaskListFragment tlf = getTaskListFragment();
if (tlf != null && tlf.isInbox()) { if (tlf != null && tlf.isInbox()) {
DialogInterface.OnClickListener okListener = new DialogInterface.OnClickListener() { DialogInterface.OnClickListener okListener = new DialogInterface.OnClickListener() {
@Override @Override
@ -664,9 +672,67 @@ public class TaskListActivity extends AstridActivity implements MainMenuListener
}; };
DialogUtilities.okCancelCustomDialog(this, DialogUtilities.okCancelCustomDialog(this,
getString(R.string.actfm_view_task_title), 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, R.string.actfm_view_task_ok, R.string.actfm_view_task_cancel,
0, okListener, null); 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);
}
} }
} }

@ -8,6 +8,9 @@ package com.todoroo.astrid.ui;
import java.util.HashSet; import java.util.HashSet;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity; import android.app.Activity;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context; import android.content.Context;
@ -309,6 +312,15 @@ public class QuickAddBar extends LinearLayout {
TaskService.createWithValues(task, fragment.getFilter().valuesForNewTasks, title); TaskService.createWithValues(task, fragment.getFilter().valuesForNewTasks, title);
String assignedTo = peopleControl.getAssignedToString(); 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(); resetControlSets();
@ -318,7 +330,7 @@ public class QuickAddBar extends LinearLayout {
fragment.showTaskEditHelpPopover(); fragment.showTaskEditHelpPopover();
if (activity instanceof TaskListActivity && !assignedToMe) if (activity instanceof TaskListActivity && !assignedToMe)
((TaskListActivity) activity).switchToAssignedFilter(assignedTo); ((TaskListActivity) activity).taskAssignedTo(assignedTo, assignedEmail, assignedId);
TextView quickAdd = (TextView) findViewById(R.id.quickAddText); TextView quickAdd = (TextView) findViewById(R.id.quickAddText);
quickAdd.setText(""); //$NON-NLS-1$ quickAdd.setText(""); //$NON-NLS-1$

Loading…
Cancel
Save