Search in sources :

Example 1 with Media

use of com.github.moko256.latte.client.base.entity.Media in project twicalico by moko256.

the class PostTweetModelImpl method postTweet.

@Override
public Single<Status> postTweet() {
    return Single.create(subscriber -> {
        try {
            List<Long> ids = null;
            if (uriList.size() > 0) {
                ids = new ArrayList<>(uriList.size());
                for (Uri uri : uriList) {
                    InputStream image = contentResolver.openInputStream(uri);
                    ByteArrayOutputStream bout = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    while (true) {
                        int len = image.read(buffer);
                        if (len < 0) {
                            break;
                        }
                        bout.write(buffer, 0, len);
                    }
                    Attachment attachment = new Media(((MastodonTwitterImpl) GlobalApplication.twitter).client).postMedia(MultipartBody.Part.createFormData("file", uri.getLastPathSegment(), RequestBody.create(MediaType.parse(contentResolver.getType(uri)), bout.toByteArray()))).execute();
                    ids.add(attachment.getId());
                }
            }
            subscriber.onSuccess(new MTStatus(new Statuses(client).postStatus(tweetText, inReplyToStatusId == -1 ? null : inReplyToStatusId, ids, possiblySensitive, null, com.sys1yagi.mastodon4j.api.entity.Status.Visibility.Public).execute()));
        } catch (IOException | Mastodon4jRequestException e) {
            subscriber.onError(e);
        }
    });
}
Also used : MTStatus(com.github.moko256.mastodon.MTStatus) Mastodon4jRequestException(com.sys1yagi.mastodon4j.api.exception.Mastodon4jRequestException) InputStream(java.io.InputStream) Media(com.sys1yagi.mastodon4j.api.method.Media) Attachment(com.sys1yagi.mastodon4j.api.entity.Attachment) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Uri(android.net.Uri) Statuses(com.sys1yagi.mastodon4j.api.method.Statuses)

Example 2 with Media

use of com.github.moko256.latte.client.base.entity.Media in project twicalico by moko256.

the class TwitterStringUtils method setLinkedSequenceTo.

