private void changeFollowStatus( final TextView txtFollow, final int position, final boolean isAskingToFollow) { if (!isPositionValid(position)) { return; } final long blogId; final String blogUrl; switch (getBlogType()) { case RECOMMENDED: ReaderRecommendedBlog blog = mRecommendedBlogs.get(position); blogId = blog.blogId; blogUrl = blog.getBlogUrl(); break; case FOLLOWED: ReaderBlog info = mFollowedBlogs.get(position); blogId = info.blogId; blogUrl = info.getUrl(); break; default: return; } ReaderActions.ActionListener actionListener = new ReaderActions.ActionListener() { @Override public void onActionResult(boolean succeeded) { if (!succeeded && getContext() != null) { int resId = (isAskingToFollow ? R.string.reader_toast_err_follow_blog : R.string.reader_toast_err_unfollow_blog); ToastUtils.showToast(getContext(), resId); ReaderUtils.showFollowStatus(txtFollow, !isAskingToFollow); checkFollowStatus(); } } }; if (ReaderBlogActions.performFollowAction(blogId, blogUrl, isAskingToFollow, actionListener)) { if (getBlogType() == ReaderBlogType.FOLLOWED) { mFollowedBlogs.get(position).isFollowing = isAskingToFollow; } ReaderUtils.showFollowStatus(txtFollow, isAskingToFollow); notifyDataSetChanged(); // <-- required for getView() to know correct follow status if (mFollowListener != null) { mFollowListener.onFollowBlogChanged(); } } }
@Override public Object getItem(int position) { switch (getBlogType()) { case RECOMMENDED: return mRecommendedBlogs.get(position); case FOLLOWED: return mFollowedBlogs.get(position); default: return null; } }
@Override public int getCount() { switch (getBlogType()) { case RECOMMENDED: return mRecommendedBlogs.size(); case FOLLOWED: return mFollowedBlogs.size(); default: return 0; } }
@Override protected void onPostExecute(Boolean result) { if (result) { switch (getBlogType()) { case RECOMMENDED: mRecommendedBlogs = (ReaderRecommendBlogList) (tmpRecommendedBlogs.clone()); break; case FOLLOWED: mFollowedBlogs = (ReaderBlogList) (tmpFollowedBlogs.clone()); break; } notifyDataSetChanged(); } mIsTaskRunning = false; }