diff --git a/astrid/plugin-src/com/todoroo/astrid/tags/TagFilterExposer.java b/astrid/plugin-src/com/todoroo/astrid/tags/TagFilterExposer.java index 20ae96e87..da0b82135 100644 --- a/astrid/plugin-src/com/todoroo/astrid/tags/TagFilterExposer.java +++ b/astrid/plugin-src/com/todoroo/astrid/tags/TagFilterExposer.java @@ -4,11 +4,6 @@ package com.todoroo.astrid.tags; import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map.Entry; import android.app.Activity; import android.app.PendingIntent; @@ -22,22 +17,18 @@ import android.content.res.Resources; import android.graphics.Color; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; -import android.text.TextUtils; import android.widget.EditText; import android.widget.Toast; import com.timsu.astrid.R; -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.Query; import com.todoroo.andlib.sql.QueryTemplate; import com.todoroo.andlib.utility.DateUtilities; import com.todoroo.andlib.utility.DialogUtilities; import com.todoroo.astrid.actfm.TagViewActivity; -import com.todoroo.astrid.actfm.sync.ActFmPreferenceService; import com.todoroo.astrid.api.AstridApiConstants; import com.todoroo.astrid.api.Filter; import com.todoroo.astrid.api.FilterCategory; @@ -49,7 +40,6 @@ import com.todoroo.astrid.core.PluginServices; import com.todoroo.astrid.dao.TaskDao.TaskCriteria; import com.todoroo.astrid.data.Metadata; import com.todoroo.astrid.data.TagData; -import com.todoroo.astrid.data.Update; import com.todoroo.astrid.service.AstridDependencyInjector; import com.todoroo.astrid.service.TagDataService; import com.todoroo.astrid.tags.TagService.Tag; @@ -141,48 +131,7 @@ public class TagFilterExposer extends BroadcastReceiver { } private void addTags(ArrayList list) { - HashMap tags = new HashMap(); - - Tag[] tagsByAlpha = tagService.getGroupedTags(TagService.GROUPED_TAGS_BY_ALPHA, - TaskCriteria.activeAndVisible()); - for(Tag tag : tagsByAlpha) - if(!TextUtils.isEmpty(tag.tag)) - tags.put(tag.tag, tag); - - TodorooCursor cursor = tagDataService.query(Query.select(TagData.PROPERTIES)); - try { - TagData tagData = new TagData(); - for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { - tagData.readFromCursor(cursor); - String tagName = tagData.getValue(TagData.NAME).trim(); - Tag tag = new Tag(tagData); - if(tagData.getValue(TagData.DELETION_DATE) > 0 && !tags.containsKey(tagName)) continue; - if(TextUtils.isEmpty(tag.tag)) - continue; - tags.put(tagName, tag); - - Update update = tagDataService.getLatestUpdate(tagData); - if(update != null) - tag.updateText = ActFmPreferenceService.updateToString(update); - } - } finally { - cursor.close(); - } - - ArrayList tagList = new ArrayList(tags.values()); - Collections.sort(tagList, - new Comparator() { - @Override - public int compare(Tag object1, Tag object2) { - return object1.tag.compareToIgnoreCase(object2.tag); - } - }); - for(Iterator> i = tags.entrySet().iterator(); i.hasNext(); ) { - Entry entry = i.next(); - if(TextUtils.isEmpty(entry.getValue().tag)) - i.remove(); - } - + ArrayList tagList = TagService.getInstance().getTagList(); list.add(filterFromTags(tagList.toArray(new Tag[tagList.size()]), R.string.tag_FEx_header)); } diff --git a/astrid/plugin-src/com/todoroo/astrid/tags/TagService.java b/astrid/plugin-src/com/todoroo/astrid/tags/TagService.java index 0671719ef..f08aa8f31 100644 --- a/astrid/plugin-src/com/todoroo/astrid/tags/TagService.java +++ b/astrid/plugin-src/com/todoroo/astrid/tags/TagService.java @@ -1,9 +1,14 @@ package com.todoroo.astrid.tags; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashSet; +import android.text.TextUtils; + import com.todoroo.andlib.data.Property.CountProperty; import com.todoroo.andlib.data.Property.LongProperty; import com.todoroo.andlib.data.Property.StringProperty; @@ -17,6 +22,7 @@ import com.todoroo.andlib.sql.Join; import com.todoroo.andlib.sql.Order; import com.todoroo.andlib.sql.Query; import com.todoroo.andlib.sql.QueryTemplate; +import com.todoroo.astrid.actfm.sync.ActFmPreferenceService; import com.todoroo.astrid.core.PluginServices; import com.todoroo.astrid.dao.MetadataDao; import com.todoroo.astrid.dao.MetadataDao.MetadataCriteria; @@ -24,6 +30,7 @@ import com.todoroo.astrid.dao.TaskDao.TaskCriteria; import com.todoroo.astrid.data.Metadata; import com.todoroo.astrid.data.TagData; import com.todoroo.astrid.data.Task; +import com.todoroo.astrid.data.Update; import com.todoroo.astrid.service.MetadataService; import com.todoroo.astrid.service.TagDataService; import com.todoroo.astrid.service.TaskService; @@ -214,6 +221,49 @@ public final class TagService { return tagBuilder.toString(); } + /** + * Return all tags (including metadata tags and TagData tags) in an array list + * @return + */ + public ArrayList getTagList() { + HashMap tags = new HashMap(); + + Tag[] tagsByAlpha = getGroupedTags(TagService.GROUPED_TAGS_BY_ALPHA, + TaskCriteria.activeAndVisible()); + for(Tag tag : tagsByAlpha) + if(!TextUtils.isEmpty(tag.tag)) + tags.put(tag.tag, tag); + + TodorooCursor cursor = tagDataService.query(Query.select(TagData.PROPERTIES)); + try { + TagData tagData = new TagData(); + for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { + tagData.readFromCursor(cursor); + String tagName = tagData.getValue(TagData.NAME).trim(); + Tag tag = new Tag(tagData); + if(tagData.getValue(TagData.DELETION_DATE) > 0 && !tags.containsKey(tagName)) continue; + if(TextUtils.isEmpty(tag.tag)) + continue; + tags.put(tagName, tag); + + Update update = tagDataService.getLatestUpdate(tagData); + if(update != null) + tag.updateText = ActFmPreferenceService.updateToString(update); + } + } finally { + cursor.close(); + } + ArrayList tagList = new ArrayList(tags.values()); + Collections.sort(tagList, + new Comparator() { + @Override + public int compare(Tag object1, Tag object2) { + return object1.tag.compareToIgnoreCase(object2.tag); + } + }); + return tagList; + } + /** * Save the given array of tags into the database * @param taskId diff --git a/astrid/plugin-src/com/todoroo/astrid/tags/TagsControlSet.java b/astrid/plugin-src/com/todoroo/astrid/tags/TagsControlSet.java index 9f0641e87..3adc067f6 100644 --- a/astrid/plugin-src/com/todoroo/astrid/tags/TagsControlSet.java +++ b/astrid/plugin-src/com/todoroo/astrid/tags/TagsControlSet.java @@ -2,9 +2,7 @@ package com.todoroo.astrid.tags; import java.util.ArrayList; import java.util.Arrays; -import java.util.HashSet; import java.util.LinkedHashSet; -import java.util.Set; import android.app.Activity; import android.text.Editable; @@ -28,12 +26,9 @@ import com.todoroo.andlib.data.AbstractModel; import com.todoroo.andlib.data.TodorooCursor; import com.todoroo.andlib.service.Autowired; import com.todoroo.andlib.service.DependencyInjectionService; -import com.todoroo.andlib.sql.Criterion; -import com.todoroo.andlib.sql.Query; import com.todoroo.andlib.utility.DateUtilities; import com.todoroo.astrid.activity.TaskEditActivity.TaskEditControlSet; import com.todoroo.astrid.data.Metadata; -import com.todoroo.astrid.data.TagData; import com.todoroo.astrid.data.Task; import com.todoroo.astrid.service.TagDataService; import com.todoroo.astrid.tags.TagService.Tag; @@ -94,29 +89,7 @@ public final class TagsControlSet implements TaskEditControlSet { } private Tag[] getTagArray() { - //Get metadata tags - ArrayList tagsList = new ArrayList(); - Tag[] groupedTags = tagService.getGroupedTags(TagService.GROUPED_TAGS_BY_ALPHA, Criterion.all); - Set tagNames = new HashSet(); - for (Tag t : groupedTags) { - tagNames.add(t.tag); - tagsList.add(t); - } - - // Get tag data tags to include those tags that aren't in metadata - TodorooCursor tagDataTags = tagDataService.query(Query.select(TagData.NAME, TagData.TASK_COUNT, TagData.REMOTE_ID, TagData.PICTURE)); - try { - for (tagDataTags.moveToFirst(); !tagDataTags.isAfterLast(); tagDataTags.moveToNext()) { - TagData tagData = new TagData(tagDataTags); - String name = tagData.getValue(TagData.NAME); - if (!tagNames.contains(name)) { - tagNames.add(name); - tagsList.add(new Tag(tagData)); - } - } - } finally { - tagDataTags.close(); - } + ArrayList tagsList = TagService.getInstance().getTagList(); return tagsList.toArray(new Tag[tagsList.size()]); }