diff --git a/WordPress/src/main/java/org/wordpress/android/datasets/AsyncTaskHandler.kt b/WordPress/src/main/java/org/wordpress/android/datasets/AsyncTaskHandler.kt
new file mode 100644
index 000000000000..b1bdfecde5a8
--- /dev/null
+++ b/WordPress/src/main/java/org/wordpress/android/datasets/AsyncTaskHandler.kt
@@ -0,0 +1,33 @@
+package org.wordpress.android.datasets
+
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
+
+/**
+ * Helper class to handle async tasks by using coroutines
+ * @see Introduction
+ */
+object AsyncTaskHandler {
+ /**
+ * Load data in the background and handle the result on the main thread
+ */
+ @JvmStatic
+ fun load(backgroundTask: () -> T, callback: AsyncTaskCallback) {
+ CoroutineScope(Dispatchers.IO).launch {
+ // handle the background task
+ val result = backgroundTask()
+
+ withContext(Dispatchers.Main) {
+ // handle the result on the main thread
+ callback.onTaskFinished(result)
+ }
+ }
+ }
+
+ interface AsyncTaskCallback {
+ fun onTaskFinished(result: T)
+ }
+}
+
diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/adapters/ReaderPostAdapter.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/adapters/ReaderPostAdapter.java
index af30a157d08c..54b22755cc1a 100644
--- a/WordPress/src/main/java/org/wordpress/android/ui/reader/adapters/ReaderPostAdapter.java
+++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/adapters/ReaderPostAdapter.java
@@ -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;
@@ -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) {