package com.xwray.groupie;

import android.os.AsyncTask;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;

import java.lang.ref.WeakReference;
import java.util.Collection;

/**
 * An async task implementation that runs {@link DiffUtil#calculateDiff(DiffUtil.Callback)}
 * in a background thread. This task will call {@link AsyncDiffUtil.Callback#onDispatchResult(Collection)}
 * passing the new list just before dispatching the diff result to the provided
 * {@link DiffUtil.Callback} so that the new list.
 * <p>This task is executed via {@link AsyncDiffUtil#calculateDiff(Collection, DiffUtil.Callback)}.
 */
class DiffTask extends AsyncTask<Void, Void, DiffUtil.DiffResult> {
    private final DiffUtil.Callback diffCallback;
    private final WeakReference<AsyncDiffUtil> asyncListDiffer;
    private final int runGeneration;

    DiffTask(@NonNull AsyncDiffUtil asyncDiffUtil,
             @NonNull DiffUtil.Callback callback,
             int runGeneration) {
        this.diffCallback = callback;
        this.asyncListDiffer = new WeakReference<>(asyncDiffUtil);
        this.runGeneration = runGeneration;
    }

    @Override
    protected DiffUtil.DiffResult doInBackground(Void... voids) {
        return DiffUtil.calculateDiff(diffCallback);
    }

    @Override
    protected void onPostExecute(DiffUtil.DiffResult diffResult) {
        AsyncDiffUtil async = asyncListDiffer.get();
        if (async != null && runGeneration == async.getMaxScheduledGeneration()) {
            async.getAsyncDiffUtilCallback().onDispatchResult(async.getGroups());
            diffResult.dispatchUpdatesTo(async.getAsyncDiffUtilCallback());
        }
    }
}

@@ -86,16 +86,53 @@
 onNull;%0A
+import androidx.annotation.Nullable;%0A
 import a
@@ -892,16 +892,66 @@
 eration;
+%0A    private Exception backgroundException = null;
 %0A%0A    Di
@@ -1241,32 +1241,46 @@
 %0A%0A    @Override%0A
+    @Nullable%0A
     protected Di
@@ -1325,24 +1325,42 @@
 .. voids) %7B%0A
+        try %7B%0A    
         retu
@@ -1400,16 +1400,120 @@
 lback);%0A
+        %7D catch (Exception e) %7B%0A            backgroundException = e;%0A            return null;%0A        %7D%0A
     %7D%0A%0A 
@@ -1558,16 +1558,26 @@
 Execute(
+@Nullable 
 DiffUtil
@@ -1614,303 +1614,608 @@
     
-AsyncDiffUtil async = asyncListDiffer.get();%0A        if (async != null && runGeneration == async.getMaxScheduledGeneration()) %7B%0A            async.getAsyncDiffUtilCallback().onDispatchResult(async.getGroups());%0A            diffResult.dispatchUpdatesTo(async.getAsyncDiffUtilCallback());%0A        %7D
+if (backgroundException != null) %7B%0A            throw new RuntimeException(backgroundException);%0A        %7D%0A        AsyncDiffUtil async = asyncListDiffer.get();%0A        if (shouldDispatchResult(diffResult, async)) %7B%0A            async.getAsyncDiffUtilCallback().onDispatchResult(async.getGroups());%0A            diffResult.dispatchUpdatesTo(async.getAsyncDiffUtilCallback());%0A        %7D%0A    %7D%0A%0A    private boolean shouldDispatchResult(@Nullable DiffUtil.DiffResult diffResult, AsyncDiffUtil async) %7B%0A        return diffResult != null && async != null && runGeneration == async.getMaxScheduledGeneration();
 %0A   

Update diff to androidx