Skip to content

Commit

Permalink
Wrap the database-accessing code with AsyncTaskHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarvis Lin committed Jun 4, 2024
1 parent 83c6a66 commit 3eb24e4
Showing 1 changed file with 40 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.wordpress.android.R;
import org.wordpress.android.WordPress;
import org.wordpress.android.analytics.AnalyticsTracker;
import org.wordpress.android.datasets.AsyncTaskHandler;
import org.wordpress.android.datasets.ReaderPostTable;
import org.wordpress.android.datasets.ReaderTagTable;
import org.wordpress.android.fluxc.store.AccountStore;
Expand Down Expand Up @@ -371,44 +372,47 @@ private void toggleFollowButton(
return;
}

final boolean isAskingToFollow = !ReaderTagTable.isFollowedTagName(currentTag.getTagSlug());

final String slugForTracking = currentTag.getTagSlug();
AsyncTaskHandler.load(
() -> !ReaderTagTable.isFollowedTagName(currentTag.getTagSlug()),
isAskingToFollow -> {
final String slugForTracking = currentTag.getTagSlug();

ReaderActions.ActionListener listener = succeeded -> {
if (!succeeded) {
int errResId = isAskingToFollow ? R.string.reader_toast_err_adding_tag
: R.string.reader_toast_err_removing_tag;
ToastUtils.showToast(context, errResId);
} else {
if (isAskingToFollow) {
mReaderTracker.trackTag(
AnalyticsTracker.Stat.READER_TAG_FOLLOWED,
slugForTracking,
mSource
);
} else {
mReaderTracker.trackTag(
AnalyticsTracker.Stat.READER_TAG_UNFOLLOWED,
slugForTracking,
mSource
);
}
}
renderTagHeader(currentTag, tagHolder, true);
};

boolean success;
boolean isLoggedIn = mAccountStore.hasAccessToken();
if (isAskingToFollow) {
success = ReaderTagActions.addTag(mCurrentTag, listener, isLoggedIn);
} else {
success = ReaderTagActions.deleteTag(mCurrentTag, listener, isLoggedIn);
}

ReaderActions.ActionListener listener = succeeded -> {
if (!succeeded) {
int errResId = isAskingToFollow ? R.string.reader_toast_err_adding_tag
: R.string.reader_toast_err_removing_tag;
ToastUtils.showToast(context, errResId);
} else {
if (isAskingToFollow) {
mReaderTracker.trackTag(
AnalyticsTracker.Stat.READER_TAG_FOLLOWED,
slugForTracking,
mSource
);
} else {
mReaderTracker.trackTag(
AnalyticsTracker.Stat.READER_TAG_UNFOLLOWED,
slugForTracking,
mSource
);
if (isLoggedIn && success) {
renderTagHeader(currentTag, tagHolder, false);
}
}
}
renderTagHeader(currentTag, tagHolder, true);
};

boolean success;
boolean isLoggedIn = mAccountStore.hasAccessToken();
if (isAskingToFollow) {
success = ReaderTagActions.addTag(mCurrentTag, listener, isLoggedIn);
} else {
success = ReaderTagActions.deleteTag(mCurrentTag, listener, isLoggedIn);
}

if (isLoggedIn && success) {
renderTagHeader(currentTag, tagHolder, false);
}
);
}

private void renderXPost(int position, ReaderXPostViewHolder holder) {
Expand Down

0 comments on commit 3eb24e4

Please sign in to comment.