Add better error dialog

This commit is contained in:
MrLetsplay 2023-09-29 22:55:04 +02:00
parent 54a92727e8
commit 7e44ffc915
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
2 changed files with 89 additions and 6 deletions

View File

@ -14,6 +14,7 @@ import androidx.core.util.Consumer;
import com.cringe_studios.cringe_authenticator.R; import com.cringe_studios.cringe_authenticator.R;
import com.cringe_studios.cringe_authenticator.backup.BackupData; import com.cringe_studios.cringe_authenticator.backup.BackupData;
import com.cringe_studios.cringe_authenticator.databinding.DialogCreateGroupBinding; import com.cringe_studios.cringe_authenticator.databinding.DialogCreateGroupBinding;
import com.cringe_studios.cringe_authenticator.databinding.DialogErrorBinding;
import com.cringe_studios.cringe_authenticator.databinding.DialogInputCodeHotpBinding; import com.cringe_studios.cringe_authenticator.databinding.DialogInputCodeHotpBinding;
import com.cringe_studios.cringe_authenticator.databinding.DialogInputCodeTotpBinding; import com.cringe_studios.cringe_authenticator.databinding.DialogInputCodeTotpBinding;
import com.cringe_studios.cringe_authenticator.databinding.DialogInputPasswordBinding; import com.cringe_studios.cringe_authenticator.databinding.DialogInputPasswordBinding;
@ -48,13 +49,36 @@ public class DialogUtil {
dialog.show(); dialog.show();
} }
public static void showErrorDialog(Context context, String errorMessage, Runnable closed) { public static void showErrorDialog(Context context, String errorMessage, String details, Runnable closed) {
new StyledDialogBuilder(context) DialogErrorBinding binding = DialogErrorBinding.inflate(LayoutInflater.from(context));
binding.errorMessage.setText(errorMessage);
AlertDialog.Builder b = new StyledDialogBuilder(context)
.setTitle(R.string.failed_title) .setTitle(R.string.failed_title)
.setMessage(errorMessage) .setView(binding.getRoot())
.setPositiveButton(R.string.ok, (d, which) -> {}) .setPositiveButton(R.string.ok, (d, which) -> {})
.setOnDismissListener(d -> { if(closed != null) closed.run(); }) .setOnDismissListener(d -> { if(closed != null) closed.run(); });
.show();
if(details != null) {
binding.errorDetailsText.setText(details);
b.setNeutralButton("Details", (d, which) -> {});
}
AlertDialog dialog = b.create();
if(details != null) {
dialog.setOnShowListener(d -> {
Button detailsButton = dialog.getButton(AlertDialog.BUTTON_NEUTRAL);
detailsButton.setOnClickListener(v -> binding.errorDetails.setVisibility(binding.errorDetails.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE));
});
}
dialog.show();
}
public static void showErrorDialog(Context context, String errorMessage, Runnable closed) {
showErrorDialog(context, errorMessage, null, closed);
} }
public static void showErrorDialog(Context context, String errorMessage) { public static void showErrorDialog(Context context, String errorMessage) {
@ -62,7 +86,22 @@ public class DialogUtil {
} }
public static void showErrorDialog(Context context, String errorMessage, Exception exception) { public static void showErrorDialog(Context context, String errorMessage, Exception exception) {
showErrorDialog(context, errorMessage + ": " + exception.toString(), (Runnable) null); // TODO: exception details button showErrorDialog(context, errorMessage, stackTraceToString(exception), (Runnable) null);
}
private static String stackTraceToString(Throwable t) {
StringBuilder b = new StringBuilder();
b.append(t.toString()).append('\n');
for(StackTraceElement e : t.getStackTrace()) {
b.append(" ").append(e.toString()).append('\n');
}
if(t.getCause() != null) {
b.append("Caused by: ").append(stackTraceToString(t.getCause()));
}
return b.toString().trim();
} }
public static void showTOTPDialog(LayoutInflater inflater, OTPData initialData, Consumer<OTPData> callback, boolean view) { public static void showTOTPDialog(LayoutInflater inflater, OTPData initialData, Consumer<OTPData> callback, boolean view) {

View File

@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/error_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="Failed to do the thing you wanted to do" />
<LinearLayout
android:id="@+id/error_details"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone"
tools:visibility="visible">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Details"
android:textStyle="bold" />
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/error_details_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Lorem ipsum dolor sit amet something went wrong and we don't know why" />
</HorizontalScrollView>
</LinearLayout>
</LinearLayout>