Search in sources :

Example 1 with ReaderComment

use of org.wordpress.android.models.ReaderComment in project WordPress-Android by wordpress-mobile.

the class ReaderCommentTable method addOrUpdateComments.

public static void addOrUpdateComments(ReaderCommentList comments) {
    if (comments == null || comments.size() == 0) {
        return;
    }
    SQLiteDatabase db = ReaderDatabase.getWritableDb();
    db.beginTransaction();
    SQLiteStatement stmt = db.compileStatement("INSERT OR REPLACE INTO tbl_comments (" + COLUMN_NAMES + ") VALUES (?1,?2,?3,?4,?5,?6,?7,?8,?9,?10,?11,?12,?13,?14,?15,?16)");
    try {
        for (ReaderComment comment : comments) {
            stmt.bindLong(1, comment.blogId);
            stmt.bindLong(2, comment.postId);
            stmt.bindLong(3, comment.commentId);
            stmt.bindLong(4, comment.parentId);
            stmt.bindString(5, comment.getAuthorName());
            stmt.bindString(6, comment.getAuthorAvatar());
            stmt.bindString(7, comment.getAuthorUrl());
            stmt.bindLong(8, comment.authorId);
            stmt.bindLong(9, comment.authorBlogId);
            stmt.bindString(10, comment.getPublished());
            stmt.bindLong(11, comment.timestamp);
            stmt.bindString(12, comment.getStatus());
            stmt.bindString(13, comment.getText());
            stmt.bindLong(14, comment.numLikes);
            stmt.bindLong(15, SqlUtils.boolToSql(comment.isLikedByCurrentUser));
            stmt.bindLong(16, comment.pageNumber);
            stmt.execute();
        }
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
        SqlUtils.closeStatement(stmt);
    }
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLiteStatement(android.database.sqlite.SQLiteStatement) ReaderComment(org.wordpress.android.models.ReaderComment)

Example 2 with ReaderComment

use of org.wordpress.android.models.ReaderComment in project WordPress-Android by wordpress-mobile.

the class ReaderCommentListActivity method doDirectOperation.

private void doDirectOperation() {
    if (mDirectOperation != null) {
        switch(mDirectOperation) {
            case COMMENT_JUMP:
                mCommentAdapter.setHighlightCommentId(mCommentId, false);
                // clear up the direct operation vars. Only performing it once.
                mDirectOperation = null;
                mCommentId = 0;
                break;
            case COMMENT_REPLY:
                setReplyToCommentId(mCommentId, mAccountStore.hasAccessToken());
                // clear up the direct operation vars. Only performing it once.
                mDirectOperation = null;
                mCommentId = 0;
                break;
            case COMMENT_LIKE:
                getCommentAdapter().setHighlightCommentId(mCommentId, false);
                if (!mAccountStore.hasAccessToken()) {
                    Snackbar.make(mRecyclerView, R.string.reader_snackbar_err_cannot_like_post_logged_out, Snackbar.LENGTH_INDEFINITE).setAction(R.string.sign_in, mSignInClickListener).show();
                } else {
                    ReaderComment comment = ReaderCommentTable.getComment(mPost.blogId, mPost.postId, mCommentId);
                    if (comment == null) {
                        ToastUtils.showToast(ReaderCommentListActivity.this, R.string.reader_toast_err_comment_not_found);
                    } else if (comment.isLikedByCurrentUser) {
                        ToastUtils.showToast(ReaderCommentListActivity.this, R.string.reader_toast_err_already_liked);
                    } else {
                        long wpComUserId = mAccountStore.getAccount().getUserId();
                        if (ReaderCommentActions.performLikeAction(comment, true, wpComUserId) && getCommentAdapter().refreshComment(mCommentId)) {
                            getCommentAdapter().setAnimateLikeCommentId(mCommentId);
                            AnalyticsUtils.trackWithReaderPostDetails(AnalyticsTracker.Stat.READER_ARTICLE_COMMENT_LIKED, mPost);
                        } else {
                            ToastUtils.showToast(ReaderCommentListActivity.this, R.string.reader_toast_err_generic);
                        }
                    }
                    // clear up the direct operation vars. Only performing it once.
                    mDirectOperation = null;
                }
                break;
            case POST_LIKE:
                // nothing special to do in this case
                break;
        }
    } else {
        mCommentId = 0;
    }
}
Also used : ReaderComment(org.wordpress.android.models.ReaderComment)

Example 3 with ReaderComment

use of org.wordpress.android.models.ReaderComment in project WordPress-Android by wordpress-mobile.

the class ReaderCommentListActivity method submitComment.

/*
     * submit the text typed into the comment box as a comment on the current post
     */
private void submitComment() {
    final String commentText = EditTextUtils.getText(mEditComment);
    if (TextUtils.isEmpty(commentText)) {
        return;
    }
    if (!NetworkUtils.checkConnection(this)) {
        return;
    }
    AnalyticsUtils.trackWithReaderPostDetails(AnalyticsTracker.Stat.READER_ARTICLE_COMMENTED_ON, mPost);
    mSubmitReplyBtn.setEnabled(false);
    mEditComment.setEnabled(false);
    mIsSubmittingComment = true;
    // 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();
    ReaderActions.CommentActionListener actionListener = new ReaderActions.CommentActionListener() {

        @Override
        public void onActionResult(boolean succeeded, ReaderComment newComment) {
            if (isFinishing()) {
                return;
            }
            mIsSubmittingComment = false;
            mSubmitReplyBtn.setEnabled(true);
            mEditComment.setEnabled(true);
            if (succeeded) {
                // stop highlighting the fake comment and replace it with the real one
                getCommentAdapter().setHighlightCommentId(0, false);
                getCommentAdapter().replaceComment(fakeCommentId, newComment);
                setReplyToCommentId(0, false);
                mEditComment.getAutoSaveTextHelper().clearSavedText(mEditComment);
            } else {
                mEditComment.setText(commentText);
                getCommentAdapter().removeComment(fakeCommentId);
                ToastUtils.showToast(ReaderCommentListActivity.this, R.string.reader_toast_err_comment_failed, ToastUtils.Duration.LONG);
            }
            checkEmptyView();
        }
    };
    long wpComUserId = mAccountStore.getAccount().getUserId();
    ReaderComment newComment = ReaderCommentActions.submitPostComment(getPost(), fakeCommentId, commentText, mReplyToCommentId, actionListener, wpComUserId);
    if (newComment != null) {
        mEditComment.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);
        checkEmptyView();
    }
}
Also used : ReaderComment(org.wordpress.android.models.ReaderComment) ReaderActions(org.wordpress.android.ui.reader.actions.ReaderActions)

