use of org.wordpress.android.models.ReaderPost in project WordPress-Android by wordpress-mobile.
the class ReaderPostAdapter method setFollowStatusForBlog.
public void setFollowStatusForBlog(long blogId, boolean isFollowing) {
ReaderPost post;
for (int i = 0; i < mPosts.size(); i++) {
post = mPosts.get(i);
if (post.blogId == blogId && post.isFollowedByCurrentUser != isFollowing) {
post.isFollowedByCurrentUser = isFollowing;
mPosts.set(i, post);
notifyItemChanged(i);
}
}
}
use of org.wordpress.android.models.ReaderPost in project WordPress-Android by wordpress-mobile.
the class ReaderPostAdapter method toggleLike.
/*
* triggered when user taps the like button (textView)
*/
private void toggleLike(Context context, ReaderPostViewHolder holder, ReaderPost post) {
if (post == null || !NetworkUtils.checkConnection(context)) {
return;
}
boolean isCurrentlyLiked = ReaderPostTable.isPostLikedByCurrentUser(post);
boolean isAskingToLike = !isCurrentlyLiked;
ReaderAnim.animateLikeButton(holder.likeCount.getImageView(), isAskingToLike);
if (!ReaderPostActions.performLikeAction(post, isAskingToLike, mAccountStore.getAccount().getUserId())) {
ToastUtils.showToast(context, R.string.reader_toast_err_generic);
return;
}
if (isAskingToLike) {
AnalyticsUtils.trackWithReaderPostDetails(AnalyticsTracker.Stat.READER_ARTICLE_LIKED, post);
// Consider a like to be enough to push a page view - solves a long-standing question
// from folks who ask 'why do I have more likes than page views?'.
ReaderPostActions.bumpPageViewForPost(mSiteStore, post);
} else {
AnalyticsUtils.trackWithReaderPostDetails(AnalyticsTracker.Stat.READER_ARTICLE_LIKED, post);
}
// update post in array and on screen
int position = mPosts.indexOfPost(post);
ReaderPost updatedPost = ReaderPostTable.getBlogPost(post.blogId, post.postId, true);
if (updatedPost != null && position > -1) {
mPosts.set(position, updatedPost);
showLikes(holder, updatedPost);
}
}
use of org.wordpress.android.models.ReaderPost in project WordPress-Android by wordpress-mobile.
the class ReaderPostAdapter method renderXPost.
private void renderXPost(int position, ReaderXPostViewHolder holder) {
final ReaderPost post = getItem(position);
if (post.hasPostAvatar()) {
holder.imgAvatar.setImageUrl(GravatarUtils.fixGravatarUrl(post.getPostAvatar(), mAvatarSzSmall), WPNetworkImageView.ImageType.AVATAR);
} else {
holder.imgAvatar.showDefaultGravatarImage();
}
if (post.hasBlogImageUrl()) {
holder.imgBlavatar.setImageUrl(GravatarUtils.fixGravatarUrl(post.getBlogImageUrl(), mAvatarSzSmall), WPNetworkImageView.ImageType.BLAVATAR);
} else {
holder.imgBlavatar.showDefaultBlavatarImage();
}
holder.txtTitle.setText(ReaderXPostUtils.getXPostTitle(post));
holder.txtSubtitle.setText(ReaderXPostUtils.getXPostSubtitleHtml(post));
checkLoadMore(position);
}
use of org.wordpress.android.models.ReaderPost in project WordPress-Android by wordpress-mobile.
the class ReaderPostActions method requestPost.
private static void requestPost(RestClientUtils restClientUtils, String path, final ReaderActions.OnRequestListener requestListener) {
com.wordpress.rest.RestRequest.Listener listener = new RestRequest.Listener() {
@Override
public void onResponse(JSONObject jsonObject) {
ReaderPost post = ReaderPost.fromJson(jsonObject);
ReaderPostTable.addOrUpdatePost(post);
handlePostLikes(post, jsonObject);
if (requestListener != null) {
requestListener.onSuccess();
}
}
};
RestRequest.ErrorListener errorListener = new RestRequest.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
AppLog.e(T.READER, volleyError);
if (requestListener != null) {
int statusCode = 0;
// first try to get the error code from the JSON response, example:
// {"code":403,"headers":[{"name":"Content-Type","value":"application\/json"}],
// "body":{"error":"unauthorized","message":"User cannot access this private blog."}}
JSONObject jsonObject = VolleyUtils.volleyErrorToJSON(volleyError);
if (jsonObject != null && jsonObject.has("code")) {
statusCode = jsonObject.optInt("code");
}
if (statusCode == 0) {
statusCode = VolleyUtils.statusCodeFromVolleyError(volleyError);
}
requestListener.onFailure(statusCode);
}
}
};
AppLog.d(T.READER, "requesting post");
restClientUtils.get(path, null, null, listener, errorListener);
}
use of org.wordpress.android.models.ReaderPost in project WordPress-Android by wordpress-mobile.
the class ReaderPostActions method handleUpdatePostResponse.
private static void handleUpdatePostResponse(final ReaderPost localPost, final JSONObject jsonObject, final UpdateResultListener resultListener) {
if (jsonObject == null) {
if (resultListener != null) {
resultListener.onUpdateResult(UpdateResult.FAILED);
}
return;
}
final Handler handler = new Handler();
new Thread() {
@Override
public void run() {
ReaderPost serverPost = ReaderPost.fromJson(jsonObject);
// before calling isSamePost (since the difference in those IDs causes it to return false)
if (serverPost.feedId == 0 && localPost.feedId != 0) {
serverPost.feedId = localPost.feedId;
}
if (serverPost.feedItemId == 0 && localPost.feedItemId != 0) {
serverPost.feedItemId = localPost.feedItemId;
}
boolean hasChanges = !serverPost.isSamePost(localPost);
if (hasChanges) {
AppLog.d(T.READER, "post updated");
// copy changes over to the local post - this is done instead of simply overwriting
// the local post with the server post because the server post was retrieved using
// the read/sites/$siteId/posts/$postId endpoint which is missing some information
// https://github.com/wordpress-mobile/WordPress-Android/issues/3164
localPost.numReplies = serverPost.numReplies;
localPost.numLikes = serverPost.numLikes;
localPost.isFollowedByCurrentUser = serverPost.isFollowedByCurrentUser;
localPost.isLikedByCurrentUser = serverPost.isLikedByCurrentUser;
localPost.isCommentsOpen = serverPost.isCommentsOpen;
localPost.setTitle(serverPost.getTitle());
localPost.setText(serverPost.getText());
ReaderPostTable.addOrUpdatePost(localPost);
}
// ensures that the liking avatars are immediately available to post detail
if (handlePostLikes(serverPost, jsonObject)) {
hasChanges = true;
}
if (resultListener != null) {
final UpdateResult result = (hasChanges ? UpdateResult.CHANGED : UpdateResult.UNCHANGED);
handler.post(new Runnable() {
public void run() {
resultListener.onUpdateResult(result);
}
});
}
}
}.start();
}
Aggregations