Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add follow/unfollow button to the Followed Sites tab on Reader's management section #11219

Merged
merged 5 commits into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*/
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -189,21 +206,60 @@ 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;
}
}
}

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<Void, Void, Boolean> {
private ReaderRecommendBlogList mTmpRecommendedBlogs;
private ReaderBlogList mTmpFollowedBlogs;
Expand Down
35 changes: 23 additions & 12 deletions WordPress/src/main/res/layout/reader_listitem_blog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@
list item which shows a recommended or followed blog - see ReaderBlogAdapter
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:selectableItemBackground"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingBottom="@dimen/margin_large"
android:paddingStart="@dimen/margin_extra_large"
android:paddingTop="@dimen/margin_large"
android:paddingEnd="@dimen/margin_extra_large"
android:paddingStart="@dimen/margin_extra_large">
android:paddingBottom="@dimen/margin_large">

<ImageView
android:id="@+id/image_blog"
android:layout_width="@dimen/avatar_sz_medium"
android:layout_height="@dimen/avatar_sz_medium"
android:layout_marginEnd="@dimen/margin_extra_large"
android:layout_weight="0"
android:layout_marginEnd="@dimen/margin_large"
android:contentDescription="@null"/>
android:contentDescription="@null"
tools:src="@drawable/bg_rectangle_neutral_10_globe_32dp" />

<LinearLayout
android:id="@+id/layout_content"
Expand All @@ -31,42 +33,51 @@
android:orientation="vertical">

<org.wordpress.android.widgets.WPTextView
android:textAlignment="viewStart"
android:gravity="start"
android:id="@+id/text_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/margin_medium"
android:ellipsize="end"
android:gravity="start"
android:maxLines="1"
android:textAlignment="viewStart"
android:textColor="?attr/wpColorText"
android:textSize="@dimen/text_sz_medium"
tools:text="text_title"
android:layout_marginEnd="@dimen/margin_medium"/>
tools:text="text_title" />

<org.wordpress.android.widgets.WPTextView
android:textAlignment="viewStart"
android:gravity="start"
android:id="@+id/text_url"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/margin_medium"
android:ellipsize="end"
android:gravity="start"
android:maxLines="1"
android:textAlignment="viewStart"
android:textColor="@color/link_reader"
android:textSize="@dimen/text_sz_small"
tools:text="text_url" />

<org.wordpress.android.widgets.WPTextView
android:textAlignment="viewStart"
android:gravity="start"
android:id="@+id/text_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/margin_medium"
android:ellipsize="end"
android:gravity="start"
android:maxLines="2"
android:textAlignment="viewStart"
android:textColor="@color/neutral_40"
android:textSize="@dimen/text_sz_small"
tools:text="text_description" />

</LinearLayout>

<org.wordpress.android.ui.reader.views.ReaderFollowButton
android:id="@+id/follow_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/margin_medium"
app:wpShowFollowButtonCaption="false" />

</LinearLayout>