From 312104dead0e39b43b58e796f1d56f7151a6bc6e Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Mon, 18 Mar 2013 16:25:21 -0700 Subject: [PATCH] Fixed several bugs with how tag deleting and renaming from long press work --- .../astrid/actfm/TagSettingsActivity.java | 7 +- .../todoroo/astrid/tags/TagFilterExposer.java | 24 +++-- .../com/todoroo/astrid/tags/TagService.java | 11 +-- .../astrid/activity/FilterListFragment.java | 6 +- .../astrid/activity/TaskListActivity.java | 98 ++++++++++--------- 5 files changed, 75 insertions(+), 71 deletions(-) diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/TagSettingsActivity.java b/astrid/plugin-src/com/todoroo/astrid/actfm/TagSettingsActivity.java index 6218933b6..49a8eabc4 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/TagSettingsActivity.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/TagSettingsActivity.java @@ -588,11 +588,10 @@ public class TagSettingsActivity extends FragmentActivity { }, null ); } - protected boolean deleteTag() { - boolean result = tagService.deleteOrLeaveTag(this, tagData.getValue(TagData.NAME), tagData.getUuid()); - setResult(Activity.RESULT_OK); + protected void deleteTag() { + Intent result = tagService.deleteOrLeaveTag(this, tagData.getValue(TagData.NAME), tagData.getUuid()); + setResult(Activity.RESULT_OK, result); finish(); - return result; } } diff --git a/astrid/plugin-src/com/todoroo/astrid/tags/TagFilterExposer.java b/astrid/plugin-src/com/todoroo/astrid/tags/TagFilterExposer.java index e79639118..a1a13aa90 100644 --- a/astrid/plugin-src/com/todoroo/astrid/tags/TagFilterExposer.java +++ b/astrid/plugin-src/com/todoroo/astrid/tags/TagFilterExposer.java @@ -232,8 +232,9 @@ public class TagFilterExposer extends BroadcastReceiver implements AstridFilterE @Override public void onClick(DialogInterface dialog, int which) { try { - if (ok()) { - setResult(RESULT_OK); + Intent result = ok(); + if (result != null) { + setResult(RESULT_OK, result); } else { toastNoChanges(); setResult(RESULT_CANCELED); @@ -267,7 +268,7 @@ public class TagFilterExposer extends BroadcastReceiver implements AstridFilterE protected abstract void showDialog(TagData tagData); - protected abstract boolean ok(); + protected abstract Intent ok(); } public static class DeleteTagActivity extends TagActivity { @@ -287,7 +288,7 @@ public class TagFilterExposer extends BroadcastReceiver implements AstridFilterE } @Override - protected boolean ok() { + protected Intent ok() { return tagService.deleteOrLeaveTag(this, tag, uuid); } @@ -304,18 +305,25 @@ public class TagFilterExposer extends BroadcastReceiver implements AstridFilterE } @Override - protected boolean ok() { + protected Intent ok() { if(editor == null) - return false; + return null; String text = editor.getText().toString(); if (text == null || text.length() == 0) { - return false; + return null; } else { int renamed = tagService.rename(uuid, text); Toast.makeText(this, getString(R.string.TEA_tags_renamed, tag, text, renamed), Toast.LENGTH_SHORT).show(); - return true; + + if (renamed > 0) { + Intent intent = new Intent(AstridApiConstants.BROADCAST_EVENT_TAG_RENAMED); + intent.putExtra(TagViewFragment.EXTRA_TAG_UUID, uuid); + ContextManager.getContext().sendBroadcast(intent); + return intent; + } + return null; } } } diff --git a/astrid/plugin-src/com/todoroo/astrid/tags/TagService.java b/astrid/plugin-src/com/todoroo/astrid/tags/TagService.java index a12397f0d..bbeffbeb9 100644 --- a/astrid/plugin-src/com/todoroo/astrid/tags/TagService.java +++ b/astrid/plugin-src/com/todoroo/astrid/tags/TagService.java @@ -22,7 +22,6 @@ import com.todoroo.andlib.data.Property; import com.todoroo.andlib.data.Property.CountProperty; import com.todoroo.andlib.data.TodorooCursor; import com.todoroo.andlib.service.Autowired; -import com.todoroo.andlib.service.ContextManager; import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.andlib.sql.Criterion; import com.todoroo.andlib.sql.Field; @@ -374,7 +373,7 @@ public final class TagService { return tagBuilder.toString(); } - public boolean deleteOrLeaveTag(Context context, String tag, String uuid) { + public Intent deleteOrLeaveTag(Context context, String tag, String uuid) { int deleted = deleteTagMetadata(uuid); TagData tagData = tagDataDao.fetch(uuid, TagData.ID, TagData.UUID, TagData.DELETION_DATE, TagData.MEMBER_COUNT, TagData.USER_ID); boolean shared = false; @@ -389,7 +388,7 @@ public final class TagService { Toast.LENGTH_SHORT).show(); context.sendBroadcast(tagDeleted); - return true; + return tagDeleted; } /** @@ -557,12 +556,6 @@ public final class TagService { result = metadataDao.update(Criterion.and(MetadataCriteria.withKey(TaskToTagMetadata.KEY), TaskToTagMetadata.TAG_UUID.eq(uuid)), metadataTemplate); tagRenamed = tagRenamed || result > 0; - if (tagRenamed) { - Intent intent = new Intent(AstridApiConstants.BROADCAST_EVENT_TAG_RENAMED); - intent.putExtra(TagViewFragment.EXTRA_TAG_UUID, uuid); - ContextManager.getContext().sendBroadcast(intent); - } - return result; } diff --git a/astrid/src/com/todoroo/astrid/activity/FilterListFragment.java b/astrid/src/com/todoroo/astrid/activity/FilterListFragment.java index e2a9d41ca..a4bf3e832 100644 --- a/astrid/src/com/todoroo/astrid/activity/FilterListFragment.java +++ b/astrid/src/com/todoroo/astrid/activity/FilterListFragment.java @@ -82,7 +82,7 @@ public class FilterListFragment extends ListFragment { private static final int CONTEXT_MENU_SHORTCUT = R.string.FLA_context_shortcut; private static final int CONTEXT_MENU_INTENT = Menu.FIRST + 4; - private static final int REQUEST_CUSTOM_INTENT = 1; + public static final int REQUEST_CUSTOM_INTENT = 10; static final int REQUEST_VIEW_TASKS = 2; public static final int REQUEST_NEW_BUTTON = 3; public static final int REQUEST_NEW_LIST = 4; @@ -257,7 +257,7 @@ public class FilterListFragment extends ListFragment { if (which == 0) { showCreateShortcutDialog(getActivity(), ShortcutActivity.createIntent(filter), filter); } else { - startActivityForResult(intents[which - 1], REQUEST_CUSTOM_INTENT); + getActivity().startActivityForResult(intents[which - 1], REQUEST_CUSTOM_INTENT); } } }); @@ -391,7 +391,7 @@ public class FilterListFragment extends ListFragment { } case CONTEXT_MENU_INTENT: { Intent intent = item.getIntent(); - startActivityForResult(intent, REQUEST_CUSTOM_INTENT); + getActivity().startActivityForResult(intent, REQUEST_CUSTOM_INTENT); return true; } default: { diff --git a/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java b/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java index a12ca24e5..b6eafc759 100644 --- a/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java +++ b/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java @@ -12,11 +12,9 @@ import org.json.JSONObject; import android.animation.LayoutTransition; import android.app.Activity; import android.app.SearchManager; -import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; -import android.content.IntentFilter; import android.os.Bundle; import android.support.v4.app.ActionBar; import android.support.v4.app.Fragment; @@ -51,7 +49,9 @@ import com.todoroo.astrid.adapter.TaskListFragmentPagerAdapter; import com.todoroo.astrid.api.AstridApiConstants; import com.todoroo.astrid.api.Filter; import com.todoroo.astrid.api.FilterListItem; +import com.todoroo.astrid.core.CoreFilterExposer; import com.todoroo.astrid.core.CustomFilterExposer; +import com.todoroo.astrid.core.PluginServices; import com.todoroo.astrid.dao.TagMetadataDao; import com.todoroo.astrid.data.RemoteModel; import com.todoroo.astrid.data.TagData; @@ -62,6 +62,7 @@ import com.todoroo.astrid.service.StatisticsConstants; import com.todoroo.astrid.service.StatisticsService; import com.todoroo.astrid.service.ThemeService; import com.todoroo.astrid.service.abtesting.ABTestEventReportingService; +import com.todoroo.astrid.tags.TagFilterExposer; import com.todoroo.astrid.tags.TagsPlugin; import com.todoroo.astrid.tags.reusable.FeaturedListFilterMode; import com.todoroo.astrid.ui.DateChangedAlerts; @@ -82,7 +83,7 @@ public class TaskListActivity extends AstridActivity implements MainMenuListener /** token for indicating source of TLA launch */ public static final String TOKEN_SOURCE = "source"; //$NON-NLS-1$ - public static final String TOKEN_NEW_LIST_CREATED = "newListCreated"; //$NON-NLS-1$ + public static final String TOKEN_SWITCH_TO_FILTER = "newListCreated"; //$NON-NLS-1$ /** For indicating the new list screen should be launched at fragment setup time */ public static final String TOKEN_CREATE_NEW_LIST = "createNewList"; //$NON-NLS-1$ @@ -121,9 +122,6 @@ public class TaskListActivity extends AstridActivity implements MainMenuListener private boolean swipeEnabled = false; - private final TagDeletedReceiver tagDeletedReceiver = new TagDeletedReceiver(); - private final TagRenamedReceiver tagRenamedReceiver = new TagRenamedReceiver(); - private final OnClickListener mainMenuClickListener = new OnClickListener() { @Override public void onClick(View v) { @@ -465,9 +463,9 @@ public class TaskListActivity extends AstridActivity implements MainMenuListener onBackPressed(); } - if (getIntent().hasExtra(TOKEN_NEW_LIST_CREATED)) { - Filter newList = getIntent().getParcelableExtra(TOKEN_NEW_LIST_CREATED); - getIntent().removeExtra(TOKEN_NEW_LIST_CREATED); + if (getIntent().hasExtra(TOKEN_SWITCH_TO_FILTER)) { + Filter newList = getIntent().getParcelableExtra(TOKEN_SWITCH_TO_FILTER); + getIntent().removeExtra(TOKEN_SWITCH_TO_FILTER); onFilterItemClicked(newList); } @@ -520,8 +518,6 @@ public class TaskListActivity extends AstridActivity implements MainMenuListener @Override protected void onResume() { super.onResume(); - registerReceiver(tagDeletedReceiver, new IntentFilter(AstridApiConstants.BROADCAST_EVENT_TAG_DELETED)); - registerReceiver(tagRenamedReceiver, new IntentFilter(AstridApiConstants.BROADCAST_EVENT_TAG_RENAMED)); if (Preferences.getBoolean(WelcomeWalkthrough.KEY_SHOWED_WELCOME_LOGIN, false)) SyncUpgradePrompt.showSyncUpgradePrompt(this); @@ -537,8 +533,6 @@ public class TaskListActivity extends AstridActivity implements MainMenuListener @Override protected void onStop() { super.onStop(); - AndroidUtilities.tryUnregisterReceiver(this, tagDeletedReceiver); - AndroidUtilities.tryUnregisterReceiver(this, tagRenamedReceiver); } public void setSelectedItem(Filter item) { @@ -624,7 +618,7 @@ public class TaskListActivity extends AstridActivity implements MainMenuListener Filter newList = data.getParcelableExtra(TagSettingsActivity.TOKEN_NEW_FILTER); if (newList != null) { - getIntent().putExtra(TOKEN_NEW_LIST_CREATED, newList); // Handle in onPostResume() + getIntent().putExtra(TOKEN_SWITCH_TO_FILTER, newList); // Handle in onPostResume() FilterListFragment fla = getFilterListFragment(); if (fla != null && getFragmentLayout() != LAYOUT_SINGLE) fla.clear(); @@ -649,6 +643,49 @@ public class TaskListActivity extends AstridActivity implements MainMenuListener } tlf.refresh(); } + } else if (requestCode == FilterListFragment.REQUEST_CUSTOM_INTENT && resultCode == RESULT_OK && data != null) { + // Tag renamed or deleted + String action = data.getAction(); + String uuid = data.getStringExtra(TagViewFragment.EXTRA_TAG_UUID); + + if (AstridApiConstants.BROADCAST_EVENT_TAG_DELETED.equals(action)) { + TaskListFragment tlf = getTaskListFragment(); + FilterListFragment fl = getFilterListFragment(); + if (tlf != null) { + TagData tagData = tlf.getActiveTagData(); + String activeUuid = RemoteModel.NO_UUID; + if (tagData != null) + activeUuid = tagData.getUuid(); + + if (activeUuid.equals(uuid)) { + getIntent().putExtra(TOKEN_SWITCH_TO_FILTER, CoreFilterExposer.buildInboxFilter(getResources())); // Handle in onPostResume() + fl.clear(); // Should auto refresh + } else { + tlf.refresh(); + } + } + + if (fl != null) + fl.refresh(); + } else if (AstridApiConstants.BROADCAST_EVENT_TAG_RENAMED.equals(action)) { + TaskListFragment tlf = getTaskListFragment(); + if (tlf != null) { + TagData td = tlf.getActiveTagData(); + if (td != null && td.getUuid().equals(uuid)) { + td = PluginServices.getTagDataDao().fetch(uuid, TagData.PROPERTIES); + if (td != null) { + Filter filter = TagFilterExposer.filterFromTagData(this, td); + getIntent().putExtra(TOKEN_SWITCH_TO_FILTER, filter); + } + } else { + tlf.refresh(); + } + } + + FilterListFragment flf = getFilterListFragment(); + if (flf != null) + flf.refresh(); + } } super.onActivityResult(requestCode, resultCode, data); @@ -921,37 +958,4 @@ public class TaskListActivity extends AstridActivity implements MainMenuListener } return super.onKeyDown(keyCode, event); } - - private class TagRenamedReceiver extends BroadcastReceiver { - @Override - public void onReceive(Context context, Intent intent) { - TaskListFragment tlf = getTaskListFragment(); - if (tlf != null) - tlf.refresh(); - - FilterListFragment flf = getFilterListFragment(); - if (flf != null) - flf.refresh(); - } - } - - private class TagDeletedReceiver extends BroadcastReceiver { - @Override - public void onReceive(Context context, Intent intent) { - String uuid = intent.getStringExtra(TagViewFragment.EXTRA_TAG_UUID); - TaskListFragment tlf = getTaskListFragment(); - FilterListFragment fl = getFilterListFragment(); - if (tlf != null) { - TagData tagData = tlf.getActiveTagData(); - String activeUuid = RemoteModel.NO_UUID; - if (tagData != null) - activeUuid = tagData.getUuid(); - - if (activeUuid.equals(uuid)) { - fl.switchToActiveTasks(); - fl.clear(); // Should auto refresh - } - } - } - } }