discord-jadx/app/src/main/java/com/discord/utilities/stateful/StatefulViews.java

271 lines
12 KiB
Java

package com.discord.utilities.stateful;
import android.app.Activity;
import android.text.SpannableStringBuilder;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.ActivityChooserModel;
import androidx.fragment.app.FragmentActivity;
import com.discord.R;
import com.discord.app.AppFragment;
import com.discord.databinding.ViewDialogConfirmationBinding;
import com.discord.utilities.view.extensions.ViewExtensions;
import com.discord.utilities.view.text.TextWatcherKt;
import com.discord.views.CheckedSetting;
import com.google.android.material.textfield.TextInputLayout;
import d0.g0.t;
import d0.t.k;
import d0.t.o;
import d0.z.d.m;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import kotlin.jvm.functions.Function0;
/* compiled from: StatefulViews.kt */
public final class StatefulViews {
private final List<Integer> ids;
private final HashMap<Integer, Boolean> requiredFieldIds;
private final HashMap<Integer, Object> viewValues;
private final HashMap<Integer, Object> viewValuesEdited;
/* compiled from: StatefulViews.kt */
public static final class FragmentOnBackPressedHandler {
private final Activity activity;
private final AtomicBoolean discardConfirmed = new AtomicBoolean(false);
private final Function0<Boolean> hasAnythingChanged;
public FragmentOnBackPressedHandler(Activity activity, Function0<Boolean> function0) {
m.checkNotNullParameter(activity, ActivityChooserModel.ATTRIBUTE_ACTIVITY);
m.checkNotNullParameter(function0, "hasAnythingChanged");
this.activity = activity;
this.hasAnythingChanged = function0;
}
public static final /* synthetic */ AtomicBoolean access$getDiscardConfirmed$p(FragmentOnBackPressedHandler fragmentOnBackPressedHandler) {
return fragmentOnBackPressedHandler.discardConfirmed;
}
public final Activity getActivity() {
return this.activity;
}
public final Function0<Boolean> getHasAnythingChanged() {
return this.hasAnythingChanged;
}
public final boolean onBackPressed() {
if (!this.hasAnythingChanged.mo1invoke().booleanValue() || this.discardConfirmed.get()) {
return false;
}
ViewDialogConfirmationBinding b2 = ViewDialogConfirmationBinding.b(LayoutInflater.from(this.activity));
m.checkNotNullExpressionValue(b2, "ViewDialogConfirmationBi…tInflater.from(activity))");
AlertDialog create = new AlertDialog.Builder(this.activity).setView(b2.a).create();
m.checkNotNullExpressionValue(create, "AlertDialog.Builder(acti…logBinding.root).create()");
b2.d.setText(R.string.discard_changes);
b2.e.setText(R.string.discard_changes_description);
b2.f2160b.setOnClickListener(new StatefulViews$FragmentOnBackPressedHandler$onBackPressed$1(create));
b2.c.setText(R.string.okay);
b2.c.setOnClickListener(new StatefulViews$FragmentOnBackPressedHandler$onBackPressed$2(this, create));
create.show();
return true;
}
}
/* JADX WARNING: Illegal instructions before constructor call */
public StatefulViews(Collection<? extends View> collection) {
this((List<Integer>) r0);
m.checkNotNullParameter(collection, "views");
ArrayList arrayList = new ArrayList(o.collectionSizeOrDefault(collection, 10));
for (View view : collection) {
arrayList.add(Integer.valueOf(view.getId()));
}
}
public StatefulViews(List<Integer> list) {
m.checkNotNullParameter(list, "ids");
this.ids = list;
this.viewValues = new HashMap<>();
this.viewValuesEdited = new HashMap<>();
this.requiredFieldIds = new HashMap<>();
}
/* JADX INFO: this call moved to the top of the method (can break code semantics) */
public StatefulViews(int... iArr) {
this(k.toList(iArr));
m.checkNotNullParameter(iArr, "ids");
}
public static final /* synthetic */ boolean access$hasAnythingChanged(StatefulViews statefulViews) {
return statefulViews.hasAnythingChanged();
}
public static /* synthetic */ void clear$default(StatefulViews statefulViews, boolean z2, int i, Object obj) {
if ((i & 1) != 0) {
z2 = false;
}
statefulViews.clear(z2);
}
private final <T> T get(int i) {
return (T) (this.viewValuesEdited.containsKey(Integer.valueOf(i)) ? this.viewValuesEdited : this.viewValues).get(Integer.valueOf(i));
}
/* JADX WARNING: Removed duplicated region for block: B:22:0x0075 A[SYNTHETIC] */
private final boolean hasAnythingChanged() {
boolean z2;
List<Integer> list = this.ids;
if (!(list instanceof Collection) || !list.isEmpty()) {
for (Number number : list) {
int intValue = number.intValue();
if (this.viewValuesEdited.containsKey(Integer.valueOf(intValue))) {
Object obj = this.viewValuesEdited.get(Integer.valueOf(intValue));
if (obj instanceof SpannableStringBuilder) {
obj = obj.toString();
}
if (!m.areEqual(obj, this.viewValues.get(Integer.valueOf(intValue)) instanceof SpannableStringBuilder ? String.valueOf(this.viewValues.get(Integer.valueOf(intValue))) : this.viewValues.get(Integer.valueOf(intValue)))) {
z2 = true;
continue;
if (z2) {
return true;
}
}
}
z2 = false;
continue;
if (z2) {
}
}
}
return false;
}
/* JADX WARNING: Removed duplicated region for block: B:18:0x004e A[SYNTHETIC] */
private final boolean isAnyRequiredFieldBlank() {
boolean z2;
HashMap<Integer, Boolean> hashMap = this.requiredFieldIds;
if (!hashMap.isEmpty()) {
for (Map.Entry<Integer, Boolean> entry : hashMap.entrySet()) {
int intValue = entry.getKey().intValue();
if (entry.getValue().booleanValue()) {
Object obj = get(intValue);
if (!(obj instanceof String)) {
obj = null;
}
String str = (String) obj;
if (str != null && t.isBlank(str)) {
z2 = true;
continue;
if (z2) {
return true;
}
}
}
z2 = false;
continue;
if (z2) {
}
}
}
return false;
}
public final void addOptionalFields(View... viewArr) {
m.checkNotNullParameter(viewArr, "views");
for (View view : viewArr) {
this.requiredFieldIds.put(Integer.valueOf(view.getId()), Boolean.FALSE);
}
}
public final void clear() {
clear$default(this, false, 1, null);
}
public final void clear(boolean z2) {
this.viewValuesEdited.clear();
if (z2) {
this.viewValues.clear();
}
}
public final void configureSaveActionView(View view) {
StatefulViews$configureSaveActionView$1 statefulViews$configureSaveActionView$1 = StatefulViews$configureSaveActionView$1.INSTANCE;
StatefulViews$configureSaveActionView$2 statefulViews$configureSaveActionView$2 = StatefulViews$configureSaveActionView$2.INSTANCE;
if (view == null) {
return;
}
if (!hasAnythingChanged() || isAnyRequiredFieldBlank()) {
statefulViews$configureSaveActionView$1.invoke(view);
} else {
statefulViews$configureSaveActionView$2.invoke(view);
}
}
public final <T> T get(int i, T t) {
this.viewValues.put(Integer.valueOf(i), t);
T t2 = (T) get(i);
return (!m.areEqual(this.requiredFieldIds.get(Integer.valueOf(i)), Boolean.FALSE) && t2 == null) ? t : t2;
}
public final <T> T getIfChanged(int i) {
if (hasChanged(i)) {
return (T) this.viewValuesEdited.get(Integer.valueOf(i));
}
return null;
}
public final boolean hasChanged(int i) {
if (this.viewValuesEdited.containsKey(Integer.valueOf(i))) {
return !m.areEqual(this.viewValuesEdited.get(Integer.valueOf(i)), this.viewValues.get(Integer.valueOf(i)));
}
return false;
}
public final <T> void put(int i, T t) {
this.viewValuesEdited.put(Integer.valueOf(i), t);
}
public final void setupTextWatcherWithSaveAction(AppFragment appFragment, View view, View... viewArr) {
m.checkNotNullParameter(appFragment, "fragment");
m.checkNotNullParameter(viewArr, "views");
for (View view2 : viewArr) {
HashMap<Integer, Boolean> hashMap = this.requiredFieldIds;
Integer valueOf = Integer.valueOf(view2.getId());
Boolean bool = this.requiredFieldIds.get(Integer.valueOf(view2.getId()));
if (bool == null) {
bool = Boolean.TRUE;
}
m.checkNotNullExpressionValue(bool, "requiredFieldIds[view.id] ?: true");
hashMap.put(valueOf, bool);
StatefulViews$setupTextWatcherWithSaveAction$$inlined$forEach$lambda$1 statefulViews$setupTextWatcherWithSaveAction$$inlined$forEach$lambda$1 = new StatefulViews$setupTextWatcherWithSaveAction$$inlined$forEach$lambda$1(view2, this, view, appFragment);
StatefulViews$setupTextWatcherWithSaveAction$$inlined$forEach$lambda$2 statefulViews$setupTextWatcherWithSaveAction$$inlined$forEach$lambda$2 = new StatefulViews$setupTextWatcherWithSaveAction$$inlined$forEach$lambda$2(view2, this, view, appFragment);
if (view2 instanceof TextView) {
TextWatcherKt.addBindedTextWatcher((TextView) view2, appFragment, statefulViews$setupTextWatcherWithSaveAction$$inlined$forEach$lambda$1);
} else if (view2 instanceof TextInputLayout) {
ViewExtensions.addBindedTextWatcher((TextInputLayout) view2, appFragment, statefulViews$setupTextWatcherWithSaveAction$$inlined$forEach$lambda$1);
} else if (view2 instanceof CheckedSetting) {
((CheckedSetting) view2).setOnCheckedListener(new StatefulViews$setupTextWatcherWithSaveAction$$inlined$forEach$lambda$3(statefulViews$setupTextWatcherWithSaveAction$$inlined$forEach$lambda$2));
} else {
throw new UnsupportedOperationException(view2 + " must support using `TextWatcher`.");
}
}
}
public final void setupUnsavedChangesConfirmation(AppFragment appFragment) {
FragmentOnBackPressedHandler fragmentOnBackPressedHandler;
m.checkNotNullParameter(appFragment, "fragment");
FragmentActivity activity = appFragment.e();
if (activity != null) {
m.checkNotNullExpressionValue(activity, "it");
fragmentOnBackPressedHandler = new FragmentOnBackPressedHandler(activity, new StatefulViews$setupUnsavedChangesConfirmation$$inlined$let$lambda$1(this));
} else {
fragmentOnBackPressedHandler = null;
}
AppFragment.setOnBackPressed$default(appFragment, new StatefulViews$setupUnsavedChangesConfirmation$1(fragmentOnBackPressedHandler), 0, 2, null);
}
}