Search in sources :

Example 1 with ClickableBoldSpan

use of com.github.moko256.twitlatte.text.style.ClickableBoldSpan in project twicalico by moko256.

the class TwitterStringUtils method getLinkedSequence.

public static CharSequence getLinkedSequence(AccessToken accessToken, String text, Link[] links) {
    if (links == null) {
        return text;
    }
    SpannableString spannableString = new SpannableString(text);
    for (Link link : links) {
        Object span;
        final Uri uri = Uri.parse(link.getUrl());
        if (uri.getScheme() != null && uri.getHost() != null && uri.getScheme().equals("twitlatte")) {
            switch(uri.getHost()) {
                case "symbol":
                    span = new ClickableNoLineSpan() {

                        @Override
                        public void onClick(@NonNull View view) {
                            Context context = view.getContext();
                            context.startActivity(SearchResultActivity.getIntent(context, "$" + uri.getLastPathSegment()));
                        }
                    };
                    break;
                case "hashtag":
                    span = new ClickableNoLineSpan() {

                        @Override
                        public void onClick(@NonNull View view) {
                            Context context = view.getContext();
                            context.startActivity(SearchResultActivity.getIntent(context, "#" + uri.getLastPathSegment()));
                        }
                    };
                    break;
                case "user":
                    String name = uri.getLastPathSegment();
                    if (name != null) {
                        if (name.split("@")[0].equals(accessToken.getScreenName())) {
                            span = new ClickableBoldSpan() {

                                @Override
                                public void onClick(@NonNull View view) {
                                    Context context = view.getContext();
                                    context.startActivity(ShowUserActivity.getIntent(context, name));
                                }
                            };
                        } else {
                            span = new ClickableNoLineSpan() {

                                @Override
                                public void onClick(@NonNull View view) {
                                    Context context = view.getContext();
                                    context.startActivity(ShowUserActivity.getIntent(context, name));
                                }
                            };
                        }
                    } else {
                        span = null;
                    }
                    break;
                default:
                    span = new ClickableNoLineSpan() {

                        @Override
                        public void onClick(@NonNull View view) {
                            Context context = view.getContext();
                            context.startActivity(SearchResultActivity.getIntent(context, uri.getLastPathSegment()));
                        }
                    };
                    break;
            }
        } else {
            span = new ClickableNoLineSpan() {

                @Override
                public void onClick(@NonNull View view) {
                    Context context = view.getContext();
                    AppCustomTabsKt.launchChromeCustomTabs(context, uri, false);
                }
            };
        }
        int nowLength = text.length();
        int start = link.getStart();
        int end = link.getEnd();
        if (start < end && end <= nowLength) {
            spannableString.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
    return spannableString;
}
Also used : SpannableString(android.text.SpannableString) Context(android.content.Context) ClickableBoldSpan(com.github.moko256.twitlatte.text.style.ClickableBoldSpan) ClickableNoLineSpan(com.github.moko256.twitlatte.text.style.ClickableNoLineSpan) SpannableString(android.text.SpannableString) Uri(android.net.Uri) View(android.view.View) TextView(android.widget.TextView) Link(com.github.moko256.latte.html.entity.Link)

Aggregations

Context (android.content.Context)1 Uri (android.net.Uri)1 SpannableString (android.text.SpannableString)1 View (android.view.View)1 TextView (android.widget.TextView)1 Link (com.github.moko256.latte.html.entity.Link)1 ClickableBoldSpan (com.github.moko256.twitlatte.text.style.ClickableBoldSpan)1 ClickableNoLineSpan (com.github.moko256.twitlatte.text.style.ClickableNoLineSpan)1