From 318fdf68efbfab7d6fe924e0dde1c71ead334de6 Mon Sep 17 00:00:00 2001 From: Nick Bradbury Date: Thu, 5 Dec 2013 06:49:41 -0500 Subject: [PATCH 1/3] Added exception logging to comment reply & moderation --- .../wordpress/android/ui/comments/CommentActions.java | 4 ++++ .../android/ui/comments/CommentDetailFragment.java | 10 ++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/org/wordpress/android/ui/comments/CommentActions.java b/src/org/wordpress/android/ui/comments/CommentActions.java index 1c99370b266a..2d9f586036a5 100644 --- a/src/org/wordpress/android/ui/comments/CommentActions.java +++ b/src/org/wordpress/android/ui/comments/CommentActions.java @@ -2,6 +2,7 @@ import android.os.Handler; import android.text.TextUtils; +import android.util.Log; import org.wordpress.android.WordPress; import org.wordpress.android.models.Blog; @@ -82,6 +83,7 @@ public void run() { try { newCommentID = (Integer) client.call("wp.newComment", params); } catch (XMLRPCException e) { + Log.e(WordPress.TAG, e.getMessage(), e); newCommentID = -1; } @@ -141,6 +143,7 @@ public void run() { try { result = client.call("wp.editComment", params); } catch (final XMLRPCException e) { + Log.e(WordPress.TAG, e.getMessage(), e); result = null; } @@ -193,6 +196,7 @@ public void run() { try { result = client.call("wp.deleteComment", params); } catch (final XMLRPCException e) { + Log.e(WordPress.TAG, e.getMessage(), e); result = null; } diff --git a/src/org/wordpress/android/ui/comments/CommentDetailFragment.java b/src/org/wordpress/android/ui/comments/CommentDetailFragment.java index 63a15f2ab0d7..68fc2164cf5b 100644 --- a/src/org/wordpress/android/ui/comments/CommentDetailFragment.java +++ b/src/org/wordpress/android/ui/comments/CommentDetailFragment.java @@ -7,6 +7,7 @@ import android.text.Html; import android.text.SpannableStringBuilder; import android.text.TextUtils; +import android.util.Log; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; @@ -37,6 +38,7 @@ import org.wordpress.android.util.ReaderAniUtils; import org.wordpress.android.util.StringUtils; import org.wordpress.android.util.ToastUtils; +import org.wordpress.android.util.VolleyUtils; import org.wordpress.android.util.WPImageGetter; import java.util.Map; @@ -157,12 +159,15 @@ private void hideReplyBox(boolean hideImmediately) { return; final ViewGroup layoutReply = (ViewGroup) getActivity().findViewById(R.id.layout_comment_box); - if (layoutReply == null || layoutReply.getVisibility() != View.VISIBLE) + if (layoutReply == null) return; + // showReplyBox() animation may still be happening, so clear it here + layoutReply.clearAnimation(); + if (hideImmediately) { layoutReply.setVisibility(View.GONE); - } else { + } else if (layoutReply.getVisibility() != View.VISIBLE) { ReaderAniUtils.flyOut(layoutReply); } } @@ -454,6 +459,7 @@ public void onResponse(JSONObject jsonObject) { @Override public void onErrorResponse(VolleyError volleyError) { mIsRequestingComment = false; + Log.e(WordPress.TAG, VolleyUtils.errStringFromVolleyError(volleyError), volleyError); if (hasActivity()) { if (progress != null) progress.setVisibility(View.GONE); From b756020746a72c945142198ed9830fd75057c640 Mon Sep 17 00:00:00 2001 From: Nick Bradbury Date: Thu, 5 Dec 2013 07:23:20 -0500 Subject: [PATCH 2/3] CommentsActivity & CommentDetailFragment now pass the accountId to CommentActions (previously passed the current blog object) --- src/org/wordpress/android/WordPress.java | 4 ++ .../android/ui/comments/CommentActions.java | 48 ++++++++++--------- .../ui/comments/CommentDetailFragment.java | 5 +- .../android/ui/comments/CommentsActivity.java | 5 +- 4 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/org/wordpress/android/WordPress.java b/src/org/wordpress/android/WordPress.java index dc0a2af25481..cf8945fb8e54 100644 --- a/src/org/wordpress/android/WordPress.java +++ b/src/org/wordpress/android/WordPress.java @@ -290,6 +290,10 @@ public static int getCurrentBlogId() { return (currentBlog != null ? currentBlog.getBlogId() : 0); } + public static int getCurrentBlogAccountId() { + return (currentBlog != null ? currentBlog.getId() : 0); + } + /** * Checks for WordPress.com credentials * diff --git a/src/org/wordpress/android/ui/comments/CommentActions.java b/src/org/wordpress/android/ui/comments/CommentActions.java index 2d9f586036a5..a430913483f1 100644 --- a/src/org/wordpress/android/ui/comments/CommentActions.java +++ b/src/org/wordpress/android/ui/comments/CommentActions.java @@ -43,13 +43,13 @@ protected interface OnCommentChangeListener { /** * reply to an individual comment */ - protected static void submitReplyToComment(final Blog account, - final int blogId, + protected static void submitReplyToComment(final int accountId, final Comment comment, final String replyText, final CommentActionListener actionListener) { - if (account==null || comment==null || TextUtils.isEmpty(replyText)) { + final Blog blog = WordPress.getBlog(accountId); + if (blog==null || comment==null || TextUtils.isEmpty(replyText)) { if (actionListener != null) actionListener.onActionResult(false); return; @@ -61,9 +61,9 @@ protected static void submitReplyToComment(final Blog account, @Override public void run() { XMLRPCClient client = new XMLRPCClient( - account.getUrl(), - account.getHttpuser(), - account.getHttppassword()); + blog.getUrl(), + blog.getHttpuser(), + blog.getHttppassword()); Map replyHash = new HashMap(); replyHash.put("comment_parent", comment.commentID); @@ -72,9 +72,10 @@ public void run() { replyHash.put("author_url", ""); replyHash.put("author_email", ""); - Object[] params = { blogId, - account.getUsername(), - account.getPassword(), + Object[] params = { + blog.getBlogId(), + blog.getUsername(), + blog.getPassword(), Integer.valueOf(comment.postID), replyHash }; @@ -89,7 +90,7 @@ public void run() { final boolean succeeded = (newCommentID >= 0); if (succeeded) - WordPress.wpDB.updateLatestCommentID(account.getId(), newCommentID); + WordPress.wpDB.updateLatestCommentID(accountId, newCommentID); if (actionListener != null) { handler.post(new Runnable() { @@ -106,11 +107,13 @@ public void run() { /** * change the status of a comment */ - protected static void moderateComment(final Blog blog, + protected static void moderateComment(final int accountId, final Comment comment, final CommentStatus newStatus, final CommentActionListener actionListener) { + final Blog blog = WordPress.getBlog(accountId); + if (blog==null || comment==null || newStatus==null || newStatus==CommentStatus.UNKNOWN) { if (actionListener != null) actionListener.onActionResult(false); @@ -122,7 +125,8 @@ protected static void moderateComment(final Blog blog, new Thread() { @Override public void run() { - XMLRPCClient client = new XMLRPCClient(blog.getUrl(), + XMLRPCClient client = new XMLRPCClient( + blog.getUrl(), blog.getHttpuser(), blog.getHttppassword()); @@ -166,11 +170,11 @@ public void run() { /** * delete (trash) a single comment */ - protected static void deleteComment(final Blog account, - final int blogId, + protected static void deleteComment(final int accountId, final Comment comment, final CommentActionListener actionListener) { - if (account==null || comment==null) { + final Blog blog = WordPress.getBlog(accountId); + if (blog==null || comment==null) { if (actionListener != null) actionListener.onActionResult(false); return; @@ -182,14 +186,14 @@ protected static void deleteComment(final Blog account, @Override public void run() { XMLRPCClient client = new XMLRPCClient( - account.getUrl(), - account.getHttpuser(), - account.getHttppassword()); + blog.getUrl(), + blog.getHttpuser(), + blog.getHttppassword()); Object[] params = { - blogId, - account.getUsername(), - account.getPassword(), + blog.getBlogId(), + blog.getUsername(), + blog.getPassword(), comment.commentID }; Object result; @@ -202,7 +206,7 @@ public void run() { final boolean success = (result != null && Boolean.parseBoolean(result.toString())); if (success) { - WordPress.wpDB.deleteComment(account.getId(), comment.commentID); + WordPress.wpDB.deleteComment(accountId, comment.commentID); } if (actionListener != null) { diff --git a/src/org/wordpress/android/ui/comments/CommentDetailFragment.java b/src/org/wordpress/android/ui/comments/CommentDetailFragment.java index 68fc2164cf5b..9cb96e7dfe98 100644 --- a/src/org/wordpress/android/ui/comments/CommentDetailFragment.java +++ b/src/org/wordpress/android/ui/comments/CommentDetailFragment.java @@ -337,7 +337,7 @@ public void onActionResult(boolean succeeded) { } } }; - CommentActions.moderateComment(WordPress.currentBlog, mComment, CommentStatus.APPROVED, actionListener); + CommentActions.moderateComment(mAccountId, mComment, CommentStatus.APPROVED, actionListener); } /* @@ -388,8 +388,7 @@ public void onActionResult(boolean succeeded) { }; mIsSubmittingReply = true; - CommentActions.submitReplyToComment(WordPress.currentBlog, - mBlogId, + CommentActions.submitReplyToComment(mAccountId, mComment, replyText, actionListener); diff --git a/src/org/wordpress/android/ui/comments/CommentsActivity.java b/src/org/wordpress/android/ui/comments/CommentsActivity.java index ea424daed6c3..e52027156901 100644 --- a/src/org/wordpress/android/ui/comments/CommentsActivity.java +++ b/src/org/wordpress/android/ui/comments/CommentsActivity.java @@ -252,8 +252,7 @@ public boolean onContextItemSelected(android.view.MenuItem item) { */ showDialog(ID_DIALOG_DELETING); CommentActions.deleteComment( - WordPress.currentBlog, - WordPress.getCurrentBlogId(), + WordPress.getCurrentBlogAccountId(), WordPress.currentComment, new CommentActions.CommentActionListener() { @Override @@ -288,7 +287,7 @@ public void onActionResult(boolean succeeded) { return true; } - CommentActions.moderateComment(WordPress.currentBlog, + CommentActions.moderateComment(WordPress.getCurrentBlogAccountId(), WordPress.currentComment, status, new CommentActions.CommentActionListener() { From e2d2aa62994907324eddec45af86eb564fd00a29 Mon Sep 17 00:00:00 2001 From: Nick Bradbury Date: Thu, 5 Dec 2013 11:08:09 -0500 Subject: [PATCH 3/3] * Fixed bug that could cause "approve" button to overlap reply box when moderation fails * Added connection test before moderating or replying --- .../ui/comments/CommentDetailFragment.java | 51 +++++++++++++------ 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/src/org/wordpress/android/ui/comments/CommentDetailFragment.java b/src/org/wordpress/android/ui/comments/CommentDetailFragment.java index 9cb96e7dfe98..1bd2e990dfca 100644 --- a/src/org/wordpress/android/ui/comments/CommentDetailFragment.java +++ b/src/org/wordpress/android/ui/comments/CommentDetailFragment.java @@ -2,7 +2,10 @@ import android.app.Activity; import android.content.Context; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; import android.os.Bundle; +import android.os.Handler; import android.support.v4.app.Fragment; import android.text.Html; import android.text.SpannableStringBuilder; @@ -295,6 +298,21 @@ public void onClick(View v) { } } + /* + * returns true if there's an active network connection, otherwise displays a toast error + * and returns false + */ + // TODO: move this routine to NetworkUtils once that class is merged into develop + private boolean checkConnection(Context context) { + ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo info = cm.getActiveNetworkInfo(); + if (info != null && info.isConnected()) + return true; + + ToastUtils.showToast(getActivity(), R.string.no_network_message); + return false; + } + /* * approve the current comment */ @@ -302,23 +320,14 @@ private void approveComment() { if (!hasActivity() || !hasComment() || mIsApprovingComment) return; + if (!checkConnection(getActivity())) + return; + final TextView txtBtnApprove = (TextView) getActivity().findViewById(R.id.text_approve); ReaderAniUtils.flyOut(txtBtnApprove); - // immediately show MessageBox saying comment has been approved - runnable below executes - // once MessageBar disappears - Runnable runnable = new Runnable() { - @Override - public void run() { - showReplyBox(); - } - }; - MessageBarUtils.showMessageBar(getActivity(), - getString(R.string.comment_approved), - MessageBarUtils.MessageBarType.INFO, - runnable); - - mIsApprovingComment = true; + // immediately show MessageBox saying comment has been approved + MessageBarUtils.showMessageBar(getActivity(), getString(R.string.comment_approved)); CommentActions.CommentActionListener actionListener = new CommentActions.CommentActionListener() { @Override @@ -326,17 +335,24 @@ public void onActionResult(boolean succeeded) { mIsApprovingComment = false; if (hasActivity()) { if (succeeded) { + showReplyBox(); mComment.setStatus(CommentStatus.toString(CommentStatus.APPROVED)); if (mChangeListener != null) mChangeListener.onCommentModerated(); } else { - hideReplyBox(true); - txtBtnApprove.setVisibility(View.VISIBLE); ToastUtils.showToast(getActivity(), R.string.error_moderate_comment, ToastUtils.Duration.LONG); + // fly in "Approve" after we know it had enough time to fly out + new Handler().postDelayed(new Runnable() { + @Override + public void run() { + ReaderAniUtils.flyIn(txtBtnApprove); + } + }, 500); } } } }; + mIsApprovingComment = true; CommentActions.moderateComment(mAccountId, mComment, CommentStatus.APPROVED, actionListener); } @@ -347,6 +363,9 @@ private void submitReply() { if (!hasActivity() || mIsSubmittingReply) return; + if (!checkConnection(getActivity())) + return; + final EditText editComment = (EditText) getActivity().findViewById(R.id.edit_comment); final ProgressBar progress = (ProgressBar) getActivity().findViewById(R.id.progress_submit_comment); final ImageView imgSubmit = (ImageView) getActivity().findViewById(R.id.image_post_comment);