package com.lytefast.flexinput.utils; import android.os.Parcelable; import android.util.Log; import androidx.recyclerview.widget.RecyclerView; import com.lytefast.flexinput.adapters.AttachmentPreviewAdapter; import com.lytefast.flexinput.model.Attachment; import com.lytefast.flexinput.utils.SelectionCoordinator; import d0.z.d.e0; import d0.z.d.m; import java.util.ArrayList; import java.util.Iterator; import java.util.Objects; import kotlin.jvm.internal.DefaultConstructorMarker; /* compiled from: SelectionAggregator.kt */ public class SelectionAggregator> { public static final Companion Companion = new Companion(null); private static final String TAG; private final AttachmentPreviewAdapter adapter; private final ArrayList attachments; private final ArrayList> childSelectionCoordinators; private ArrayList> itemSelectionListeners; /* compiled from: SelectionAggregator.kt */ public static final class Companion { public Companion() { } public Companion(DefaultConstructorMarker defaultConstructorMarker) { } } /* compiled from: SelectionAggregator.kt */ public static final class a implements SelectionCoordinator.ItemSelectionListener { public final /* synthetic */ SelectionAggregator a; public final /* synthetic */ SelectionCoordinator b; public a(SelectionAggregator selectionAggregator, SelectionCoordinator selectionCoordinator) { this.a = selectionAggregator; this.b = selectionCoordinator; } @Override // com.lytefast.flexinput.utils.SelectionCoordinator.ItemSelectionListener public void onItemSelected(Object obj) { Attachment attachment = (Attachment) obj; m.checkNotNullParameter(attachment, "item"); SelectionAggregator.access$addItem(this.a, attachment); } @Override // com.lytefast.flexinput.utils.SelectionCoordinator.ItemSelectionListener public void onItemUnselected(Object obj) { Attachment attachment = (Attachment) obj; m.checkNotNullParameter(attachment, "item"); SelectionAggregator.access$removeItem(this.a, attachment); } @Override // com.lytefast.flexinput.utils.SelectionCoordinator.ItemSelectionListener public void unregister() { this.a.getChildSelectionCoordinators().remove(this.b); } } static { String canonicalName = SelectionAggregator.class.getCanonicalName(); m.checkNotNull(canonicalName); TAG = canonicalName; } public SelectionAggregator(AttachmentPreviewAdapter attachmentPreviewAdapter) { this(attachmentPreviewAdapter, null, null, null, 14, null); } public SelectionAggregator(AttachmentPreviewAdapter attachmentPreviewAdapter, ArrayList arrayList) { this(attachmentPreviewAdapter, arrayList, null, null, 12, null); } public SelectionAggregator(AttachmentPreviewAdapter attachmentPreviewAdapter, ArrayList arrayList, ArrayList> arrayList2) { this(attachmentPreviewAdapter, arrayList, arrayList2, null, 8, null); } public SelectionAggregator(AttachmentPreviewAdapter attachmentPreviewAdapter, ArrayList arrayList, ArrayList> arrayList2, ArrayList> arrayList3) { m.checkNotNullParameter(attachmentPreviewAdapter, "adapter"); m.checkNotNullParameter(arrayList, "attachments"); m.checkNotNullParameter(arrayList2, "childSelectionCoordinators"); m.checkNotNullParameter(arrayList3, "itemSelectionListeners"); this.adapter = attachmentPreviewAdapter; this.attachments = arrayList; this.childSelectionCoordinators = arrayList2; this.itemSelectionListeners = arrayList3; } /* JADX INFO: this call moved to the top of the method (can break code semantics) */ public /* synthetic */ SelectionAggregator(AttachmentPreviewAdapter attachmentPreviewAdapter, ArrayList arrayList, ArrayList arrayList2, ArrayList arrayList3, int i, DefaultConstructorMarker defaultConstructorMarker) { this(attachmentPreviewAdapter, (i & 2) != 0 ? new ArrayList() : arrayList, (i & 4) != 0 ? new ArrayList(4) : arrayList2, (i & 8) != 0 ? new ArrayList(4) : arrayList3); } public static final /* synthetic */ void access$addItem(SelectionAggregator selectionAggregator, Attachment attachment) { selectionAggregator.addItem(attachment); } public static final /* synthetic */ String access$getTAG$cp() { return TAG; } public static final /* synthetic */ boolean access$removeItem(SelectionAggregator selectionAggregator, Attachment attachment) { return selectionAggregator.removeItem(attachment); } private final void addItem(T t) { if (!this.attachments.contains(t)) { this.attachments.add(t); this.adapter.notifyItemInserted(this.attachments.size() - 1); Iterator it = this.itemSelectionListeners.iterator(); while (it.hasNext()) { ((SelectionCoordinator.ItemSelectionListener) it.next()).onItemSelected(t); } } } private final boolean removeItem(T t) { int indexOf = this.attachments.indexOf(t); boolean remove = this.attachments.remove(t); if (remove) { this.adapter.notifyItemRemoved(indexOf); } Iterator it = this.itemSelectionListeners.iterator(); while (it.hasNext()) { ((SelectionCoordinator.ItemSelectionListener) it.next()).onItemUnselected(t); } return remove; } public final SelectionAggregator addItemSelectionListener(SelectionCoordinator.ItemSelectionListener itemSelectionListener) { m.checkNotNullParameter(itemSelectionListener, "itemSelectionListener"); if (!this.itemSelectionListeners.contains(itemSelectionListener)) { this.itemSelectionListeners.add(itemSelectionListener); } return this; } public final void clear() { this.attachments.clear(); Iterator it = this.childSelectionCoordinators.iterator(); while (it.hasNext()) { SelectionCoordinator selectionCoordinator = (SelectionCoordinator) it.next(); Objects.requireNonNull(selectionCoordinator); ArrayList arrayList = new ArrayList(selectionCoordinator.b.values()); selectionCoordinator.b.clear(); RecyclerView.Adapter adapter = selectionCoordinator.a; if (adapter != null) { Iterator it2 = arrayList.iterator(); while (it2.hasNext()) { Integer num = (Integer) it2.next(); m.checkNotNullExpressionValue(num, "position"); adapter.notifyItemChanged(num.intValue()); } } } } public final T get(int i) { T t = this.attachments.get(i); m.checkNotNullExpressionValue(t, "attachments[position]"); return t; } public final AttachmentPreviewAdapter getAdapter() { return this.adapter; } public final ArrayList getAttachments() { return this.attachments; } public final ArrayList> getChildSelectionCoordinators() { return this.childSelectionCoordinators; } public final ArrayList> getItemSelectionListeners() { return this.itemSelectionListeners; } public final int getSize() { return this.attachments.size(); } public final SelectionAggregator initFrom(SelectionAggregator selectionAggregator) { if (selectionAggregator != null) { this.attachments.addAll(selectionAggregator.attachments); Iterator> it = selectionAggregator.childSelectionCoordinators.iterator(); while (it.hasNext()) { SelectionCoordinator next = it.next(); m.checkNotNullExpressionValue(next, "coordinator"); registerSelectionCoordinatorInternal(next); } this.itemSelectionListeners.addAll(selectionAggregator.itemSelectionListeners); } return this; } /* JADX DEBUG: Multi-variable search result rejected for r3v0, resolved type: com.lytefast.flexinput.utils.SelectionAggregator> */ /* JADX WARN: Multi-variable type inference failed */ public final SelectionAggregator initFrom(ArrayList arrayList) { m.checkNotNullParameter(arrayList, "savedAttachments"); ArrayList arrayList2 = new ArrayList(); for (Object obj : arrayList) { if (!(obj instanceof Attachment)) { obj = null; } Attachment attachment = (Attachment) obj; if (attachment != null) { arrayList2.add(attachment); } } for (Attachment attachment2 : arrayList2) { toggleItemInternal(attachment2); } return this; } public final void registerSelectionCoordinator(SelectionCoordinator selectionCoordinator) { m.checkNotNullParameter(selectionCoordinator, "selectionCoordinator"); registerSelectionCoordinatorInternal(selectionCoordinator); try { selectionCoordinator.b(this.attachments); } catch (SelectionCoordinator.RestorationException e) { Log.d(TAG, "selections could not be synced", e); } } public void registerSelectionCoordinatorInternal(SelectionCoordinator selectionCoordinator) { m.checkNotNullParameter(selectionCoordinator, "selectionCoordinator"); a aVar = new a(this, selectionCoordinator); Objects.requireNonNull(selectionCoordinator); m.checkNotNullParameter(aVar, ""); selectionCoordinator.f2389c = aVar; this.childSelectionCoordinators.add(selectionCoordinator); } public final void removeItemSelectionListener(SelectionCoordinator.ItemSelectionListener itemSelectionListener) { m.checkNotNullParameter(itemSelectionListener, "itemSelectionListener"); ArrayList> arrayList = this.itemSelectionListeners; Objects.requireNonNull(arrayList, "null cannot be cast to non-null type kotlin.collections.MutableCollection"); e0.asMutableCollection(arrayList).remove(itemSelectionListener); } public final void setItemSelectionListeners(ArrayList> arrayList) { m.checkNotNullParameter(arrayList, ""); this.itemSelectionListeners = arrayList; } public final boolean toggleItemInternal(T t) { m.checkNotNullParameter(t, "item"); boolean removeItem = removeItem(t); if (!removeItem) { addItem(t); } return removeItem; } public final void unselectItem(T t) { m.checkNotNullParameter(t, "item"); Iterator it = this.childSelectionCoordinators.iterator(); while (it.hasNext()) { ((SelectionCoordinator) it.next()).d(t); } removeItem(t); } }