use of com.github.moko256.latte.client.base.entity.Status in project twicalico by moko256.
the class StatusesAdapter method getItemViewType.
@Override
public int getItemViewType(int position) {
if (data.get(position) == -1L) {
return 1;
}
Status status = GlobalApplication.statusCache.get(data.get(position));
if (status == null) {
return 1;
}
Status item = status.isRetweet() ? status.getRetweetedStatus() : status;
if (item == null) {
return 1;
}
AppConfiguration conf = GlobalApplication.configuration;
if ((conf.isPatternTweetMuteEnabled && conf.tweetMutePattern.matcher(item.getText()).find()) || (conf.isPatternUserScreenNameMuteEnabled && conf.userScreenNameMutePattern.matcher(item.getUser().getScreenName()).find()) || (conf.isPatternUserNameMuteEnabled && conf.userNameMutePattern.matcher(item.getUser().getName()).find()) || (conf.isPatternTweetSourceMuteEnabled && conf.tweetSourceMutePattern.matcher(item.getSource()).find())) {
return 2;
}
if (shouldShowMediaOnly || (conf.isPatternTweetMuteShowOnlyImageEnabled && item.getMediaEntities().length > 0 && conf.tweetMuteShowOnlyImagePattern.matcher(item.getText()).find())) {
return 3;
}
return super.getItemViewType(position);
}
use of com.github.moko256.latte.client.base.entity.Status in project twicalico by moko256.
the class StatusCacheMap method addAll.
public void addAll(Collection<? extends Status> c) {
if (c.size() > 0) {
Observable<Status> statusesObservable = Observable.unsafeCreate(subscriber -> {
for (Status status : c) {
if (status != null) {
subscriber.onNext(status);
if (status.isRetweet()) {
subscriber.onNext(status.getRetweetedStatus());
}
}
}
subscriber.onCompleted();
});
GlobalApplication.userCache.addAll(statusesObservable.map(Status::getUser).distinct().toList().toSingle().toBlocking().value());
Observable<Status> cachedStatusObservable = statusesObservable.map(CachedStatus::new);
cachedStatusObservable.forEach(status -> cache.put(status.getId(), status));
diskCache.addCachedStatuses(cachedStatusObservable.toList().toSingle().toBlocking().value());
}
}
use of com.github.moko256.latte.client.base.entity.Status in project twicalico by moko256.
the class StatusCacheMap method add.
public void add(@Nullable final Status status) {
if (status != null) {
GlobalApplication.userCache.add(status.getUser());
if (status.isRetweet()) {
add(status.getRetweetedStatus());
}
Status cacheStatus = new CachedStatus(status);
cache.put(status.getId(), cacheStatus);
diskCache.addCachedStatus(cacheStatus);
}
}
use of com.github.moko256.latte.client.base.entity.Status 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);
}
});
}
use of com.github.moko256.latte.client.base.entity.Status 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);
}
Aggregations