From 4384b1b1d95ff26b1031cba8ea296fdd82b99d7d Mon Sep 17 00:00:00 2001 From: Jarvis Lin Date: Thu, 6 Jun 2024 16:12:31 +0200 Subject: [PATCH] Fix a potential issue related to lifecycle, and rename a helper class to emphasize the implementation is related to I/O --- ...syncTaskHandler.kt => AsyncIoTaskExecutor.kt} | 16 +++++++++------- .../ui/reader/ReaderPostListFragment.java | 4 +++- .../ui/reader/adapters/ReaderPostAdapter.java | 11 ++++++++--- 3 files changed, 20 insertions(+), 11 deletions(-) rename WordPress/src/main/java/org/wordpress/android/datasets/{AsyncTaskHandler.kt => AsyncIoTaskExecutor.kt} (56%) diff --git a/WordPress/src/main/java/org/wordpress/android/datasets/AsyncTaskHandler.kt b/WordPress/src/main/java/org/wordpress/android/datasets/AsyncIoTaskExecutor.kt similarity index 56% rename from WordPress/src/main/java/org/wordpress/android/datasets/AsyncTaskHandler.kt rename to WordPress/src/main/java/org/wordpress/android/datasets/AsyncIoTaskExecutor.kt index b1bdfecde5a8..8b3bd9f15403 100644 --- a/WordPress/src/main/java/org/wordpress/android/datasets/AsyncTaskHandler.kt +++ b/WordPress/src/main/java/org/wordpress/android/datasets/AsyncIoTaskExecutor.kt @@ -6,18 +6,20 @@ import kotlinx.coroutines.launch import kotlinx.coroutines.withContext /** - * Helper class to handle async tasks by using coroutines + * Helper class to handle asynchronous I/O tasks using coroutines * @see Introduction */ -object AsyncTaskHandler { +object AsyncIoTaskExecutor { /** - * Load data in the background and handle the result on the main thread + * Execute a data loading task in the IO thread and handle the result on the main thread */ @JvmStatic - fun load(backgroundTask: () -> T, callback: AsyncTaskCallback) { - CoroutineScope(Dispatchers.IO).launch { + fun execute(scope: CoroutineScope, backgroundTask: () -> T, callback: IoTaskResultCallback) { + scope.launch { // handle the background task - val result = backgroundTask() + val result = withContext(Dispatchers.IO) { + backgroundTask() + } withContext(Dispatchers.Main) { // handle the result on the main thread @@ -26,7 +28,7 @@ object AsyncTaskHandler { } } - interface AsyncTaskCallback { + interface IoTaskResultCallback { fun onTaskFinished(result: T) } } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java index d62a859bfdeb..5702987f5286 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java @@ -153,6 +153,7 @@ import javax.inject.Inject; +import static androidx.lifecycle.LifecycleOwnerKt.getLifecycleScope; import static org.wordpress.android.fluxc.generated.AccountActionBuilder.newUpdateSubscriptionNotificationPostAction; import static org.wordpress.android.ui.reader.ReaderActivityLauncher.OpenUrlType.INTERNAL; @@ -1923,7 +1924,8 @@ private ReaderPostAdapter getPostAdapter() { mImageManager, mUiHelpers, mNetworkUtilsWrapper, - mIsTopLevel + mIsTopLevel, + getLifecycleScope(this) ); mPostAdapter.setOnFollowListener(this); mPostAdapter.setOnPostSelectedListener(this); 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 54b22755cc1a..68975cec5bf2 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 @@ -15,12 +15,13 @@ import android.widget.TextView; import androidx.annotation.NonNull; +import androidx.lifecycle.LifecycleCoroutineScope; import androidx.recyclerview.widget.RecyclerView; 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.AsyncIoTaskExecutor; import org.wordpress.android.datasets.ReaderPostTable; import org.wordpress.android.datasets.ReaderTagTable; import org.wordpress.android.fluxc.store.AccountStore; @@ -87,6 +88,7 @@ public class ReaderPostAdapter extends RecyclerView.Adapter !ReaderTagTable.isFollowedTagName(currentTag.getTagSlug()), isAskingToFollow -> { final String slugForTracking = currentTag.getTagSlug(); @@ -688,7 +691,8 @@ public ReaderPostAdapter( ImageManager imageManager, UiHelpers uiHelpers, @NonNull final NetworkUtilsWrapper networkUtilsWrapper, - boolean isMainReader + boolean isMainReader, + LifecycleCoroutineScope scope ) { super(); ((WordPress) context.getApplicationContext()).component().inject(this); @@ -699,6 +703,7 @@ public ReaderPostAdapter( mNetworkUtilsWrapper = networkUtilsWrapper; mAvatarSzSmall = context.getResources().getDimensionPixelSize(R.dimen.avatar_sz_small); mIsMainReader = isMainReader; + mScope = scope; int displayWidth = DisplayUtils.getWindowPixelWidth(context); int cardMargin = context.getResources().getDimensionPixelSize(R.dimen.reader_card_margin);