Skip to content

Commit

Permalink
Fix PR comment: tag was being deleted on success but never added again
Browse files Browse the repository at this point in the history
  • Loading branch information
RenanLukas committed Feb 9, 2024
1 parent 920d4f8 commit a02167a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Context> mWeakContext;
private final ReaderTagList mTags = new ReaderTagList();
private TagDeletedListener mTagDeletedListener;
private TagAddedListener mTagAddedListener;
private ReaderInterfaces.DataLoadedListener mDataLoadedListener;
private final Map<String, Boolean> mBlogIdIsFollowedMap = new HashMap<>();
private final Map<String, Boolean> mTagSlugIsFollowedMap = new HashMap<>();

public ReaderTagAdapter(Context context) {
super();
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -125,28 +134,32 @@ 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);
refresh();
}
};

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);
}
}
}

Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit a02167a

Please sign in to comment.