diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderSubsActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderSubsActivity.java index 3a6de1069ea2..955a08936ce5 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderSubsActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderSubsActivity.java @@ -70,7 +70,7 @@ * followed tags and followed blogs */ public class ReaderSubsActivity extends LocaleAwareActivity - implements ReaderTagAdapter.TagDeletedListener { + implements ReaderTagAdapter.TagDeletedListener, ReaderTagAdapter.TagAddedListener { private EditText mEditAdd; private FloatingActionButton mFabButton; private ReaderFollowButton mBtnAdd; @@ -498,6 +498,14 @@ public void onTagDeleted(ReaderTag tag) { } } + @Override public void onTagAdded(@NonNull ReaderTag readerTag) { + mReaderTracker.trackTag( + AnalyticsTracker.Stat.READER_TAG_FOLLOWED, + readerTag.getTagSlug(), + ReaderTracker.SOURCE_SETTINGS + ); + } + /* * return to the previously selected page in the viewPager */ diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderTagFragment.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderTagFragment.java index b9a6ab66e30a..f767642e9c8f 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderTagFragment.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderTagFragment.java @@ -25,7 +25,8 @@ /* * fragment hosted by ReaderSubsActivity which shows followed tags */ -public class ReaderTagFragment extends Fragment implements ReaderTagAdapter.TagDeletedListener { +public class ReaderTagFragment extends Fragment + implements ReaderTagAdapter.TagDeletedListener, ReaderTagAdapter.TagAddedListener { private ReaderRecyclerView mRecyclerView; private ReaderTagAdapter mTagAdapter; @@ -103,6 +104,7 @@ private ReaderTagAdapter getTagAdapter() { Context context = WPActivityUtils.getThemedContext(getActivity()); mTagAdapter = new ReaderTagAdapter(context); mTagAdapter.setTagDeletedListener(this); + mTagAdapter.setTagAddedListener(this); mTagAdapter.setDataLoadedListener(isEmpty -> { checkEmptyView(); if (mIsFirstDataLoaded) { @@ -133,4 +135,10 @@ public void onTagDeleted(ReaderTag tag) { ((ReaderTagAdapter.TagDeletedListener) getActivity()).onTagDeleted(tag); } } + + @Override public void onTagAdded(@NonNull ReaderTag readerTag) { + if (getActivity() instanceof ReaderTagAdapter.TagDeletedListener) { + ((ReaderTagAdapter.TagAddedListener) getActivity()).onTagAdded(readerTag); + } + } } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/adapters/ReaderTagAdapter.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/adapters/ReaderTagAdapter.java index 54b62b95bca9..955783b7a630 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/adapters/ReaderTagAdapter.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/adapters/ReaderTagAdapter.java @@ -38,12 +38,17 @@ public interface TagDeletedListener { void onTagDeleted(ReaderTag tag); } + public interface TagAddedListener { + void onTagAdded(@NonNull ReaderTag readerTag); + } + @Inject AccountStore mAccountStore; private final WeakReference mWeakContext; private final ReaderTagList mTags = new ReaderTagList(); private TagDeletedListener mTagDeletedListener; + private TagAddedListener mTagAddedListener; private ReaderInterfaces.DataLoadedListener mDataLoadedListener; - private final Map mBlogIdIsFollowedMap = new HashMap<>(); + private final Map mTagSlugIsFollowedMap = new HashMap<>(); public ReaderTagAdapter(Context context) { super(); @@ -56,6 +61,10 @@ public void setTagDeletedListener(TagDeletedListener listener) { mTagDeletedListener = listener; } + public void setTagAddedListener(@NonNull final TagAddedListener listener) { + mTagAddedListener = listener; + } + public void setDataLoadedListener(ReaderInterfaces.DataLoadedListener listener) { mDataLoadedListener = listener; } @@ -106,7 +115,7 @@ public ReaderTagList getItems() { public ReaderTagList getSubscribedItems() { final ReaderTagList readerSubscribedTagsList = new ReaderTagList(); for (final ReaderTag readerTag : mTags) { - if (Boolean.TRUE.equals(mBlogIdIsFollowedMap.get(readerTag.getTagSlug()))) { + if (Boolean.TRUE.equals(mTagSlugIsFollowedMap.get(readerTag.getTagSlug()))) { readerSubscribedTagsList.add(readerTag); } } @@ -125,15 +134,15 @@ private void performDeleteTag(@NonNull ReaderTag tag, @NonNull final ReaderFollo return; } - final boolean currentFollowValue = Boolean.TRUE.equals(mBlogIdIsFollowedMap.get(tag.getTagSlug())); - final boolean newFollowValue = !currentFollowValue; - readerFollowButton.setIsFollowed(newFollowValue); + final boolean isFollowingCurrent = Boolean.TRUE.equals(mTagSlugIsFollowedMap.get(tag.getTagSlug())); + final boolean isFollowingNew = !isFollowingCurrent; + readerFollowButton.setIsFollowed(isFollowingNew); // Disable follow button until API call returns readerFollowButton.setEnabled(false); ReaderActions.ActionListener actionListener = succeeded -> { - mBlogIdIsFollowedMap.put(tag.getTagSlug(), newFollowValue); + mTagSlugIsFollowedMap.put(tag.getTagSlug(), isFollowingNew); readerFollowButton.setEnabled(true); if (!succeeded && hasContext()) { ToastUtils.showToast(getContext(), R.string.reader_toast_err_removing_tag); @@ -141,12 +150,16 @@ private void performDeleteTag(@NonNull ReaderTag tag, @NonNull final ReaderFollo } }; - boolean success = ReaderTagActions.deleteTag(tag, actionListener, mAccountStore.hasAccessToken()); - - if (success) { - if (mTagDeletedListener != null) { + if (isFollowingCurrent) { + boolean success = ReaderTagActions.deleteTag(tag, actionListener, mAccountStore.hasAccessToken()); + if (success && mTagDeletedListener != null) { mTagDeletedListener.onTagDeleted(tag); } + } else { + boolean success = ReaderTagActions.addTag(tag, actionListener, mAccountStore.hasAccessToken()); + if (success && mTagAddedListener != null) { + mTagAddedListener.onTagAdded(tag); + } } } @@ -190,11 +203,9 @@ protected void onPostExecute(ReaderTagList tagList) { if (tagList != null && !tagList.isSameList(mTags)) { mTags.clear(); mTags.addAll(tagList); - mBlogIdIsFollowedMap.clear(); + mTagSlugIsFollowedMap.clear(); for (final ReaderTag tag : mTags) { - if (!mBlogIdIsFollowedMap.containsKey(tag.getTagSlug())) { - mBlogIdIsFollowedMap.put(tag.getTagSlug(), true); - } + mTagSlugIsFollowedMap.put(tag.getTagSlug(), true); } notifyDataSetChanged(); }