Show author avatars in comments.

This commit is contained in:
Alex Ning 2022-01-21 19:52:31 +08:00
parent de105359f9
commit e68f8ed4bc
6 changed files with 98 additions and 14 deletions

View File

@ -45,6 +45,8 @@ import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.Subscribe;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import javax.inject.Inject; import javax.inject.Inject;
@ -128,6 +130,7 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele
ArrayList<Post> posts; ArrayList<Post> posts;
@State @State
Post post; Post post;
public Map<String, String> authorIcons = new HashMap<>();
private FragmentManager fragmentManager; private FragmentManager fragmentManager;
private SlidrInterface mSlidrInterface; private SlidrInterface mSlidrInterface;
private SectionsPagerAdapter sectionsPagerAdapter; private SectionsPagerAdapter sectionsPagerAdapter;

View File

@ -388,6 +388,9 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
replyButton.setVisibility(View.GONE); replyButton.setVisibility(View.GONE);
((ConstraintLayout.LayoutParams) authorTextView.getLayoutParams()).setMarginStart(0);
((ConstraintLayout.LayoutParams) authorFlairTextView.getLayoutParams()).setMarginStart(0);
if (mVoteButtonsOnTheRight) { if (mVoteButtonsOnTheRight) {
ConstraintSet constraintSet = new ConstraintSet(); ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(bottomConstraintLayout); constraintSet.clone(bottomConstraintLayout);

View File

@ -28,6 +28,9 @@ import androidx.core.graphics.drawable.DrawableCompat;
import androidx.recyclerview.widget.ItemTouchHelper; import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.request.RequestOptions;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar; import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;
import java.util.ArrayList; import java.util.ArrayList;
@ -49,6 +52,7 @@ import io.noties.markwon.inlineparser.HtmlInlineProcessor;
import io.noties.markwon.inlineparser.MarkwonInlineParserPlugin; import io.noties.markwon.inlineparser.MarkwonInlineParserPlugin;
import io.noties.markwon.linkify.LinkifyPlugin; import io.noties.markwon.linkify.LinkifyPlugin;
import io.noties.markwon.movement.MovementMethodPlugin; import io.noties.markwon.movement.MovementMethodPlugin;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
import me.saket.bettermovementmethod.BetterLinkMovementMethod; import me.saket.bettermovementmethod.BetterLinkMovementMethod;
import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.SaveThing; import ml.docilealligator.infinityforreddit.SaveThing;
@ -96,6 +100,7 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
private Post mPost; private Post mPost;
private ArrayList<Comment> mVisibleComments; private ArrayList<Comment> mVisibleComments;
private Locale mLocale; private Locale mLocale;
private RequestManager mGlide;
private String mSingleCommentId; private String mSingleCommentId;
private boolean mIsSingleCommentThreadMode; private boolean mIsSingleCommentThreadMode;
private boolean mVoteButtonsOnTheRight; private boolean mVoteButtonsOnTheRight;
@ -159,6 +164,7 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
mExecutor = executor; mExecutor = executor;
mRetrofit = retrofit; mRetrofit = retrofit;
mOauthRetrofit = oauthRetrofit; mOauthRetrofit = oauthRetrofit;
mGlide = Glide.with(activity);
mSecondaryTextColor = customThemeWrapper.getSecondaryTextColor(); mSecondaryTextColor = customThemeWrapper.getSecondaryTextColor();
mCommentTextColor = customThemeWrapper.getCommentColor(); mCommentTextColor = customThemeWrapper.getCommentColor();
int commentSpoilerBackgroundColor = mCommentTextColor | 0xFF000000; int commentSpoilerBackgroundColor = mCommentTextColor | 0xFF000000;
@ -397,6 +403,25 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
currentUserDrawable, null, null, null); currentUserDrawable, null, null, null);
} }
if (comment.getAuthorIconUrl() == null) {
mFragment.loadIcon(comment.getAuthor(), (authorName, iconUrl) -> {
if (authorName.equals(comment.getAuthor())) {
mGlide.load(iconUrl)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
.error(mGlide.load(R.drawable.subreddit_default_icon)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))))
.into(((CommentViewHolder) holder).authorIconImageView);
comment.setAuthorIconUrl(iconUrl);
}
});
} else {
mGlide.load(comment.getAuthorIconUrl())
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
.error(mGlide.load(R.drawable.subreddit_default_icon)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))))
.into(((CommentViewHolder) holder).authorIconImageView);
}
if (mShowElapsedTime) { if (mShowElapsedTime) {
((CommentViewHolder) holder).commentTimeTextView.setText( ((CommentViewHolder) holder).commentTimeTextView.setText(
Utils.getElapsedTime(mActivity, comment.getCommentTimeMillis())); Utils.getElapsedTime(mActivity, comment.getCommentTimeMillis()));
@ -1036,6 +1061,7 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
((CommentViewHolder) holder).authorTextView.setTextColor(mUsernameColor); ((CommentViewHolder) holder).authorTextView.setTextColor(mUsernameColor);
((CommentViewHolder) holder).authorFlairTextView.setVisibility(View.GONE); ((CommentViewHolder) holder).authorFlairTextView.setVisibility(View.GONE);
((CommentViewHolder) holder).authorTextView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null); ((CommentViewHolder) holder).authorTextView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
mGlide.clear(((CommentViewHolder) holder).authorIconImageView);
((CommentViewHolder) holder).topScoreTextView.setTextColor(mSecondaryTextColor); ((CommentViewHolder) holder).topScoreTextView.setTextColor(mSecondaryTextColor);
((CommentViewHolder) holder).awardsTextView.setText(""); ((CommentViewHolder) holder).awardsTextView.setText("");
((CommentViewHolder) holder).awardsTextView.setVisibility(View.GONE); ((CommentViewHolder) holder).awardsTextView.setVisibility(View.GONE);
@ -1078,6 +1104,8 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
public class CommentViewHolder extends RecyclerView.ViewHolder { public class CommentViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.linear_layout_item_comment) @BindView(R.id.linear_layout_item_comment)
LinearLayout linearLayout; LinearLayout linearLayout;
@BindView(R.id.author_icon_image_view_item_post_comment)
ImageView authorIconImageView;
@BindView(R.id.author_text_view_item_post_comment) @BindView(R.id.author_text_view_item_post_comment)
TextView authorTextView; TextView authorTextView;
@BindView(R.id.author_flair_text_view_item_post_comment) @BindView(R.id.author_flair_text_view_item_post_comment)
@ -1174,6 +1202,9 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
if (mActivity.contentTypeface != null) { if (mActivity.contentTypeface != null) {
commentMarkdownView.setTypeface(mActivity.contentTypeface); commentMarkdownView.setTypeface(mActivity.contentTypeface);
} }
authorIconImageView.setVisibility(View.VISIBLE);
itemView.setBackgroundColor(mCommentBackgroundColor); itemView.setBackgroundColor(mCommentBackgroundColor);
authorTextView.setTextColor(mUsernameColor); authorTextView.setTextColor(mUsernameColor);
commentTimeTextView.setTextColor(mSecondaryTextColor); commentTimeTextView.setTextColor(mSecondaryTextColor);

View File

@ -30,6 +30,7 @@ public class Comment implements Parcelable {
private String author; private String author;
private String authorFlair; private String authorFlair;
private String authorFlairHTML; private String authorFlairHTML;
private String authorIconUrl;
private String linkAuthor; private String linkAuthor;
private long commentTimeMillis; private long commentTimeMillis;
private String commentMarkdown; private String commentMarkdown;
@ -117,6 +118,7 @@ public class Comment implements Parcelable {
author = in.readString(); author = in.readString();
authorFlair = in.readString(); authorFlair = in.readString();
authorFlairHTML = in.readString(); authorFlairHTML = in.readString();
authorIconUrl = in.readString();
linkAuthor = in.readString(); linkAuthor = in.readString();
commentTimeMillis = in.readLong(); commentTimeMillis = in.readLong();
commentMarkdown = in.readString(); commentMarkdown = in.readString();
@ -169,6 +171,14 @@ public class Comment implements Parcelable {
return authorFlairHTML; return authorFlairHTML;
} }
public String getAuthorIconUrl() {
return authorIconUrl;
}
public void setAuthorIconUrl(String authorIconUrl) {
this.authorIconUrl = authorIconUrl;
}
public String getLinkAuthor() { public String getLinkAuthor() {
return linkAuthor; return linkAuthor;
} }
@ -385,6 +395,7 @@ public class Comment implements Parcelable {
parcel.writeString(author); parcel.writeString(author);
parcel.writeString(authorFlair); parcel.writeString(authorFlair);
parcel.writeString(authorFlairHTML); parcel.writeString(authorFlairHTML);
parcel.writeString(authorIconUrl);
parcel.writeString(linkAuthor); parcel.writeString(linkAuthor);
parcel.writeLong(commentTimeMillis); parcel.writeLong(commentTimeMillis);
parcel.writeString(commentMarkdown); parcel.writeString(commentMarkdown);

View File

@ -84,6 +84,7 @@ import ml.docilealligator.infinityforreddit.activities.ViewPostDetailActivity;
import ml.docilealligator.infinityforreddit.adapters.CommentsRecyclerViewAdapter; import ml.docilealligator.infinityforreddit.adapters.CommentsRecyclerViewAdapter;
import ml.docilealligator.infinityforreddit.adapters.PostDetailRecyclerViewAdapter; import ml.docilealligator.infinityforreddit.adapters.PostDetailRecyclerViewAdapter;
import ml.docilealligator.infinityforreddit.apis.RedditAPI; import ml.docilealligator.infinityforreddit.apis.RedditAPI;
import ml.docilealligator.infinityforreddit.asynctasks.LoadUserData;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.FlairBottomSheetFragment; import ml.docilealligator.infinityforreddit.bottomsheetfragments.FlairBottomSheetFragment;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostCommentSortTypeBottomSheetFragment; import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostCommentSortTypeBottomSheetFragment;
import ml.docilealligator.infinityforreddit.comment.Comment; import ml.docilealligator.infinityforreddit.comment.Comment;
@ -842,6 +843,18 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
} }
} }
public void loadIcon(String authorName, LoadIconListener loadIconListener) {
if (activity.authorIcons.containsKey(authorName)) {
loadIconListener.loadIconSuccess(authorName, activity.authorIcons.get(authorName));
} else {
LoadUserData.loadUserData(mExecutor, new Handler(), mRedditDataRoomDatabase, authorName,
mRetrofit, iconImageUrl -> {
activity.authorIcons.put(authorName, iconImageUrl);
loadIconListener.loadIconSuccess(authorName, iconImageUrl);
});
}
}
@Override @Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) { public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
inflater.inflate(R.menu.view_post_detail_fragment, menu); inflater.inflate(R.menu.view_post_detail_fragment, menu);
@ -1952,4 +1965,8 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
mPostAdapter.setCanPlayVideo(hasWindowsFocus); mPostAdapter.setCanPlayVideo(hasWindowsFocus);
} }
} }
public interface LoadIconListener {
void loadIconSuccess(String authorName, String iconUrl);
}
} }

