Search in sources :

Example 1 with NoteBlockClickableSpan

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;
}
Also used : NoteBlockClickableSpan(org.wordpress.android.ui.notifications.blocks.NoteBlockClickableSpan) JSONObject(org.json.JSONObject) StyleSpan(android.text.style.StyleSpan) JSONArray(org.json.JSONArray) View(android.view.View) TextView(android.widget.TextView) SpannableStringBuilder(android.text.SpannableStringBuilder)

Aggregations

SpannableStringBuilder (android.text.SpannableStringBuilder)1 StyleSpan (android.text.style.StyleSpan)1 View (android.view.View)1 TextView (android.widget.TextView)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1 NoteBlockClickableSpan (org.wordpress.android.ui.notifications.blocks.NoteBlockClickableSpan)1