Skip to content

Commit e5866cb

Browse files
author
Jarvis Lin
committed
Make some refactors
1 parent 4384b1b commit e5866cb

3 files changed

Lines changed: 55 additions & 38 deletions

File tree

WordPress/src/main/java/org/wordpress/android/datasets/AsyncIoTaskExecutor.kt

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.wordpress.android.datasets
2+
3+
import kotlinx.coroutines.CoroutineDispatcher
4+
import kotlinx.coroutines.CoroutineScope
5+
import kotlinx.coroutines.Dispatchers
6+
import kotlinx.coroutines.launch
7+
import kotlinx.coroutines.withContext
8+
9+
/**
10+
* Helper class to handle asynchronous I/O tasks using coroutines
11+
* @see <a href="https://github.com/wordpress-mobile/WordPress-Android/pull/20937">Introduction</a>
12+
*/
13+
object AsyncTaskExecutor {
14+
/**
15+
* Execute a data loading task in the IO thread and handle the result on the main thread
16+
*/
17+
@JvmStatic
18+
fun <T> executeIo(scope: CoroutineScope, backgroundTask: () -> T, callback: AsyncTaskCallback<T>) {
19+
execute(scope, Dispatchers.IO, backgroundTask, callback)
20+
}
21+
22+
/**
23+
* Execute a data loading task in the default thread and handle the result on the main thread
24+
*/
25+
@JvmStatic
26+
fun <T> executeDefault(scope: CoroutineScope, backgroundTask: () -> T, callback: AsyncTaskCallback<T>) {
27+
execute(scope, Dispatchers.Default, backgroundTask, callback)
28+
}
29+
30+
private fun <T> execute(
31+
scope: CoroutineScope,
32+
dispatcher: CoroutineDispatcher,
33+
backgroundTask: () -> T,
34+
callback: AsyncTaskCallback<T>
35+
) {
36+
scope.launch(dispatcher) {
37+
// handle the background task
38+
val result = backgroundTask()
39+
40+
withContext(Dispatchers.Main) {
41+
// handle the result on the main thread
42+
callback.onTaskFinished(result)
43+
}
44+
}
45+
}
46+
47+
interface AsyncTaskCallback<T> {
48+
fun onTaskFinished(result: T)
49+
}
50+
}
51+

WordPress/src/main/java/org/wordpress/android/ui/reader/adapters/ReaderPostAdapter.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.wordpress.android.R;
2222
import org.wordpress.android.WordPress;
2323
import org.wordpress.android.analytics.AnalyticsTracker;
24-
import org.wordpress.android.datasets.AsyncIoTaskExecutor;
24+
import org.wordpress.android.datasets.AsyncTaskExecutor;
2525
import org.wordpress.android.datasets.ReaderPostTable;
2626
import org.wordpress.android.datasets.ReaderTagTable;
2727
import org.wordpress.android.fluxc.store.AccountStore;
@@ -83,12 +83,13 @@
8383
import kotlin.jvm.functions.Function1;
8484
import kotlin.jvm.functions.Function2;
8585
import kotlin.jvm.functions.Function3;
86+
import kotlinx.coroutines.CoroutineScope;
8687

8788
public class ReaderPostAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
8889
private final ImageManager mImageManager;
8990
private final UiHelpers mUiHelpers;
9091
private final NetworkUtilsWrapper mNetworkUtilsWrapper;
91-
private final LifecycleCoroutineScope mScope;
92+
private final CoroutineScope mScope;
9293
private ReaderTag mCurrentTag;
9394
private long mCurrentBlogId;
9495
private long mCurrentFeedId;
@@ -374,7 +375,7 @@ private void toggleFollowButton(
374375
return;
375376
}
376377

377-
AsyncIoTaskExecutor.execute(
378+
AsyncTaskExecutor.executeIo(
378379
mScope,
379380
() -> !ReaderTagTable.isFollowedTagName(currentTag.getTagSlug()),
380381
isAskingToFollow -> {

0 commit comments

Comments
 (0)