View File

@ -24,35 +24,48 @@
android:paddingStart="16dp" android:paddingStart="16dp"
android:paddingEnd="16dp"> android:paddingEnd="16dp">
<ImageView
android:id="@+id/author_icon_image_view_item_post_comment"
android:layout_width="24dp"
android:layout_height="24dp"
android:visibility="gone"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="@id/barrier4" />
<TextView <TextView
android:id="@+id/author_text_view_item_post_comment" android:id="@+id/author_text_view_item_post_comment"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" android:layout_marginEnd="8dp"
android:drawablePadding="4dp" android:drawablePadding="4dp"
android:textSize="?attr/font_default"
android:fontFamily="?attr/font_family" android:fontFamily="?attr/font_family"
app:layout_constraintBottom_toTopOf="@id/author_flair_text_view_item_post_comment" android:textSize="?attr/font_default"
app:layout_constraintEnd_toStartOf="@+id/barrier"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constrainedWidth="true" app:layout_constrainedWidth="true"
app:layout_constraintHorizontal_bias="0" /> app:layout_constraintBottom_toTopOf="@id/author_flair_text_view_item_post_comment"
app:layout_constraintStart_toEndOf="@id/author_icon_image_view_item_post_comment"
app:layout_constraintEnd_toStartOf="@+id/barrier"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintTop_toTopOf="parent" />
<TextView <TextView
android:id="@+id/author_flair_text_view_item_post_comment" android:id="@+id/author_flair_text_view_item_post_comment"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" android:layout_marginEnd="8dp"
android:fontFamily="?attr/font_family" android:fontFamily="?attr/font_family"
android:textSize="?attr/font_12" android:textSize="?attr/font_12"
android:visibility="gone" android:visibility="gone"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/author_icon_image_view_item_post_comment"
app:layout_constraintEnd_toStartOf="@+id/barrier" app:layout_constraintEnd_toStartOf="@+id/barrier"
app:layout_constraintHorizontal_bias="0" app:layout_constraintHorizontal_bias="0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/author_text_view_item_post_comment" app:layout_constraintTop_toBottomOf="@id/author_text_view_item_post_comment"
app:layout_constrainedWidth="true"
tools:visibility="visible" /> tools:visibility="visible" />
<TextView <TextView
@ -60,9 +73,9 @@
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginEnd="8dp" android:layout_marginEnd="8dp"
android:fontFamily="?attr/font_family"
android:gravity="end" android:gravity="end"
android:textSize="?attr/font_default" android:textSize="?attr/font_default"
android:fontFamily="?attr/font_family"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/comment_time_text_view_item_post_comment" app:layout_constraintEnd_toStartOf="@id/comment_time_text_view_item_post_comment"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
@ -71,9 +84,9 @@
android:id="@+id/comment_time_text_view_item_post_comment" android:id="@+id/comment_time_text_view_item_post_comment"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:fontFamily="?attr/font_family"
android:gravity="end" android:gravity="end"
android:textSize="?attr/font_default" android:textSize="?attr/font_default"
android:fontFamily="?attr/font_family"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
@ -85,6 +98,13 @@
app:barrierDirection="start" app:barrierDirection="start"
app:constraint_referenced_ids="top_score_text_view_item_post_comment" /> app:constraint_referenced_ids="top_score_text_view_item_post_comment" />
<androidx.constraintlayout.widget.Barrier
android:id="@+id/barrier4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="start"
app:constraint_referenced_ids="author_text_view_item_post_comment,author_flair_text_view_item_post_comment" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
<TextView <TextView
@ -103,10 +123,9 @@
android:id="@+id/comment_markdown_view_item_post_comment" android:id="@+id/comment_markdown_view_item_post_comment"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp" android:layout_margin="8dp"
android:layout_marginStart="16dp" android:paddingStart="8dp"
android:layout_marginEnd="16dp" android:paddingEnd="8dp"
android:layout_marginBottom="8dp"
android:textColor="?attr/primaryTextColor" android:textColor="?attr/primaryTextColor"
android:textSize="?attr/content_font_default" android:textSize="?attr/content_font_default"
android:fontFamily="?attr/content_font_family" /> android:fontFamily="?attr/content_font_family" />