Skip to content

Commit

Permalink
Temp workaround for blank link issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbutcher committed Jul 25, 2016
1 parent e140956 commit 8f8cdb0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
7 changes: 4 additions & 3 deletions app/src/main/java/io/plaidapp/util/DribbbleUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ private DribbbleUtils() { }
* An extension to {@link HtmlUtils#parseHtml(String, ColorStateList, int)} which adds Dribbble
* specific behaviour.
*/
public static Spanned parseDribbbleHtml(String input,
ColorStateList linkTextColor,
@ColorInt int linkHighlightColor) {
public static Spanned parseDribbbleHtml(
String input,
ColorStateList linkTextColor,
@ColorInt int linkHighlightColor) {
SpannableStringBuilder ssb = HtmlUtils.parseHtml(input, linkTextColor, linkHighlightColor);

TouchableUrlSpan[] urlSpans = ssb.getSpans(0, ssb.length(), TouchableUrlSpan.class);
Expand Down
49 changes: 33 additions & 16 deletions app/src/main/java/io/plaidapp/util/HtmlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.plaidapp.util;

import android.content.res.ColorStateList;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.text.Html;
import android.text.SpannableString;
Expand Down Expand Up @@ -57,10 +58,11 @@ public static void setTextWithNiceLinks(TextView textView, CharSequence input) {
* Parse the given input using {@link TouchableUrlSpan}s rather than vanilla {@link URLSpan}s
* so that they respond to touch.
*/
public static SpannableStringBuilder parseHtml(String input,
ColorStateList linkTextColor,
@ColorInt int linkHighlightColor) {
SpannableStringBuilder spanned = (SpannableStringBuilder) Html.fromHtml(input);
public static SpannableStringBuilder parseHtml(
String input,
ColorStateList linkTextColor,
@ColorInt int linkHighlightColor) {
SpannableStringBuilder spanned = fromHtml(input);

// strip any trailing newlines
while (spanned.charAt(spanned.length() - 1) == '\n') {
Expand All @@ -76,17 +78,22 @@ public static void parseAndSetText(TextView textView, String input) {
textView.getHighlightColor()));
}

private static SpannableStringBuilder linkifyPlainLinks(CharSequence input,
ColorStateList linkTextColor,
@ColorInt int linkHighlightColor) {
private static SpannableStringBuilder linkifyPlainLinks(
CharSequence input,
ColorStateList linkTextColor,
@ColorInt int linkHighlightColor) {
final SpannableString plainLinks = new SpannableString(input); // copy of input
Linkify.addLinks(plainLinks, Linkify.WEB_URLS);

// Linkify doesn't seem to work as expected on M+
// TODO: figure out why
//Linkify.addLinks(plainLinks, Linkify.WEB_URLS);

final URLSpan[] urlSpans = plainLinks.getSpans(0, plainLinks.length(), URLSpan.class);

// add any plain links to the output
final SpannableStringBuilder ssb = new SpannableStringBuilder(input);
for (URLSpan urlSpan : urlSpans) {
ssb.removeSpan(urlSpan);
ssb.setSpan(new TouchableUrlSpan(urlSpan.getURL(), linkTextColor, linkHighlightColor),
plainLinks.getSpanStart(urlSpan),
plainLinks.getSpanEnd(urlSpan),
Expand All @@ -95,6 +102,15 @@ private static SpannableStringBuilder linkifyPlainLinks(CharSequence input,

return ssb;
}

private static SpannableStringBuilder fromHtml(String input) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return (SpannableStringBuilder) Html.fromHtml(input, Html.FROM_HTML_MODE_LEGACY);
} else {
return (SpannableStringBuilder) Html.fromHtml(input);
}
}

/**
* Parse Markdown and plain-text links.
* <p/>
Expand All @@ -107,10 +123,10 @@ private static SpannableStringBuilder linkifyPlainLinks(CharSequence input,
* Best of both worlds.
*/
public static CharSequence parseMarkdownAndPlainLinks(
TextView textView,
String input,
Bypass markdown,
Bypass.LoadImageCallback loadImageCallback) {
TextView textView,
String input,
Bypass markdown,
Bypass.LoadImageCallback loadImageCallback) {
CharSequence markedUp = markdown.markdownToSpannable(input, textView, loadImageCallback);
return linkifyPlainLinks(
markedUp, textView.getLinkTextColors(), textView.getHighlightColor());
Expand All @@ -120,10 +136,11 @@ public static CharSequence parseMarkdownAndPlainLinks(
* Parse Markdown and plain-text links and set on the {@link TextView} with proper clickable
* spans.
*/
public static void parseMarkdownAndSetText(TextView textView,
String input,
Bypass markdown,
Bypass.LoadImageCallback loadImageCallback) {
public static void parseMarkdownAndSetText(
TextView textView,
String input,
Bypass markdown,
Bypass.LoadImageCallback loadImageCallback) {
if (TextUtils.isEmpty(input)) return;
setTextWithNiceLinks(textView,
parseMarkdownAndPlainLinks(textView, input, markdown, loadImageCallback));
Expand Down

0 comments on commit 8f8cdb0

Please sign in to comment.