bug fix, added comments

This commit is contained in:
nuclearfog 2022-12-16 21:51:58 +01:00
parent 67a820b983
commit b88bba7407
No known key found for this signature in database
GPG Key ID: 03488A185C476379
6 changed files with 64 additions and 19 deletions

View File

@ -27,7 +27,7 @@ public class CardAdapter extends RecyclerView.Adapter<CardHolder> implements OnI
private Picasso picasso;
private OnCardClickListener listener;
private Card[] cards;
private Card[] cards = {};
public CardAdapter(Context context, OnCardClickListener listener) {
@ -71,7 +71,7 @@ public class CardAdapter extends RecyclerView.Adapter<CardHolder> implements OnI
* replace all items
* @param newCards new items to insert
*/
public void replaceAll(Card[] newCards) {
public void replaceAll(@NonNull Card[] newCards) {
cards = Arrays.copyOf(newCards, newCards.length);
notifyDataSetChanged();
}
@ -81,8 +81,14 @@ public class CardAdapter extends RecyclerView.Adapter<CardHolder> implements OnI
*/
public interface OnCardClickListener {
/**
* indicates a link click
*/
int TYPE_LINK = 1;
/**
* indicates an image thumbnail click
*/
int TYPE_IMAGE = 2;
/**

View File

@ -31,9 +31,15 @@ public class CardHolder extends ViewHolder implements OnClickListener {
*/
private static final int TEXT_TRANSPARENCY = 0xafffffff;
/**
* how much views should be fit in the window
*/
private static final int COLUMN_COUNT = 2;
private static final int MAX_TITLE_LEN = 20;
/**
* maximum char count of the title before truncating
*/
private static final int TITLE_MAX_LEN = 30;
private TextView linkText;
private ImageView preview;
@ -80,17 +86,17 @@ public class CardHolder extends ViewHolder implements OnClickListener {
public void setContent(Card card) {
String textStr;
String title = card.getTitle();
if (title.length() > MAX_TITLE_LEN)
textStr = title.substring(0, MAX_TITLE_LEN - 3) + "...";
else
if (title.length() > TITLE_MAX_LEN) {
textStr = title.substring(0, TITLE_MAX_LEN - 3) + "...";
} else {
textStr = title;
}
if (!card.getDescription().isEmpty())
textStr += '\n' + card.getDescription();
SpannableString textSpan = new SpannableString(textStr);
if (!title.isEmpty())
textSpan.setSpan(new StyleSpan(Typeface.BOLD), 0, title.length() - 1, 0);
textSpan.setSpan(new StyleSpan(Typeface.BOLD), 0, Math.min(textStr.length() - 1, TITLE_MAX_LEN), 0);
linkText.setText(textSpan);
//description.setText(card.getDescription());
if (!card.getImageUrl().isEmpty()) {
picasso.load(card.getImageUrl()).into(preview);
}
@ -108,8 +114,14 @@ public class CardHolder extends ViewHolder implements OnClickListener {
*/
public interface OnItemClickListener {
/**
* indicates a link click
*/
int TYPE_LINK = 1;
/**
* indicates a click on the image
*/
int TYPE_IMAGE = 2;
/**

View File

@ -1,5 +1,7 @@
package org.nuclearfog.twidda.backend.api.twitter.impl;
import android.util.Patterns;
import org.json.JSONArray;
import org.json.JSONObject;
import org.nuclearfog.twidda.model.Card;
@ -16,7 +18,7 @@ public class TwitterCard implements Card {
private String url;
private String title;
private String description;
private String imageUrl;
private String imageUrl = "";
public TwitterCard(JSONObject json) {
@ -25,14 +27,14 @@ public class TwitterCard implements Card {
title = json.optString("title", "");
description = json.optString("description", "");
if (images != null && images.length() > 0) {
// first index contains image with the highest resolutuion
JSONObject image = images.optJSONObject(0);
if (image != null) {
imageUrl = image.optString("url");
} else {
imageUrl = "";
String imageUrl = image.optString("url", "");
if (Patterns.WEB_URL.matcher(imageUrl).matches()) {
this.imageUrl = imageUrl;
}
}
} else {
imageUrl = "";
}
}

View File

@ -33,6 +33,7 @@ public class TwitterNotification implements Notification {
@Override
public int getType() {
// Twitte rcurrently only supports mentions as notification
return TYPE_MENTION;
}

View File

@ -1,5 +1,7 @@
package org.nuclearfog.twidda.backend.api.twitter.impl;
import android.util.Patterns;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -54,11 +56,11 @@ public class UserV1 implements User {
public UserV1(JSONObject json) throws JSONException {
String idStr = json.getString("id_str");
String profileImageUrl = json.optString("profile_image_url_https", "");
String profileBannerUrl = json.optString("profile_banner_url", "");
username = json.optString("name", "");
screenName = '@' + json.optString("screen_name", "");
isVerified = json.optBoolean("verified");
isLocked = json.optBoolean("protected");
profileBannerUrl = json.optString("profile_banner_url", "");
location = json.optString("location", "");
following = json.optInt("friends_count");
follower = json.optInt("followers_count");
@ -70,6 +72,17 @@ public class UserV1 implements User {
description = getDescription(json);
url = getUrl(json);
//
if (Patterns.WEB_URL.matcher(profileImageUrl).matches()) {
this.profileImageUrl = profileImageUrl;
} else {
this.profileImageUrl = "";
}
if (Patterns.WEB_URL.matcher(profileBannerUrl).matches()) {
this.profileBannerUrl = profileBannerUrl;
} else {
this.profileBannerUrl = "";
}
if (defaultImage) {
this.profileImageUrl = profileImageUrl;
} else {

View File

@ -1,5 +1,7 @@
package org.nuclearfog.twidda.backend.api.twitter.impl;
import android.util.Patterns;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -50,12 +52,12 @@ public class UserV2 implements User {
String idStr = json.getString("id");
String profileImageUrl = json.optString("profile_image_url", "");
String profileBannerUrl = json.optString("profile_banner_url", "");
username = json.optString("name", "");
screenName = '@' + json.optString("username", "");
isProtected = json.optBoolean("protected");
location = json.optString("location", "");
isVerified = json.optBoolean("verified");
profileBannerUrl = json.optString("profile_banner_url", "");
created = StringTools.getTime(json.optString("created_at", ""), StringTools.TIME_TWITTER_V2);
defaultImage = profileImageUrl.contains("default_profile_images");
@ -67,10 +69,19 @@ public class UserV2 implements User {
follower = metrics.optInt("followers_count");
tweetCount = metrics.optInt("tweet_count");
}
if (defaultImage) {
this.profileImageUrl = profileImageUrl;
if (Patterns.WEB_URL.matcher(profileImageUrl).matches()) {
if (defaultImage) {
this.profileImageUrl = profileImageUrl;
} else {
this.profileImageUrl = StringTools.createProfileImageLink(profileImageUrl);
}
} else {
this.profileImageUrl = StringTools.createProfileImageLink(profileImageUrl);
this.profileImageUrl = "";
}
if (Patterns.WEB_URL.matcher(profileBannerUrl).matches()) {
this.profileBannerUrl = profileBannerUrl;
} else {
this.profileBannerUrl = "";
}
try {
id = Long.parseLong(idStr);