Show tag colors in tag control set display string

pull/413/head
Alex Baker 10 years ago
parent 458cdfc11a
commit 3f76d355dc

@ -10,7 +10,6 @@ import android.app.PendingIntent.CanceledException;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.database.Cursor; import android.database.Cursor;
import android.graphics.Color;
import android.graphics.Paint; import android.graphics.Paint;
import android.text.SpannableString; import android.text.SpannableString;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;

@ -5,14 +5,18 @@
*/ */
package com.todoroo.astrid.tags; package com.todoroo.astrid.tags;
import android.app.Dialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.text.Editable; import android.text.Editable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -30,6 +34,8 @@ import com.google.common.base.Predicate;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.todoroo.andlib.sql.Criterion; import com.todoroo.andlib.sql.Criterion;
import com.todoroo.andlib.sql.Query; import com.todoroo.andlib.sql.Query;
@ -42,14 +48,14 @@ import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.utility.Flags; import com.todoroo.astrid.utility.Flags;
import org.tasks.R; import org.tasks.R;
import org.tasks.dialogs.AlertDialogBuilder;
import org.tasks.dialogs.DialogBuilder; import org.tasks.dialogs.DialogBuilder;
import org.tasks.injection.FragmentComponent; import org.tasks.injection.FragmentComponent;
import org.tasks.themes.ThemeCache;
import org.tasks.ui.TaskEditControlFragment; import org.tasks.ui.TaskEditControlFragment;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set; import java.util.Set;
import javax.inject.Inject; import javax.inject.Inject;
@ -74,12 +80,15 @@ public final class TagsControlSet extends TaskEditControlFragment {
public static final int TAG = R.string.TEA_ctrl_lists_pref; public static final int TAG = R.string.TEA_ctrl_lists_pref;
private static final char NO_BREAK_SPACE = '\u00a0';
private static final char HAIR_SPACE = '\u200a';
private static final String EXTRA_TAGS = "extra_tags"; private static final String EXTRA_TAGS = "extra_tags";
@Inject MetadataDao metadataDao; @Inject MetadataDao metadataDao;
@Inject TagDataDao tagDataDao; @Inject TagDataDao tagDataDao;
@Inject TagService tagService; @Inject TagService tagService;
@Inject DialogBuilder dialogBuilder; @Inject DialogBuilder dialogBuilder;
@Inject ThemeCache themeCache;
@BindView(R.id.display_row_edit) TextView tagsDisplay; @BindView(R.id.display_row_edit) TextView tagsDisplay;
@ -91,21 +100,44 @@ public final class TagsControlSet extends TaskEditControlFragment {
private AlertDialog dialog; private AlertDialog dialog;
private ArrayList<String> tagList; private ArrayList<String> tagList;
private String buildTagString() { private final Ordering<TagData> orderByName = new Ordering<TagData>() {
StringBuilder builder = new StringBuilder(); @Override
public int compare(TagData left, TagData right) {
Collections.sort(tagList); return left.getName().compareTo(right.getName());
for (String tag : tagList) {
if (tag.trim().length() == 0) {
continue;
} }
if (builder.length() != 0) { };
builder.append(", "); //$NON-NLS-1$
private Function<TagData, SpannableString> tagToString(final float maxLength) {
return new Function<TagData, SpannableString>() {
@Override
public SpannableString apply(TagData tagData) {
String tagName = tagData.getName();
tagName = tagName
.substring(0, Math.min(tagName.length(), (int) maxLength))
.replace(' ', NO_BREAK_SPACE);
SpannableString string = new SpannableString(NO_BREAK_SPACE + tagName + NO_BREAK_SPACE);
int themeIndex = tagData.getColor() >= 0 ? tagData.getColor() : 19;
int backgroundColor = themeCache.getThemeColor(themeIndex).getPrimaryColor();
int foregroundColor = themeCache.getThemeColor(themeIndex).getActionBarTint();
string.setSpan(new BackgroundColorSpan(backgroundColor), 0, string.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
string.setSpan(new ForegroundColorSpan(foregroundColor), 0, string.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
return string;
} }
builder.append(tag); };
} }
return builder.toString(); private CharSequence buildTagString() {
Set<TagData> selectedTags = getSelectedTags(false);
List<TagData> sortedTagData = orderByName.sortedCopy(selectedTags);
List<SpannableString> tagStrings = Lists.transform(sortedTagData, tagToString(Float.MAX_VALUE));
SpannableStringBuilder builder = new SpannableStringBuilder();
for (SpannableString tagString : tagStrings) {
if (builder.length() > 0) {
builder.append(HAIR_SPACE);
}
builder.append(tagString);
}
return builder;
} }
@Nullable @Nullable
@ -329,13 +361,13 @@ public final class TagsControlSet extends TaskEditControlFragment {
} }
protected void refreshDisplayView() { protected void refreshDisplayView() {
String tagString = buildTagString(); CharSequence tagString = buildTagString();
if (!TextUtils.isEmpty(tagString)) { if (TextUtils.isEmpty(tagString)) {
tagsDisplay.setText(tagString);
tagsDisplay.setAlpha(1.0f);
} else {
tagsDisplay.setText(R.string.tag_FEx_untagged); tagsDisplay.setText(R.string.tag_FEx_untagged);
tagsDisplay.setAlpha(0.5f); tagsDisplay.setAlpha(0.5f);
} else {
tagsDisplay.setText(tagString);
tagsDisplay.setAlpha(1.0f);
} }
} }

@ -241,7 +241,7 @@ public class CommentBarFragment extends TaskEditControlFragment {
private void resetPictureButton() { private void resetPictureButton() {
TypedValue typedValue = new TypedValue(); TypedValue typedValue = new TypedValue();
getContext().getTheme().resolveAttribute(R.attr.actionBarPrimaryText, typedValue, true); getActivity().getTheme().resolveAttribute(R.attr.actionBarPrimaryText, typedValue, true);
Drawable drawable = DrawableCompat.wrap(getResources().getDrawable(R.drawable.ic_camera_alt_black_24dp)); Drawable drawable = DrawableCompat.wrap(getResources().getDrawable(R.drawable.ic_camera_alt_black_24dp));
DrawableCompat.setTint(drawable, typedValue.data); DrawableCompat.setTint(drawable, typedValue.data);
pictureButton.setImageDrawable(drawable); pictureButton.setImageDrawable(drawable);

@ -5,7 +5,7 @@
--> -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android" <TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/display_row_edit" android:id="@+id/display_row_edit"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="top" android:layout_gravity="top"
android:textColor="?attr/asTextColor" android:textColor="?attr/asTextColor"

Loading…
Cancel
Save