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 View getView(final int position, View convertView, final ViewGroup parent) { final BlogViewHolder holder; if (convertView == null || !(convertView.getTag() instanceof BlogViewHolder)) { convertView = mInflater.inflate(R.layout.reader_listitem_blog, parent, false); holder = new BlogViewHolder(convertView); convertView.setTag(holder); } else { holder = (BlogViewHolder) convertView.getTag(); } final long blogId; final String blogUrl; final boolean isFollowing; switch (getBlogType()) { case RECOMMENDED: final ReaderRecommendedBlog blog = (ReaderRecommendedBlog) getItem(position); blogId = blog.blogId; blogUrl = blog.getBlogUrl(); isFollowing = ReaderBlogTable.isFollowedBlog(blogId, blogUrl); holder.txtTitle.setText(blog.getTitle()); holder.txtDescription.setText(blog.getReason()); holder.txtUrl.setText(UrlUtils.getDomainFromUrl(blogUrl)); holder.imgBlog.setImageUrl(blog.getImageUrl(), WPNetworkImageView.ImageType.AVATAR); break; case FOLLOWED: final ReaderBlog blogInfo = (ReaderBlog) getItem(position); blogId = blogInfo.blogId; blogUrl = blogInfo.getUrl(); isFollowing = blogInfo.isFollowing; String domain = UrlUtils.getDomainFromUrl(blogUrl); if (blogInfo.hasName()) { holder.txtTitle.setText(blogInfo.getName()); } else { holder.txtTitle.setText(domain); } holder.txtUrl.setText(domain); break; default: blogId = 0; blogUrl = null; isFollowing = false; break; } // show the correct following status ReaderUtils.showFollowStatus(holder.txtFollow, isFollowing); holder.txtFollow.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { AniUtils.zoomAction(holder.txtFollow); changeFollowStatus(holder.txtFollow, position, !isFollowing); } }); // show blog preview when view is clicked convertView.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { // make sure we have either the blog id or url if (blogId != 0 || !TextUtils.isEmpty(blogUrl)) { ReaderActivityLauncher.showReaderBlogPreview(getContext(), blogId, blogUrl); } } }); return convertView; }