use of org.wordpress.android.ui.notifications.blocks.NoteBlockClickableSpan in project WordPress-Android by wordpress-mobile.
the class NotificationsUtils method getSpannableContentForRanges.
/**
* Returns a spannable with formatted content based on WP.com note content 'range' data
* @param blockObject the JSON data
* @param textView the TextView that will display the spannnable
* @param onNoteBlockTextClickListener - click listener for ClickableSpans in the spannable
* @param isFooter - Set if spannable should apply special formatting
* @return Spannable string with formatted content
*/
public static Spannable getSpannableContentForRanges(JSONObject blockObject, TextView textView, final NoteBlock.OnNoteBlockTextClickListener onNoteBlockTextClickListener, boolean isFooter) {
if (blockObject == null) {
return new SpannableStringBuilder();
}
String text = blockObject.optString("text", "");
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text);
boolean shouldLink = onNoteBlockTextClickListener != null;
// Add ImageSpans for note media
addImageSpansForBlockMedia(textView, blockObject, spannableStringBuilder);
// Process Ranges to add links and text formatting
JSONArray rangesArray = blockObject.optJSONArray("ranges");
if (rangesArray != null) {
for (int i = 0; i < rangesArray.length(); i++) {
JSONObject rangeObject = rangesArray.optJSONObject(i);
if (rangeObject == null) {
continue;
}
NoteBlockClickableSpan clickableSpan = new NoteBlockClickableSpan(WordPress.getContext(), rangeObject, shouldLink, isFooter) {
@Override
public void onClick(View widget) {
if (onNoteBlockTextClickListener != null) {
onNoteBlockTextClickListener.onNoteBlockTextClicked(this);
}
}
};
int[] indices = clickableSpan.getIndices();
if (indices.length == 2 && indices[0] <= spannableStringBuilder.length() && indices[1] <= spannableStringBuilder.length()) {
spannableStringBuilder.setSpan(clickableSpan, indices[0], indices[1], Spanned.SPAN_INCLUSIVE_INCLUSIVE);
// Add additional styling if the range wants it
if (clickableSpan.getSpanStyle() != Typeface.NORMAL) {
StyleSpan styleSpan = new StyleSpan(clickableSpan.getSpanStyle());
spannableStringBuilder.setSpan(styleSpan, indices[0], indices[1], Spanned.SPAN_INCLUSIVE_INCLUSIVE);
}
}
}
}
return spannableStringBuilder;
}
Aggregations