From 029bbc951b3b7d128b9eb1a469942a8f03676d83 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 20 Aug 2019 15:01:54 +0800 Subject: [PATCH] Handle sending comment fails. Fixed snackbar hidden by keyboard in some activities. Minor bugs fixed. --- app/src/main/AndroidManifest.xml | 1 + .../infinityforreddit/CommentActivity.java | 37 +++++--- .../infinityforreddit/CommentData.java | 10 ++- .../CommentsListingRecyclerViewAdapter.java | 87 ++++++++++--------- .../EditCommentActivity.java | 17 +++- .../infinityforreddit/EditPostActivity.java | 17 +++- .../infinityforreddit/JSONUtils.java | 1 + .../infinityforreddit/ParseComment.java | 42 +++++++-- .../infinityforreddit/SearchActivity.java | 17 +++- .../infinityforreddit/SendComment.java | 12 +-- .../infinityforreddit/SubmitPost.java | 10 +-- app/src/main/res/layout/activity_comment.xml | 44 ++++++---- app/src/main/res/values/strings.xml | 5 +- 13 files changed, 202 insertions(+), 98 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 8e3f013e..674b1642 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -124,6 +124,7 @@ { + Intent intent = new Intent(mContext, ViewUserDetailActivity.class); + intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, comment.getAuthor()); + mContext.startActivity(intent); + }); + } else { + ((DataViewHolder) holder).authorTextView.setText("r/" + comment.getSubredditName()); + ((DataViewHolder) holder).authorTextView.setTextColor(mColorAccent); + ((DataViewHolder) holder).authorTextView.setOnClickListener(view -> { + Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class); + intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, comment.getSubredditName()); + mContext.startActivity(intent); + }); + } - if(comment.getAuthor().equals(comment.getSubredditName().substring(2))) { - ((DataViewHolder) holder).authorTextView.setText("u/" + comment.getAuthor()); - ((DataViewHolder) holder).authorTextView.setTextColor(mTextColorPrimaryDark); - ((DataViewHolder) holder).authorTextView.setOnClickListener(view -> { - Intent intent = new Intent(mContext, ViewUserDetailActivity.class); - intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, comment.getAuthor()); - mContext.startActivity(intent); - }); - } else { - ((DataViewHolder) holder).authorTextView.setText("r/" + comment.getSubredditName()); - ((DataViewHolder) holder).authorTextView.setTextColor(mColorAccent); - ((DataViewHolder) holder).authorTextView.setOnClickListener(view -> { - Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class); - intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, comment.getSubredditName()); - mContext.startActivity(intent); - }); - } + ((DataViewHolder) holder).commentTimeTextView.setText(comment.getCommentTime()); - ((DataViewHolder) holder).commentTimeTextView.setText(comment.getCommentTime()); + ((DataViewHolder) holder).commentMarkdownView.setMarkdown(comment.getCommentContent(), mContext); + ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(comment.getScore())); - ((DataViewHolder) holder).commentMarkdownView.setMarkdown(comment.getCommentContent(), mContext); - ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(comment.getScore())); + switch (comment.getVoteType()) { + case 1: + ((DataViewHolder) holder).upvoteButton + .setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN); + break; + case 2: + ((DataViewHolder) holder).downvoteButton + .setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN); + break; + } - switch (comment.getVoteType()) { - case 1: - ((DataViewHolder) holder).upvoteButton - .setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN); - break; - case 2: - ((DataViewHolder) holder).downvoteButton - .setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN); - break; - } - - if(comment.getAuthor().equals(mAccountName)) { - ((DataViewHolder) holder).moreButton.setVisibility(View.VISIBLE); - ((DataViewHolder) holder).moreButton.setOnClickListener(view -> { - ModifyCommentBottomSheetFragment modifyCommentBottomSheetFragment = new ModifyCommentBottomSheetFragment(); - Bundle bundle = new Bundle(); - bundle.putString(ModifyCommentBottomSheetFragment.EXTRA_ACCESS_TOKEN, mAccessToken); - bundle.putString(ModifyCommentBottomSheetFragment.EXTRA_COMMENT_CONTENT, comment.getCommentContent()); - bundle.putString(ModifyCommentBottomSheetFragment.EXTRA_COMMENT_FULLNAME, comment.getFullName()); - bundle.putInt(ModifyCommentBottomSheetFragment.EXTRA_POSITION, holder.getAdapterPosition() - 1); - modifyCommentBottomSheetFragment.setArguments(bundle); - modifyCommentBottomSheetFragment.show(((AppCompatActivity) mContext).getSupportFragmentManager(), modifyCommentBottomSheetFragment.getTag()); - }); + if(comment.getAuthor().equals(mAccountName)) { + ((DataViewHolder) holder).moreButton.setVisibility(View.VISIBLE); + ((DataViewHolder) holder).moreButton.setOnClickListener(view -> { + ModifyCommentBottomSheetFragment modifyCommentBottomSheetFragment = new ModifyCommentBottomSheetFragment(); + Bundle bundle = new Bundle(); + bundle.putString(ModifyCommentBottomSheetFragment.EXTRA_ACCESS_TOKEN, mAccessToken); + bundle.putString(ModifyCommentBottomSheetFragment.EXTRA_COMMENT_CONTENT, comment.getCommentContent()); + bundle.putString(ModifyCommentBottomSheetFragment.EXTRA_COMMENT_FULLNAME, comment.getFullName()); + bundle.putInt(ModifyCommentBottomSheetFragment.EXTRA_POSITION, holder.getAdapterPosition() - 1); + modifyCommentBottomSheetFragment.setArguments(bundle); + modifyCommentBottomSheetFragment.show(((AppCompatActivity) mContext).getSupportFragmentManager(), modifyCommentBottomSheetFragment.getTag()); + }); + } } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/EditCommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/EditCommentActivity.java index 0570f133..5739dfe6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/EditCommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/EditCommentActivity.java @@ -1,5 +1,6 @@ package ml.docilealligator.infinityforreddit; +import android.content.Context; import android.content.Intent; import android.content.res.Configuration; import android.os.Build; @@ -8,7 +9,7 @@ import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.Window; -import android.view.WindowManager; +import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import android.widget.Toast; @@ -85,7 +86,19 @@ public class EditCommentActivity extends AppCompatActivity { contentEditText.setText(getIntent().getExtras().getString(EXTRA_CONTENT)); contentEditText.requestFocus(); - getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); + InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); + if(imm != null) { + imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); + } + } + + @Override + protected void onPause() { + super.onPause(); + InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); + if(imm != null) { + imm.hideSoftInputFromWindow(contentEditText.getWindowToken(), 0); + } } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/EditPostActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/EditPostActivity.java index 29282fe1..e535b31d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/EditPostActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/EditPostActivity.java @@ -1,5 +1,6 @@ package ml.docilealligator.infinityforreddit; +import android.content.Context; import android.content.Intent; import android.content.res.Configuration; import android.os.Build; @@ -8,7 +9,7 @@ import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.Window; -import android.view.WindowManager; +import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; @@ -85,7 +86,19 @@ public class EditPostActivity extends AppCompatActivity { contentEditText.setText(getIntent().getExtras().getString(EXTRA_CONTENT)); contentEditText.requestFocus(); - getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); + InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); + if(imm != null) { + imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); + } + } + + @Override + protected void onPause() { + super.onPause(); + InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); + if(imm != null) { + imm.hideSoftInputFromWindow(contentEditText.getWindowToken(), 0); + } } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java index 8d6539ec..f3d40d58 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java @@ -15,6 +15,7 @@ public class JSONUtils { static final String SUBREDDIT_NAME_PREFIX_KEY = "subreddit_name_prefixed"; static final String SELFTEXT_KEY = "selftext"; static final String AUTHOR_KEY = "author"; + static final String LINK_AUTHOR_KEY = "link_author"; static final String LINK_FLAIR_TEXT_KEY = "link_flair_text"; static final String SCORE_KEY = "score"; static final String LIKES_KEY = "likes"; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java index 1ea058e6..aeddffc0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java @@ -24,7 +24,7 @@ class ParseComment { interface ParseSentCommentListener { void onParseSentCommentSuccess(CommentData commentData); - void onParseSentCommentFailed(); + void onParseSentCommentFailed(@Nullable String errorMessage); } static void parseComment(String response, ArrayList commentData, Locale locale, @@ -179,6 +179,7 @@ class ParseComment { private Locale locale; private ParseSentCommentListener parseSentCommentListener; private boolean parseFailed; + private String errorMessage; private CommentData commentData; ParseSentCommentAsyncTask(String response, int depth, Locale locale, ParseSentCommentListener parseSentCommentListener) { @@ -196,6 +197,7 @@ class ParseComment { commentData = parseSingleComment(sentCommentData, depth, locale); } catch (JSONException e) { e.printStackTrace(); + errorMessage = parseSentCommentErrorMessage(response); parseFailed = true; } return null; @@ -205,7 +207,7 @@ class ParseComment { protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); if(parseFailed) { - parseSentCommentListener.onParseSentCommentFailed(); + parseSentCommentListener.onParseSentCommentFailed(errorMessage); } else { parseSentCommentListener.onParseSentCommentSuccess(commentData); } @@ -213,10 +215,10 @@ class ParseComment { } static CommentData parseSingleComment(JSONObject singleCommentData, int depth, Locale locale) throws JSONException { - Log.i("adfasdf", singleCommentData.toString()); String id = singleCommentData.getString(JSONUtils.ID_KEY); String fullName = singleCommentData.getString(JSONUtils.NAME_KEY); String author = singleCommentData.getString(JSONUtils.AUTHOR_KEY); + String linkAuthor = singleCommentData.has(JSONUtils.LINK_AUTHOR_KEY) ? singleCommentData.getString(JSONUtils.LINK_AUTHOR_KEY) : null; String linkId = singleCommentData.getString(JSONUtils.LINK_ID_KEY).substring(3); String subredditName = singleCommentData.getString(JSONUtils.SUBREDDIT_KEY); String parentId = singleCommentData.getString(JSONUtils.PARENT_ID_KEY); @@ -242,7 +244,37 @@ class ParseComment { boolean collapsed = singleCommentData.getBoolean(JSONUtils.COLLAPSED_KEY); boolean hasReply = !(singleCommentData.get(JSONUtils.REPLIES_KEY) instanceof String); - return new CommentData(id, fullName, author, formattedSubmitTime, commentContent, linkId, - subredditName, parentId, score, isSubmitter, permalink, depth, collapsed, hasReply, scoreHidden); + return new CommentData(id, fullName, author, linkAuthor, formattedSubmitTime, commentContent, + linkId, subredditName, parentId, score, isSubmitter, permalink, depth, collapsed, + hasReply, scoreHidden); + } + + @Nullable + private static String parseSentCommentErrorMessage(String response) { + try { + JSONObject responseObject = new JSONObject(response).getJSONObject(JSONUtils.JSON_KEY); + + if(responseObject.getJSONArray(JSONUtils.ERRORS_KEY).length() != 0) { + JSONArray error = responseObject.getJSONArray(JSONUtils.ERRORS_KEY) + .getJSONArray(responseObject.getJSONArray(JSONUtils.ERRORS_KEY).length() - 1); + if(error.length() != 0) { + String errorString; + if(error.length() >= 2) { + errorString = error.getString(1); + } else { + errorString = error.getString(0); + } + return errorString.substring(0, 1).toUpperCase() + errorString.substring(1); + } else { + return null; + } + } else { + return null; + } + } catch (JSONException e) { + e.printStackTrace(); + } + + return null; } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchActivity.java index e44c6bf5..f6558013 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchActivity.java @@ -1,6 +1,7 @@ package ml.docilealligator.infinityforreddit; import android.app.Activity; +import android.content.Context; import android.content.Intent; import android.content.res.Configuration; import android.os.Build; @@ -9,7 +10,7 @@ import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.Window; -import android.view.WindowManager; +import android.view.inputmethod.InputMethodManager; import android.widget.RelativeLayout; import android.widget.TextView; @@ -171,7 +172,19 @@ public class SearchActivity extends AppCompatActivity { query = null; } - getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); + InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); + if(imm != null) { + imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); + } + } + + @Override + protected void onPause() { + super.onPause(); + InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); + if(imm != null) { + imm.hideSoftInputFromWindow(simpleSearchView.getSearchEditText().getWindowToken(), 0); + } } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SendComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SendComment.java index 18eac5cf..6be33004 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SendComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SendComment.java @@ -1,6 +1,7 @@ package ml.docilealligator.infinityforreddit; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import java.util.HashMap; import java.util.Locale; @@ -15,8 +16,7 @@ class SendComment { interface SendCommentListener { void sendCommentSuccess(CommentData commentData); - void sendCommentFailed(); - void parseSentCommentFailed(); + void sendCommentFailed(String errorMessage); } static void sendComment(String commentMarkdown, String thingFullname, int parentDepth, @@ -43,18 +43,18 @@ class SendComment { } @Override - public void onParseSentCommentFailed() { - + public void onParseSentCommentFailed(@Nullable String errorMessage) { + sendCommentListener.sendCommentFailed(errorMessage); } }); } else { - sendCommentListener.sendCommentFailed(); + sendCommentListener.sendCommentFailed(response.message()); } } @Override public void onFailure(@NonNull Call call, @NonNull Throwable t) { - sendCommentListener.sendCommentFailed(); + sendCommentListener.sendCommentFailed(t.getMessage()); } }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java index e0a9dcc8..018d3a7c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java @@ -204,6 +204,7 @@ class SubmitPost { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { if(response.isSuccessful()) { + Log.i("afasdfadsfasdfasdfasdf", "a " + response.body()); try { getSubmittedPost(response.body(), kind, oauthRetrofit, accessToken, locale, submitPostListener); @@ -219,8 +220,7 @@ class SubmitPost { @Override public void onFailure(@NonNull Call call, @NonNull Throwable t) { - Log.i("call_failed", call.request().url().toString()); - submitPostListener.submitFailed(null); + submitPostListener.submitFailed(t.getMessage()); } }); } @@ -414,13 +414,11 @@ class SubmitPost { String errorString; if(error.length() >= 2) { errorString = error.getString(1); - errorString = errorString.substring(0, 1).toUpperCase() + errorString.substring(1); - submitPostListener.submitFailed(errorString); } else { errorString = error.getString(0); - errorString = errorString.substring(0, 1).toUpperCase() + errorString.substring(1); - submitPostListener.submitFailed(errorString); } + errorString = errorString.substring(0, 1).toUpperCase() + errorString.substring(1); + submitPostListener.submitFailed(errorString); } else { submitPostListener.submitFailed(null); } diff --git a/app/src/main/res/layout/activity_comment.xml b/app/src/main/res/layout/activity_comment.xml index 65abe106..76a053bf 100644 --- a/app/src/main/res/layout/activity_comment.xml +++ b/app/src/main/res/layout/activity_comment.xml @@ -21,31 +21,37 @@ - - - - + android:orientation="vertical" + android:padding="16dp"> - + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f2b482a3..5a245f76 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -2,7 +2,7 @@ Infinity Login Search - Add Comment + Send Comment Reply Text Post Select a Subreddit @@ -104,7 +104,8 @@ Lazy Mode starts in %1$.1fs Lazy Mode stopped - Your interesting thoughts here + Your interesting thought here + Where is your interesting thought Sending Could not send this comment The comment is sent but unable to get the sent comment