Files
RidgeScout/app/src/main/java/com/ridgebotics/ridgescout/utility/AlertManager.java
T

114 lines
3.5 KiB
Java
Raw Normal View History

2024-09-15 22:47:45 -06:00
package com.ridgebotics.ridgescout.utility;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
2025-03-24 12:41:43 -06:00
import android.content.DialogInterface;
import android.widget.Toast;
import java.io.PrintWriter;
import java.io.StringWriter;
2025-03-24 12:41:43 -06:00
import java.util.ArrayList;
import java.util.List;
public class AlertManager {
public static Context context;
public static void init(Context c){
context = c;
}
2025-03-24 12:41:43 -06:00
private static AlertDialog currentError;
private static final List<String> simpleErrorList = new ArrayList<>();
private static final List<String> errorList = new ArrayList<>();
public static void alert(String title, String content) {
2025-03-24 12:41:43 -06:00
((Activity) context).runOnUiThread(() -> {
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle(title);
2025-03-24 12:41:43 -06:00
alert.setMessage(content);
alert.setPositiveButton("OK", null);
alert.setCancelable(true);
alert.create().show();
2025-03-24 12:41:43 -06:00
});
}
public static void toast(String content) {
2025-03-24 12:41:43 -06:00
((Activity) context).runOnUiThread(() -> Toast.makeText(context, content, Toast.LENGTH_LONG).show());
}
public static void addSimpleError(String error) {
simpleErrorList.add(error);
updateErrors();
}
public static void error(String content) {
2025-03-24 12:41:43 -06:00
errorList.add(content);
updateErrors();
}
2025-03-24 12:41:43 -06:00
public static void error(String title, String content) {
simpleErrorList.add(title);
errorList.add(content);
updateErrors();
}
public static void error(Exception e) {
e.printStackTrace();
2025-04-08 12:27:09 -06:00
// simpleErrorList.add(e.getMessage());
2025-03-24 12:41:43 -06:00
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
2025-04-09 18:35:28 -06:00
errorList.add(sw.getBuffer().toString());
2025-03-24 12:41:43 -06:00
updateErrors();
}
public static void error(String title, Exception e) {
simpleErrorList.add(title);
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
2025-04-09 18:35:28 -06:00
errorList.add(sw.toString());
e.printStackTrace();
2025-03-24 12:41:43 -06:00
updateErrors();
}
public static void updateErrors(){
2025-02-20 12:57:14 -07:00
((Activity) context).runOnUiThread(() -> {
2025-03-24 12:41:43 -06:00
if(currentError != null && currentError.isShowing()){
DialogInterface tmp = currentError;
currentError = null;
tmp.dismiss();
}
2025-02-20 12:57:14 -07:00
AlertDialog.Builder alert = new AlertDialog.Builder(context);
2025-03-24 12:41:43 -06:00
if(!simpleErrorList.isEmpty())
alert.setTitle(simpleErrorList.get(0) + (simpleErrorList.size() > 1 ? "..." : ""));
else
alert.setTitle(errorList.size() + " Error" + (errorList.size() != 1 ? "s" : "") + "!");
if(simpleErrorList.size() > 1)
alert.setMessage(String.join("\n", simpleErrorList));
alert.setPositiveButton("OK", (dialogInterface, i) -> {if(currentError != null){errorList.clear(); simpleErrorList.clear();}});
2025-04-08 12:27:09 -06:00
2025-04-09 18:35:28 -06:00
String detailedErrors = String.join("\n\n\n\n\n", errorList);
2025-03-24 12:41:43 -06:00
if(!errorList.isEmpty())
2025-04-09 18:35:28 -06:00
alert.setNeutralButton("View Detailed Error" + (errorList.size() != 1 ? "s" : ""), (dialogInterface, i) -> alert("Details", detailedErrors));
2025-04-08 12:27:09 -06:00
2025-04-13 10:45:33 -06:00
// alert.setOnDismissListener((x) -> {if(currentError != null){errorList.clear(); simpleErrorList.clear();}});
2025-03-24 12:41:43 -06:00
2025-02-20 12:57:14 -07:00
alert.setCancelable(true);
2025-03-24 12:41:43 -06:00
currentError = alert.create();
currentError.show();
});
}
}