Skip to content

Commit

Permalink
Fix a potential issue related to lifecycle, and rename a helper class…
Browse files Browse the repository at this point in the history
… to emphasize the implementation is related to I/O
  • Loading branch information
Jarvis Lin committed Jun 6, 2024
1 parent 032d503 commit 4384b1b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="https://github.com/wordpress-mobile/WordPress-Android/pull/20937">Introduction</a>
*/
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 <T> load(backgroundTask: () -> T, callback: AsyncTaskCallback<T>) {
CoroutineScope(Dispatchers.IO).launch {
fun <T> execute(scope: CoroutineScope, backgroundTask: () -> T, callback: IoTaskResultCallback<T>) {
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
Expand All @@ -26,7 +28,7 @@ object AsyncTaskHandler {
}
}

interface AsyncTaskCallback<T> {
interface IoTaskResultCallback<T> {
fun onTaskFinished(result: T)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -1923,7 +1924,8 @@ private ReaderPostAdapter getPostAdapter() {
mImageManager,
mUiHelpers,
mNetworkUtilsWrapper,
mIsTopLevel
mIsTopLevel,
getLifecycleScope(this)
);
mPostAdapter.setOnFollowListener(this);
mPostAdapter.setOnPostSelectedListener(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -87,6 +88,7 @@ public class ReaderPostAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol
private final ImageManager mImageManager;
private final UiHelpers mUiHelpers;
private final NetworkUtilsWrapper mNetworkUtilsWrapper;
private final LifecycleCoroutineScope mScope;
private ReaderTag mCurrentTag;
private long mCurrentBlogId;
private long mCurrentFeedId;
Expand Down Expand Up @@ -372,7 +374,8 @@ private void toggleFollowButton(
return;
}

AsyncTaskHandler.load(
AsyncIoTaskExecutor.execute(
mScope,
() -> !ReaderTagTable.isFollowedTagName(currentTag.getTagSlug()),
isAskingToFollow -> {
final String slugForTracking = currentTag.getTagSlug();
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 4384b1b

Please sign in to comment.