Skip to content

Commit

Permalink
Make some refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarvis Lin committed Jun 10, 2024
1 parent 4384b1b commit e5866cb
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 38 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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 <a href="https://github.com/wordpress-mobile/WordPress-Android/pull/20937">Introduction</a>
*/
object AsyncTaskExecutor {
/**
* Execute a data loading task in the IO thread and handle the result on the main thread
*/
@JvmStatic
fun <T> executeIo(scope: CoroutineScope, backgroundTask: () -> T, callback: AsyncTaskCallback<T>) {
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 <T> executeDefault(scope: CoroutineScope, backgroundTask: () -> T, callback: AsyncTaskCallback<T>) {
execute(scope, Dispatchers.Default, backgroundTask, callback)
}

private fun <T> execute(
scope: CoroutineScope,
dispatcher: CoroutineDispatcher,
backgroundTask: () -> T,
callback: AsyncTaskCallback<T>
) {
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<T> {
fun onTaskFinished(result: T)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<RecyclerView.ViewHolder> {
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;
Expand Down Expand Up @@ -374,7 +375,7 @@ private void toggleFollowButton(
return;
}

AsyncIoTaskExecutor.execute(
AsyncTaskExecutor.executeIo(
mScope,
() -> !ReaderTagTable.isFollowedTagName(currentTag.getTagSlug()),
isAskingToFollow -> {
Expand Down

0 comments on commit e5866cb

Please sign in to comment.