199 lines
8.0 KiB
Java
199 lines
8.0 KiB
Java
package androidx.recyclerview.widget;
|
|
|
|
import android.os.Handler;
|
|
import android.os.Looper;
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
import androidx.recyclerview.widget.AsyncDifferConfig;
|
|
import androidx.recyclerview.widget.DiffUtil;
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
|
import java.util.concurrent.Executor;
|
|
public class AsyncListDiffer<T> {
|
|
private static final Executor sMainThreadExecutor = new MainThreadExecutor();
|
|
public final AsyncDifferConfig<T> mConfig;
|
|
@Nullable
|
|
private List<T> mList;
|
|
private final List<ListListener<T>> mListeners;
|
|
public Executor mMainThreadExecutor;
|
|
public int mMaxScheduledGeneration;
|
|
@NonNull
|
|
private List<T> mReadOnlyList;
|
|
private final ListUpdateCallback mUpdateCallback;
|
|
|
|
/* renamed from: androidx.recyclerview.widget.AsyncListDiffer$1 reason: invalid class name */
|
|
public class AnonymousClass1 implements Runnable {
|
|
public final /* synthetic */ Runnable val$commitCallback;
|
|
public final /* synthetic */ List val$newList;
|
|
public final /* synthetic */ List val$oldList;
|
|
public final /* synthetic */ int val$runGeneration;
|
|
|
|
/* renamed from: androidx.recyclerview.widget.AsyncListDiffer$1$1 reason: invalid class name */
|
|
public class AnonymousClass1 extends DiffUtil.Callback {
|
|
public AnonymousClass1() {
|
|
}
|
|
|
|
@Override // androidx.recyclerview.widget.DiffUtil.Callback
|
|
public boolean areContentsTheSame(int i, int i2) {
|
|
Object obj = AnonymousClass1.this.val$oldList.get(i);
|
|
Object obj2 = AnonymousClass1.this.val$newList.get(i2);
|
|
if (obj != null && obj2 != null) {
|
|
return AsyncListDiffer.this.mConfig.getDiffCallback().areContentsTheSame(obj, obj2);
|
|
}
|
|
if (obj == null && obj2 == null) {
|
|
return true;
|
|
}
|
|
throw new AssertionError();
|
|
}
|
|
|
|
@Override // androidx.recyclerview.widget.DiffUtil.Callback
|
|
public boolean areItemsTheSame(int i, int i2) {
|
|
Object obj = AnonymousClass1.this.val$oldList.get(i);
|
|
Object obj2 = AnonymousClass1.this.val$newList.get(i2);
|
|
return (obj == null || obj2 == null) ? obj == null && obj2 == null : AsyncListDiffer.this.mConfig.getDiffCallback().areItemsTheSame(obj, obj2);
|
|
}
|
|
|
|
@Override // androidx.recyclerview.widget.DiffUtil.Callback
|
|
@Nullable
|
|
public Object getChangePayload(int i, int i2) {
|
|
Object obj = AnonymousClass1.this.val$oldList.get(i);
|
|
Object obj2 = AnonymousClass1.this.val$newList.get(i2);
|
|
if (obj != null && obj2 != null) {
|
|
return AsyncListDiffer.this.mConfig.getDiffCallback().getChangePayload(obj, obj2);
|
|
}
|
|
throw new AssertionError();
|
|
}
|
|
|
|
@Override // androidx.recyclerview.widget.DiffUtil.Callback
|
|
public int getNewListSize() {
|
|
return AnonymousClass1.this.val$newList.size();
|
|
}
|
|
|
|
@Override // androidx.recyclerview.widget.DiffUtil.Callback
|
|
public int getOldListSize() {
|
|
return AnonymousClass1.this.val$oldList.size();
|
|
}
|
|
}
|
|
|
|
/* renamed from: androidx.recyclerview.widget.AsyncListDiffer$1$2 reason: invalid class name */
|
|
public class AnonymousClass2 implements Runnable {
|
|
public final /* synthetic */ DiffUtil.DiffResult val$result;
|
|
|
|
public AnonymousClass2(DiffUtil.DiffResult diffResult) {
|
|
this.val$result = diffResult;
|
|
}
|
|
|
|
@Override // java.lang.Runnable
|
|
public void run() {
|
|
AnonymousClass1 r0 = AnonymousClass1.this;
|
|
AsyncListDiffer asyncListDiffer = AsyncListDiffer.this;
|
|
if (asyncListDiffer.mMaxScheduledGeneration == r0.val$runGeneration) {
|
|
asyncListDiffer.latchList(r0.val$newList, this.val$result, r0.val$commitCallback);
|
|
}
|
|
}
|
|
}
|
|
|
|
public AnonymousClass1(List list, List list2, int i, Runnable runnable) {
|
|
this.val$oldList = list;
|
|
this.val$newList = list2;
|
|
this.val$runGeneration = i;
|
|
this.val$commitCallback = runnable;
|
|
}
|
|
|
|
@Override // java.lang.Runnable
|
|
public void run() {
|
|
AsyncListDiffer.this.mMainThreadExecutor.execute(new AnonymousClass2(DiffUtil.calculateDiff(new AnonymousClass1())));
|
|
}
|
|
}
|
|
|
|
public interface ListListener<T> {
|
|
void onCurrentListChanged(@NonNull List<T> list, @NonNull List<T> list2);
|
|
}
|
|
|
|
public static class MainThreadExecutor implements Executor {
|
|
public final Handler mHandler = new Handler(Looper.getMainLooper());
|
|
|
|
@Override // java.util.concurrent.Executor
|
|
public void execute(@NonNull Runnable runnable) {
|
|
this.mHandler.post(runnable);
|
|
}
|
|
}
|
|
|
|
public AsyncListDiffer(@NonNull ListUpdateCallback listUpdateCallback, @NonNull AsyncDifferConfig<T> asyncDifferConfig) {
|
|
this.mListeners = new CopyOnWriteArrayList();
|
|
this.mReadOnlyList = Collections.emptyList();
|
|
this.mUpdateCallback = listUpdateCallback;
|
|
this.mConfig = asyncDifferConfig;
|
|
if (asyncDifferConfig.getMainThreadExecutor() != null) {
|
|
this.mMainThreadExecutor = asyncDifferConfig.getMainThreadExecutor();
|
|
} else {
|
|
this.mMainThreadExecutor = sMainThreadExecutor;
|
|
}
|
|
}
|
|
|
|
public AsyncListDiffer(@NonNull RecyclerView.Adapter adapter, @NonNull DiffUtil.ItemCallback<T> itemCallback) {
|
|
this(new AdapterListUpdateCallback(adapter), new AsyncDifferConfig.Builder(itemCallback).build());
|
|
}
|
|
|
|
private void onCurrentListChanged(@NonNull List<T> list, @Nullable Runnable runnable) {
|
|
for (ListListener<T> listListener : this.mListeners) {
|
|
listListener.onCurrentListChanged(list, this.mReadOnlyList);
|
|
}
|
|
if (runnable != null) {
|
|
runnable.run();
|
|
}
|
|
}
|
|
|
|
public void addListListener(@NonNull ListListener<T> listListener) {
|
|
this.mListeners.add(listListener);
|
|
}
|
|
|
|
@NonNull
|
|
public List<T> getCurrentList() {
|
|
return this.mReadOnlyList;
|
|
}
|
|
|
|
public void latchList(@NonNull List<T> list, @NonNull DiffUtil.DiffResult diffResult, @Nullable Runnable runnable) {
|
|
List<T> list2 = this.mReadOnlyList;
|
|
this.mList = list;
|
|
this.mReadOnlyList = Collections.unmodifiableList(list);
|
|
diffResult.dispatchUpdatesTo(this.mUpdateCallback);
|
|
onCurrentListChanged(list2, runnable);
|
|
}
|
|
|
|
public void removeListListener(@NonNull ListListener<T> listListener) {
|
|
this.mListeners.remove(listListener);
|
|
}
|
|
|
|
public void submitList(@Nullable List<T> list) {
|
|
submitList(list, null);
|
|
}
|
|
|
|
public void submitList(@Nullable List<T> list, @Nullable Runnable runnable) {
|
|
int i = this.mMaxScheduledGeneration + 1;
|
|
this.mMaxScheduledGeneration = i;
|
|
List<T> list2 = this.mList;
|
|
if (list != list2) {
|
|
List<T> list3 = this.mReadOnlyList;
|
|
if (list == null) {
|
|
int size = list2.size();
|
|
this.mList = null;
|
|
this.mReadOnlyList = Collections.emptyList();
|
|
this.mUpdateCallback.onRemoved(0, size);
|
|
onCurrentListChanged(list3, runnable);
|
|
} else if (list2 == null) {
|
|
this.mList = list;
|
|
this.mReadOnlyList = Collections.unmodifiableList(list);
|
|
this.mUpdateCallback.onInserted(0, list.size());
|
|
onCurrentListChanged(list3, runnable);
|
|
} else {
|
|
this.mConfig.getBackgroundThreadExecutor().execute(new AnonymousClass1(list2, list, i, runnable));
|
|
}
|
|
} else if (runnable != null) {
|
|
runnable.run();
|
|
}
|
|
}
|
|
}
|