discord-jadx/app/src/main/java/com/discord/utilities/recycler/DiffCreator.java

253 lines
10 KiB
Java

package com.discord.utilities.recycler;
import android.annotation.SuppressLint;
import android.content.Context;
import androidx.annotation.MainThread;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import c.d.b.a.a;
import com.discord.app.AppComponent;
import com.discord.utilities.collections.ShallowPartitionMap;
import com.discord.utilities.rx.ObservableExtensionsKt;
import d0.z.d.m;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import kotlin.Unit;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.functions.Function1;
import kotlin.jvm.internal.DefaultConstructorMarker;
import rx.Observable;
import rx.Subscription;
import rx.subjects.PublishSubject;
/* compiled from: DiffCreator.kt */
public final class DiffCreator<T extends List<? extends DiffKeyProvider>, VH extends RecyclerView.ViewHolder> {
public static final Companion Companion = new Companion(null);
private static final int EXPENSIVE_DIFF_THRESHOLD = 225;
private Subscription asyncDiffSubscription;
private final AppComponent asyncDiffSubscriptionScope;
private final PublishSubject<UpdateRequest<T, VH>> updateRequestsSubject = PublishSubject.k0();
/* compiled from: DiffCreator.kt */
public static final class Companion {
private Companion() {
}
public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) {
this();
}
}
/* compiled from: DiffCreator.kt */
public static final class UpdateRequest<T, VH extends RecyclerView.ViewHolder> {
private final RecyclerView.Adapter<VH> adapter;
private final T newItems;
private final T oldItems;
private final Function1<T, Unit> setItems;
/* JADX DEBUG: Multi-variable search result rejected for r3v0, resolved type: kotlin.jvm.functions.Function1<? super T, kotlin.Unit> */
/* JADX WARN: Multi-variable type inference failed */
public UpdateRequest(RecyclerView.Adapter<VH> adapter, Function1<? super T, Unit> function1, T t, T t2) {
m.checkNotNullParameter(adapter, "adapter");
m.checkNotNullParameter(function1, "setItems");
this.adapter = adapter;
this.setItems = function1;
this.oldItems = t;
this.newItems = t2;
}
/* JADX DEBUG: Multi-variable search result rejected for r0v0, resolved type: com.discord.utilities.recycler.DiffCreator$UpdateRequest */
/* JADX WARN: Multi-variable type inference failed */
public static /* synthetic */ UpdateRequest copy$default(UpdateRequest updateRequest, RecyclerView.Adapter adapter, Function1 function1, Object obj, Object obj2, int i, Object obj3) {
if ((i & 1) != 0) {
adapter = updateRequest.adapter;
}
if ((i & 2) != 0) {
function1 = updateRequest.setItems;
}
if ((i & 4) != 0) {
obj = updateRequest.oldItems;
}
if ((i & 8) != 0) {
obj2 = updateRequest.newItems;
}
return updateRequest.copy(adapter, function1, obj, obj2);
}
public final RecyclerView.Adapter<VH> component1() {
return this.adapter;
}
public final Function1<T, Unit> component2() {
return this.setItems;
}
public final T component3() {
return this.oldItems;
}
public final T component4() {
return this.newItems;
}
public final UpdateRequest<T, VH> copy(RecyclerView.Adapter<VH> adapter, Function1<? super T, Unit> function1, T t, T t2) {
m.checkNotNullParameter(adapter, "adapter");
m.checkNotNullParameter(function1, "setItems");
return new UpdateRequest<>(adapter, function1, t, t2);
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof UpdateRequest)) {
return false;
}
UpdateRequest updateRequest = (UpdateRequest) obj;
return m.areEqual(this.adapter, updateRequest.adapter) && m.areEqual(this.setItems, updateRequest.setItems) && m.areEqual(this.oldItems, updateRequest.oldItems) && m.areEqual(this.newItems, updateRequest.newItems);
}
public final RecyclerView.Adapter<VH> getAdapter() {
return this.adapter;
}
public final T getNewItems() {
return this.newItems;
}
public final T getOldItems() {
return this.oldItems;
}
public final Function1<T, Unit> getSetItems() {
return this.setItems;
}
public int hashCode() {
RecyclerView.Adapter<VH> adapter = this.adapter;
int i = 0;
int hashCode = (adapter != null ? adapter.hashCode() : 0) * 31;
Function1<T, Unit> function1 = this.setItems;
int hashCode2 = (hashCode + (function1 != null ? function1.hashCode() : 0)) * 31;
T t = this.oldItems;
int hashCode3 = (hashCode2 + (t != null ? t.hashCode() : 0)) * 31;
T t2 = this.newItems;
if (t2 != null) {
i = t2.hashCode();
}
return hashCode3 + i;
}
public String toString() {
StringBuilder K = a.K("UpdateRequest(adapter=");
K.append(this.adapter);
K.append(", setItems=");
K.append(this.setItems);
K.append(", oldItems=");
K.append((Object) this.oldItems);
K.append(", newItems=");
K.append((Object) this.newItems);
K.append(")");
return K.toString();
}
}
public DiffCreator(AppComponent appComponent) {
this.asyncDiffSubscriptionScope = appComponent;
if (appComponent != null) {
subscribeToAsyncUpdateRequests(appComponent);
}
}
public static final /* synthetic */ Subscription access$getAsyncDiffSubscription$p(DiffCreator diffCreator) {
return diffCreator.asyncDiffSubscription;
}
public static final /* synthetic */ void access$setAsyncDiffSubscription$p(DiffCreator diffCreator, Subscription subscription) {
diffCreator.asyncDiffSubscription = subscription;
}
public static final /* synthetic */ void access$setItemsAndDispatchUpdate(DiffCreator diffCreator, RecyclerView.Adapter adapter, Function1 function1, List list, DiffUtil.DiffResult diffResult) {
diffCreator.setItemsAndDispatchUpdate(adapter, function1, list, diffResult);
}
private final DiffCreator$createDiffUtilCallback$1 createDiffUtilCallback(T t, T t2) {
return new DiffCreator$createDiffUtilCallback$1(t, t2);
}
private final boolean isExpensiveDiff(T t, T t2) {
if (Math.abs(t2.size() - t.size()) > 225) {
return true;
}
ShallowPartitionMap create$default = ShallowPartitionMap.Companion.create$default(ShallowPartitionMap.Companion, t.size(), 0, 0, null, 14, null);
ArrayList arrayList = new ArrayList();
Iterator it = t.iterator();
while (it.hasNext()) {
create$default.put(((DiffKeyProvider) it.next()).getKey(), Unit.a);
}
Iterator it2 = t2.iterator();
while (it2.hasNext()) {
String key = ((DiffKeyProvider) it2.next()).getKey();
if (!create$default.containsKey(key)) {
arrayList.add(key);
}
create$default.remove(key);
}
return arrayList.size() + create$default.size() > 225;
}
@SuppressLint({"NotifyDataSetChanged"})
private final void setItemsAndDispatchUpdate(RecyclerView.Adapter<VH> adapter, Function1<? super T, Unit> function1, T t, DiffUtil.DiffResult diffResult) {
function1.invoke(t);
if (diffResult != null) {
diffResult.dispatchUpdatesTo(adapter);
} else {
adapter.notifyDataSetChanged();
}
}
private final void subscribeToAsyncUpdateRequests(AppComponent appComponent) {
PublishSubject<UpdateRequest<T, VH>> publishSubject = this.updateRequestsSubject;
m.checkNotNullExpressionValue(publishSubject, "updateRequestsSubject");
Observable F = ObservableExtensionsKt.computationBuffered(publishSubject).F(new DiffCreator$subscribeToAsyncUpdateRequests$1(this));
m.checkNotNullExpressionValue(F, "updateRequestsSubject\n …ms)\n )\n }");
ObservableExtensionsKt.appSubscribe$default(ObservableExtensionsKt.ui$default(F, appComponent, null, 2, null), DiffCreator.class, (Context) null, new DiffCreator$subscribeToAsyncUpdateRequests$2(this), (Function1) null, (Function0) null, (Function0) null, new DiffCreator$subscribeToAsyncUpdateRequests$3(this), 58, (Object) null);
}
public final DiffUtil.DiffResult calculateDiffResult(T t, T t2) {
m.checkNotNullParameter(t, "oldItems");
m.checkNotNullParameter(t2, "newItems");
if (isExpensiveDiff(t, t2)) {
return null;
}
return DiffUtil.calculateDiff(createDiffUtilCallback(t, t2), true);
}
public final void dispatchDiffUpdates(RecyclerView.Adapter<VH> adapter, Function1<? super T, Unit> function1, T t, T t2) {
m.checkNotNullParameter(adapter, "adapter");
m.checkNotNullParameter(function1, "setItems");
m.checkNotNullParameter(t, "oldItems");
m.checkNotNullParameter(t2, "newItems");
setItemsAndDispatchUpdate(adapter, function1, t2, calculateDiffResult(t, t2));
}
@MainThread
public final void dispatchDiffUpdatesAsync(RecyclerView.Adapter<VH> adapter, Function1<? super T, Unit> function1, T t, T t2) {
m.checkNotNullParameter(adapter, "adapter");
m.checkNotNullParameter(function1, "setItems");
m.checkNotNullParameter(t, "oldItems");
m.checkNotNullParameter(t2, "newItems");
if (this.asyncDiffSubscriptionScope != null) {
Subscription subscription = this.asyncDiffSubscription;
if (subscription == null || subscription.isUnsubscribed()) {
subscribeToAsyncUpdateRequests(this.asyncDiffSubscriptionScope);
}
PublishSubject<UpdateRequest<T, VH>> publishSubject = this.updateRequestsSubject;
publishSubject.j.onNext(new UpdateRequest<>(adapter, function1, t, t2));
return;
}
throw new IllegalStateException("to use async diffs, provide an asyncDiffSubscriptionScope");
}
}