Skip to content

Commit

Permalink
Use annotations for string markup.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbutcher committed Feb 3, 2018
1 parent 9be3604 commit 951c361
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
49 changes: 30 additions & 19 deletions app/src/main/java/io/plaidapp/ui/HomeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.text.Annotation;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
Expand Down Expand Up @@ -649,26 +651,35 @@ private void setNoFiltersEmptyTextVisibility(int visibility) {
// create the no filters empty text
ViewStub stub = findViewById(R.id.stub_no_filters);
noFiltersEmptyText = (TextView) stub.inflate();
String emptyText = getString(R.string.no_filters_selected);
int filterPlaceholderStart = emptyText.indexOf('\u08B4');
int altMethodStart = filterPlaceholderStart + 3;
SpannedString emptyText = (SpannedString) getText(R.string.no_filters_selected);
SpannableStringBuilder ssb = new SpannableStringBuilder(emptyText);
// show an image of the filter icon
ssb.setSpan(new ImageSpan(this, R.drawable.ic_filter_small,
ImageSpan.ALIGN_BASELINE),
filterPlaceholderStart,
filterPlaceholderStart + 1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// make the alt method (swipe from right) less prominent and italic
ssb.setSpan(new ForegroundColorSpan(
ContextCompat.getColor(this, R.color.text_secondary_light)),
altMethodStart,
emptyText.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ssb.setSpan(new StyleSpan(Typeface.ITALIC),
altMethodStart,
emptyText.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
final Annotation[] annotations =
emptyText.getSpans(0, emptyText.length(), Annotation.class);
if (annotations != null && annotations.length > 0) {
for (int i = 0; i < annotations.length; i++) {
final Annotation annotation = annotations[i];
if (annotation.getKey().equals("src")) {
// image span markup
String name = annotation.getValue();
int id = getResources().getIdentifier(name, null, getPackageName());
if (id == 0) continue;
ssb.setSpan(new ImageSpan(this, id,
ImageSpan.ALIGN_BASELINE),
emptyText.getSpanStart(annotation),
emptyText.getSpanEnd(annotation),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} else if (annotation.getKey().equals("foregroundColor")) {
// foreground color span markup
String name = annotation.getValue();
int id = getResources().getIdentifier(name, null, getPackageName());
if (id == 0) continue;
ssb.setSpan(new ForegroundColorSpan(ContextCompat.getColor(this, id)),
emptyText.getSpanStart(annotation),
emptyText.getSpanEnd(annotation),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
noFiltersEmptyText.setText(ssb);
noFiltersEmptyText.setOnClickListener(v -> drawer.openDrawer(GravityCompat.END));
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<string name="designer_news_logged_out">Logged out of Designer News</string>
<string name="add">Add</string>
<string name="edit">Edit</string>
<string name="no_filters_selected">No filters selected, press the filter button above (&#2228;)\nor swipe from the&#160;right</string>
<string name="no_filters_selected">No filters selected, press the filter button above (<annotation src="@drawable/ic_filter_small">&#2228;</annotation>)\n<i><annotation foregroundColor="@color/text_secondary_light">or swipe from the&#160;right</annotation></i></string>

<!-- data sources / filters -->
<string name="source_designer_news_popular">Popular Designer News</string>
Expand Down

0 comments on commit 951c361

Please sign in to comment.