示例#1
0
  private void submitComment(final long replyToCommentId) {
    final EditText editComment = (EditText) findViewById(R.id.edit_comment);
    final String commentText = EditTextUtils.getText(editComment);
    if (TextUtils.isEmpty(commentText)) return;

    // hide the comment box - this provides immediate indication that comment is being posted
    // and prevents users from submitting the same comment twice
    hideAddCommentBox();

    // generate a "fake" comment id to assign to the new comment so we can add it to the db
    // and reflect it in the adapter before the API call returns
    final long fakeCommentId = ReaderCommentActions.generateFakeCommentId();

    mIsSubmittingComment = true;
    ReaderActions.CommentActionListener actionListener =
        new ReaderActions.CommentActionListener() {
          @Override
          public void onActionResult(boolean succeeded, ReaderComment newComment) {
            mIsSubmittingComment = false;
            if (succeeded) {
              // comment posted successfully so stop highlighting the fake one and replace
              // it with the real one
              getCommentAdapter().setHighlightCommentId(0, false);
              getCommentAdapter().replaceComment(fakeCommentId, newComment);
              getListView().invalidateViews();
            } else {
              // comment failed to post - show the comment box again with the comment text intact,
              // and remove the "fake" comment from the adapter
              editComment.setText(commentText);
              showAddCommentBox(replyToCommentId);
              getCommentAdapter().removeComment(fakeCommentId);
              ToastUtils.showToast(
                  ReaderPostDetailActivity.this,
                  R.string.reader_toast_err_comment_failed,
                  ToastUtils.Duration.LONG);
            }
          }
        };

    final ReaderComment newComment =
        ReaderCommentActions.submitPostComment(
            mPost, fakeCommentId, commentText, replyToCommentId, actionListener);
    if (newComment != null) {
      mIsPostChanged = true;
      editComment.setText(null);
      // add the "fake" comment to the adapter, highlight it, and show a progress bar
      // next to it while it's submitted
      getCommentAdapter().setHighlightCommentId(newComment.commentId, true);
      getCommentAdapter().addComment(newComment);
      // make sure it's scrolled into view
      scrollToCommentId(fakeCommentId);
    }
  }
示例#2
0
  /*
   * request comments for this post
   */
  private void updateComments() {
    if (mPost == null || !mPost.isWP()) return;

    if (mIsUpdatingComments) return;

    mIsUpdatingComments = true;

    if (!isCommentAdapterEmpty()) showProgressFooter();

    ReaderActions.UpdateResultListener resultListener =
        new ReaderActions.UpdateResultListener() {
          @Override
          public void onUpdateResult(ReaderActions.UpdateResult result) {
            mIsUpdatingComments = false;
            hideProgressFooter();
            if (result == ReaderActions.UpdateResult.CHANGED) refreshComments();
          }
        };
    ReaderCommentActions.updateCommentsForPost(mPost, resultListener);
  }