@SuppressLint("StaticFieldLeak")
public static void setLinkedSequenceTo(Status item, TextView textView) {
    Context context = textView.getContext();
    String tweet = item.getText();
    if (GlobalApplication.clientType == Type.MASTODON) {
        Spanned html = Html.fromHtml(tweet);
        int length = html.length();
        // Trim unless \n\n made by fromHtml() after post
        if (length == 3 && item.getMediaEntities().length > 0 && html.charAt(0) == ".".charAt(0)) {
            // If post has media only, context of post from Mastodon is "."
            textView.setText("");
            return;
        } else if (length >= 2) {
            html = (Spanned) html.subSequence(0, length - 2);
        }
        SpannableStringBuilder builder = convertUrlSpanToCustomTabs(html, context);
        textView.setText(builder);
        List<Emoji> list = ((StatusCacheMap.CachedStatus) item).getEmojis();
        if (list != null) {
            Matcher matcher = containsEmoji.matcher(builder);
            boolean matches = matcher.matches();
            int imageSize;
            if (matches) {
                imageSize = (int) Math.floor((textView.getTextSize() * 1.15) * 2.0);
            } else {
                imageSize = (int) Math.floor(textView.getTextSize() * 1.15);
            }
            new AsyncTask<Void, Void, Map<String, Drawable>>() {

                @Override
                protected Map<String, Drawable> doInBackground(Void... params) {
                    Map<String, Drawable> map = new ArrayMap<>();
                    GlideRequests glideRequests = GlideApp.with(context);
                    for (Emoji emoji : list) {
                        try {
                            Drawable value = glideRequests.load(emoji.getUrl()).submit().get();
                            value.setBounds(0, 0, imageSize, imageSize);
                            map.put(emoji.getShortCode(), value);
                        } catch (InterruptedException | ExecutionException e) {
                            e.printStackTrace();
                        }
                    }
                    return map;
                }

                @Override
                protected void onPostExecute(Map<String, Drawable> map) {
                    if (TextUtils.equals(builder, textView.getText())) {
                        boolean found = matches || matcher.find();
                        while (found) {
                            String shortCode = matcher.group(1);
                            Drawable drawable = map.get(shortCode);
                            if (drawable != null) {
                                builder.setSpan(new ImageSpan(drawable), matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            }
                            found = matcher.find();
                        }
                        textView.setText(builder);
                    }
                }
            }.execute();
        }
        return;
    }
    SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(tweet);
    for (SymbolEntity symbolEntity : item.getSymbolEntities()) {
        spannableStringBuilder.setSpan(new ClickableSpan() {

            @Override
            public void onClick(View view) {
                context.startActivity(SearchResultActivity.getIntent(context, symbolEntity.getText()));
            }
        }, tweet.offsetByCodePoints(0, symbolEntity.getStart()), tweet.offsetByCodePoints(0, symbolEntity.getEnd()), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    }
    for (HashtagEntity hashtagEntity : item.getHashtagEntities()) {
        spannableStringBuilder.setSpan(new ClickableSpan() {

            @Override
            public void onClick(View view) {
                context.startActivity(SearchResultActivity.getIntent(context, "#" + hashtagEntity.getText()));
            }
        }, tweet.offsetByCodePoints(0, hashtagEntity.getStart()), tweet.offsetByCodePoints(0, hashtagEntity.getEnd()), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    }
    for (UserMentionEntity userMentionEntity : item.getUserMentionEntities()) {
        spannableStringBuilder.setSpan(new ClickableSpan() {

            @Override
            public void onClick(View view) {
                context.startActivity(ShowUserActivity.getIntent(context, userMentionEntity.getScreenName()));
            }
        }, tweet.offsetByCodePoints(0, userMentionEntity.getStart()), tweet.offsetByCodePoints(0, userMentionEntity.getEnd()), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    }
    boolean hasMedia = item.getMediaEntities().length > 0;
    List<URLEntity> urlEntities = new ArrayList<>(item.getURLEntities().length + (hasMedia ? 1 : 0));
    urlEntities.addAll(Arrays.asList(item.getURLEntities()));
    if (hasMedia) {
        urlEntities.add(item.getMediaEntities()[0]);
    }
    int tweetLength = tweet.codePointCount(0, tweet.length());
    int sp = 0;
    for (URLEntity entity : urlEntities) {
        String url = entity.getURL();
        String displayUrl = entity.getDisplayURL();
        int urlLength = url.codePointCount(0, url.length());
        int displayUrlLength = displayUrl.codePointCount(0, displayUrl.length());
        if (entity.getStart() <= tweetLength && entity.getEnd() <= tweetLength) {
            int dusp = displayUrlLength - urlLength;
            spannableStringBuilder.replace(tweet.offsetByCodePoints(0, entity.getStart()) + sp, tweet.offsetByCodePoints(0, entity.getEnd()) + sp, displayUrl);
            spannableStringBuilder.setSpan(new ClickableSpan() {

                @Override
                public void onClick(View view) {
                    AppCustomTabsKt.launchChromeCustomTabs(context, entity.getExpandedURL());
                }
            }, tweet.offsetByCodePoints(0, entity.getStart()) + sp, tweet.offsetByCodePoints(0, entity.getEnd()) + sp + dusp, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
            sp += dusp;
        }
    }
    textView.setText(spannableStringBuilder);
}
Also used : Matcher(java.util.regex.Matcher) GlideRequests(com.github.moko256.twicalico.GlideRequests) ArrayList(java.util.ArrayList) SymbolEntity(twitter4j.SymbolEntity) Emoji(com.github.moko256.twicalico.entity.Emoji) HashtagEntity(twitter4j.HashtagEntity) Context(android.content.Context) URLEntity(twitter4j.URLEntity) Drawable(android.graphics.drawable.Drawable) Spanned(android.text.Spanned) ClickableSpan(android.text.style.ClickableSpan) View(android.view.View) TextView(android.widget.TextView) SuppressLint(android.annotation.SuppressLint) UserMentionEntity(twitter4j.UserMentionEntity) ArrayMap(android.support.v4.util.ArrayMap) Map(java.util.Map) StatusCacheMap(com.github.moko256.twicalico.cacheMap.StatusCacheMap) SpannableStringBuilder(android.text.SpannableStringBuilder) ImageSpan(android.text.style.ImageSpan) SuppressLint(android.annotation.SuppressLint)

Example 3 with Media

use of com.github.moko256.latte.client.base.entity.Media 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)

Aggregations

View (android.view.View)2 TextView (android.widget.TextView)2 SuppressLint (android.annotation.SuppressLint)1 Context (android.content.Context)1 Drawable (android.graphics.drawable.Drawable)1 Uri (android.net.Uri)1 ArrayMap (android.support.v4.util.ArrayMap)1 SpannableString (android.text.SpannableString)1 SpannableStringBuilder (android.text.SpannableStringBuilder)1 Spanned (android.text.Spanned)1 ClickableSpan (android.text.style.ClickableSpan)1 ImageSpan (android.text.style.ImageSpan)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 MTStatus (com.github.moko256.mastodon.MTStatus)1