From 4c6902a399bc63d097f844f4717b82a3dd60464d Mon Sep 17 00:00:00 2001
From: Alex Ning <chineseperson5@gmail.com>
Date: Sat, 22 Feb 2020 18:22:02 +0800
Subject: Show post or body (if it has) in CommentActivity.

---
 .../Activity/CommentActivity.java                  | 64 +++++++++++++++++++++-
 .../Activity/ViewPostDetailActivity.java           |  7 ++-
 .../Adapter/CommentAndPostRecyclerViewAdapter.java | 12 +++-
 app/src/main/res/layout/activity_comment.xml       | 24 +++++++-
 app/src/main/res/values/strings.xml                |  2 +-
 5 files changed, 99 insertions(+), 10 deletions(-)

(limited to 'app/src/main')

diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CommentActivity.java
index 452c92f2..5faaa174 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CommentActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CommentActivity.java
@@ -5,10 +5,12 @@ import android.content.Intent;
 import android.content.SharedPreferences;
 import android.net.Uri;
 import android.os.Bundle;
+import android.text.Spanned;
 import android.text.style.SuperscriptSpan;
 import android.text.util.Linkify;
 import android.view.Menu;
 import android.view.MenuItem;
+import android.view.View;
 import android.view.inputmethod.InputMethodManager;
 import android.widget.EditText;
 import android.widget.TextView;
@@ -18,10 +20,14 @@ import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
 import androidx.appcompat.widget.Toolbar;
 import androidx.coordinatorlayout.widget.CoordinatorLayout;
+import androidx.core.content.ContextCompat;
+import androidx.recyclerview.widget.LinearLayoutManager;
+import androidx.recyclerview.widget.RecyclerView;
 
 import com.google.android.material.dialog.MaterialAlertDialogBuilder;
 import com.google.android.material.snackbar.Snackbar;
 
+import org.commonmark.ext.gfm.tables.TableBlock;
 import org.greenrobot.eventbus.EventBus;
 import org.greenrobot.eventbus.Subscribe;
 
@@ -33,8 +39,13 @@ 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.recycler.MarkwonAdapter;
+import io.noties.markwon.recycler.table.TableEntry;
+import io.noties.markwon.recycler.table.TableEntryPlugin;
 import io.noties.markwon.simple.ext.SimpleExtPlugin;
+import io.noties.markwon.urlprocessor.UrlProcessorRelativeToAbsolute;
 import ml.docilealligator.infinityforreddit.AsyncTask.GetCurrentAccountAsyncTask;
 import ml.docilealligator.infinityforreddit.CommentData;
 import ml.docilealligator.infinityforreddit.Event.SwitchAccountEvent;
