Skip to content

Commit

Permalink
Merge pull request #20943 from wordpress-mobile/hackweek/database-anr
Browse files Browse the repository at this point in the history
Fix a potential issue related to lifecycle
  • Loading branch information
jarvislin authored Jun 11, 2024
2 parents 689aecc + e5866cb commit a0b6ece
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 37 deletions.
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)
}
}

This file was deleted.

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.AsyncTaskExecutor;
import org.wordpress.android.datasets.ReaderPostTable;
import org.wordpress.android.datasets.ReaderTagTable;
import org.wordpress.android.fluxc.store.AccountStore;
Expand Down Expand Up @@ -82,11 +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 CoroutineScope mScope;
private ReaderTag mCurrentTag;
private long mCurrentBlogId;
private long mCurrentFeedId;
Expand Down Expand Up @@ -372,7 +375,8 @@ private void toggleFollowButton(
return;
}

AsyncTaskHandler.load(
AsyncTaskExecutor.executeIo(
mScope,
() -> !ReaderTagTable.isFollowedTagName(currentTag.getTagSlug()),
isAskingToFollow -> {
final String slugForTracking = currentTag.getTagSlug();
Expand Down Expand Up @@ -688,7 +692,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 +704,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 a0b6ece

Please sign in to comment.