From 611d6a90ec39707b4d4161da1eb5b19249e670c4 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 18 Jul 2019 12:00:46 +0800 Subject: Viewing rules after pressing rules button in PostXXXActivity is available. Extend MarkwonView to support opening reddit subreddit and user links. --- .../main/java/CustomView/CustomMarkwonView.java | 49 ++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 app/src/main/java/CustomView/CustomMarkwonView.java (limited to 'app/src/main/java/CustomView/CustomMarkwonView.java') diff --git a/app/src/main/java/CustomView/CustomMarkwonView.java b/app/src/main/java/CustomView/CustomMarkwonView.java new file mode 100644 index 00000000..c0d59f05 --- /dev/null +++ b/app/src/main/java/CustomView/CustomMarkwonView.java @@ -0,0 +1,49 @@ +package CustomView; + +import android.content.Context; +import android.content.Intent; +import android.net.Uri; +import android.util.AttributeSet; + +import androidx.annotation.Nullable; +import androidx.browser.customtabs.CustomTabsIntent; + +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.ViewSubredditDetailActivity; +import ml.docilealligator.infinityforreddit.ViewUserDetailActivity; +import ru.noties.markwon.SpannableConfiguration; +import ru.noties.markwon.view.MarkwonView; + +public class CustomMarkwonView extends MarkwonView { + + public CustomMarkwonView(Context context) { + super(context); + } + + public CustomMarkwonView(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public void setMarkdown(@Nullable String markdown, Context conte) { + SpannableConfiguration configuration = SpannableConfiguration.builder(conte).linkResolver((view, link) -> { + if(link.startsWith("/u/") || link.startsWith("u/")) { + Intent intent = new Intent(conte, ViewUserDetailActivity.class); + intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, link.substring(3)); + conte.startActivity(intent); + } else if(link.startsWith("/r/") || link.startsWith("r/")) { + Intent intent = new Intent(conte, ViewSubredditDetailActivity.class); + intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, link.substring(3)); + conte.startActivity(intent); + } else { + CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); + // add share action to menu list + builder.addDefaultShareMenuItem(); + builder.setToolbarColor(conte.getResources().getColor(R.color.colorPrimary)); + CustomTabsIntent customTabsIntent = builder.build(); + customTabsIntent.launchUrl(conte, Uri.parse(link)); + } + }).build(); + + super.setMarkdown(configuration, markdown); + } +} -- cgit v1.2.3