Skip to content

Commit e1a2f17

Browse files
committed
Added support for moderation actions when CommentDetailFragment doesn't have a comment action listener.
1 parent 1a18fa5 commit e1a2f17

File tree

3 files changed

+124
-8
lines changed

3 files changed

+124
-8
lines changed

WordPress/src/main/java/org/wordpress/android/ui/comments/CommentActions.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,68 @@ public void onErrorResponse(VolleyError volleyError) {
243243
WordPress.getRestClientUtils().replyToComment(reply.getContent(), reply.getRestPath(), listener, errorListener);
244244
}
245245

246+
/**
247+
* reply to an individual comment via the WP.com REST API
248+
*/
249+
public static void submitReplyToCommentRestApi(long siteId, long commentId,
250+
final String replyText,
251+
final CommentActionListener actionListener) {
252+
if (TextUtils.isEmpty(replyText)) {
253+
if (actionListener != null)
254+
actionListener.onActionResult(false);
255+
return;
256+
}
257+
258+
RestRequest.Listener listener = new RestRequest.Listener() {
259+
@Override
260+
public void onResponse(JSONObject jsonObject) {
261+
if (actionListener != null)
262+
actionListener.onActionResult(true);
263+
}
264+
};
265+
RestRequest.ErrorListener errorListener = new RestRequest.ErrorListener() {
266+
@Override
267+
public void onErrorResponse(VolleyError volleyError) {
268+
if (volleyError != null)
269+
AppLog.e(T.COMMENTS, volleyError.getMessage(), volleyError);
270+
if (actionListener != null)
271+
actionListener.onActionResult(false);
272+
}
273+
};
274+
275+
WordPress.getRestClientUtils().replyToComment(siteId, commentId, replyText, listener, errorListener);
276+
}
277+
278+
/**
279+
* Moderate a comment from a WPCOM notification
280+
*/
281+
public static void moderateCommentRestApi(long siteId,
282+
long commentId,
283+
CommentStatus newStatus,
284+
final CommentActionListener actionListener) {
285+
286+
WordPress.getRestClientUtils().moderateComment(
287+
String.valueOf(siteId),
288+
String.valueOf(commentId),
289+
CommentStatus.toRESTString(newStatus),
290+
new RestRequest.Listener() {
291+
@Override
292+
public void onResponse(JSONObject response) {
293+
if (actionListener != null) {
294+
actionListener.onActionResult(true);
295+
}
296+
}
297+
}, new RestRequest.ErrorListener() {
298+
@Override
299+
public void onErrorResponse(VolleyError error) {
300+
if (actionListener != null) {
301+
actionListener.onActionResult(false);
302+
}
303+
}
304+
}
305+
);
306+
}
307+
246308
/**
247309
* Moderate a comment from a WPCOM notification
248310
*/

WordPress/src/main/java/org/wordpress/android/ui/comments/CommentDetailFragment.java

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public class CommentDetailFragment extends Fragment implements NotificationFragm
9797

9898
private boolean mIsUsersBlog = false;
9999
private boolean mIsCommentReply = false;
100+
private boolean mIsSubmittingReply = false;
100101

101102
private NotificationsDetailListFragment mNotificationsDetailListFragment;
102103

@@ -625,19 +626,41 @@ private void moderateComment(final CommentStatus newStatus) {
625626
if (!NetworkUtils.checkConnection(getActivity()))
626627
return;
627628

628-
// Fire the appropriate listener
629+
// Fire the appropriate listener if we have one
629630
if (mNote != null && mOnNoteCommentActionListener != null) {
630631
mOnNoteCommentActionListener.onModerateCommentForNote(mNote, newStatus);
632+
return;
631633
} else if (mOnCommentActionListener != null) {
632634
mOnCommentActionListener.onModerateComment(mLocalBlogId, mComment, newStatus);
635+
return;
633636
}
637+
638+
if (mNote == null) return;
639+
640+
// Basic moderation support, currently only used when this Fragment is in a CommentDetailActivity
641+
// Uses WP.com REST API and requires a note object
642+
final CommentStatus oldStatus = mComment.getStatusEnum();
643+
mComment.setStatus(CommentStatus.toString(newStatus));
644+
updateStatusViews();
645+
CommentActions.moderateCommentRestApi(mNote.getSiteId(), mComment.commentID, newStatus, new CommentActions.CommentActionListener() {
646+
@Override
647+
public void onActionResult(boolean succeeded) {
648+
if (!isAdded()) return;
649+
650+
if (!succeeded) {
651+
mComment.setStatus(CommentStatus.toString(oldStatus));
652+
updateStatusViews();
653+
ToastUtils.showToast(getActivity(), R.string.error_moderate_comment);
654+
}
655+
}
656+
});
634657
}
635658

