diff --git a/WordPress/src/main/java/org/wordpress/android/datasets/AsyncIoTaskExecutor.kt b/WordPress/src/main/java/org/wordpress/android/datasets/AsyncIoTaskExecutor.kt deleted file mode 100644 index 8b3bd9f15403..000000000000 --- a/WordPress/src/main/java/org/wordpress/android/datasets/AsyncIoTaskExecutor.kt +++ /dev/null @@ -1,35 +0,0 @@ -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 asynchronous I/O tasks using coroutines - * @see Introduction - */ -object AsyncIoTaskExecutor { - /** - * Execute a data loading task in the IO thread and handle the result on the main thread - */ - @JvmStatic - fun execute(scope: CoroutineScope, backgroundTask: () -> T, callback: IoTaskResultCallback) { - scope.launch { - // handle the background task - val result = withContext(Dispatchers.IO) { - backgroundTask() - } - - withContext(Dispatchers.Main) { - // handle the result on the main thread - callback.onTaskFinished(result) - } - } - } - - interface IoTaskResultCallback { - fun onTaskFinished(result: T) - } -} - diff --git a/WordPress/src/main/java/org/wordpress/android/datasets/AsyncTaskExecutor.kt b/WordPress/src/main/java/org/wordpress/android/datasets/AsyncTaskExecutor.kt new file mode 100644 index 000000000000..5b63e65e5b8e --- /dev/null +++ b/WordPress/src/main/java/org/wordpress/android/datasets/AsyncTaskExecutor.kt @@ -0,0 +1,51 @@ +package org.wordpress.android.datasets + +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext + +/** + * Helper class to handle asynchronous I/O tasks using coroutines + * @see Introduction + */ +object AsyncTaskExecutor { + /** + * Execute a data loading task in the IO thread and handle the result on the main thread + */ + @JvmStatic + fun executeIo(scope: CoroutineScope, backgroundTask: () -> T, callback: AsyncTaskCallback) { + execute(scope, Dispatchers.IO, backgroundTask, callback) + } + + /** + * Execute a data loading task in the default thread and handle the result on the main thread + */ + @JvmStatic + fun executeDefault(scope: CoroutineScope, backgroundTask: () -> T, callback: AsyncTaskCallback) { + execute(scope, Dispatchers.Default, backgroundTask, callback) + } + + private fun execute( + scope: CoroutineScope, + dispatcher: CoroutineDispatcher, + backgroundTask: () -> T, + callback: AsyncTaskCallback + ) { + scope.launch(dispatcher) { + // 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 68975cec5bf2..6dc4ad0d99d3 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 @@ -21,7 +21,7 @@ import org.wordpress.android.R; import org.wordpress.android.WordPress; import org.wordpress.android.analytics.AnalyticsTracker; -import org.wordpress.android.datasets.AsyncIoTaskExecutor; +import org.wordpress.android.datasets.AsyncTaskExecutor; import org.wordpress.android.datasets.ReaderPostTable; import org.wordpress.android.datasets.ReaderTagTable; import org.wordpress.android.fluxc.store.AccountStore; @@ -83,12 +83,13 @@ import kotlin.jvm.functions.Function1; import kotlin.jvm.functions.Function2; import kotlin.jvm.functions.Function3; +import kotlinx.coroutines.CoroutineScope; public class ReaderPostAdapter extends RecyclerView.Adapter { private final ImageManager mImageManager; private final UiHelpers mUiHelpers; private final NetworkUtilsWrapper mNetworkUtilsWrapper; - private final LifecycleCoroutineScope mScope; + private final CoroutineScope mScope; private ReaderTag mCurrentTag; private long mCurrentBlogId; private long mCurrentFeedId; @@ -374,7 +375,7 @@ private void toggleFollowButton( return; } - AsyncIoTaskExecutor.execute( + AsyncTaskExecutor.executeIo( mScope, () -> !ReaderTagTable.isFollowedTagName(currentTag.getTagSlug()), isAskingToFollow -> {