From 840216b3d9b0999abfce490d3d5ffdfda685ccb2 Mon Sep 17 00:00:00 2001 From: Docile-Alligator Date: Sat, 2 Apr 2022 16:09:16 +0800 Subject: Render full markdown in CommentsListingRecyclerViewAdapter. --- .../activities/AccountSavedThingActivity.java | 4 +- .../activities/ViewUserDetailActivity.java | 4 +- .../CommentsListingRecyclerViewAdapter.java | 141 ++++++++++++++------- .../markdown/SpoilerParserPlugin.java | 2 +- 4 files changed, 101 insertions(+), 50 deletions(-) (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountSavedThingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountSavedThingActivity.java index 32896b06..ea973995 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountSavedThingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountSavedThingActivity.java @@ -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(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java index 78625af4..48f43088 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java @@ -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(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsListingRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsListingRecyclerViewAdapter.java index 7ebfb52e..aecd9581 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsListingRecyclerViewAdapter.java @@ -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 { plugin.excludeInlineProcessor(AutolinkInlineProcessor.class); @@ -133,8 +169,12 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter { + 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(); - mLocale = locale; - mAccessToken = accessToken; - mAccountName = accountName; - mShowElapsedTime = sharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_ELAPSED_TIME_KEY, false); - mShowCommentDivider = sharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_COMMENT_DIVIDER, false); - mShowAbsoluteNumberOfVotes = sharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_ABSOLUTE_NUMBER_OF_VOTES, true); - mVoteButtonsOnTheRight = sharedPreferences.getBoolean(SharedPreferencesUtils.VOTE_BUTTONS_ON_THE_RIGHT_KEY, false); - mTimeFormatPattern = sharedPreferences.getString(SharedPreferencesUtils.TIME_FORMAT_KEY, SharedPreferencesUtils.TIME_FORMAT_DEFAULT_VALUE); - mRetryLoadingMoreCallback = retryLoadingMoreCallback; - mColorPrimaryLightTheme = customThemeWrapper.getColorPrimaryLightTheme(); - mSecondaryTextColor = customThemeWrapper.getSecondaryTextColor(); - mCommentBackgroundColor = customThemeWrapper.getCommentBackgroundColor(); - mCommentColor = customThemeWrapper.getCommentColor(); - mDividerColor = customThemeWrapper.getDividerColor(); - mSubredditColor = customThemeWrapper.getSubreddit(); - mUsernameColor = customThemeWrapper.getUsername(); - mAuthorFlairColor = customThemeWrapper.getAuthorFlairTextColor(); - mUpvotedColor = customThemeWrapper.getUpvoted(); - mDownvotedColor = customThemeWrapper.getDownvoted(); - mButtonTextColor = customThemeWrapper.getButtonTextColor(); - mColorAccent = customThemeWrapper.getColorAccent(); - mCommentIconAndInfoColor = customThemeWrapper.getCommentIconAndInfoColor(); + recycledViewPool = new RecyclerView.RecycledViewPool(); } @NonNull @@ -217,7 +251,8 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter { - 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(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/SpoilerParserPlugin.java b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/SpoilerParserPlugin.java index d1dde6f2..6f0280e7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/SpoilerParserPlugin.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/SpoilerParserPlugin.java @@ -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); } -- cgit v1.2.3