From 502cbe02ba9ad397d616961bd7cb85ae66aced8e Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 14 Jun 2019 18:14:49 +0800 Subject: Reply to comments. Preparing for comment deletion. --- .../infinityforreddit/CommentActivity.java | 24 +++++-- .../CommentMultiLevelRecyclerViewAdapter.java | 77 +++++++++++++++------- .../infinityforreddit/JSONUtils.java | 1 - .../infinityforreddit/ParseComment.java | 2 +- .../infinityforreddit/RedditAPI.java | 4 ++ .../infinityforreddit/ViewPostDetailActivity.java | 8 ++- 6 files changed, 86 insertions(+), 30 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java index bc2697da..9bf4ebc7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java @@ -3,10 +3,10 @@ package ml.docilealligator.infinityforreddit; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; +import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.widget.EditText; -import android.widget.TextView; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; @@ -21,6 +21,7 @@ import javax.inject.Named; import butterknife.BindView; import butterknife.ButterKnife; import retrofit2.Retrofit; +import ru.noties.markwon.view.MarkwonView; public class CommentActivity extends AppCompatActivity { @@ -28,15 +29,19 @@ public class CommentActivity extends AppCompatActivity { static final String EXTRA_PARENT_FULLNAME_KEY = "EPFK"; static final String EXTRA_COMMENT_DATA_KEY = "ECDK"; static final String EXTRA_PARENT_DEPTH_KEY = "EPDK"; + static final String EXTRA_PARENT_POSITION_KEY = "EPPK"; + static final String EXTRA_IS_REPLYING_KEY = "EIRK"; static final int WRITE_COMMENT_REQUEST_CODE = 1; @BindView(R.id.coordinator_layout_comment_activity) CoordinatorLayout coordinatorLayout; @BindView(R.id.toolbar_comment_activity) Toolbar toolbar; - @BindView(R.id.comment_parent_text_view_comment_activity) TextView commentParentTextView; + @BindView(R.id.comment_parent_markwon_view_comment_activity) MarkwonView commentParentMarkwonView; @BindView(R.id.comment_edit_text_comment_activity) EditText commentEditText; private String parentFullname; private int parentDepth; + private int parentPosition; + private boolean isReplying; @Inject @Named("oauth") @@ -55,12 +60,18 @@ public class CommentActivity extends AppCompatActivity { ((Infinity) getApplication()).getmAppComponent().inject(this); - setSupportActionBar(toolbar); - Intent intent = getIntent(); - commentParentTextView.setText(intent.getExtras().getString(EXTRA_COMMENT_PARENT_TEXT_KEY)); + commentParentMarkwonView.setMarkdown(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); + isReplying = intent.getExtras().getBoolean(EXTRA_IS_REPLYING_KEY); + if(isReplying) { + toolbar.setTitle(getString(R.string.comment_activity_label_is_replying)); + } + + Log.i("fullname", parentFullname); + setSupportActionBar(toolbar); } @Override @@ -89,6 +100,9 @@ public class CommentActivity extends AppCompatActivity { public void sendCommentSuccess(CommentData commentData) { Intent returnIntent = new Intent(); returnIntent.putExtra(EXTRA_COMMENT_DATA_KEY, commentData); + if(isReplying) { + returnIntent.putExtra(EXTRA_PARENT_POSITION_KEY, parentPosition); + } setResult(RESULT_OK, returnIntent); finish(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentMultiLevelRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentMultiLevelRecyclerViewAdapter.java index 10a89be2..1f5795fe 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentMultiLevelRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentMultiLevelRecyclerViewAdapter.java @@ -1,6 +1,6 @@ package ml.docilealligator.infinityforreddit; -import android.content.Context; +import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.ColorFilter; @@ -33,7 +33,7 @@ import ru.noties.markwon.SpannableConfiguration; import ru.noties.markwon.view.MarkwonView; class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter { - private Context mContext; + private Activity mActivity; private Retrofit mRetrofit; private Retrofit mOauthRetrofit; private SharedPreferences mSharedPreferences; @@ -43,12 +43,12 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter { private String article; private Locale locale; - CommentMultiLevelRecyclerViewAdapter(Context context, Retrofit retrofit, Retrofit oauthRetrofit, + CommentMultiLevelRecyclerViewAdapter(Activity activity, Retrofit retrofit, Retrofit oauthRetrofit, SharedPreferences sharedPreferences, ArrayList commentData, MultiLevelRecyclerView multiLevelRecyclerView, String subredditNamePrefixed, String article, Locale locale) { super(commentData); - mContext = context; + mActivity = activity; mRetrofit = retrofit; mOauthRetrofit = oauthRetrofit; mSharedPreferences = sharedPreferences; @@ -72,28 +72,28 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter { String authorPrefixed = "u/" + commentItem.getAuthor(); ((CommentViewHolder) holder).authorTextView.setText(authorPrefixed); ((CommentViewHolder) holder).authorTextView.setOnClickListener(view -> { - Intent intent = new Intent(mContext, ViewUserDetailActivity.class); + Intent intent = new Intent(mActivity, ViewUserDetailActivity.class); intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, commentItem.getAuthor()); - mContext.startActivity(intent); + mActivity.startActivity(intent); }); ((CommentViewHolder) holder).commentTimeTextView.setText(commentItem.getCommentTime()); - SpannableConfiguration spannableConfiguration = SpannableConfiguration.builder(mContext).linkResolver((view, link) -> { + SpannableConfiguration spannableConfiguration = SpannableConfiguration.builder(mActivity).linkResolver((view, link) -> { if (link.startsWith("/u/") || link.startsWith("u/")) { - Intent intent = new Intent(mContext, ViewUserDetailActivity.class); + Intent intent = new Intent(mActivity, ViewUserDetailActivity.class); intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, link.substring(3)); - mContext.startActivity(intent); + mActivity.startActivity(intent); } else if (link.startsWith("/r/") || link.startsWith("r/")) { - Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class); + Intent intent = new Intent(mActivity, ViewSubredditDetailActivity.class); intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, link.substring(3)); - mContext.startActivity(intent); + mActivity.startActivity(intent); } else { CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); // add share action to menu list builder.addDefaultShareMenuItem(); - builder.setToolbarColor(mContext.getResources().getColor(R.color.colorPrimary)); + builder.setToolbarColor(mActivity.getResources().getColor(R.color.colorPrimary)); CustomTabsIntent customTabsIntent = builder.build(); - customTabsIntent.launchUrl(mContext, Uri.parse(link)); + customTabsIntent.launchUrl(mActivity, Uri.parse(link)); } }).build(); @@ -105,6 +105,17 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter { setExpandButton(((CommentViewHolder) holder).expandButton, commentItem.isExpanded()); } + ((CommentViewHolder) holder).shareButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + Intent intent = new Intent(Intent.ACTION_SEND); + intent.setType("text/plain"); + String extraText = commentItem.getPermalink(); + intent.putExtra(Intent.EXTRA_TEXT, extraText); + mActivity.startActivity(Intent.createChooser(intent, "Share")); + } + }); + ((CommentViewHolder) holder).expandButton.setOnClickListener(view -> { if (commentItem.hasChildren() && commentItem.getChildren().size() > 0) { mMultiLevelRecyclerView.toggleItemsGroup(holder.getAdapterPosition()); @@ -132,14 +143,23 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter { } }); + ((CommentViewHolder) holder).replyButton.setOnClickListener(view -> { + Intent intent = new Intent(mActivity, CommentActivity.class); + intent.putExtra(CommentActivity.EXTRA_PARENT_DEPTH_KEY, commentItem.getDepth() + 1); + intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_TEXT_KEY, commentItem.getCommentContent()); + intent.putExtra(CommentActivity.EXTRA_PARENT_FULLNAME_KEY, commentItem.getFullName()); + intent.putExtra(CommentActivity.EXTRA_IS_REPLYING_KEY, true); + mActivity.startActivityForResult(intent, CommentActivity.WRITE_COMMENT_REQUEST_CODE); + }); + switch (commentItem.getVoteType()) { case 1: ((CommentViewHolder) holder).upvoteButton - .setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN); + .setColorFilter(ContextCompat.getColor(mActivity, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN); break; case 2: ((CommentViewHolder) holder).downvoteButton - .setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN); + .setColorFilter(ContextCompat.getColor(mActivity, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN); break; } @@ -156,7 +176,7 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter { commentItem.setVoteType(1); newVoteType = RedditUtils.DIR_UPVOTE; ((CommentViewHolder) holder).upvoteButton - .setColorFilter(ContextCompat.getColor(mContext, R.color.backgroundColorPrimaryDark), android.graphics.PorterDuff.Mode.SRC_IN); + .setColorFilter(ContextCompat.getColor(mActivity, R.color.backgroundColorPrimaryDark), android.graphics.PorterDuff.Mode.SRC_IN); } else { //Upvoted before commentItem.setVoteType(0); @@ -172,7 +192,7 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter { if(newVoteType.equals(RedditUtils.DIR_UPVOTE)) { commentItem.setVoteType(1); ((CommentViewHolder) holder).upvoteButton - .setColorFilter(ContextCompat.getColor(mContext, R.color.backgroundColorPrimaryDark), android.graphics.PorterDuff.Mode.SRC_IN); + .setColorFilter(ContextCompat.getColor(mActivity, R.color.backgroundColorPrimaryDark), android.graphics.PorterDuff.Mode.SRC_IN); } else { commentItem.setVoteType(0); ((CommentViewHolder) holder).upvoteButton.clearColorFilter(); @@ -184,7 +204,7 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter { @Override public void onVoteThingFail(int position1) { - Toast.makeText(mContext, R.string.vote_failed, Toast.LENGTH_SHORT).show(); + Toast.makeText(mActivity, R.string.vote_failed, Toast.LENGTH_SHORT).show(); commentItem.setVoteType(previousVoteType); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + previousVoteType)); ((CommentViewHolder) holder).upvoteButton.setColorFilter(previousUpvoteButtonColorFilter); @@ -206,7 +226,7 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter { commentItem.setVoteType(-1); newVoteType = RedditUtils.DIR_DOWNVOTE; ((CommentViewHolder) holder).downvoteButton - .setColorFilter(ContextCompat.getColor(mContext, R.color.colorAccent), android.graphics.PorterDuff.Mode.SRC_IN); + .setColorFilter(ContextCompat.getColor(mActivity, R.color.colorAccent), android.graphics.PorterDuff.Mode.SRC_IN); } else { //Downvoted before commentItem.setVoteType(0); @@ -222,7 +242,7 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter { if(newVoteType.equals(RedditUtils.DIR_DOWNVOTE)) { commentItem.setVoteType(-1); ((CommentViewHolder) holder).downvoteButton - .setColorFilter(ContextCompat.getColor(mContext, R.color.colorAccent), android.graphics.PorterDuff.Mode.SRC_IN); + .setColorFilter(ContextCompat.getColor(mActivity, R.color.colorAccent), android.graphics.PorterDuff.Mode.SRC_IN); } else { commentItem.setVoteType(0); ((CommentViewHolder) holder).downvoteButton.clearColorFilter(); @@ -234,7 +254,7 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter { @Override public void onVoteThingFail(int position1) { - Toast.makeText(mContext, R.string.vote_failed, Toast.LENGTH_SHORT).show(); + Toast.makeText(mActivity, R.string.vote_failed, Toast.LENGTH_SHORT).show(); commentItem.setVoteType(previousVoteType); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + previousVoteType)); ((CommentViewHolder) holder).upvoteButton.setColorFilter(previousUpvoteButtonColorFilter); @@ -263,6 +283,18 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter { notifyItemInserted(0); } + void addChildComment(CommentData comment, int parentPosition) { + List childComments = mCommentData.get(parentPosition).getChildren(); + if(childComments == null) { + childComments = new ArrayList<>(); + } + childComments.add(0, comment); + mCommentData.get(parentPosition).addChildren(childComments); + mCommentData.get(parentPosition).setHasReply(true); + notifyItemChanged(parentPosition); + mMultiLevelRecyclerView.toggleItemsGroup(parentPosition); + } + private void setExpandButton(ImageView expandButton, boolean isExpanded) { // set the icon based on the current state expandButton.setVisibility(View.VISIBLE); @@ -276,8 +308,9 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter { @BindView(R.id.plus_button_item_post_comment) ImageView upvoteButton; @BindView(R.id.score_text_view_item_post_comment) TextView scoreTextView; @BindView(R.id.minus_button_item_post_comment) ImageView downvoteButton; - @BindView(R.id.expand_button_item_post_comment) ImageView expandButton; @BindView(R.id.load_more_comments_progress_bar) ProgressBar loadMoreCommentsProgressBar; + @BindView(R.id.expand_button_item_post_comment) ImageView expandButton; + @BindView(R.id.share_button_item_post_comment) ImageView shareButton; @BindView(R.id.reply_button_item_post_comment) ImageView replyButton; @BindView(R.id.vertical_block_item_post_comment) View verticalBlock; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java index 1a219619..8b2f3374 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java @@ -64,7 +64,6 @@ public class JSONUtils { static final String SUBSCRIBERS_KEY = "subscribers"; static final String PUBLIC_DESCRIPTION_KEY = "public_description"; static final String ACTIVE_USER_COUNT_KEY = "active_user_count"; - static final String LINK_ID_KEY = "link_id"; public static final String IS_GOLD_KEY = "is_gold"; public static final String IS_FRIEND_KEY = "is_friend"; static final String KIND_KEY = "kind"; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java index fba61483..16aaa609 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java @@ -232,7 +232,7 @@ class ParseComment { private static CommentData parseSingleComment(JSONObject singleCommentData, int parentDepth, Locale locale) throws JSONException { String id = singleCommentData.getString(JSONUtils.ID_KEY); - String fullName = singleCommentData.getString(JSONUtils.LINK_ID_KEY); + String fullName = singleCommentData.getString(JSONUtils.NAME_KEY); String author = singleCommentData.getString(JSONUtils.AUTHOR_KEY); boolean isSubmitter = singleCommentData.getBoolean(JSONUtils.IS_SUBMITTER_KEY); String commentContent = ""; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java index 682d4fb3..ebdb5f91 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java @@ -77,4 +77,8 @@ public interface RedditAPI { @FormUrlEncoded @POST("/api/comment") Call sendComment(@HeaderMap Map headers, @FieldMap Map params); + + @FormUrlEncoded + @POST("/api/del") + Call delete(@HeaderMap Map headers, @FieldMap Map params); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index fc4a0fe5..741a3a57 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -688,6 +688,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_TEXT_KEY, mPost.getTitle()); 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); startActivityForResult(intent, WRITE_COMMENT_REQUEST_CODE); return true; case android.R.id.home: @@ -703,7 +704,12 @@ public class ViewPostDetailActivity extends AppCompatActivity { if(data != null && resultCode == RESULT_OK && requestCode == WRITE_COMMENT_REQUEST_CODE) { if(data.hasExtra(EXTRA_COMMENT_DATA_KEY)) { CommentData comment = data.getExtras().getParcelable(EXTRA_COMMENT_DATA_KEY); - mAdapter.addComment(comment); + if(comment.getDepth() == 0) { + mAdapter.addComment(comment); + } else { + int parentPosition = data.getExtras().getInt(CommentActivity.EXTRA_PARENT_POSITION_KEY); + mAdapter.addChildComment(comment, parentPosition); + } } else { Toast.makeText(this, R.string.send_comment_failed, Toast.LENGTH_SHORT).show(); } -- cgit v1.2.3