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 1c99370b266a..a430913483f1 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; @@ -42,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; @@ -60,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); @@ -71,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 }; @@ -82,12 +84,13 @@ public void run() { try { newCommentID = (Integer) client.call("wp.newComment", params); } catch (XMLRPCException e) { + Log.e(WordPress.TAG, e.getMessage(), e); newCommentID = -1; } 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() { @@ -104,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); @@ -120,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()); @@ -141,6 +147,7 @@ public void run() { try { result = client.call("wp.editComment", params); } catch (final XMLRPCException e) { + Log.e(WordPress.TAG, e.getMessage(), e); result = null; } @@ -163,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; @@ -179,26 +186,27 @@ 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; try { result = client.call("wp.deleteComment", params); } catch (final XMLRPCException e) { + Log.e(WordPress.TAG, e.getMessage(), e); result = null; } 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 63a15f2ab0d7..1bd2e990dfca 100644 --- a/src/org/wordpress/android/ui/comments/CommentDetailFragment.java +++ b/src/org/wordpress/android/ui/comments/CommentDetailFragment.java @@ -2,11 +2,15 @@ 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; import android.text.TextUtils; +import android.util.Log; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; @@ -37,6 +41,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 +162,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); } } @@ -290,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 */ @@ -297,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 @@ -321,18 +335,25 @@ 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); } } } }; - CommentActions.moderateComment(WordPress.currentBlog, mComment, CommentStatus.APPROVED, actionListener); + mIsApprovingComment = true; + CommentActions.moderateComment(mAccountId, mComment, CommentStatus.APPROVED, actionListener); } /* @@ -342,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); @@ -383,8 +407,7 @@ public void onActionResult(boolean succeeded) { }; mIsSubmittingReply = true; - CommentActions.submitReplyToComment(WordPress.currentBlog, - mBlogId, + CommentActions.submitReplyToComment(mAccountId, mComment, replyText, actionListener); @@ -454,6 +477,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); 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() {