636659
/*
637660
* post comment box text as a reply to the current comment
638661
*/
639662
private void submitReply() {
640-
if (!isAdded())
663+
if (!isAdded() || mIsSubmittingReply)
641664
return;
642665

643666
if (!NetworkUtils.checkConnection(getActivity()))
@@ -653,15 +676,46 @@ private void submitReply() {
653676
mEditReply.setEnabled(false);
654677
EditTextUtils.hideSoftInput(mEditReply);
655678
mImgSubmitReply.setVisibility(View.GONE);
656-
ProgressBar progress = (ProgressBar) getView().findViewById(R.id.progress_submit_comment);
657-
progress.setVisibility(View.VISIBLE);
679+
final ProgressBar progressBar = getView() != null ? (ProgressBar) getView().findViewById(R.id.progress_submit_comment) : null;
680+
if (getView() != null) {
681+
progressBar.setVisibility(View.VISIBLE);
682+
}
658683

659-
// Fire the appropriate action listener
684+
// Fire the appropriate action listener if we have one
660685
if (mNote != null && mOnNoteCommentActionListener != null) {
661686
mOnNoteCommentActionListener.onReplyToNote(mNote, replyText);
687+
return;
662688
} else if (mOnCommentActionListener != null) {
663689
mOnCommentActionListener.onReplyToComment(mLocalBlogId, mComment, replyText);
690+
return;
664691
}
692+
693+
if (mNote == null) return;
694+
695+
// Handle the reply here if no OnCommentActionListener was provided. Uses WP.com REST API
696+
mIsSubmittingReply = true;
697+
CommentActions.submitReplyToCommentRestApi(mNote.getSiteId(), mComment.commentID, replyText, new CommentActions.CommentActionListener() {
698+
@Override
699+
public void onActionResult(boolean succeeded) {
700+
if (!isAdded()) return;
701+
702+
mIsSubmittingReply = false;
703+
mEditReply.setEnabled(true);
704+
mImgSubmitReply.setVisibility(View.VISIBLE);
705+
if (progressBar != null) {
706+
progressBar.setVisibility(View.GONE);
707+
}
708+
updateStatusViews();
709+
if (succeeded) {
710+
ToastUtils.showToast(getActivity(), getString(R.string.note_reply_successful));
711+
mEditReply.setText(null);
712+
} else {
713+
ToastUtils.showToast(getActivity(), R.string.reply_failed, ToastUtils.Duration.LONG);
714+
// refocus editor on failure and show soft keyboard
715+
EditTextUtils.showSoftInput(mEditReply);
716+
}
717+
}
718+
});
665719
}
666720

667721
/*
@@ -827,7 +881,7 @@ private boolean canEdit() {
827881
return (mLocalBlogId > 0 && canModerate());
828882
}
829883
private boolean canLike() {
830-
return (mEnabledActions != null && mEnabledActions.contains(EnabledActions.ACTION_LIKE));
884+
return (!mIsCommentReply && mEnabledActions != null && mEnabledActions.contains(EnabledActions.ACTION_LIKE));
831885
}
832886

833887
/*

libs/networking/WordPressNetworking/src/main/java/org/wordpress/android/networking/RestClientUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ public void replyToComment(String reply, String path, Listener listener, ErrorLi
7676
* <p/>
7777
* https://developer.wordpress.com/docs/api/1/post/sites/%24site/posts/%24post_ID/replies/new/
7878
*/
79-
public void replyToComment(String siteId, String commentId, String content, Listener listener,
79+
public void replyToComment(long siteId, long commentId, String content, Listener listener,
8080
ErrorListener errorListener) {
8181
Map<String, String> params = new HashMap<String, String>();
8282
params.put(COMMENT_REPLY_CONTENT_FIELD, content);
83-
String path = String.format("sites/%s/comments/%s/replies/new", siteId, commentId);
83+
String path = String.format("sites/%d/comments/%d/replies/new", siteId, commentId);
8484
post(path, params, null, listener, errorListener);
8585
}
8686

0 commit comments

Comments
 (0)