Example 4 with ReaderComment

use of org.wordpress.android.models.ReaderComment in project WordPress-Android by wordpress-mobile.

the class ReaderCommentActions method submitPostComment.

/*
     * add the passed comment text to the passed post - caller must pass a unique "fake" comment id
     * to give the comment that's generated locally
     */
public static ReaderComment submitPostComment(final ReaderPost post, final long fakeCommentId, final String commentText, final long replyToCommentId, final ReaderActions.CommentActionListener actionListener, final long wpComUserId) {
    if (post == null || TextUtils.isEmpty(commentText)) {
        return null;
    }
    // determine which page this new comment should be assigned to
    final int pageNumber;
    if (replyToCommentId != 0) {
        pageNumber = ReaderCommentTable.getPageNumberForComment(post.blogId, post.postId, replyToCommentId);
    } else {
        pageNumber = ReaderCommentTable.getLastPageNumberForPost(post.blogId, post.postId);
    }
    // create a "fake" comment that's added to the db so it can be shown right away - will be
    // replaced with actual comment if it succeeds to be posted, or deleted if comment fails
    // to be posted
    ReaderComment newComment = new ReaderComment();
    newComment.commentId = fakeCommentId;
    newComment.postId = post.postId;
    newComment.blogId = post.blogId;
    newComment.parentId = replyToCommentId;
    newComment.pageNumber = pageNumber;
    newComment.setText(commentText);
    Date dtPublished = DateTimeUtils.nowUTC();
    newComment.setPublished(DateTimeUtils.iso8601FromDate(dtPublished));
    newComment.timestamp = dtPublished.getTime();
    ReaderUser currentUser = ReaderUserTable.getCurrentUser(wpComUserId);
    if (currentUser != null) {
        newComment.setAuthorAvatar(currentUser.getAvatarUrl());
        newComment.setAuthorName(currentUser.getDisplayName());
    }
    ReaderCommentTable.addOrUpdateComment(newComment);
    // different endpoint depending on whether the new comment is a reply to another comment
    final String path;
    if (replyToCommentId == 0) {
        path = "sites/" + post.blogId + "/posts/" + post.postId + "/replies/new";
    } else {
        path = "sites/" + post.blogId + "/comments/" + Long.toString(replyToCommentId) + "/replies/new";
    }
    Map<String, String> params = new HashMap<>();
    params.put("content", commentText);
    RestRequest.Listener listener = new RestRequest.Listener() {

        @Override
        public void onResponse(JSONObject jsonObject) {
            ReaderCommentTable.deleteComment(post, fakeCommentId);
            AppLog.i(T.READER, "comment succeeded");
            ReaderComment newComment = ReaderComment.fromJson(jsonObject, post.blogId);
            newComment.pageNumber = pageNumber;
            ReaderCommentTable.addOrUpdateComment(newComment);
            if (actionListener != null) {
                actionListener.onActionResult(true, newComment);
            }
        }
    };
    RestRequest.ErrorListener errorListener = new RestRequest.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError volleyError) {
            ReaderCommentTable.deleteComment(post, fakeCommentId);
            AppLog.w(T.READER, "comment failed");
            AppLog.e(T.READER, volleyError);
            if (actionListener != null) {
                actionListener.onActionResult(false, null);
            }
        }
    };
    AppLog.i(T.READER, "submitting comment");
    WordPress.getRestClientUtilsV1_1().post(path, params, null, listener, errorListener);
    return newComment;
}
Also used : VolleyError(com.android.volley.VolleyError) ReaderUser(org.wordpress.android.models.ReaderUser) HashMap(java.util.HashMap) Date(java.util.Date) RestRequest(com.wordpress.rest.RestRequest) JSONObject(org.json.JSONObject) ReaderComment(org.wordpress.android.models.ReaderComment)