@@ -47,11 +58,12 @@ import retrofit2.Retrofit;
 public class CommentActivity extends BaseActivity {
 
     public static final String EXTRA_COMMENT_PARENT_TEXT_KEY = "ECPTK";
+    public static final String EXTRA_COMMENT_PARENT_BODY_KEY = "ECPBK";
     public static final String EXTRA_PARENT_FULLNAME_KEY = "EPFK";
-    public static final String EXTRA_COMMENT_DATA_KEY = "ECDK";
     public static final String EXTRA_PARENT_DEPTH_KEY = "EPDK";
     public static final String EXTRA_PARENT_POSITION_KEY = "EPPK";
     public static final String EXTRA_IS_REPLYING_KEY = "EIRK";
+    public static final String RETURN_EXTRA_COMMENT_DATA_KEY = "RECDK";
     public static final int WRITE_COMMENT_REQUEST_CODE = 1;
 
     private static final String NULL_ACCESS_TOKEN_STATE = "NATS";
@@ -63,6 +75,8 @@ public class CommentActivity extends BaseActivity {
     Toolbar toolbar;
     @BindView(R.id.comment_parent_markwon_view_comment_activity)
     TextView commentParentMarkwonView;
+    @BindView(R.id.content_markdown_view_comment_activity)
+    RecyclerView contentMarkdownRecyclerView;
     @BindView(R.id.comment_edit_text_comment_activity)
     EditText commentEditText;
     @Inject
@@ -128,6 +142,52 @@ public class CommentActivity extends BaseActivity {
                 )
                 .build();
         markwon.setMarkdown(commentParentMarkwonView, intent.getStringExtra(EXTRA_COMMENT_PARENT_TEXT_KEY));
+        String parentBody = intent.getStringExtra(EXTRA_COMMENT_PARENT_BODY_KEY);
+        if (parentBody != null && !parentBody.equals("")) {
+            contentMarkdownRecyclerView.setVisibility(View.VISIBLE);
+            contentMarkdownRecyclerView.setNestedScrollingEnabled(false);
+            int markdownColor = ContextCompat.getColor(this, R.color.defaultTextColor);
+            Markwon postBodyMarkwon = Markwon.builder(this)
+                    .usePlugin(new AbstractMarkwonPlugin() {
+                        @Override
+                        public void beforeSetText(@NonNull TextView textView, @NonNull Spanned markdown) {
+                            textView.setTextColor(markdownColor);
+                        }
+
+                        @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);
+                            }).urlProcessor(new UrlProcessorRelativeToAbsolute("https://www.reddit.com"));
+                        }
+                    })
+                    .usePlugin(StrikethroughPlugin.create())
+                    .usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS))
+                    .usePlugin(SimpleExtPlugin.create(plugin ->
+                                    plugin.addExtension(1, '^', (configuration, props) -> {
+                                        return new SuperscriptSpan();
+                                    })
+                            )
+                    )
+                    .usePlugin(TableEntryPlugin.create(this))
+                    .build();
+            MarkwonAdapter markwonAdapter = MarkwonAdapter.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();
+            contentMarkdownRecyclerView.setLayoutManager(new LinearLayoutManager(this));
+            contentMarkdownRecyclerView.setAdapter(markwonAdapter);
+            markwonAdapter.setMarkdown(postBodyMarkwon, parentBody);
+            markwonAdapter.notifyDataSetChanged();
+        }
         parentFullname = intent.getStringExtra(EXTRA_PARENT_FULLNAME_KEY);
         parentDepth = intent.getExtras().getInt(EXTRA_PARENT_DEPTH_KEY);
         parentPosition = intent.getExtras().getInt(EXTRA_PARENT_POSITION_KEY);
@@ -205,7 +265,7 @@ public class CommentActivity extends BaseActivity {
                                     item.getIcon().setAlpha(255);
                                     Toast.makeText(CommentActivity.this, R.string.send_comment_success, Toast.LENGTH_SHORT).show();
                                     Intent returnIntent = new Intent();
-                                    returnIntent.putExtra(EXTRA_COMMENT_DATA_KEY, commentData);
+                                    returnIntent.putExtra(RETURN_EXTRA_COMMENT_DATA_KEY, commentData);
                                     returnIntent.putExtra(EXTRA_PARENT_FULLNAME_KEY, parentFullname);
                                     if (isReplying) {
                                         returnIntent.putExtra(EXTRA_PARENT_POSITION_KEY, parentPosition);
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java
index d8950f2c..4d118525 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java
@@ -87,7 +87,7 @@ import retrofit2.Callback;
 import retrofit2.Response;
 import retrofit2.Retrofit;
 
-import static ml.docilealligator.infinityforreddit.Activity.CommentActivity.EXTRA_COMMENT_DATA_KEY;
+import static ml.docilealligator.infinityforreddit.Activity.CommentActivity.RETURN_EXTRA_COMMENT_DATA_KEY;
 import static ml.docilealligator.infinityforreddit.Activity.CommentActivity.WRITE_COMMENT_REQUEST_CODE;
 
 public class ViewPostDetailActivity extends BaseActivity implements FlairBottomSheetFragment.FlairSelectionCallback,
@@ -1317,6 +1317,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS
 
                     Intent intent = new Intent(this, CommentActivity.class);
                     intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_TEXT_KEY, mPost.getTitle());
+                    intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_BODY_KEY, mPost.getSelfText());
                     intent.putExtra(CommentActivity.EXTRA_PARENT_FULLNAME_KEY, mPost.getFullName());
                     intent.putExtra(CommentActivity.EXTRA_PARENT_DEPTH_KEY, 0);
                     intent.putExtra(CommentActivity.EXTRA_IS_REPLYING_KEY, false);
