Long press a url to see options in CommentsListingRecyclerViewAdapter.

This commit is contained in:
Alex Ning 2021-01-04 14:42:04 +08:00
parent eb381eb3f4
commit d1aa3df4a7
3 changed files with 42 additions and 38 deletions

View File

@ -14,7 +14,6 @@ import android.os.Bundle;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
import android.text.Spanned; import android.text.Spanned;
import android.text.TextPaint; import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan; import android.text.style.ClickableSpan;
import android.text.style.SuperscriptSpan; import android.text.style.SuperscriptSpan;
import android.text.util.Linkify; import android.text.util.Linkify;
@ -321,7 +320,6 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
textView.setTextColor(markdownColor); textView.setTextColor(markdownColor);
textView.setOnLongClickListener(view -> { textView.setOnLongClickListener(view -> {
if (textView.getSelectionStart() == -1 && textView.getSelectionEnd() == -1) { if (textView.getSelectionStart() == -1 && textView.getSelectionEnd() == -1) {
// do your code here this will only call if its not a hyperlink
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putString(CopyTextBottomSheetFragment.EXTRA_RAW_TEXT, mPost.getSelfTextPlain()); bundle.putString(CopyTextBottomSheetFragment.EXTRA_RAW_TEXT, mPost.getSelfTextPlain());
bundle.putString(CopyTextBottomSheetFragment.EXTRA_MARKDOWN, mPost.getSelfText()); bundle.putString(CopyTextBottomSheetFragment.EXTRA_MARKDOWN, mPost.getSelfText());
@ -3705,8 +3703,6 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
} }
}); });
commentMarkdownView.setMovementMethod(LinkMovementMethod.getInstance());
if (mSwapTapAndLong) { if (mSwapTapAndLong) {
if (mCommentToolbarHideOnClick) { if (mCommentToolbarHideOnClick) {
View.OnLongClickListener hideToolbarOnLongClickListener = view -> hideToolbar(); View.OnLongClickListener hideToolbarOnLongClickListener = view -> hideToolbar();
@ -3742,7 +3738,6 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
return true; return true;
}); });
} }
commentMarkdownView.setMovementMethod(LinkMovementMethod.getInstance());
commentMarkdownView.setHighlightColor(Color.TRANSPARENT); commentMarkdownView.setHighlightColor(Color.TRANSPARENT);
} }

View File

