From 5e144c6de94d4fe2233b83b3c804d101ebcbe703 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 26 Aug 2019 13:52:19 +0800 Subject: Reimplement Markdown display due to update of Markwon library. Fixed RetryLoadingMoreCallback is null in MessageRecyclerViewAdapter. --- .../main/java/CustomView/CustomMarkwonView.java | 83 +++++++++++++++------- .../infinityforreddit/CommentActivity.java | 30 +++++++- .../CommentAndPostRecyclerViewAdapter.java | 43 ++++++++--- .../CommentsListingRecyclerViewAdapter.java | 38 ++++++++-- .../infinityforreddit/FetchMessages.java | 2 +- .../infinityforreddit/JSONUtils.java | 2 +- .../infinityforreddit/LinkResolverActivity.java | 11 ++- .../MessageRecyclerViewAdapter.java | 37 ++++++++-- .../infinityforreddit/ParseComment.java | 2 +- .../infinityforreddit/ParsePost.java | 8 +-- .../infinityforreddit/RulesActivity.java | 10 +-- .../RulesRecyclerViewAdapter.java | 35 +++++++-- .../docilealligator/infinityforreddit/Utils.java | 8 +++ .../infinityforreddit/ViewMessageActivity.java | 3 +- .../infinityforreddit/ViewPostDetailActivity.java | 7 +- app/src/main/res/layout/activity_comment.xml | 2 +- app/src/main/res/layout/item_comment.xml | 2 +- app/src/main/res/layout/item_message.xml | 2 +- app/src/main/res/layout/item_post_detail.xml | 2 +- app/src/main/res/layout/item_rule.xml | 2 +- 20 files changed, 257 insertions(+), 72 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Utils.java (limited to 'app/src/main') diff --git a/app/src/main/java/CustomView/CustomMarkwonView.java b/app/src/main/java/CustomView/CustomMarkwonView.java index 940b6b76..c89ff405 100644 --- a/app/src/main/java/CustomView/CustomMarkwonView.java +++ b/app/src/main/java/CustomView/CustomMarkwonView.java @@ -1,38 +1,73 @@ package CustomView; import android.content.Context; -import android.content.Intent; -import android.net.Uri; -import android.util.AttributeSet; +import android.text.Spanned; +import android.widget.TextView; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import ml.docilealligator.infinityforreddit.LinkResolverActivity; -import ru.noties.markwon.SpannableConfiguration; -import ru.noties.markwon.view.MarkwonView; +import org.commonmark.node.Node; -public class CustomMarkwonView extends MarkwonView { +import java.util.List; + +import io.noties.markwon.Markwon; +import io.noties.markwon.MarkwonPlugin; + +public class CustomMarkwonView extends Markwon { + + public void setMarkdown(@Nullable String markdown, Context context) { - public CustomMarkwonView(Context context) { - super(context); } - public CustomMarkwonView(Context context, AttributeSet attrs) { - super(context, attrs); + @NonNull + @Override + public Node parse(@NonNull String input) { + return null; } - public void setMarkdown(@Nullable String markdown, Context context) { - SpannableConfiguration configuration = SpannableConfiguration.builder(context).linkResolver((view, link) -> { - Intent intent = new Intent(context, LinkResolverActivity.class); - Uri uri = Uri.parse(link); - if(uri.getScheme() == null && uri.getHost() == null) { - intent.setData(LinkResolverActivity.getRedditUriByPath(link)); - } else { - intent.setData(uri); - } - context.startActivity(intent); - }).build(); - - super.setMarkdown(configuration, markdown); + @NonNull + @Override + public Spanned render(@NonNull Node node) { + return null; + } + + @NonNull + @Override + public Spanned toMarkdown(@NonNull String input) { + return null; + } + + @Override + public void setMarkdown(@NonNull TextView textView, @NonNull String markdown) { + + } + + @Override + public void setParsedMarkdown(@NonNull TextView textView, @NonNull Spanned markdown) { + + } + + @Override + public boolean hasPlugin(@NonNull Class plugin) { + return false; + } + + @Nullable + @Override + public

P getPlugin(@NonNull Class

type) { + return null; + } + + @NonNull + @Override + public

P requirePlugin(@NonNull Class

type) { + return null; + } + + @NonNull + @Override + public List getPlugins() { + return null; } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java index 5b5cd0ab..17e4061c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java @@ -4,14 +4,17 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; +import android.net.Uri; import android.os.Build; import android.os.Bundle; +import android.text.util.Linkify; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.Window; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; +import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -31,8 +34,11 @@ import javax.inject.Named; import butterknife.BindView; import butterknife.ButterKnife; +import io.noties.markwon.AbstractMarkwonPlugin; +import io.noties.markwon.Markwon; +import io.noties.markwon.MarkwonConfiguration; +import io.noties.markwon.linkify.LinkifyPlugin; import retrofit2.Retrofit; -import ru.noties.markwon.view.MarkwonView; import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY; import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM; @@ -54,7 +60,7 @@ public class CommentActivity extends AppCompatActivity { @BindView(R.id.coordinator_layout_comment_activity) CoordinatorLayout coordinatorLayout; @BindView(R.id.toolbar_comment_activity) Toolbar toolbar; - @BindView(R.id.comment_parent_markwon_view_comment_activity) MarkwonView commentParentMarkwonView; + @BindView(R.id.comment_parent_markwon_view_comment_activity) TextView commentParentMarkwonView; @BindView(R.id.comment_edit_text_comment_activity) EditText commentEditText; private boolean mNullAccessToken = false; @@ -122,7 +128,25 @@ public class CommentActivity extends AppCompatActivity { } Intent intent = getIntent(); - commentParentMarkwonView.setMarkdown(intent.getExtras().getString(EXTRA_COMMENT_PARENT_TEXT_KEY)); + Markwon markwon = Markwon.builder(this) + .usePlugin(new AbstractMarkwonPlugin() { + @Override + public void configureConfiguration(@NonNull MarkwonConfiguration.Builder builder) { + builder.linkResolver((view, link) -> { + Intent intent = new Intent(CommentActivity.this, LinkResolverActivity.class); + Uri uri = Uri.parse(link); + if(uri.getScheme() == null && uri.getHost() == null) { + intent.setData(LinkResolverActivity.getRedditUriByPath(link)); + } else { + intent.setData(uri); + } + startActivity(intent); + }); + } + }) + .usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS)) + .build(); + markwon.setMarkdown(commentParentMarkwonView, intent.getExtras().getString(EXTRA_COMMENT_PARENT_TEXT_KEY)); parentFullname = intent.getExtras().getString(EXTRA_PARENT_FULLNAME_KEY); parentDepth = intent.getExtras().getInt(EXTRA_PARENT_DEPTH_KEY); parentPosition = intent.getExtras().getInt(EXTRA_PARENT_POSITION_KEY); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java index 996ba720..0f29b823 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java @@ -7,6 +7,7 @@ import android.graphics.PorterDuff; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; +import android.text.util.Linkify; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -40,9 +41,14 @@ import java.util.ArrayList; import java.util.Locale; import CustomView.AspectRatioGifImageView; -import CustomView.CustomMarkwonView; import butterknife.BindView; import butterknife.ButterKnife; +import io.noties.markwon.AbstractMarkwonPlugin; +import io.noties.markwon.Markwon; +import io.noties.markwon.MarkwonConfiguration; +import io.noties.markwon.ext.strikethrough.StrikethroughPlugin; +import io.noties.markwon.linkify.LinkifyPlugin; +import io.noties.markwon.urlprocessor.UrlProcessorRelativeToAbsolute; import jp.wasabeef.glide.transformations.BlurTransformation; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import retrofit2.Retrofit; @@ -63,6 +69,7 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { + Intent intent = new Intent(mActivity, LinkResolverActivity.class); + Uri uri = Uri.parse(link); + if(uri.getScheme() == null && uri.getHost() == null) { + intent.setData(LinkResolverActivity.getRedditUriByPath(link)); + } else { + intent.setData(uri); + } + mActivity.startActivity(intent); + }).urlProcessor(new UrlProcessorRelativeToAbsolute("https://www.reddit.com")); + } + }) + .usePlugin(StrikethroughPlugin.create()) + .usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS)) + .build(); mAccessToken = accessToken; mAccountName = accountName; mPost = post; @@ -422,8 +448,9 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); @@ -447,7 +474,7 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { private Context mContext; private Retrofit mOauthRetrofit; + private Markwon mMarkwon; private String mAccessToken; private String mAccountName; private int mTextColorPrimaryDark; @@ -43,11 +50,31 @@ class CommentsListingRecyclerViewAdapter extends PagedListAdapter { + Intent intent = new Intent(mContext, LinkResolverActivity.class); + Uri uri = Uri.parse(link); + if(uri.getScheme() == null && uri.getHost() == null) { + intent.setData(LinkResolverActivity.getRedditUriByPath(link)); + } else { + intent.setData(uri); + } + mContext.startActivity(intent); + }); + } + }) + .usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS)) + .usePlugin(StrikethroughPlugin.create()) + .build(); mAccessToken = accessToken; mAccountName = accountName; mRetryLoadingMoreCallback = retryLoadingMoreCallback; @@ -104,7 +131,8 @@ class CommentsListingRecyclerViewAdapter extends PagedListAdapter { @@ -27,20 +32,42 @@ class MessageRecyclerViewAdapter extends PagedListAdapter { + Intent intent = new Intent(mContext, LinkResolverActivity.class); + Uri uri = Uri.parse(link); + if(uri.getScheme() == null && uri.getHost() == null) { + intent.setData(LinkResolverActivity.getRedditUriByPath(link)); + } else { + intent.setData(uri); + } + mContext.startActivity(intent); + }); + } + }) + .usePlugin(StrikethroughPlugin.create()) + .usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS)) + .build(); mAccessToken = accessToken; mResources = context.getResources(); } @@ -89,7 +116,7 @@ class MessageRecyclerViewAdapter extends PagedListAdapter { if(message.getContext() != null && !message.getContext().equals("")) { @@ -185,7 +212,7 @@ class MessageRecyclerViewAdapter extends PagedListAdapter rules = new ArrayList<>(); for(int i = 0; i < rulesArray.length(); i++) { String shortName = rulesArray.getJSONObject(i).getString(JSONUtils.SHORT_NAME_KEY); - String descriptionHtml = null; - if(rulesArray.getJSONObject(i).has(JSONUtils.DESCRIPTION_HTML_KEY)) { - descriptionHtml = rulesArray.getJSONObject(i).getString(JSONUtils.DESCRIPTION_HTML_KEY); + String description = null; + if(rulesArray.getJSONObject(i).has(JSONUtils.DESCRIPTION_KEY)) { + Log.i("asdfasdf", "" + rulesArray.getJSONObject(i).getString(JSONUtils.DESCRIPTION_KEY)); + description = Utils.addSubredditAndUserLink(rulesArray.getJSONObject(i).getString(JSONUtils.DESCRIPTION_KEY)); } - rules.add(new Rule(shortName, descriptionHtml)); + rules.add(new Rule(shortName, description)); } return rules; } catch (JSONException e) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RulesRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RulesRecyclerViewAdapter.java index 941b0316..4ab0f201 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RulesRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RulesRecyclerViewAdapter.java @@ -1,6 +1,9 @@ package ml.docilealligator.infinityforreddit; import android.content.Context; +import android.content.Intent; +import android.net.Uri; +import android.text.util.Linkify; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -11,16 +14,38 @@ import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; -import CustomView.CustomMarkwonView; import butterknife.BindView; import butterknife.ButterKnife; +import io.noties.markwon.AbstractMarkwonPlugin; +import io.noties.markwon.Markwon; +import io.noties.markwon.MarkwonConfiguration; +import io.noties.markwon.ext.strikethrough.StrikethroughPlugin; +import io.noties.markwon.linkify.LinkifyPlugin; class RulesRecyclerViewAdapter extends RecyclerView.Adapter { - private Context context; + private Markwon markwon; private ArrayList rules; RulesRecyclerViewAdapter(Context context) { - this.context = context; + markwon = Markwon.builder(context) + .usePlugin(new AbstractMarkwonPlugin() { + @Override + public void configureConfiguration(@NonNull MarkwonConfiguration.Builder builder) { + builder.linkResolver((view, link) -> { + Intent intent = new Intent(context, LinkResolverActivity.class); + Uri uri = Uri.parse(link); + if(uri.getScheme() == null && uri.getHost() == null) { + intent.setData(LinkResolverActivity.getRedditUriByPath(link)); + } else { + intent.setData(uri); + } + context.startActivity(intent); + }); + } + }) + .usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS)) + .usePlugin(StrikethroughPlugin.create()) + .build(); } @NonNull @@ -35,7 +60,7 @@ class RulesRecyclerViewAdapter extends RecyclerView.Adapter mMessageViewModel.retryLoadingMore()); LinearLayoutManager layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); recyclerView.setAdapter(mAdapter); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index 9659d620..a170275c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -433,9 +433,10 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo mMenu.findItem(R.id.action_delete_view_post_detail_activity).setVisible(true); } - mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit, - mOauthRetrofit, mRedditDataRoomDatabase, mGlide, mAccessToken, mAccountName, mPost, - mLocale, mSingleCommentId, isSingleCommentThreadMode, + mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, + mRetrofit, mOauthRetrofit, mRedditDataRoomDatabase, mGlide, + mAccessToken, mAccountName, mPost, mLocale, mSingleCommentId, + isSingleCommentThreadMode, new CommentAndPostRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() { @Override public void updatePost(Post post) { diff --git a/app/src/main/res/layout/activity_comment.xml b/app/src/main/res/layout/activity_comment.xml index 76a053bf..494872ce 100644 --- a/app/src/main/res/layout/activity_comment.xml +++ b/app/src/main/res/layout/activity_comment.xml @@ -32,7 +32,7 @@ android:orientation="vertical" android:padding="16dp"> - - - diff --git a/app/src/main/res/layout/item_post_detail.xml b/app/src/main/res/layout/item_post_detail.xml index a1fc7776..886f169f 100644 --- a/app/src/main/res/layout/item_post_detail.xml +++ b/app/src/main/res/layout/item_post_detail.xml @@ -71,7 +71,7 @@ android:textColor="@color/primaryTextColor" android:textSize="18sp" /> - -