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);
}
}
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;
}
}
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();
}
}
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;
}
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);
}
}
Aggregations