diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/adapters/ReaderBlogAdapter.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/adapters/ReaderBlogAdapter.java index 5e7e0d2ae0d2..1f801c9a1517 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/adapters/ReaderBlogAdapter.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/adapters/ReaderBlogAdapter.java @@ -19,9 +19,14 @@ import org.wordpress.android.models.ReaderRecommendBlogList; import org.wordpress.android.models.ReaderRecommendedBlog; import org.wordpress.android.ui.reader.ReaderInterfaces; +import org.wordpress.android.ui.reader.actions.ReaderActions.ActionListener; +import org.wordpress.android.ui.reader.actions.ReaderBlogActions; +import org.wordpress.android.ui.reader.views.ReaderFollowButton; import org.wordpress.android.util.AppLog; import org.wordpress.android.util.AppLog.T; +import org.wordpress.android.util.NetworkUtils; import org.wordpress.android.util.StringUtils; +import org.wordpress.android.util.ToastUtils; import org.wordpress.android.util.UrlUtils; import org.wordpress.android.util.image.ImageManager; import org.wordpress.android.util.image.ImageType; @@ -32,6 +37,10 @@ import javax.inject.Inject; +import static android.view.View.GONE; +import static android.view.View.VISIBLE; +import static org.wordpress.android.BuildConfig.INFORMATION_ARCHITECTURE_AVAILABLE; + /* * adapter which shows either recommended or followed blogs - used by ReaderBlogFragment */ @@ -151,6 +160,13 @@ public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { blogHolder.mTxtUrl.setText(""); } mImageManager.load(blogHolder.mImgBlog, ImageType.BLAVATAR, blogInfo.getImageUrl()); + if (INFORMATION_ARCHITECTURE_AVAILABLE) { + blogHolder.mFollowButton.setIsFollowed(blogInfo.isFollowing); + blogHolder.mFollowButton.setOnClickListener(v -> toggleFollow( + blogHolder.itemView.getContext(), + blogHolder.mFollowButton, + blogInfo)); + } break; } @@ -181,6 +197,7 @@ class BlogViewHolder extends RecyclerView.ViewHolder { private final TextView mTxtDescription; private final TextView mTxtUrl; private final ImageView mImgBlog; + private final ReaderFollowButton mFollowButton; BlogViewHolder(View view) { super(view); @@ -189,14 +206,18 @@ class BlogViewHolder extends RecyclerView.ViewHolder { mTxtDescription = view.findViewById(R.id.text_description); mTxtUrl = view.findViewById(R.id.text_url); mImgBlog = view.findViewById(R.id.image_blog); + mFollowButton = view.findViewById(R.id.follow_button); // followed blogs don't have a description + // recommended blogs don't have a follow button switch (getBlogType()) { case FOLLOWED: - mTxtDescription.setVisibility(View.GONE); + mTxtDescription.setVisibility(GONE); + mFollowButton.setVisibility(INFORMATION_ARCHITECTURE_AVAILABLE ? VISIBLE : GONE); break; case RECOMMENDED: - mTxtDescription.setVisibility(View.VISIBLE); + mTxtDescription.setVisibility(VISIBLE); + mFollowButton.setVisibility(GONE); break; } } @@ -204,6 +225,41 @@ class BlogViewHolder extends RecyclerView.ViewHolder { private boolean mIsTaskRunning = false; + private void toggleFollow(Context context, ReaderFollowButton followButton, ReaderBlog blog) { + if (!NetworkUtils.checkConnection(context)) { + return; + } + + final boolean isAskingToFollow = !blog.isFollowing; + + // disable follow button until API call returns + followButton.setEnabled(false); + + final ActionListener listener = succeeded -> { + followButton.setEnabled(true); + if (!succeeded) { + int errResId = isAskingToFollow ? R.string.reader_toast_err_follow_blog + : R.string.reader_toast_err_unfollow_blog; + ToastUtils.showToast(context, errResId); + followButton.setIsFollowed(!isAskingToFollow); + blog.isFollowing = !isAskingToFollow; + } + }; + + final boolean result; + + if (blog.feedId != 0) { + result = ReaderBlogActions.followFeedById(blog.feedId, isAskingToFollow, listener); + } else { + result = ReaderBlogActions.followBlogById(blog.blogId, isAskingToFollow, listener); + } + + if (result) { + followButton.setIsFollowedAnimated(isAskingToFollow); + blog.isFollowing = isAskingToFollow; + } + } + private class LoadBlogsTask extends AsyncTask { private ReaderRecommendBlogList mTmpRecommendedBlogs; private ReaderBlogList mTmpFollowedBlogs; diff --git a/WordPress/src/main/res/layout/reader_listitem_blog.xml b/WordPress/src/main/res/layout/reader_listitem_blog.xml index aeaa25fb9bdc..5aeba08485b0 100644 --- a/WordPress/src/main/res/layout/reader_listitem_blog.xml +++ b/WordPress/src/main/res/layout/reader_listitem_blog.xml @@ -4,24 +4,26 @@ list item which shows a recommended or followed blog - see ReaderBlogAdapter --> + android:paddingBottom="@dimen/margin_large"> + android:contentDescription="@null" + tools:src="@drawable/bg_rectangle_neutral_10_globe_32dp" /> + tools:text="text_title" /> + +