From 83c6a669102867289780ca5f28aa143522685ed9 Mon Sep 17 00:00:00 2001 From: Jarvis Lin Date: Tue, 4 Jun 2024 11:42:27 +0200 Subject: [PATCH 1/3] Create an alternative of AsyncTask for handling thread-switching requirements --- .../android/datasets/AsyncTaskHandler.kt | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 WordPress/src/main/java/org/wordpress/android/datasets/AsyncTaskHandler.kt 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..54888fd52711 --- /dev/null +++ b/WordPress/src/main/java/org/wordpress/android/datasets/AsyncTaskHandler.kt @@ -0,0 +1,32 @@ +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 + */ +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) + } +} + From 3eb24e4ea17e6c3ccdcc557f72ad422280d13178 Mon Sep 17 00:00:00 2001 From: Jarvis Lin Date: Tue, 4 Jun 2024 16:44:35 +0200 Subject: [PATCH 2/3] Wrap the database-accessing code with AsyncTaskHandler --- .../ui/reader/adapters/ReaderPostAdapter.java | 76 ++++++++++--------- 1 file changed, 40 insertions(+), 36 deletions(-) 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) { From 032d50347a9f1d14c69c9616e10b5d436ea0ddc2 Mon Sep 17 00:00:00 2001 From: Jarvis Lin Date: Tue, 4 Jun 2024 17:32:17 +0200 Subject: [PATCH 3/3] Add a comment --- .../main/java/org/wordpress/android/datasets/AsyncTaskHandler.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/WordPress/src/main/java/org/wordpress/android/datasets/AsyncTaskHandler.kt b/WordPress/src/main/java/org/wordpress/android/datasets/AsyncTaskHandler.kt index 54888fd52711..b1bdfecde5a8 100644 --- a/WordPress/src/main/java/org/wordpress/android/datasets/AsyncTaskHandler.kt +++ b/WordPress/src/main/java/org/wordpress/android/datasets/AsyncTaskHandler.kt @@ -7,6 +7,7 @@ import kotlinx.coroutines.withContext /** * Helper class to handle async tasks by using coroutines + * @see Introduction */ object AsyncTaskHandler { /**