make keyword styling work in quotes

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/ui/text/QuoteSpan.java   |  6 
src/main/java/eu/siacs/conversations/utils/StylingHelper.java | 33 +++-
2 files changed, 27 insertions(+), 12 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/ui/text/QuoteSpan.java 🔗

@@ -2,6 +2,7 @@ package eu.siacs.conversations.ui.text;
 
 import android.graphics.Canvas;
 import android.graphics.Paint;
+import android.support.annotation.ColorInt;
 import android.text.Layout;
 import android.text.TextPaint;
 import android.text.style.CharacterStyle;
@@ -49,4 +50,9 @@ public class QuoteSpan extends CharacterStyle implements LeadingMarginSpan {
 		p.setStyle(style);
 		p.setColor(color);
 	}
+
+	@ColorInt
+	public int getColor() {
+		return this.color;
+	}
 }

src/main/java/eu/siacs/conversations/utils/StylingHelper.java 🔗

@@ -45,6 +45,8 @@ import android.widget.EditText;
 import java.util.Arrays;
 import java.util.List;
 
+import eu.siacs.conversations.ui.text.QuoteSpan;
+
 public class StylingHelper {
 
 	private static List<? extends Class<? extends ParcelableSpan>> SPAN_CLASSES = Arrays.asList(
@@ -56,24 +58,18 @@ public class StylingHelper {
 
 	public static void clear(final Editable editable) {
 		final int end = editable.length() - 1;
-		for(Class<?extends ParcelableSpan> clazz : SPAN_CLASSES) {
+		for (Class<? extends ParcelableSpan> clazz : SPAN_CLASSES) {
 			for (ParcelableSpan span : editable.getSpans(0, end, clazz)) {
 				editable.removeSpan(span);
 			}
 		}
 	}
 
-	public static void format(final Editable editable, @ColorInt int color) {
-		final int syntaxColor = Color.argb(
-				Math.round(Color.alpha(color) * 0.6f),
-				Color.red(color),
-				Color.green(color),
-				Color.blue(color)
-		);
-		for(ImStyleParser.Style style : ImStyleParser.parse(editable)) {
+	public static void format(final Editable editable, @ColorInt int textColor) {
+		for (ImStyleParser.Style style : ImStyleParser.parse(editable)) {
 			editable.setSpan(createSpanForStyle(style), style.getStart() + 1, style.getEnd(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
-			editable.setSpan(new ForegroundColorSpan(syntaxColor),style.getStart(),style.getStart()+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
-			editable.setSpan(new ForegroundColorSpan(syntaxColor),style.getEnd(),style.getEnd()+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+			makeKeywordOpaque(editable, style.getStart(), style.getStart() + 1, textColor);
+			makeKeywordOpaque(editable, style.getEnd(), style.getEnd() + 1, textColor);
 		}
 	}
 
@@ -92,6 +88,19 @@ public class StylingHelper {
 		}
 	}
 
+	private static void makeKeywordOpaque(final Editable editable, int start, int end, @ColorInt int fallbackTextColor) {
+		QuoteSpan[] quoteSpans = editable.getSpans(start, end, QuoteSpan.class);
+		@ColorInt int textColor = quoteSpans.length > 0 ? quoteSpans[0].getColor() : fallbackTextColor;
+		@ColorInt int keywordColor = transformColor(textColor);
+		editable.setSpan(new ForegroundColorSpan(keywordColor), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+	}
+
+	private static
+	@ColorInt
+	int transformColor(@ColorInt int c) {
+		return Color.argb(Math.round(Color.alpha(c) * 0.6f), Color.red(c), Color.green(c), Color.blue(c));
+	}
+
 	public static class MessageEditorStyler implements TextWatcher {
 
 		private final EditText mEditText;
@@ -113,7 +122,7 @@ public class StylingHelper {
 		@Override
 		public void afterTextChanged(Editable editable) {
 			clear(editable);
-			format(editable,mEditText.getCurrentTextColor());
+			format(editable, mEditText.getCurrentTextColor());
 		}
 	}
 }