Example 5 with ReaderComment

use of org.wordpress.android.models.ReaderComment in project WordPress-Android by wordpress-mobile.

the class ReaderCommentAdapter method showLikeStatus.

private void showLikeStatus(final CommentHolder holder, int position) {
    ReaderComment comment = getItem(position);
    if (comment == null) {
        return;
    }
    if (mPost.canLikePost()) {
        holder.countLikes.setVisibility(View.VISIBLE);
        holder.countLikes.setSelected(comment.isLikedByCurrentUser);
        holder.countLikes.setCount(comment.numLikes);
        if (!mAccountStore.hasAccessToken()) {
            holder.countLikes.setEnabled(false);
        } else {
            holder.countLikes.setEnabled(true);
            holder.countLikes.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    int clickedPosition = holder.getAdapterPosition();
                    toggleLike(v.getContext(), holder, clickedPosition);
                }
            });
        }
    } else {
        holder.countLikes.setVisibility(View.GONE);
        holder.countLikes.setOnClickListener(null);
    }
}
Also used : ReaderComment(org.wordpress.android.models.ReaderComment) WPNetworkImageView(org.wordpress.android.widgets.WPNetworkImageView) View(android.view.View) ReaderCommentsPostHeaderView(org.wordpress.android.ui.reader.views.ReaderCommentsPostHeaderView) ReaderIconCountView(org.wordpress.android.ui.reader.views.ReaderIconCountView) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView)

Aggregations

ReaderComment (org.wordpress.android.models.ReaderComment)10 RecyclerView (android.support.v7.widget.RecyclerView)2 View (android.view.View)2 TextView (android.widget.TextView)2 ReaderCommentsPostHeaderView (org.wordpress.android.ui.reader.views.ReaderCommentsPostHeaderView)2 ReaderIconCountView (org.wordpress.android.ui.reader.views.ReaderIconCountView)2 WPNetworkImageView (org.wordpress.android.widgets.WPNetworkImageView)2 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1 SQLiteStatement (android.database.sqlite.SQLiteStatement)1 RelativeLayout (android.widget.RelativeLayout)1 VolleyError (com.android.volley.VolleyError)1 RestRequest (com.wordpress.rest.RestRequest)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 JSONObject (org.json.JSONObject)1 ReaderUser (org.wordpress.android.models.ReaderUser)1 ReaderActions (org.wordpress.android.ui.reader.actions.ReaderActions)1