Render full markdown in CommentsListingRecyclerViewAdapter.

This commit is contained in:
Docile-Alligator 2022-04-02 16:09:16 +08:00
parent 95d792779e
commit 840216b3d9
4 changed files with 113 additions and 62 deletions

View File

@ -248,13 +248,13 @@ public class AccountSavedThingActivity extends BaseActivity implements ActivityT
}
}
private void lockSwipeRightToGoBack() {
public void lockSwipeRightToGoBack() {
if (mSlidrInterface != null) {
mSlidrInterface.lock();
}
}
private void unlockSwipeRightToGoBack() {
public void unlockSwipeRightToGoBack() {
if (mSlidrInterface != null) {
mSlidrInterface.unlock();
}

View File

@ -1755,13 +1755,13 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
}
}
private void lockSwipeRightToGoBack() {
public void lockSwipeRightToGoBack() {
if (mSlidrInterface != null) {
mSlidrInterface.lock();
}
}
private void unlockSwipeRightToGoBack() {
public void unlockSwipeRightToGoBack() {
if (mSlidrInterface != null) {
mSlidrInterface.unlock();
}

View File

@ -3,9 +3,11 @@ package ml.docilealligator.infinityforreddit.adapters;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.net.Uri;
import android.os.Bundle;
import android.text.Spanned;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
@ -26,6 +28,8 @@ import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;
import org.commonmark.ext.gfm.tables.TableBlock;
import java.util.Locale;
import butterknife.BindView;
@ -42,20 +46,28 @@ import io.noties.markwon.inlineparser.BangInlineProcessor;
import io.noties.markwon.inlineparser.HtmlInlineProcessor;
import io.noties.markwon.inlineparser.MarkwonInlineParserPlugin;
import io.noties.markwon.linkify.LinkifyPlugin;
import io.noties.markwon.movement.MovementMethodPlugin;
import io.noties.markwon.recycler.table.TableEntry;
import io.noties.markwon.recycler.table.TableEntryPlugin;
import me.saket.bettermovementmethod.BetterLinkMovementMethod;
import ml.docilealligator.infinityforreddit.NetworkState;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.SaveThing;
import ml.docilealligator.infinityforreddit.VoteThing;
import ml.docilealligator.infinityforreddit.activities.AccountSavedThingActivity;
import ml.docilealligator.infinityforreddit.activities.BaseActivity;
import ml.docilealligator.infinityforreddit.activities.LinkResolverActivity;
import ml.docilealligator.infinityforreddit.activities.ViewPostDetailActivity;
import ml.docilealligator.infinityforreddit.activities.ViewSubredditDetailActivity;
import ml.docilealligator.infinityforreddit.activities.ViewUserDetailActivity;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.CommentMoreBottomSheetFragment;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.UrlMenuBottomSheetFragment;
import ml.docilealligator.infinityforreddit.comment.Comment;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.customviews.CommentIndentationView;
import ml.docilealligator.infinityforreddit.customviews.CustomMarkwonAdapter;
import ml.docilealligator.infinityforreddit.customviews.LinearLayoutManagerBugFixed;
import ml.docilealligator.infinityforreddit.customviews.MarkwonLinearLayoutManager;
import ml.docilealligator.infinityforreddit.customviews.SpoilerOnClickTextView;
import ml.docilealligator.infinityforreddit.markdown.SpoilerParserPlugin;
import ml.docilealligator.infinityforreddit.markdown.SuperscriptInlineProcessor;
@ -83,6 +95,7 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
private Retrofit mOauthRetrofit;
private Locale mLocale;
private Markwon mMarkwon;
private RecyclerView.RecycledViewPool recycledViewPool;
private String mAccessToken;
private String mAccountName;
private int mColorPrimaryLightTheme;
@ -115,43 +128,6 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
mOauthRetrofit = oauthRetrofit;
mCommentColor = customThemeWrapper.getCommentColor();
int commentSpoilerBackgroundColor = mCommentColor | 0xFF000000;
mMarkwon = Markwon.builder(mActivity)
.usePlugin(MarkwonInlineParserPlugin.create(plugin -> {
plugin.excludeInlineProcessor(AutolinkInlineProcessor.class);
plugin.excludeInlineProcessor(HtmlInlineProcessor.class);
plugin.excludeInlineProcessor(BangInlineProcessor.class);
plugin.addInlineProcessor(new SuperscriptInlineProcessor());
}))
.usePlugin(HtmlPlugin.create(plugin -> {
plugin.excludeDefaults(true).addHandler(new SuperScriptHandler());
}))
.usePlugin(new AbstractMarkwonPlugin() {
@NonNull
@Override
public String processMarkdown(@NonNull String markdown) {
return Utils.fixSuperScript(markdown);
}
@Override
public void configureTheme(@NonNull MarkwonTheme.Builder builder) {
builder.linkColor(customThemeWrapper.getLinkColor());
}
@Override
public void configureConfiguration(@NonNull MarkwonConfiguration.Builder builder) {
builder.linkResolver((view, link) -> {
Intent intent = new Intent(mActivity, LinkResolverActivity.class);
Uri uri = Uri.parse(link);
intent.setData(uri);
mActivity.startActivity(intent);
});
}
})
.usePlugin(SpoilerParserPlugin.create(mCommentColor, commentSpoilerBackgroundColor))
.usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS))
.usePlugin(StrikethroughPlugin.create())
.build();
mLocale = locale;
mAccessToken = accessToken;
mAccountName = accountName;
@ -174,6 +150,64 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
mButtonTextColor = customThemeWrapper.getButtonTextColor();
mColorAccent = customThemeWrapper.getColorAccent();
mCommentIconAndInfoColor = customThemeWrapper.getCommentIconAndInfoColor();
int linkColor = customThemeWrapper.getLinkColor();
mMarkwon = Markwon.builder(mActivity)
.usePlugin(MarkwonInlineParserPlugin.create(plugin -> {
plugin.excludeInlineProcessor(AutolinkInlineProcessor.class);
plugin.excludeInlineProcessor(HtmlInlineProcessor.class);
plugin.excludeInlineProcessor(BangInlineProcessor.class);
plugin.addInlineProcessor(new SuperscriptInlineProcessor());
}))
.usePlugin(HtmlPlugin.create(plugin -> {
plugin.excludeDefaults(true).addHandler(new SuperScriptHandler());
}))
.usePlugin(new AbstractMarkwonPlugin() {
@NonNull
@Override
public String processMarkdown(@NonNull String markdown) {
return Utils.fixSuperScript(markdown);
}
@Override
public void beforeSetText(@NonNull TextView textView, @NonNull Spanned markdown) {
if (mActivity.contentTypeface != null) {
textView.setTypeface(mActivity.contentTypeface);
}
textView.setTextColor(mCommentColor);
textView.setHighlightColor(Color.TRANSPARENT);
}
@Override
public void configureConfiguration(@NonNull MarkwonConfiguration.Builder builder) {
builder.linkResolver((view, link) -> {
Intent intent = new Intent(mActivity, LinkResolverActivity.class);
Uri uri = Uri.parse(link);
intent.setData(uri);
mActivity.startActivity(intent);
});
}
@Override
public void configureTheme(@NonNull MarkwonTheme.Builder builder) {
builder.linkColor(linkColor);
}
})
.usePlugin(SpoilerParserPlugin.create(mCommentColor, commentSpoilerBackgroundColor))
.usePlugin(StrikethroughPlugin.create())
.usePlugin(MovementMethodPlugin.create(BetterLinkMovementMethod.linkify(Linkify.WEB_URLS).setOnLinkLongClickListener((textView, url) -> {
if (!activity.isDestroyed() && !activity.isFinishing()) {
UrlMenuBottomSheetFragment urlMenuBottomSheetFragment = new UrlMenuBottomSheetFragment();
Bundle bundle = new Bundle();
bundle.putString(UrlMenuBottomSheetFragment.EXTRA_URL, url);
urlMenuBottomSheetFragment.setArguments(bundle);
urlMenuBottomSheetFragment.show(activity.getSupportFragmentManager(), urlMenuBottomSheetFragment.getTag());
}
return true;
})))
.usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS))
.usePlugin(TableEntryPlugin.create(mActivity))
.build();
recycledViewPool = new RecyclerView.RecycledViewPool();
}
@NonNull
@ -217,7 +251,8 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
Utils.setHTMLWithImageToTextView(((CommentViewHolder) holder).awardsTextView, comment.getAwards(), true);
}
mMarkwon.setMarkdown(((CommentViewHolder) holder).commentMarkdownView, comment.getCommentMarkdown());
((CommentViewHolder) holder).markwonAdapter.setMarkdown(mMarkwon, comment.getCommentMarkdown());
((CommentViewHolder) holder).markwonAdapter.notifyDataSetChanged();
((CommentViewHolder) holder).scoreTextView.setText(Utils.getNVotes(mShowAbsoluteNumberOfVotes,
comment.getScore() + comment.getVoteType()));
@ -353,7 +388,7 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
@BindView(R.id.awards_text_view_item_comment)
TextView awardsTextView;
@BindView(R.id.comment_markdown_view_item_post_comment)
SpoilerOnClickTextView commentMarkdownView;
RecyclerView commentMarkdownView;
@BindView(R.id.bottom_constraint_layout_item_post_comment)
ConstraintLayout bottomConstraintLayout;
@BindView(R.id.up_vote_button_item_post_comment)
@ -374,6 +409,7 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
ImageView replyButton;
@BindView(R.id.divider_item_comment)
View commentDivider;
CustomMarkwonAdapter markwonAdapter;
CommentViewHolder(View itemView) {
super(itemView);
@ -435,15 +471,11 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
awardsTextView.setTypeface(mActivity.typeface);
scoreTextView.setTypeface(mActivity.typeface);
}
if (mActivity.contentTypeface != null) {
commentMarkdownView.setTypeface(mActivity.typeface);
}
itemView.setBackgroundColor(mCommentBackgroundColor);
authorTextView.setTextColor(mUsernameColor);
authorFlairTextView.setTextColor(mAuthorFlairColor);
commentTimeTextView.setTextColor(mSecondaryTextColor);
awardsTextView.setTextColor(mSecondaryTextColor);
commentMarkdownView.setTextColor(mCommentColor);
upvoteButton.setColorFilter(mCommentIconAndInfoColor, PorterDuff.Mode.SRC_IN);
scoreTextView.setTextColor(mCommentIconAndInfoColor);
downvoteButton.setColorFilter(mCommentIconAndInfoColor, PorterDuff.Mode.SRC_IN);
@ -500,24 +532,43 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
}
});
commentMarkdownView.setOnClickListener(view -> {
if (commentMarkdownView.isSpoilerOnClick()) {
commentMarkdownView.setSpoilerOnClick(false);
return;
commentMarkdownView.setRecycledViewPool(recycledViewPool);
LinearLayoutManagerBugFixed linearLayoutManager = new MarkwonLinearLayoutManager(mActivity, new MarkwonLinearLayoutManager.HorizontalScrollViewScrolledListener() {
@Override
public void onScrolledLeft() {
if (mActivity instanceof AccountSavedThingActivity) {
((AccountSavedThingActivity) mActivity).lockSwipeRightToGoBack();
} else {
((ViewUserDetailActivity) mActivity).lockSwipeRightToGoBack();
}
}
@Override
public void onScrolledRight() {
if (mActivity instanceof AccountSavedThingActivity) {
((AccountSavedThingActivity) mActivity).unlockSwipeRightToGoBack();
} else {
((ViewUserDetailActivity) mActivity).unlockSwipeRightToGoBack();
}
}
});
commentMarkdownView.setLayoutManager(linearLayoutManager);
markwonAdapter = CustomMarkwonAdapter.builder(R.layout.adapter_default_entry, R.id.text)
.include(TableBlock.class, TableEntry.create(builder -> builder
.tableLayout(R.layout.adapter_table_block, R.id.table_layout)
.textLayoutIsRoot(R.layout.view_table_entry_cell)))
.build();
markwonAdapter.setOnClickListener(view -> {
if (view instanceof SpoilerOnClickTextView) {
if (((SpoilerOnClickTextView) view).isSpoilerOnClick()) {
((SpoilerOnClickTextView) view).setSpoilerOnClick(false);
return;
}
}
itemView.callOnClick();
});
commentMarkdownView.setAdapter(markwonAdapter);
commentMarkdownView.setMovementMethod(BetterLinkMovementMethod.newInstance().setOnLinkLongClickListener((textView, url) -> {
if (mActivity != null && !mActivity.isDestroyed() && !mActivity.isFinishing()) {
UrlMenuBottomSheetFragment urlMenuBottomSheetFragment = new UrlMenuBottomSheetFragment();
Bundle bundle = new Bundle();
bundle.putString(UrlMenuBottomSheetFragment.EXTRA_URL, url);
urlMenuBottomSheetFragment.setArguments(bundle);
urlMenuBottomSheetFragment.show(mActivity.getSupportFragmentManager(), urlMenuBottomSheetFragment.getTag());
}
return true;
}));
upvoteButton.setOnClickListener(view -> {
if (mAccessToken == null) {
Toast.makeText(mActivity, R.string.login_first, Toast.LENGTH_SHORT).show();

View File

@ -35,7 +35,7 @@ public class SpoilerParserPlugin extends AbstractMarkwonPlugin {
this.backgroundColor = backgroundColor;
}
public static SpoilerParserPlugin create(@NonNull int textColor, @NonNull int backgroundColor) {
public static SpoilerParserPlugin create(int textColor, int backgroundColor) {
return new SpoilerParserPlugin(textColor, backgroundColor);
}