/* * changes the like on the passed post */ private void togglePostLike() { if (!isAdded() || !hasPost() || !NetworkUtils.checkConnection(getActivity())) { return; } boolean isAskingToLike = !mPost.isLikedByCurrentUser; ReaderIconCountView likeCount = (ReaderIconCountView) getView().findViewById(R.id.count_likes); likeCount.setSelected(isAskingToLike); ReaderAnim.animateLikeButton(likeCount.getImageView(), isAskingToLike); if (!ReaderPostActions.performLikeAction(mPost, isAskingToLike)) { likeCount.setSelected(!isAskingToLike); return; } // get the post again since it has changed, then refresh to show changes mPost = ReaderPostTable.getPost(mBlogId, mPostId, false); refreshLikes(); refreshIconCounts(); if (isAskingToLike) { AnalyticsUtils.trackWithBlogDetails(AnalyticsTracker.Stat.READER_ARTICLE_LIKED, mBlogId); } else { AnalyticsUtils.trackWithBlogDetails(AnalyticsTracker.Stat.READER_ARTICLE_UNLIKED, mBlogId); } }
/* * get the latest version of this post */ private void updatePost() { if (!hasPost() || !mPost.isWP()) { return; } final int numLikesBefore = ReaderLikeTable.getNumLikesForPost(mPost); ReaderActions.UpdateResultListener resultListener = new ReaderActions.UpdateResultListener() { @Override public void onUpdateResult(ReaderActions.UpdateResult result) { if (!isAdded()) { return; } // if the post has changed, reload it from the db and update the like/comment counts if (result.isNewOrChanged()) { mPost = ReaderPostTable.getPost(mBlogId, mPostId, false); refreshIconCounts(); } // refresh likes if necessary - done regardless of whether the post has changed // since it's possible likes weren't stored until the post was updated if (result != ReaderActions.UpdateResult.FAILED && numLikesBefore != ReaderLikeTable.getNumLikesForPost(mPost)) { refreshLikes(); } } }; ReaderPostActions.updatePost(mPost, resultListener); }
/* * called when the post doesn't exist in local db, need to get it from server */ private void requestPost() { final ProgressBar progress = (ProgressBar) getView().findViewById(R.id.progress_loading); progress.setVisibility(View.VISIBLE); progress.bringToFront(); ReaderActions.ActionListener actionListener = new ReaderActions.ActionListener() { @Override public void onActionResult(boolean succeeded) { if (isAdded()) { progress.setVisibility(View.GONE); if (succeeded) { showPost(); } else { postFailed(); } } } }; ReaderPostActions.requestPost(mBlogId, mPostId, actionListener); }
private void updatePostsWithTag( final String tagName, final ReaderActions.RequestDataAction updateAction, RefreshType refreshType) { if (TextUtils.isEmpty(tagName)) return; unscheduleAutoUpdate(); if (!NetworkUtils.isNetworkAvailable(getActivity())) { AppLog.i(T.READER, "network unavailable, rescheduling reader update"); scheduleAutoUpdate(); return; } setIsUpdating(true, updateAction); setEmptyTitleAndDecriptionForCurrentTag(); // if this is "Posts I Like" or "Blogs I Follow" and it's a manual refresh (user tapped refresh // icon), // refresh the posts so posts that were unliked/unfollowed no longer appear if (refreshType == RefreshType.MANUAL && isCurrentTagName(tagName)) { if (tagName.equals(ReaderTag.TAG_NAME_LIKED) || tagName.equals(ReaderTag.TAG_NAME_FOLLOWING)) refreshPosts(); } ReaderPostActions.updatePostsWithTag( tagName, updateAction, new ReaderActions.UpdateResultAndCountListener() { @Override public void onUpdateResult(ReaderActions.UpdateResult result, int numNewPosts) { if (!hasActivity()) { AppLog.w(T.READER, "volley response when fragment has no activity"); // this fragment is no longer valid, so send a broadcast that tells the host // ReaderActivity that it needs to refresh the list of posts - this // situation occurs when the user rotates the device while the update is // still in progress if (numNewPosts > 0) LocalBroadcastManager.getInstance(WordPress.getContext()) .sendBroadcast(new Intent(ReaderActivity.ACTION_REFRESH_POSTS)); return; } setIsUpdating(false, updateAction); if (result == ReaderActions.UpdateResult.CHANGED && numNewPosts > 0 && isCurrentTagName(tagName)) { // if we loaded new posts and posts are already displayed, show the "new posts" // bar rather than immediately refreshing the list if (!isPostAdapterEmpty() && updateAction == ReaderActions.RequestDataAction.LOAD_NEWER) { showNewPostsBar(numNewPosts); } else { refreshPosts(); } } else { // update empty view title and description if the the post list is empty setEmptyTitleAndDecriptionForCurrentTag(); } // schedule the next update in this tag if (result != ReaderActions.UpdateResult.FAILED) scheduleAutoUpdate(); } }); }