@ -1,6 +1,5 @@
package ml.docilealligator.infinityforreddit.adapters; package ml.docilealligator.infinityforreddit.adapters;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.ColorStateList; import android.content.res.ColorStateList;
@ -10,7 +9,6 @@ import android.os.Bundle;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
import android.text.Spanned; import android.text.Spanned;
import android.text.TextPaint; import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan; import android.text.style.ClickableSpan;
import android.text.style.SuperscriptSpan; import android.text.style.SuperscriptSpan;
import android.text.util.Linkify; import android.text.util.Linkify;
@ -45,21 +43,24 @@ import io.noties.markwon.MarkwonConfiguration;
import io.noties.markwon.core.MarkwonTheme; import io.noties.markwon.core.MarkwonTheme;
import io.noties.markwon.ext.strikethrough.StrikethroughPlugin; import io.noties.markwon.ext.strikethrough.StrikethroughPlugin;
import io.noties.markwon.linkify.LinkifyPlugin; import io.noties.markwon.linkify.LinkifyPlugin;
import io.noties.markwon.movement.MovementMethodPlugin;
import io.noties.markwon.simple.ext.SimpleExtPlugin; import io.noties.markwon.simple.ext.SimpleExtPlugin;
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.LinkResolverActivity; import ml.docilealligator.infinityforreddit.activities.LinkResolverActivity;
import ml.docilealligator.infinityforreddit.activities.ViewPostDetailActivity; import ml.docilealligator.infinityforreddit.activities.ViewPostDetailActivity;
import ml.docilealligator.infinityforreddit.activities.ViewSubredditDetailActivity; import ml.docilealligator.infinityforreddit.activities.ViewSubredditDetailActivity;
import ml.docilealligator.infinityforreddit.activities.ViewUserDetailActivity; import ml.docilealligator.infinityforreddit.activities.ViewUserDetailActivity;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.CommentMoreBottomSheetFragment; import ml.docilealligator.infinityforreddit.bottomsheetfragments.CommentMoreBottomSheetFragment;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.UrlMenuBottomSheetFragment;
import ml.docilealligator.infinityforreddit.comment.Comment; import ml.docilealligator.infinityforreddit.comment.Comment;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.NetworkState;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.SaveThing;
import ml.docilealligator.infinityforreddit.utils.APIUtils; import ml.docilealligator.infinityforreddit.utils.APIUtils;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
import ml.docilealligator.infinityforreddit.utils.Utils; import ml.docilealligator.infinityforreddit.utils.Utils;
import ml.docilealligator.infinityforreddit.VoteThing;
import retrofit2.Retrofit; import retrofit2.Retrofit;
public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment, RecyclerView.ViewHolder> { public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment, RecyclerView.ViewHolder> {
@ -77,7 +78,7 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
return comment.getCommentMarkdown().equals(t1.getCommentMarkdown()); return comment.getCommentMarkdown().equals(t1.getCommentMarkdown());
} }
}; };
private Context mContext; private AppCompatActivity mActivity;
private Retrofit mOauthRetrofit; private Retrofit mOauthRetrofit;
private Locale mLocale; private Locale mLocale;
private Markwon mMarkwon; private Markwon mMarkwon;
@ -104,16 +105,16 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
private NetworkState networkState; private NetworkState networkState;
private RetryLoadingMoreCallback mRetryLoadingMoreCallback; private RetryLoadingMoreCallback mRetryLoadingMoreCallback;
public CommentsListingRecyclerViewAdapter(Context context, Retrofit oauthRetrofit, public CommentsListingRecyclerViewAdapter(AppCompatActivity activity, Retrofit oauthRetrofit,
CustomThemeWrapper customThemeWrapper, Locale locale, CustomThemeWrapper customThemeWrapper, Locale locale,
SharedPreferences sharedPreferences, String accessToken, SharedPreferences sharedPreferences, String accessToken,
String accountName, RetryLoadingMoreCallback retryLoadingMoreCallback) { String accountName, RetryLoadingMoreCallback retryLoadingMoreCallback) {
super(DIFF_CALLBACK); super(DIFF_CALLBACK);
mContext = context; mActivity = activity;
mOauthRetrofit = oauthRetrofit; mOauthRetrofit = oauthRetrofit;
mCommentColor = customThemeWrapper.getCommentColor(); mCommentColor = customThemeWrapper.getCommentColor();
int commentSpoilerBackgroundColor = mCommentColor | 0xFF000000; int commentSpoilerBackgroundColor = mCommentColor | 0xFF000000;
mMarkwon = Markwon.builder(mContext) mMarkwon = Markwon.builder(mActivity)
.usePlugin(new AbstractMarkwonPlugin() { .usePlugin(new AbstractMarkwonPlugin() {
@NonNull @NonNull
@Override @Override
@ -182,14 +183,24 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
@Override @Override
public void configureConfiguration(@NonNull MarkwonConfiguration.Builder builder) { public void configureConfiguration(@NonNull MarkwonConfiguration.Builder builder) {
builder.linkResolver((view, link) -> { builder.linkResolver((view, link) -> {
Intent intent = new Intent(mContext, LinkResolverActivity.class); Intent intent = new Intent(mActivity, LinkResolverActivity.class);
Uri uri = Uri.parse(link); Uri uri = Uri.parse(link);
intent.setData(uri); intent.setData(uri);
mContext.startActivity(intent); mActivity.startActivity(intent);
}); });
} }
}) })
.usePlugin(MovementMethodPlugin.create(BetterLinkMovementMethod.linkify(Linkify.WEB_URLS, activity).setOnLinkLongClickListener((textView, url) -> {
if (activity != null && !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(LinkifyPlugin.create(Linkify.WEB_URLS))
.usePlugin(StrikethroughPlugin.create()) .usePlugin(StrikethroughPlugin.create())
.usePlugin(SimpleExtPlugin.create(plugin -> .usePlugin(SimpleExtPlugin.create(plugin ->
@ -260,7 +271,7 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
if (mShowElapsedTime) { if (mShowElapsedTime) {
((CommentViewHolder) holder).commentTimeTextView.setText( ((CommentViewHolder) holder).commentTimeTextView.setText(
Utils.getElapsedTime(mContext, comment.getCommentTimeMillis())); Utils.getElapsedTime(mActivity, comment.getCommentTimeMillis()));
} else { } else {
((CommentViewHolder) holder).commentTimeTextView.setText(Utils.getFormattedTime(mLocale, comment.getCommentTimeMillis(), mTimeFormatPattern)); ((CommentViewHolder) holder).commentTimeTextView.setText(Utils.getFormattedTime(mLocale, comment.getCommentTimeMillis(), mTimeFormatPattern));
} }
@ -473,8 +484,6 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
replyButton.setColorFilter(mCommentIconAndInfoColor, android.graphics.PorterDuff.Mode.SRC_IN); replyButton.setColorFilter(mCommentIconAndInfoColor, android.graphics.PorterDuff.Mode.SRC_IN);
commentDivider.setBackgroundColor(mDividerColor); commentDivider.setBackgroundColor(mDividerColor);
commentMarkdownView.setMovementMethod(LinkMovementMethod.getInstance());
authorTextView.setOnClickListener(view -> { authorTextView.setOnClickListener(view -> {
int position = getAdapterPosition(); int position = getAdapterPosition();
if (position < 0) { if (position < 0) {
@ -483,13 +492,13 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
Comment comment = getItem(getAdapterPosition()); Comment comment = getItem(getAdapterPosition());
if (comment != null) { if (comment != null) {
if (comment.getSubredditName().substring(2).equals(comment.getLinkAuthor())) { if (comment.getSubredditName().substring(2).equals(comment.getLinkAuthor())) {
Intent intent = new Intent(mContext, ViewUserDetailActivity.class); Intent intent = new Intent(mActivity, ViewUserDetailActivity.class);
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, comment.getLinkAuthor()); intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, comment.getLinkAuthor());
mContext.startActivity(intent); mActivity.startActivity(intent);
} else { } else {
Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class); Intent intent = new Intent(mActivity, ViewSubredditDetailActivity.class);
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, comment.getSubredditName()); intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, comment.getSubredditName());
mContext.startActivity(intent); mActivity.startActivity(intent);
} }
} }
}); });
@ -511,7 +520,7 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
bundle.putString(CommentMoreBottomSheetFragment.EXTRA_COMMENT_MARKDOWN, comment.getCommentMarkdown()); bundle.putString(CommentMoreBottomSheetFragment.EXTRA_COMMENT_MARKDOWN, comment.getCommentMarkdown());
CommentMoreBottomSheetFragment commentMoreBottomSheetFragment = new CommentMoreBottomSheetFragment(); CommentMoreBottomSheetFragment commentMoreBottomSheetFragment = new CommentMoreBottomSheetFragment();
commentMoreBottomSheetFragment.setArguments(bundle); commentMoreBottomSheetFragment.setArguments(bundle);
commentMoreBottomSheetFragment.show(((AppCompatActivity) mContext).getSupportFragmentManager(), commentMoreBottomSheetFragment.getTag()); commentMoreBottomSheetFragment.show(((AppCompatActivity) mActivity).getSupportFragmentManager(), commentMoreBottomSheetFragment.getTag());
} }
}); });
@ -522,10 +531,10 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
} }
Comment comment = getItem(getAdapterPosition()); Comment comment = getItem(getAdapterPosition());
if (comment != null) { if (comment != null) {
Intent intent = new Intent(mContext, ViewPostDetailActivity.class); Intent intent = new Intent(mActivity, ViewPostDetailActivity.class);
intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, comment.getLinkId()); intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, comment.getLinkId());
intent.putExtra(ViewPostDetailActivity.EXTRA_SINGLE_COMMENT_ID, comment.getId()); intent.putExtra(ViewPostDetailActivity.EXTRA_SINGLE_COMMENT_ID, comment.getId());
mContext.startActivity(intent); mActivity.startActivity(intent);
} }
}); });
@ -537,7 +546,7 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
upvoteButton.setOnClickListener(view -> { upvoteButton.setOnClickListener(view -> {
if (mAccessToken == null) { if (mAccessToken == null) {
Toast.makeText(mContext, R.string.login_first, Toast.LENGTH_SHORT).show(); Toast.makeText(mActivity, R.string.login_first, Toast.LENGTH_SHORT).show();
return; return;
} }
@ -570,7 +579,7 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
scoreTextView.setText(Utils.getNVotes(mShowAbsoluteNumberOfVotes, scoreTextView.setText(Utils.getNVotes(mShowAbsoluteNumberOfVotes,
comment.getScore() + comment.getVoteType())); comment.getScore() + comment.getVoteType()));
VoteThing.voteThing(mContext, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() { VoteThing.voteThing(mActivity, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() {
@Override @Override
public void onVoteThingSuccess(int position1) { public void onVoteThingSuccess(int position1) {
int currentPosition = getAdapterPosition(); int currentPosition = getAdapterPosition();
@ -604,7 +613,7 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
downvoteButton.setOnClickListener(view -> { downvoteButton.setOnClickListener(view -> {
if (mAccessToken == null) { if (mAccessToken == null) {
Toast.makeText(mContext, R.string.login_first, Toast.LENGTH_SHORT).show(); Toast.makeText(mActivity, R.string.login_first, Toast.LENGTH_SHORT).show();
return; return;
} }
@ -636,7 +645,7 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
scoreTextView.setText(Utils.getNVotes(mShowAbsoluteNumberOfVotes, scoreTextView.setText(Utils.getNVotes(mShowAbsoluteNumberOfVotes,
comment.getScore() + comment.getVoteType())); comment.getScore() + comment.getVoteType()));
VoteThing.voteThing(mContext, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() { VoteThing.voteThing(mActivity, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() {
@Override @Override
public void onVoteThingSuccess(int position1) { public void onVoteThingSuccess(int position1) {
int currentPosition = getAdapterPosition(); int currentPosition = getAdapterPosition();
@ -684,7 +693,7 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
if (getAdapterPosition() == position) { if (getAdapterPosition() == position) {
saveButton.setImageResource(R.drawable.ic_bookmark_border_grey_24dp); saveButton.setImageResource(R.drawable.ic_bookmark_border_grey_24dp);
} }
Toast.makeText(mContext, R.string.comment_unsaved_success, Toast.LENGTH_SHORT).show(); Toast.makeText(mActivity, R.string.comment_unsaved_success, Toast.LENGTH_SHORT).show();
} }
@Override @Override
@ -693,7 +702,7 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
if (getAdapterPosition() == position) { if (getAdapterPosition() == position) {
saveButton.setImageResource(R.drawable.ic_bookmark_grey_24dp); saveButton.setImageResource(R.drawable.ic_bookmark_grey_24dp);
} }
Toast.makeText(mContext, R.string.comment_unsaved_failed, Toast.LENGTH_SHORT).show(); Toast.makeText(mActivity, R.string.comment_unsaved_failed, Toast.LENGTH_SHORT).show();
} }
}); });
} else { } else {
@ -705,7 +714,7 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
if (getAdapterPosition() == position) { if (getAdapterPosition() == position) {
saveButton.setImageResource(R.drawable.ic_bookmark_grey_24dp); saveButton.setImageResource(R.drawable.ic_bookmark_grey_24dp);
} }
Toast.makeText(mContext, R.string.comment_saved_success, Toast.LENGTH_SHORT).show(); Toast.makeText(mActivity, R.string.comment_saved_success, Toast.LENGTH_SHORT).show();
} }
@Override @Override
@ -714,7 +723,7 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
if (getAdapterPosition() == position) { if (getAdapterPosition() == position) {
saveButton.setImageResource(R.drawable.ic_bookmark_border_grey_24dp); saveButton.setImageResource(R.drawable.ic_bookmark_border_grey_24dp);
} }
Toast.makeText(mContext, R.string.comment_saved_failed, Toast.LENGTH_SHORT).show(); Toast.makeText(mActivity, R.string.comment_saved_failed, Toast.LENGTH_SHORT).show();
} }
}); });
} }

View File

@ -1,7 +1,6 @@
package ml.docilealligator.infinityforreddit.fragments; package ml.docilealligator.infinityforreddit.fragments;
import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Resources; import android.content.res.Resources;
@ -19,6 +18,7 @@ import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.content.res.ResourcesCompat; import androidx.core.content.res.ResourcesCompat;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
@ -101,7 +101,7 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni
private boolean mNullAccessToken = false; private boolean mNullAccessToken = false;
private String mAccessToken; private String mAccessToken;
private RequestManager mGlide; private RequestManager mGlide;
private Activity mActivity; private AppCompatActivity mActivity;
private LinearLayoutManager mLinearLayoutManager; private LinearLayoutManager mLinearLayoutManager;
private CommentsListingRecyclerViewAdapter mAdapter; private CommentsListingRecyclerViewAdapter mAdapter;
private SortType sortType; private SortType sortType;
@ -373,7 +373,7 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni
@Override @Override
public void onAttach(@NonNull Context context) { public void onAttach(@NonNull Context context) {
super.onAttach(context); super.onAttach(context);
this.mActivity = (Activity) context; this.mActivity = (AppCompatActivity) context;
} }
@Override @Override