@@ -1485,8 +1486,8 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS
         super.onActivityResult(requestCode, resultCode, data);
         if (requestCode == WRITE_COMMENT_REQUEST_CODE) {
             if (data != null && resultCode == RESULT_OK) {
-                if (data.hasExtra(EXTRA_COMMENT_DATA_KEY)) {
-                    CommentData comment = data.getParcelableExtra(EXTRA_COMMENT_DATA_KEY);
+                if (data.hasExtra(RETURN_EXTRA_COMMENT_DATA_KEY)) {
+                    CommentData comment = data.getParcelableExtra(RETURN_EXTRA_COMMENT_DATA_KEY);
                     if (comment != null && comment.getDepth() == 0) {
                         mAdapter.addComment(comment);
                     } else {
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java
index 1ddcf537..5545e1e8 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java
@@ -218,7 +218,6 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
                         .tableLayout(R.layout.adapter_table_block, R.id.table_layout)
                         .textLayoutIsRoot(R.layout.view_table_entry_cell)))
                 .build();
-        mMarkwonAdapter.setMarkdown(mPostDetailMarkwon, "");
         mAccessToken = accessToken;
         mAccountName = accountName;
         mPost = post;
@@ -624,6 +623,16 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
             }
 
             ((PostDetailViewHolder) holder).commentsCountTextView.setOnClickListener(view -> {
+                if (mPost.isArchived()) {
+                    Toast.makeText(mActivity, R.string.archived_post_reply_unavailable, Toast.LENGTH_SHORT).show();
+                    return;
+                }
+
+                if (mPost.isLocked()) {
+                    Toast.makeText(mActivity, R.string.locked_post_comment_unavailable, Toast.LENGTH_SHORT).show();
+                    return;
+                }
+
                 if (mAccessToken == null) {
                     Toast.makeText(mActivity, R.string.login_first, Toast.LENGTH_SHORT).show();
                     return;
@@ -632,6 +641,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
                 Intent intent = new Intent(mActivity, CommentActivity.class);
                 intent.putExtra(CommentActivity.EXTRA_PARENT_FULLNAME_KEY, mPost.getFullName());
                 intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_TEXT_KEY, mPost.getTitle());
+                intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_BODY_KEY, mPost.getSelfText());
                 intent.putExtra(CommentActivity.EXTRA_IS_REPLYING_KEY, false);
                 intent.putExtra(CommentActivity.EXTRA_PARENT_DEPTH_KEY, 0);
                 mActivity.startActivityForResult(intent, WRITE_COMMENT_REQUEST_CODE);
diff --git a/app/src/main/res/layout/activity_comment.xml b/app/src/main/res/layout/activity_comment.xml
index 7ea850f6..decf27dd 100644
--- a/app/src/main/res/layout/activity_comment.xml
+++ b/app/src/main/res/layout/activity_comment.xml
@@ -31,21 +31,39 @@
         <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="match_parent"
-            android:orientation="vertical"
-            android:padding="16dp">
+            android:orientation="vertical">
 
             <TextView
                 android:id="@+id/comment_parent_markwon_view_comment_activity"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
+                android:layout_margin="16dp"
+                android:textColor="@color/primaryTextColor"
+                android:textSize="?attr/title_font_16" />
+
+            <androidx.recyclerview.widget.RecyclerView
+                android:id="@+id/content_markdown_view_comment_activity"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:layout_marginBottom="16dp"
+                android:layout_marginStart="16dp"
+                android:layout_marginEnd="16dp"
+                android:visibility="gone" />
+
+            <View
+                android:layout_width="match_parent"
+                android:layout_height="1dp"
                 android:layout_marginBottom="16dp"
-                android:textSize="?attr/content_font_16" />
+                android:background="@color/dividerColor" />
 
             <EditText
                 android:id="@+id/comment_edit_text_comment_activity"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:gravity="top"
+                android:paddingBottom="16dp"
+                android:paddingStart="16dp"
+                android:paddingEnd="16dp"
                 android:hint="@string/write_comment_hint"
                 android:inputType="textCapSentences|textMultiLine"
                 android:textSize="?attr/content_font_18"
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index d0b349ae..0f9292a8 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -124,7 +124,7 @@
     <string name="lazy_mode_start">Lazy Mode starts in %1$.1fs</string>
     <string name="lazy_mode_stop">Lazy Mode stopped</string>
 
-    <string name="write_comment_hint">Your interesting thought here</string>
+    <string name="write_comment_hint">Your interesting thoughts here</string>
     <string name="comment_content_required">Where is your interesting thought?</string>
     <string name="sending_comment">Sending</string>
     <string name="send_comment_success">Comment sent</string>
-- 
cgit v1.2.3