Long press a url to see options in RulesRecyclerViewAdapter.

This commit is contained in:
Alex Ning 2021-01-04 14:55:42 +08:00
parent d1aa3df4a7
commit 9830db0303

View File

@ -1,8 +1,8 @@
package ml.docilealligator.infinityforreddit.adapters; package ml.docilealligator.infinityforreddit.adapters;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle;
import android.text.style.SuperscriptSpan; import android.text.style.SuperscriptSpan;
import android.text.util.Linkify; import android.text.util.Linkify;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -11,6 +11,7 @@ import android.view.ViewGroup;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList; import java.util.ArrayList;
@ -23,11 +24,14 @@ 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 ml.docilealligator.infinityforreddit.activities.LinkResolverActivity; import me.saket.bettermovementmethod.BetterLinkMovementMethod;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.Rule; import ml.docilealligator.infinityforreddit.Rule;
import ml.docilealligator.infinityforreddit.activities.LinkResolverActivity;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.UrlMenuBottomSheetFragment;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
public class RulesRecyclerViewAdapter extends RecyclerView.Adapter<RulesRecyclerViewAdapter.RuleViewHolder> { public class RulesRecyclerViewAdapter extends RecyclerView.Adapter<RulesRecyclerViewAdapter.RuleViewHolder> {
private Markwon markwon; private Markwon markwon;
@ -35,16 +39,16 @@ public class RulesRecyclerViewAdapter extends RecyclerView.Adapter<RulesRecycler
private int mPrimaryTextColor; private int mPrimaryTextColor;
private int mSecondaryTextColor; private int mSecondaryTextColor;
public RulesRecyclerViewAdapter(Context context, CustomThemeWrapper customThemeWrapper) { public RulesRecyclerViewAdapter(AppCompatActivity activity, CustomThemeWrapper customThemeWrapper) {
markwon = Markwon.builder(context) markwon = Markwon.builder(activity)
.usePlugin(new AbstractMarkwonPlugin() { .usePlugin(new AbstractMarkwonPlugin() {
@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(context, LinkResolverActivity.class); Intent intent = new Intent(activity, LinkResolverActivity.class);
Uri uri = Uri.parse(link); Uri uri = Uri.parse(link);
intent.setData(uri); intent.setData(uri);
context.startActivity(intent); activity.startActivity(intent);
}); });
} }
@ -53,6 +57,16 @@ public class RulesRecyclerViewAdapter extends RecyclerView.Adapter<RulesRecycler
builder.linkColor(customThemeWrapper.getLinkColor()); builder.linkColor(customThemeWrapper.getLinkColor());
} }
}) })
.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 ->