Check for null users for interactions such as likes, replies and follows.

This commit is contained in:
Andrei 2022-06-26 11:34:20 -07:00
parent 27793b60ef
commit 6092570960
2 changed files with 32 additions and 9 deletions

View file

@ -1,5 +1,7 @@
package com.newsblur.domain;
import androidx.annotation.Nullable;
import com.google.gson.annotations.SerializedName;
public class ActivityDetails {
@ -14,6 +16,7 @@ public class ActivityDetails {
@SerializedName("time_since")
public String timeSince;
@Nullable
@SerializedName("with_user")
public WithUser user;

View file

@ -17,6 +17,7 @@ import com.newsblur.util.ImageLoader;
public class InteractionsAdapter extends ActivityDetailsAdapter {
private final String nowFollowingYou, repliedToYour, comment, reply, favoritedComments, reshared, your, you;
private final String UNKNOWN_USERNAME = "Unknown";
public InteractionsAdapter(final Context context, UserDetails user, ImageLoader iconLoader) {
super(context, user, iconLoader);
@ -60,28 +61,41 @@ public class InteractionsAdapter extends ActivityDetailsAdapter {
private CharSequence getFollowContent(ActivityDetails activity, String userString) {
SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
stringBuilder.append(activity.user.username);
int usernameLength;
if (activity.user != null) {
usernameLength = activity.user.username.length();
stringBuilder.append(activity.user.username);
} else {
usernameLength = UNKNOWN_USERNAME.length();
stringBuilder.append(UNKNOWN_USERNAME);
}
stringBuilder.append(" ");
stringBuilder.append(String.format(nowFollowingYou, userString));
stringBuilder.setSpan(linkColor, 0, activity.user.username.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
stringBuilder.setSpan(contentColor, activity.user.username.length() + 1, stringBuilder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
stringBuilder.setSpan(linkColor, 0, usernameLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
stringBuilder.setSpan(contentColor, usernameLength + 1, stringBuilder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return stringBuilder;
}
private CharSequence getCommentLikeContent(ActivityDetails activity, String userString) {
SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
stringBuilder.append(activity.user.username);
int usernameLength;
if (activity.user != null) {
usernameLength = activity.user.username.length();
stringBuilder.append(activity.user.username);
} else {
usernameLength = UNKNOWN_USERNAME.length();
stringBuilder.append(UNKNOWN_USERNAME);
}
stringBuilder.append(" ");
String favoritedString = String.format(favoritedComments, userString);
stringBuilder.append(favoritedString );
stringBuilder.append(favoritedString);
stringBuilder.append(" ");
stringBuilder.append(activity.title);
stringBuilder.append("\n\n\"");
stringBuilder.append(activity.content);
stringBuilder.append("\" ");
int usernameLength = activity.user.username.length();
int titleSpanStart = usernameLength + 1 + favoritedString.length() + 1;
int titleLength = activity.title.length();
stringBuilder.setSpan(linkColor, 0, titleSpanStart + titleLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
@ -94,11 +108,18 @@ public class InteractionsAdapter extends ActivityDetailsAdapter {
private CharSequence getCommentReplyContent(ActivityDetails activity, String userString) {
SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
stringBuilder.append(activity.user.username);
int usernameLength;
if (activity.user != null) {
usernameLength = activity.user.username.length();
stringBuilder.append(activity.user.username);
} else {
usernameLength = UNKNOWN_USERNAME.length();
stringBuilder.append(UNKNOWN_USERNAME);
}
stringBuilder.append(" ");
stringBuilder.append(String.format(repliedToYour, userString));
stringBuilder.append(" ");
int commentReplyLength = 0;
int commentReplyLength;
if (activity.category == ActivityDetails.Category.COMMENT_REPLY) {
stringBuilder.append(comment);
commentReplyLength = comment.length();
@ -110,7 +131,6 @@ public class InteractionsAdapter extends ActivityDetailsAdapter {
stringBuilder.append(activity.content);
stringBuilder.append("\"");
int usernameLength = activity.user.username.length();
stringBuilder.setSpan(linkColor, 0, usernameLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
stringBuilder.setSpan(contentColor, usernameLength + 1, usernameLength + 1 + repliedToYour.length() + 1 + commentReplyLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
stringBuilder.setSpan(quoteColor, stringBuilder.length() - activity.content.length() - 2, stringBuilder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);