493 lines
17 KiB
Java
493 lines
17 KiB
Java
package com.discord.utilities.view.grid;
|
|
|
|
import android.content.Context;
|
|
import android.content.res.TypedArray;
|
|
import android.util.AttributeSet;
|
|
import android.view.View;
|
|
import android.widget.FrameLayout;
|
|
import androidx.annotation.MainThread;
|
|
import androidx.constraintlayout.solver.widgets.analyzer.BasicMeasure;
|
|
import c.d.b.a.a;
|
|
import com.discord.utils.R;
|
|
import d0.d0.f;
|
|
import d0.t.c0;
|
|
import d0.t.n;
|
|
import d0.t.o;
|
|
import d0.t.u;
|
|
import d0.z.d.m;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Objects;
|
|
import java.util.Set;
|
|
import kotlin.Unit;
|
|
import kotlin.jvm.functions.Function2;
|
|
import kotlin.jvm.internal.DefaultConstructorMarker;
|
|
import kotlin.ranges.IntRange;
|
|
/* compiled from: FrameGridLayout.kt */
|
|
public final class FrameGridLayout extends FrameLayout {
|
|
private static final Companion Companion = new Companion(null);
|
|
@Deprecated
|
|
private static final int DEFAULT_MAX_LINEAR_VIEWS = 3;
|
|
private List<? extends Data> dataset;
|
|
private final int maxLinearViews;
|
|
private Function2<? super View, ? super Data, Unit> onBindView;
|
|
private final SizeCalculator sizeCalculator;
|
|
|
|
/* compiled from: FrameGridLayout.kt */
|
|
public static final class Children {
|
|
private final Map<String, View> boundChildren;
|
|
private final List<View> unboundChildren;
|
|
|
|
/* JADX DEBUG: Multi-variable search result rejected for r2v0, resolved type: java.util.Map<java.lang.String, ? extends android.view.View> */
|
|
/* JADX DEBUG: Multi-variable search result rejected for r3v0, resolved type: java.util.List<? extends android.view.View> */
|
|
/* JADX WARN: Multi-variable type inference failed */
|
|
public Children(Map<String, ? extends View> map, List<? extends View> list) {
|
|
m.checkNotNullParameter(map, "boundChildren");
|
|
m.checkNotNullParameter(list, "unboundChildren");
|
|
this.boundChildren = map;
|
|
this.unboundChildren = list;
|
|
}
|
|
|
|
/* JADX DEBUG: Multi-variable search result rejected for r0v0, resolved type: com.discord.utilities.view.grid.FrameGridLayout$Children */
|
|
/* JADX WARN: Multi-variable type inference failed */
|
|
public static /* synthetic */ Children copy$default(Children children, Map map, List list, int i, Object obj) {
|
|
if ((i & 1) != 0) {
|
|
map = children.boundChildren;
|
|
}
|
|
if ((i & 2) != 0) {
|
|
list = children.unboundChildren;
|
|
}
|
|
return children.copy(map, list);
|
|
}
|
|
|
|
public final Map<String, View> component1() {
|
|
return this.boundChildren;
|
|
}
|
|
|
|
public final List<View> component2() {
|
|
return this.unboundChildren;
|
|
}
|
|
|
|
public final Children copy(Map<String, ? extends View> map, List<? extends View> list) {
|
|
m.checkNotNullParameter(map, "boundChildren");
|
|
m.checkNotNullParameter(list, "unboundChildren");
|
|
return new Children(map, list);
|
|
}
|
|
|
|
public boolean equals(Object obj) {
|
|
if (this == obj) {
|
|
return true;
|
|
}
|
|
if (!(obj instanceof Children)) {
|
|
return false;
|
|
}
|
|
Children children = (Children) obj;
|
|
return m.areEqual(this.boundChildren, children.boundChildren) && m.areEqual(this.unboundChildren, children.unboundChildren);
|
|
}
|
|
|
|
public final Map<String, View> getBoundChildren() {
|
|
return this.boundChildren;
|
|
}
|
|
|
|
public final List<View> getUnboundChildren() {
|
|
return this.unboundChildren;
|
|
}
|
|
|
|
public int hashCode() {
|
|
Map<String, View> map = this.boundChildren;
|
|
int i = 0;
|
|
int hashCode = (map != null ? map.hashCode() : 0) * 31;
|
|
List<View> list = this.unboundChildren;
|
|
if (list != null) {
|
|
i = list.hashCode();
|
|
}
|
|
return hashCode + i;
|
|
}
|
|
|
|
public String toString() {
|
|
StringBuilder P = a.P("Children(boundChildren=");
|
|
P.append(this.boundChildren);
|
|
P.append(", unboundChildren=");
|
|
return a.J(P, this.unboundChildren, ")");
|
|
}
|
|
}
|
|
|
|
/* compiled from: FrameGridLayout.kt */
|
|
public static final class Companion {
|
|
private Companion() {
|
|
}
|
|
|
|
public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) {
|
|
this();
|
|
}
|
|
}
|
|
|
|
/* compiled from: FrameGridLayout.kt */
|
|
public interface Data {
|
|
View createView(Context context);
|
|
|
|
String getId();
|
|
}
|
|
|
|
/* compiled from: FrameGridLayout.kt */
|
|
public interface DataView {
|
|
|
|
/* compiled from: FrameGridLayout.kt */
|
|
public static final class DefaultImpls {
|
|
public static void onRemove(DataView dataView) {
|
|
}
|
|
}
|
|
|
|
String getDataId();
|
|
|
|
void onBind(Data data);
|
|
|
|
void onRemove();
|
|
}
|
|
|
|
/* compiled from: FrameGridLayout.kt */
|
|
public static final class PositionSpec {
|
|
private final int bottom;
|
|
private final int height;
|
|
private final int left;
|
|
private final int right;
|
|
private final int top;
|
|
private final int width;
|
|
|
|
public PositionSpec(int i, int i2, int i3, int i4) {
|
|
this.left = i;
|
|
this.top = i2;
|
|
this.right = i3;
|
|
this.bottom = i4;
|
|
this.width = i3 - i;
|
|
this.height = i4 - i2;
|
|
}
|
|
|
|
public static /* synthetic */ PositionSpec copy$default(PositionSpec positionSpec, int i, int i2, int i3, int i4, int i5, Object obj) {
|
|
if ((i5 & 1) != 0) {
|
|
i = positionSpec.left;
|
|
}
|
|
if ((i5 & 2) != 0) {
|
|
i2 = positionSpec.top;
|
|
}
|
|
if ((i5 & 4) != 0) {
|
|
i3 = positionSpec.right;
|
|
}
|
|
if ((i5 & 8) != 0) {
|
|
i4 = positionSpec.bottom;
|
|
}
|
|
return positionSpec.copy(i, i2, i3, i4);
|
|
}
|
|
|
|
public final int component1() {
|
|
return this.left;
|
|
}
|
|
|
|
public final int component2() {
|
|
return this.top;
|
|
}
|
|
|
|
public final int component3() {
|
|
return this.right;
|
|
}
|
|
|
|
public final int component4() {
|
|
return this.bottom;
|
|
}
|
|
|
|
public final PositionSpec copy(int i, int i2, int i3, int i4) {
|
|
return new PositionSpec(i, i2, i3, i4);
|
|
}
|
|
|
|
public boolean equals(Object obj) {
|
|
if (this == obj) {
|
|
return true;
|
|
}
|
|
if (!(obj instanceof PositionSpec)) {
|
|
return false;
|
|
}
|
|
PositionSpec positionSpec = (PositionSpec) obj;
|
|
return this.left == positionSpec.left && this.top == positionSpec.top && this.right == positionSpec.right && this.bottom == positionSpec.bottom;
|
|
}
|
|
|
|
public final int getBottom() {
|
|
return this.bottom;
|
|
}
|
|
|
|
public final int getHeight() {
|
|
return this.height;
|
|
}
|
|
|
|
public final int getLeft() {
|
|
return this.left;
|
|
}
|
|
|
|
public final int getRight() {
|
|
return this.right;
|
|
}
|
|
|
|
public final int getTop() {
|
|
return this.top;
|
|
}
|
|
|
|
public final int getWidth() {
|
|
return this.width;
|
|
}
|
|
|
|
public int hashCode() {
|
|
return (((((this.left * 31) + this.top) * 31) + this.right) * 31) + this.bottom;
|
|
}
|
|
|
|
public String toString() {
|
|
StringBuilder P = a.P("PositionSpec(left=");
|
|
P.append(this.left);
|
|
P.append(", top=");
|
|
P.append(this.top);
|
|
P.append(", right=");
|
|
P.append(this.right);
|
|
P.append(", bottom=");
|
|
return a.z(P, this.bottom, ")");
|
|
}
|
|
}
|
|
|
|
/* compiled from: FrameGridLayout.kt */
|
|
public static final class SizeCalculator {
|
|
private final int maxLinearViews;
|
|
|
|
public SizeCalculator(int i) {
|
|
this.maxLinearViews = i;
|
|
}
|
|
|
|
private final int getItemRow(int i, int i2, boolean z2) {
|
|
if (z2) {
|
|
return i2 <= this.maxLinearViews ? i : i / 2;
|
|
}
|
|
if (i2 <= this.maxLinearViews) {
|
|
return 0;
|
|
}
|
|
return i % 2;
|
|
}
|
|
|
|
private final int getRowCount(int i, boolean z2) {
|
|
if (!z2) {
|
|
return i <= this.maxLinearViews ? 1 : 2;
|
|
}
|
|
if (i <= this.maxLinearViews) {
|
|
return i;
|
|
}
|
|
return i % 2 == 1 ? (i / 2) + 1 : i / 2;
|
|
}
|
|
|
|
public final PositionSpec getItemSize(int i, int i2, int i3, int i4) {
|
|
boolean z2 = false;
|
|
boolean z3 = i2 >= i;
|
|
int itemRow = getItemRow(i4, i3, z3);
|
|
int itemRow2 = getItemRow(i4, i3, !z3);
|
|
int rowCount = getRowCount(i3, z3);
|
|
int rowCount2 = getRowCount(i3, !z3);
|
|
if (i4 == i3 - 1 && i3 > this.maxLinearViews && i3 % 2 == 1) {
|
|
z2 = true;
|
|
}
|
|
int i5 = i / rowCount2;
|
|
int i6 = i2 / rowCount;
|
|
int i7 = itemRow * i6;
|
|
int i8 = itemRow2 * i5;
|
|
return new PositionSpec(i8, i7, ((!z2 || !z3) ? i5 : i5 * 2) + i8, ((!z2 || z3) ? i6 : i6 * 2) + i7);
|
|
}
|
|
|
|
public final int getMaxLinearViews() {
|
|
return this.maxLinearViews;
|
|
}
|
|
}
|
|
|
|
public FrameGridLayout(Context context) {
|
|
this(context, null, 0, 6, null);
|
|
}
|
|
|
|
public FrameGridLayout(Context context, AttributeSet attributeSet) {
|
|
this(context, attributeSet, 0, 4, null);
|
|
}
|
|
|
|
/* JADX INFO: super call moved to the top of the method (can break code semantics) */
|
|
public FrameGridLayout(Context context, AttributeSet attributeSet, int i) {
|
|
super(context, attributeSet, i);
|
|
m.checkNotNullParameter(context, "context");
|
|
this.dataset = n.emptyList();
|
|
if (attributeSet != null) {
|
|
TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, R.b.FrameGridLayout, 0, 0);
|
|
m.checkNotNullExpressionValue(obtainStyledAttributes, "context.obtainStyledAttr…le.FrameGridLayout, 0, 0)");
|
|
this.maxLinearViews = obtainStyledAttributes.getInt(R.b.FrameGridLayout_fgl_max_linear_views, 3);
|
|
obtainStyledAttributes.recycle();
|
|
} else {
|
|
this.maxLinearViews = 3;
|
|
}
|
|
this.sizeCalculator = new SizeCalculator(this.maxLinearViews);
|
|
}
|
|
|
|
/* JADX INFO: this call moved to the top of the method (can break code semantics) */
|
|
public /* synthetic */ FrameGridLayout(Context context, AttributeSet attributeSet, int i, int i2, DefaultConstructorMarker defaultConstructorMarker) {
|
|
this(context, (i2 & 2) != 0 ? null : attributeSet, (i2 & 4) != 0 ? 0 : i);
|
|
}
|
|
|
|
public static final /* synthetic */ Function2 access$getOnBindView$p(FrameGridLayout frameGridLayout) {
|
|
return frameGridLayout.onBindView;
|
|
}
|
|
|
|
public static final /* synthetic */ PositionSpec access$layout(FrameGridLayout frameGridLayout, View view, PositionSpec positionSpec) {
|
|
return frameGridLayout.layout(view, positionSpec);
|
|
}
|
|
|
|
public static final /* synthetic */ void access$measure(FrameGridLayout frameGridLayout, View view, PositionSpec positionSpec) {
|
|
frameGridLayout.measure(view, positionSpec);
|
|
}
|
|
|
|
public static final /* synthetic */ void access$setOnBindView$p(FrameGridLayout frameGridLayout, Function2 function2) {
|
|
frameGridLayout.onBindView = function2;
|
|
}
|
|
|
|
private final void addNecessaryViews() {
|
|
if (this.dataset.size() > getChildCount()) {
|
|
Iterator<Integer> it = f.until(getChildCount(), this.dataset.size()).iterator();
|
|
while (it.hasNext()) {
|
|
int nextInt = ((c0) it).nextInt();
|
|
Context context = getContext();
|
|
m.checkNotNullExpressionValue(context, "context");
|
|
addView(((Data) this.dataset.get(nextInt)).createView(context));
|
|
}
|
|
}
|
|
}
|
|
|
|
private final void bindViews() {
|
|
FrameGridLayout$bindViews$1 frameGridLayout$bindViews$1 = new FrameGridLayout$bindViews$1(this);
|
|
Children children = getChildren();
|
|
List<? extends Data> list = this.dataset;
|
|
ArrayList<Data> arrayList = new ArrayList();
|
|
for (Object obj : list) {
|
|
if (children.getBoundChildren().keySet().contains(((Data) obj).getId())) {
|
|
arrayList.add(obj);
|
|
}
|
|
}
|
|
for (Data data : arrayList) {
|
|
frameGridLayout$bindViews$1.invoke(data, children.getBoundChildren().get(data.getId()));
|
|
}
|
|
List<? extends Data> list2 = this.dataset;
|
|
ArrayList arrayList2 = new ArrayList();
|
|
for (Object obj2 : list2) {
|
|
if (!children.getBoundChildren().keySet().contains(((Data) obj2).getId())) {
|
|
arrayList2.add(obj2);
|
|
}
|
|
}
|
|
int i = 0;
|
|
for (Object obj3 : arrayList2) {
|
|
int i2 = i + 1;
|
|
if (i < 0) {
|
|
n.throwIndexOverflow();
|
|
}
|
|
frameGridLayout$bindViews$1.invoke((Data) obj3, children.getUnboundChildren().get(i));
|
|
i = i2;
|
|
}
|
|
}
|
|
|
|
private final Children getChildren() {
|
|
HashMap hashMap = new HashMap();
|
|
ArrayList arrayList = new ArrayList();
|
|
IntRange until = f.until(0, getChildCount());
|
|
ArrayList<View> arrayList2 = new ArrayList(o.collectionSizeOrDefault(until, 10));
|
|
Iterator<Integer> it = until.iterator();
|
|
while (it.hasNext()) {
|
|
arrayList2.add(getChildAt(((c0) it).nextInt()));
|
|
}
|
|
for (View view : arrayList2) {
|
|
Objects.requireNonNull(view, "null cannot be cast to non-null type com.discord.utilities.view.grid.FrameGridLayout.DataView");
|
|
String dataId = ((DataView) view).getDataId();
|
|
if (dataId == null) {
|
|
arrayList.add(view);
|
|
} else {
|
|
hashMap.put(dataId, view);
|
|
}
|
|
}
|
|
return new Children(hashMap, arrayList);
|
|
}
|
|
|
|
private final PositionSpec layout(View view, PositionSpec positionSpec) {
|
|
view.layout(positionSpec.getLeft(), positionSpec.getTop(), positionSpec.getRight(), positionSpec.getBottom());
|
|
return positionSpec;
|
|
}
|
|
|
|
private final void measure(View view, PositionSpec positionSpec) {
|
|
view.measure(View.MeasureSpec.makeMeasureSpec(positionSpec.getWidth(), BasicMeasure.EXACTLY), View.MeasureSpec.makeMeasureSpec(positionSpec.getHeight(), BasicMeasure.EXACTLY));
|
|
}
|
|
|
|
private final void removeStaleViews() {
|
|
List<? extends Data> list = this.dataset;
|
|
ArrayList arrayList = new ArrayList(o.collectionSizeOrDefault(list, 10));
|
|
for (Data data : list) {
|
|
arrayList.add(data.getId());
|
|
}
|
|
Set set = u.toSet(arrayList);
|
|
Iterator<Integer> it = f.downTo(getChildCount() - 1, 0).iterator();
|
|
while (it.hasNext()) {
|
|
int nextInt = ((c0) it).nextInt();
|
|
View childAt = getChildAt(nextInt);
|
|
Objects.requireNonNull(childAt, "null cannot be cast to non-null type com.discord.utilities.view.grid.FrameGridLayout.DataView");
|
|
DataView dataView = (DataView) childAt;
|
|
if (!u.contains(set, dataView.getDataId())) {
|
|
dataView.onRemove();
|
|
removeViewAt(nextInt);
|
|
}
|
|
}
|
|
}
|
|
|
|
private final void sizeChildViews(Function2<? super View, ? super PositionSpec, Unit> function2) {
|
|
Iterator<Integer> it = f.until(0, getChildCount()).iterator();
|
|
while (it.hasNext()) {
|
|
View childAt = getChildAt(((c0) it).nextInt());
|
|
Objects.requireNonNull(childAt, "null cannot be cast to non-null type com.discord.utilities.view.grid.FrameGridLayout.DataView");
|
|
String dataId = ((DataView) childAt).getDataId();
|
|
Iterator<? extends Data> it2 = this.dataset.iterator();
|
|
int i = 0;
|
|
while (true) {
|
|
if (!it2.hasNext()) {
|
|
i = -1;
|
|
break;
|
|
} else if (m.areEqual(((Data) it2.next()).getId(), dataId)) {
|
|
break;
|
|
} else {
|
|
i++;
|
|
}
|
|
}
|
|
if (i >= 0) {
|
|
function2.invoke(childAt, this.sizeCalculator.getItemSize(getWidth(), getHeight(), getChildCount(), i));
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override // android.widget.FrameLayout, android.view.View, android.view.ViewGroup
|
|
public void onLayout(boolean z2, int i, int i2, int i3, int i4) {
|
|
super.onLayout(z2, i, i2, i3, i4);
|
|
sizeChildViews(new FrameGridLayout$onLayout$1(this));
|
|
}
|
|
|
|
@Override // android.widget.FrameLayout, android.view.View
|
|
public void onMeasure(int i, int i2) {
|
|
super.onMeasure(i, i2);
|
|
sizeChildViews(new FrameGridLayout$onMeasure$1(this));
|
|
}
|
|
|
|
@MainThread
|
|
public final void setData(List<? extends Data> list) {
|
|
m.checkNotNullParameter(list, "data");
|
|
this.dataset = list;
|
|
removeStaleViews();
|
|
addNecessaryViews();
|
|
bindViews();
|
|
}
|
|
|
|
@MainThread
|
|
public final void setOnBindView(Function2<? super View, ? super Data, Unit> function2) {
|
|
this.onBindView = function2;
|
|
}
|
|
}
|