Search in sources :

Example 1 with ClickableNoLineSpan

use of com.github.moko256.twitlatte.text.style.ClickableNoLineSpan 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)

Example 2 with ClickableNoLineSpan

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

the class UserInfoFragment method setShowUserInfo.

private void setShowUserInfo(User user) {
    if (user.getId() == client.getAccessToken().getUserId()) {
        userIsYouOrFollowedYou.setVisibility(View.VISIBLE);
        userIsYouOrFollowedYou.setText(R.string.you);
    }
    MediaUrlConverter mediaUrlConverter = client.getMediaUrlConverter();
    String headerUrl = mediaUrlConverter.convertProfileBannerLargeUrl(user);
    if (headerUrl != null) {
        glideRequests.load(headerUrl).transition(DrawableTransitionOptions.withCrossFade()).into(header);
        header.setOnClickListener(v -> startActivity(ShowMediasActivity.Companion.getIntent(v.getContext(), new Media[] { new Media(null, headerUrl, null, Media.MediaType.PICTURE.getValue()) }, CLIENT_TYPE_NOTHING, 0)));
    }
    glideRequests.load(mediaUrlConverter.convertProfileIconUriBySize(user, DpToPxKt.dpToPx(this, 68))).circleCrop().transition(DrawableTransitionOptions.withCrossFade()).into(icon);
    icon.setOnClickListener(v -> startActivity(ShowMediasActivity.Companion.getIntent(v.getContext(), new Media[] { new Media(null, mediaUrlConverter.convertProfileIconOriginalUrl(user), null, Media.MediaType.PICTURE.getValue()) }, CLIENT_TYPE_NOTHING, 0)));
    CharSequence userName = TwitterStringUtils.plusUserMarks(user.getName(), userNameText, user.isProtected(), user.isVerified());
    CharSequence userBio = TwitterStringUtils.getLinkedSequence(client.getAccessToken(), user.getDescription(), user.getDescriptionLinks());
    userNameText.setText(userName);
    userBioText.setText(userBio);
    Emoji[] userNameEmojis = user.getEmojis();
    if (userNameEmojis != null) {
        EmojiToTextViewSetter nameSetter = new EmojiToTextViewSetter(glideRequests, userNameText, userName, userNameEmojis);
        EmojiToTextViewSetter bioSetter = new EmojiToTextViewSetter(glideRequests, userBioText, userBio, userNameEmojis);
        getLifecycle().addObserver(new LifecycleEventObserver() {

            @Override
            public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
                if (event == Lifecycle.Event.ON_DESTROY) {
                    nameSetter.dispose();
                    bioSetter.dispose();
                    getLifecycle().removeObserver(this);
                }
            }
        });
    }
    userIdText.setText(TwitterStringUtils.plusAtMark(user.getScreenName()));
    if (!TextUtils.isEmpty(user.getLocation())) {
        userLocation.setText(getString(R.string.location_is, user.getLocation()));
    } else {
        userLocation.setVisibility(View.GONE);
    }
    final String url = user.getUrl();
    if (!TextUtils.isEmpty(url)) {
        String text = getString(R.string.url_is, url);
        SpannableString spannableString = new SpannableString(text);
        int start = text.indexOf(url);
        spannableString.setSpan(new ClickableNoLineSpan() {

            @Override
            public void onClick(@NonNull View widget) {
                AppCustomTabsKt.launchChromeCustomTabs(widget.getContext(), url, false);
            }
        }, start, start + url.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        userUrl.setText(spannableString);
        userUrl.setMovementMethod(LinkMovementMethod.getInstance());
    } else {
        userUrl.setVisibility(View.GONE);
    }
    userCreatedAt.setText(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.FULL).format(user.getCreatedAt()));
    userCounts.setText(getString(R.string.user_counts_is, user.getStatusesCount(), user.getFriendsCount(), user.getFollowersCount()));
}
Also used : EmojiToTextViewSetter(com.github.moko256.twitlatte.view.EmojiToTextViewSetter) Lifecycle(androidx.lifecycle.Lifecycle) Media(com.github.moko256.latte.client.base.entity.Media) MediaUrlConverter(com.github.moko256.latte.client.base.MediaUrlConverter) ClickableNoLineSpan(com.github.moko256.twitlatte.text.style.ClickableNoLineSpan) SpannableString(android.text.SpannableString) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) UserHeaderImageView(com.github.moko256.twitlatte.widget.UserHeaderImageView) SpannableString(android.text.SpannableString) LifecycleEventObserver(androidx.lifecycle.LifecycleEventObserver) Emoji(com.github.moko256.latte.client.base.entity.Emoji) LifecycleOwner(androidx.lifecycle.LifecycleOwner)

Example 3 with ClickableNoLineSpan

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

the class TwitterStringUtils method appendLinkAtViaText.

public static CharSequence appendLinkAtViaText(String name, String url) {
    if (url == null) {
        return name;
    } else {
        SpannableString spannableString = new SpannableString("via:" + name);
        spannableString.setSpan(new ClickableNoLineSpan() {

            @Override
            public void onClick(@NonNull View view) {
                AppCustomTabsKt.launchChromeCustomTabs(view.getContext(), url, false);
            }
        }, 4, name.length() + 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }
}
Also used : SpannableString(android.text.SpannableString) ClickableNoLineSpan(com.github.moko256.twitlatte.text.style.ClickableNoLineSpan) View(android.view.View) TextView(android.widget.TextView)

Aggregations

SpannableString (android.text.SpannableString)3 View (android.view.View)3 TextView (android.widget.TextView)3 ClickableNoLineSpan (com.github.moko256.twitlatte.text.style.ClickableNoLineSpan)3 Context (android.content.Context)1 Uri (android.net.Uri)1 ImageView (android.widget.ImageView)1 Lifecycle (androidx.lifecycle.Lifecycle)1 LifecycleEventObserver (androidx.lifecycle.LifecycleEventObserver)1 LifecycleOwner (androidx.lifecycle.LifecycleOwner)1 MediaUrlConverter (com.github.moko256.latte.client.base.MediaUrlConverter)1 Emoji (com.github.moko256.latte.client.base.entity.Emoji)1 Media (com.github.moko256.latte.client.base.entity.Media)1 Link (com.github.moko256.latte.html.entity.Link)1 ClickableBoldSpan (com.github.moko256.twitlatte.text.style.ClickableBoldSpan)1 EmojiToTextViewSetter (com.github.moko256.twitlatte.view.EmojiToTextViewSetter)1 UserHeaderImageView (com.github.moko256.twitlatte.widget.UserHeaderImageView)1