use of in.uncod.android.bypass.style.ImageLoadingSpan in project sbt-android by scala-android.
the class DesignerNewsStory method bindDescription.
private void bindDescription() {
final TextView storyComment = (TextView) header.findViewById(R.id.story_comment);
if (!TextUtils.isEmpty(story.comment)) {
HtmlUtils.setTextWithNiceLinks(storyComment, markdown.markdownToSpannable(story.comment, storyComment, new Bypass.LoadImageCallback() {
@Override
public void loadImage(String src, ImageLoadingSpan loadingSpan) {
Glide.with(DesignerNewsStory.this).load(src).asBitmap().diskCacheStrategy(DiskCacheStrategy.ALL).into(new ImageSpanTarget(storyComment, loadingSpan));
}
}));
} else {
storyComment.setVisibility(View.GONE);
}
upvoteStory = (Button) header.findViewById(R.id.story_vote_action);
upvoteStory.setText(getResources().getQuantityString(R.plurals.upvotes, story.vote_count, NumberFormat.getInstance().format(story.vote_count)));
upvoteStory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
upvoteStory();
}
});
Button share = (Button) header.findViewById(R.id.story_share_action);
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(ShareCompat.IntentBuilder.from(DesignerNewsStory.this).setText(story.url).setType("text/plain").getIntent());
}
});
TextView storyPosterTime = (TextView) header.findViewById(R.id.story_poster_time);
SpannableString poster = new SpannableString("–" + story.user_display_name);
poster.setSpan(new TextAppearanceSpan(this, R.style.TextAppearance_CommentAuthor), 0, poster.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
CharSequence job = !TextUtils.isEmpty(story.user_job) ? "\n" + story.user_job : "";
CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(story.created_at.getTime(), System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);
storyPosterTime.setText(TextUtils.concat(poster, job, "\n", timeAgo));
ImageView avatar = (ImageView) header.findViewById(R.id.story_poster_avatar);
if (!TextUtils.isEmpty(story.user_portrait_url)) {
Glide.with(this).load(story.user_portrait_url).placeholder(R.drawable.avatar_placeholder).transform(circleTransform).into(avatar);
} else {
avatar.setVisibility(View.GONE);
}
}
use of in.uncod.android.bypass.style.ImageLoadingSpan in project sbt-android by scala-android.
the class Bypass method recurseElement.
// The 'numberOfSiblings' parameters refers to the number of siblings within the parent, including
// the 'element' parameter, as in "How many siblings are you?" rather than "How many siblings do
// you have?".
private CharSequence recurseElement(Element element, int indexWithinParent, int numberOfSiblings, TextView textView, LoadImageCallback loadImageCallback) {
Type type = element.getType();
boolean isOrderedList = false;
if (type == Type.LIST) {
String flagsStr = element.getAttribute("flags");
if (flagsStr != null) {
int flags = Integer.parseInt(flagsStr);
isOrderedList = (flags & Element.F_LIST_ORDERED) != 0;
if (isOrderedList) {
mOrderedListNumber.put(element, 1);
}
}
}
int size = element.size();
CharSequence[] spans = new CharSequence[size];
for (int i = 0; i < size; i++) {
spans[i] = recurseElement(element.children[i], i, size, textView, loadImageCallback);
}
// Clean up after we're done
if (isOrderedList) {
mOrderedListNumber.remove(this);
}
CharSequence concat = TextUtils.concat(spans);
SpannableStringBuilder builder = new ReverseSpannableStringBuilder();
String text = element.getText();
if (element.size() == 0 && element.getParent() != null && element.getParent().getType() != Type.BLOCK_CODE) {
text = text.replace('\n', ' ');
}
switch(type) {
case LIST:
if (element.getParent() != null && element.getParent().getType() == Type.LIST_ITEM) {
builder.append("\n");
}
break;
case LINEBREAK:
builder.append("\n");
break;
case LIST_ITEM:
builder.append(" ");
if (mOrderedListNumber.containsKey(element.getParent())) {
int number = mOrderedListNumber.get(element.getParent());
builder.append(Integer.toString(number) + ".");
mOrderedListNumber.put(element.getParent(), number + 1);
} else {
builder.append(mOptions.mUnorderedListItem);
}
builder.append(" ");
break;
case AUTOLINK:
builder.append(element.getAttribute("link"));
break;
case HRULE:
// This ultimately gets drawn over by the line span, but
// we need something here or the span isn't even drawn.
builder.append("-");
break;
case IMAGE:
if (loadImageCallback != null && !TextUtils.isEmpty(element.getAttribute("link"))) {
// prepend a new line so that images are always on a new line
builder.append("\n");
// Display alt text (or title text) if there is no image
String alt = element.getAttribute("alt");
if (TextUtils.isEmpty(alt)) {
alt = element.getAttribute("title");
}
if (!TextUtils.isEmpty(alt)) {
alt = "[" + alt + "]";
builder.append(alt);
} else {
// Character to be replaced
builder.append("\uFFFC");
}
} else {
// Character to be replaced
builder.append("\uFFFC");
}
break;
}
builder.append(text);
builder.append(concat);
// of the last child within the parent.
if (element.getParent() != null || indexWithinParent < (numberOfSiblings - 1)) {
if (type == Type.LIST_ITEM) {
if (element.size() == 0 || !element.children[element.size() - 1].isBlockElement()) {
builder.append("\n");
}
} else if (element.isBlockElement() && type != Type.BLOCK_QUOTE) {
if (type == Type.LIST) {
// If this is a nested list, don't include newlines
if (element.getParent() == null || element.getParent().getType() != Type.LIST_ITEM) {
builder.append("\n");
}
} else if (element.getParent() != null && element.getParent().getType() == Type.LIST_ITEM) {
// List items should never double-space their entries
builder.append("\n");
} else {
builder.append("\n\n");
}
}
}
switch(type) {
case HEADER:
String levelStr = element.getAttribute("level");
int level = Integer.parseInt(levelStr);
setSpan(builder, new RelativeSizeSpan(mOptions.mHeaderSizes[level - 1]));
setSpan(builder, new StyleSpan(Typeface.BOLD));
break;
case LIST:
setBlockSpan(builder, new LeadingMarginSpan.Standard(mListItemIndent));
break;
case EMPHASIS:
setSpan(builder, new StyleSpan(Typeface.ITALIC));
break;
case DOUBLE_EMPHASIS:
setSpan(builder, new StyleSpan(Typeface.BOLD));
break;
case TRIPLE_EMPHASIS:
setSpan(builder, new StyleSpan(Typeface.BOLD_ITALIC));
break;
case BLOCK_CODE:
setSpan(builder, new LeadingMarginSpan.Standard(mCodeBlockIndent));
setSpan(builder, new TypefaceSpan("monospace"));
break;
case CODE_SPAN:
setSpan(builder, new TypefaceSpan("monospace"));
break;
case LINK:
case AUTOLINK:
String link = element.getAttribute("link");
if (!TextUtils.isEmpty(link) && Patterns.EMAIL_ADDRESS.matcher(link).matches()) {
link = "mailto:" + link;
}
setSpan(builder, new TouchableUrlSpan(link, textView.getLinkTextColors(), textView.getHighlightColor()));
break;
case BLOCK_QUOTE:
// We add two leading margin spans so that when the order is reversed,
// the QuoteSpan will always be in the same spot.
setBlockSpan(builder, new LeadingMarginSpan.Standard(mBlockQuoteIndent));
// setBlockSpan(builder, new QuoteSpan(mOptions.mBlockQuoteLineColor));
setBlockSpan(builder, new FancyQuoteSpan(mBlockQuoteLineWidth, mBlockQuoteLineIndent, mOptions.mBlockQuoteLineColor));
setBlockSpan(builder, new ForegroundColorSpan(mOptions.mBlockQuoteTextColor));
setBlockSpan(builder, new LeadingMarginSpan.Standard(mBlockQuoteIndent));
setBlockSpan(builder, new StyleSpan(Typeface.ITALIC));
break;
case STRIKETHROUGH:
setSpan(builder, new StrikethroughSpan());
break;
case HRULE:
setSpan(builder, new HorizontalLineSpan(mOptions.mHruleColor, mHruleSize, mHruleTopBottomPadding));
break;
case IMAGE:
String url = element.getAttribute("link");
if (loadImageCallback != null && !TextUtils.isEmpty(url)) {
setPrependedNewlineSpan(builder, mOptions.mPreImageLinebreakHeight);
ImageLoadingSpan loadingSpan = new ImageLoadingSpan();
setSpanWithPrependedNewline(builder, loadingSpan);
// make the (eventually loaded) image span clickable to open in browser
setSpanWithPrependedNewline(builder, new TouchableUrlSpan(url, textView.getLinkTextColors(), textView.getHighlightColor()));
loadImageCallback.loadImage(url, loadingSpan);
}
break;
}
return builder;
}
use of in.uncod.android.bypass.style.ImageLoadingSpan in project plaid by nickbutcher.
the class Bypass method recurseElement.
// The 'numberOfSiblings' parameters refers to the number of siblings within the parent, including
// the 'element' parameter, as in "How many siblings are you?" rather than "How many siblings do
// you have?".
private CharSequence recurseElement(Element element, int indexWithinParent, int numberOfSiblings, TextView textView, LoadImageCallback loadImageCallback) {
Type type = element.getType();
boolean isOrderedList = false;
if (type == Type.LIST) {
String flagsStr = element.getAttribute("flags");
if (flagsStr != null) {
int flags = Integer.parseInt(flagsStr);
isOrderedList = (flags & Element.F_LIST_ORDERED) != 0;
if (isOrderedList) {
mOrderedListNumber.put(element, 1);
}
}
}
int size = element.size();
CharSequence[] spans = new CharSequence[size];
for (int i = 0; i < size; i++) {
spans[i] = recurseElement(element.children[i], i, size, textView, loadImageCallback);
}
// Clean up after we're done
if (isOrderedList) {
mOrderedListNumber.remove(this);
}
CharSequence concat = TextUtils.concat(spans);
SpannableStringBuilder builder = new ReverseSpannableStringBuilder();
String text = element.getText();
if (element.size() == 0 && element.getParent() != null && element.getParent().getType() != Type.BLOCK_CODE) {
text = text.replace('\n', ' ');
}
switch(type) {
case LIST:
if (element.getParent() != null && element.getParent().getType() == Type.LIST_ITEM) {
builder.append("\n");
}
break;
case LINEBREAK:
builder.append("\n");
break;
case LIST_ITEM:
builder.append(" ");
if (mOrderedListNumber.containsKey(element.getParent())) {
int number = mOrderedListNumber.get(element.getParent());
builder.append(Integer.toString(number) + ".");
mOrderedListNumber.put(element.getParent(), number + 1);
} else {
builder.append(mOptions.mUnorderedListItem);
}
builder.append(" ");
break;
case AUTOLINK:
builder.append(element.getAttribute("link"));
break;
case HRULE:
// This ultimately gets drawn over by the line span, but
// we need something here or the span isn't even drawn.
builder.append("-");
break;
case IMAGE:
if (loadImageCallback != null && !TextUtils.isEmpty(element.getAttribute("link"))) {
// prepend a new line so that images are always on a new line
builder.append("\n");
// Display alt text (or title text) if there is no image
String alt = element.getAttribute("alt");
if (TextUtils.isEmpty(alt)) {
alt = element.getAttribute("title");
}
if (!TextUtils.isEmpty(alt)) {
alt = "[" + alt + "]";
builder.append(alt);
} else {
// Character to be replaced
builder.append("");
}
} else {
// Character to be replaced
builder.append("");
}
break;
}
builder.append(text);
builder.append(concat);
// of the last child within the parent.
if (element.getParent() != null || indexWithinParent < (numberOfSiblings - 1)) {
if (type == Type.LIST_ITEM) {
if (element.size() == 0 || !element.children[element.size() - 1].isBlockElement()) {
builder.append("\n");
}
} else if (element.isBlockElement() && type != Type.BLOCK_QUOTE) {
if (type == Type.LIST) {
// If this is a nested list, don't include newlines
if (element.getParent() == null || element.getParent().getType() != Type.LIST_ITEM) {
builder.append("\n");
}
} else if (element.getParent() != null && element.getParent().getType() == Type.LIST_ITEM) {
// List items should never double-space their entries
builder.append("\n");
} else {
builder.append("\n\n");
}
}
}
switch(type) {
case HEADER:
String levelStr = element.getAttribute("level");
int level = Integer.parseInt(levelStr);
setSpan(builder, new RelativeSizeSpan(mOptions.mHeaderSizes[level - 1]));
setSpan(builder, new StyleSpan(Typeface.BOLD));
break;
case LIST:
setBlockSpan(builder, new LeadingMarginSpan.Standard(mListItemIndent));
break;
case EMPHASIS:
setSpan(builder, new StyleSpan(Typeface.ITALIC));
break;
case DOUBLE_EMPHASIS:
setSpan(builder, new StyleSpan(Typeface.BOLD));
break;
case TRIPLE_EMPHASIS:
setSpan(builder, new StyleSpan(Typeface.BOLD_ITALIC));
break;
case BLOCK_CODE:
setSpan(builder, new LeadingMarginSpan.Standard(mCodeBlockIndent));
setSpan(builder, new TypefaceSpan("monospace"));
break;
case CODE_SPAN:
setSpan(builder, new TypefaceSpan("monospace"));
break;
case LINK:
case AUTOLINK:
String link = element.getAttribute("link");
if (!TextUtils.isEmpty(link) && Patterns.EMAIL_ADDRESS.matcher(link).matches()) {
link = "mailto:" + link;
}
setSpan(builder, new TouchableUrlSpan(link, textView.getLinkTextColors(), textView.getHighlightColor()));
break;
case BLOCK_QUOTE:
// We add two leading margin spans so that when the order is reversed,
// the QuoteSpan will always be in the same spot.
setBlockSpan(builder, new LeadingMarginSpan.Standard(mBlockQuoteIndent));
//setBlockSpan(builder, new QuoteSpan(mOptions.mBlockQuoteLineColor));
setBlockSpan(builder, new FancyQuoteSpan(mBlockQuoteLineWidth, mBlockQuoteLineIndent, mOptions.mBlockQuoteLineColor));
setBlockSpan(builder, new ForegroundColorSpan(mOptions.mBlockQuoteTextColor));
setBlockSpan(builder, new LeadingMarginSpan.Standard(mBlockQuoteIndent));
setBlockSpan(builder, new StyleSpan(Typeface.ITALIC));
break;
case STRIKETHROUGH:
setSpan(builder, new StrikethroughSpan());
break;
case HRULE:
setSpan(builder, new HorizontalLineSpan(mOptions.mHruleColor, mHruleSize, mHruleTopBottomPadding));
break;
case IMAGE:
String url = element.getAttribute("link");
if (loadImageCallback != null && !TextUtils.isEmpty(url)) {
setPrependedNewlineSpan(builder, mOptions.mPreImageLinebreakHeight);
ImageLoadingSpan loadingSpan = new ImageLoadingSpan();
setSpanWithPrependedNewline(builder, loadingSpan);
// make the (eventually loaded) image span clickable to open in browser
setSpanWithPrependedNewline(builder, new TouchableUrlSpan(url, textView.getLinkTextColors(), textView.getHighlightColor()));
loadImageCallback.loadImage(url, loadingSpan);
}
break;
}
return builder;
}
use of in.uncod.android.bypass.style.ImageLoadingSpan in project plaid by nickbutcher.
the class DesignerNewsStory method bindDescription.
private void bindDescription() {
final TextView storyComment = (TextView) header.findViewById(R.id.story_comment);
if (!TextUtils.isEmpty(story.comment)) {
HtmlUtils.parseMarkdownAndSetText(storyComment, story.comment, markdown, new Bypass.LoadImageCallback() {
@Override
public void loadImage(String src, ImageLoadingSpan loadingSpan) {
Glide.with(DesignerNewsStory.this).load(src).asBitmap().diskCacheStrategy(DiskCacheStrategy.ALL).into(new ImageSpanTarget(storyComment, loadingSpan));
}
});
} else {
storyComment.setVisibility(View.GONE);
}
upvoteStory = (TextView) header.findViewById(R.id.story_vote_action);
upvoteStory.setText(getResources().getQuantityString(R.plurals.upvotes, story.vote_count, NumberFormat.getInstance().format(story.vote_count)));
upvoteStory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
upvoteStory();
}
});
final TextView share = (TextView) header.findViewById(R.id.story_share_action);
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((AnimatedVectorDrawable) share.getCompoundDrawables()[1]).start();
startActivity(ShareCompat.IntentBuilder.from(DesignerNewsStory.this).setText(story.url).setType("text/plain").setSubject(story.title).getIntent());
}
});
TextView storyPosterTime = (TextView) header.findViewById(R.id.story_poster_time);
SpannableString poster = new SpannableString(story.user_display_name.toLowerCase());
poster.setSpan(new TextAppearanceSpan(this, R.style.TextAppearance_CommentAuthor), 0, poster.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
CharSequence job = !TextUtils.isEmpty(story.user_job) ? "\n" + story.user_job.toLowerCase() : "";
CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(story.created_at.getTime(), System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS).toString().toLowerCase();
storyPosterTime.setText(TextUtils.concat(poster, job, "\n", timeAgo));
ImageView avatar = (ImageView) header.findViewById(R.id.story_poster_avatar);
if (!TextUtils.isEmpty(story.user_portrait_url)) {
Glide.with(this).load(story.user_portrait_url).placeholder(R.drawable.avatar_placeholder).transform(circleTransform).into(avatar);
} else {
avatar.setVisibility(View.GONE);
}
}
use of in.uncod.android.bypass.style.ImageLoadingSpan in project plaid by nickbutcher.
the class Bypass method recurseElement.
// The 'numberOfSiblings' parameters refers to the number of siblings within the parent, including
// the 'element' parameter, as in "How many siblings are you?" rather than "How many siblings do
// you have?".
private CharSequence recurseElement(Element element, int indexWithinParent, int numberOfSiblings, ColorStateList linksColors, int highlightColor, LoadImageCallback loadImageCallback) {
Type type = element.getType();
boolean isOrderedList = false;
if (type == Type.LIST) {
String flagsStr = element.getAttribute("flags");
if (flagsStr != null) {
int flags = Integer.parseInt(flagsStr);
isOrderedList = (flags & Element.F_LIST_ORDERED) != 0;
if (isOrderedList) {
mOrderedListNumber.put(element, 1);
}
}
}
int size = element.size();
CharSequence[] spans = new CharSequence[size];
for (int i = 0; i < size; i++) {
spans[i] = recurseElement(element.children[i], i, size, linksColors, highlightColor, loadImageCallback);
}
// Clean up after we're done
if (isOrderedList) {
mOrderedListNumber.remove(this);
}
CharSequence concat = TextUtils.concat(spans);
SpannableStringBuilder builder = new ReverseSpannableStringBuilder();
String text = element.getText();
if (element.size() == 0 && element.getParent() != null && element.getParent().getType() != Type.BLOCK_CODE) {
text = text.replace('\n', ' ');
}
switch(type) {
case LIST:
if (element.getParent() != null && element.getParent().getType() == Type.LIST_ITEM) {
builder.append("\n");
}
break;
case LINEBREAK:
builder.append("\n");
break;
case LIST_ITEM:
builder.append(" ");
if (mOrderedListNumber.containsKey(element.getParent())) {
int number = mOrderedListNumber.get(element.getParent());
builder.append(Integer.toString(number) + ".");
mOrderedListNumber.put(element.getParent(), number + 1);
} else {
builder.append(mOptions.mUnorderedListItem);
}
builder.append(" ");
break;
case AUTOLINK:
builder.append(element.getAttribute("link"));
break;
case HRULE:
// This ultimately gets drawn over by the line span, but
// we need something here or the span isn't even drawn.
builder.append("-");
break;
case IMAGE:
if (loadImageCallback != null && !TextUtils.isEmpty(element.getAttribute("link"))) {
// prepend a new line so that images are always on a new line
builder.append("\n");
// Display alt text (or title text) if there is no image
String alt = element.getAttribute("alt");
if (TextUtils.isEmpty(alt)) {
alt = element.getAttribute("title");
}
if (!TextUtils.isEmpty(alt)) {
alt = "[" + alt + "]";
builder.append(alt);
} else {
// Character to be replaced
builder.append("\uFFFC");
}
} else {
// Character to be replaced
builder.append("\uFFFC");
}
break;
}
builder.append(text);
builder.append(concat);
// of the last child within the parent.
if (element.getParent() != null || indexWithinParent < (numberOfSiblings - 1)) {
if (type == Type.LIST_ITEM) {
if (element.size() == 0 || !element.children[element.size() - 1].isBlockElement()) {
builder.append("\n");
}
} else if (element.isBlockElement() && type != Type.BLOCK_QUOTE) {
if (type == Type.LIST) {
// If this is a nested list, don't include newlines
if (element.getParent() == null || element.getParent().getType() != Type.LIST_ITEM) {
builder.append("\n");
}
} else if (element.getParent() != null && element.getParent().getType() == Type.LIST_ITEM) {
// List items should never double-space their entries
builder.append("\n");
} else {
builder.append("\n\n");
}
}
}
switch(type) {
case HEADER:
String levelStr = element.getAttribute("level");
int level = Integer.parseInt(levelStr);
setSpan(builder, new RelativeSizeSpan(mOptions.mHeaderSizes[level - 1]));
setSpan(builder, new StyleSpan(Typeface.BOLD));
break;
case LIST:
setBlockSpan(builder, new LeadingMarginSpan.Standard(mListItemIndent));
break;
case EMPHASIS:
setSpan(builder, new StyleSpan(Typeface.ITALIC));
break;
case DOUBLE_EMPHASIS:
setSpan(builder, new StyleSpan(Typeface.BOLD));
break;
case TRIPLE_EMPHASIS:
setSpan(builder, new StyleSpan(Typeface.BOLD_ITALIC));
break;
case BLOCK_CODE:
setSpan(builder, new LeadingMarginSpan.Standard(mCodeBlockIndent));
setSpan(builder, new TypefaceSpan("monospace"));
break;
case CODE_SPAN:
setSpan(builder, new TypefaceSpan("monospace"));
break;
case LINK:
case AUTOLINK:
String link = element.getAttribute("link");
if (!TextUtils.isEmpty(link) && Patterns.EMAIL_ADDRESS.matcher(link).matches()) {
link = "mailto:" + link;
}
setSpan(builder, new TouchableUrlSpan(link, linksColors, highlightColor));
break;
case BLOCK_QUOTE:
// We add two leading margin spans so that when the order is reversed,
// the QuoteSpan will always be in the same spot.
setBlockSpan(builder, new LeadingMarginSpan.Standard(mBlockQuoteIndent));
// setBlockSpan(builder, new QuoteSpan(mOptions.mBlockQuoteLineColor));
setBlockSpan(builder, new FancyQuoteSpan(mBlockQuoteLineWidth, mBlockQuoteLineIndent, mOptions.mBlockQuoteLineColor));
setBlockSpan(builder, new ForegroundColorSpan(mOptions.mBlockQuoteTextColor));
setBlockSpan(builder, new LeadingMarginSpan.Standard(mBlockQuoteIndent));
setBlockSpan(builder, new StyleSpan(Typeface.ITALIC));
break;
case STRIKETHROUGH:
setSpan(builder, new StrikethroughSpan());
break;
case HRULE:
setSpan(builder, new HorizontalLineSpan(mOptions.mHruleColor, mHruleSize, mHruleTopBottomPadding));
break;
case IMAGE:
String url = element.getAttribute("link");
if (loadImageCallback != null && !TextUtils.isEmpty(url)) {
setPrependedNewlineSpan(builder, mOptions.mPreImageLinebreakHeight);
ImageLoadingSpan loadingSpan = new ImageLoadingSpan();
setSpanWithPrependedNewline(builder, loadingSpan);
// make the (eventually loaded) image span clickable to open in browser
setSpanWithPrependedNewline(builder, new TouchableUrlSpan(url, linksColors, highlightColor));
loadImageCallback.loadImage(url, loadingSpan);
}
break;
}
return builder;
}
Aggregations