2024-09-15 22:47:45 -06:00
|
|
|
package com.ridgebotics.ridgescout.utility;
|
2024-07-24 15:27:13 -06:00
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
|
import android.app.AlertDialog;
|
|
|
|
|
import android.content.Context;
|
2025-03-24 12:41:43 -06:00
|
|
|
import android.content.DialogInterface;
|
2024-07-24 15:27:13 -06:00
|
|
|
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;
|
2024-07-24 15:27:13 -06:00
|
|
|
|
|
|
|
|
public class AlertManager {
|
2024-07-24 23:41:00 -06:00
|
|
|
public static Context context;
|
2024-07-24 15:27:13 -06:00
|
|
|
|
|
|
|
|
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<>();
|
|
|
|
|
|
2024-07-24 15:27:13 -06:00
|
|
|
public static void alert(String title, String content) {
|
2025-03-24 12:41:43 -06:00
|
|
|
|
|
|
|
|
((Activity) context).runOnUiThread(() -> {
|
2024-07-24 15:27:13 -06:00
|
|
|
AlertDialog.Builder alert = new AlertDialog.Builder(context);
|
|
|
|
|
alert.setTitle(title);
|
2025-03-24 12:41:43 -06:00
|
|
|
alert.setMessage(content);
|
2024-07-24 15:27:13 -06:00
|
|
|
alert.setPositiveButton("OK", null);
|
|
|
|
|
alert.setCancelable(true);
|
|
|
|
|
|
|
|
|
|
alert.create().show();
|
2025-03-24 12:41:43 -06:00
|
|
|
});
|
2024-07-24 15:27:13 -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();
|
2024-07-24 15:27:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void error(String content) {
|
2025-03-24 12:41:43 -06:00
|
|
|
errorList.add(content);
|
|
|
|
|
updateErrors();
|
|
|
|
|
}
|
2024-07-24 15:27:13 -06:00
|
|
|
|
2025-03-24 12:41:43 -06:00
|
|
|
public static void error(String title, String content) {
|
|
|
|
|
simpleErrorList.add(title);
|
|
|
|
|
errorList.add(content);
|
|
|
|
|
updateErrors();
|
2024-07-24 15:27:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void error(Exception e) {
|
2024-07-24 23:41:00 -06:00
|
|
|
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));
|
|
|
|
|
|
|
|
|
|
errorList.add((sw.toString()));
|
|
|
|
|
updateErrors();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void error(String title, Exception e) {
|
|
|
|
|
simpleErrorList.add(title);
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
|
|
StringWriter sw = new StringWriter();
|
|
|
|
|
e.printStackTrace(new PrintWriter(sw));
|
|
|
|
|
|
|
|
|
|
errorList.add((sw.toString()));
|
|
|
|
|
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();
|
|
|
|
|
}
|
2024-07-24 15:27:13 -06:00
|
|
|
|
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-03-24 12:41:43 -06:00
|
|
|
if(!errorList.isEmpty())
|
|
|
|
|
alert.setNeutralButton("View Detailed Error" + (errorList.size() != 1 ? "s" : ""), (dialogInterface, i) -> alert(errorList.size() + " Error" + (errorList.size() != 1 ? "s" : "") + ":", String.join("\n\n\n\n\n", errorList)));
|
2025-04-08 12:27:09 -06:00
|
|
|
|
2025-03-24 12:41:43 -06:00
|
|
|
alert.setOnDismissListener((x) -> {if(currentError != null){errorList.clear(); simpleErrorList.clear();}});
|
|
|
|
|
|
|
|
|
|
|
2025-02-20 12:57:14 -07:00
|
|
|
alert.setCancelable(true);
|
2024-07-24 15:27:13 -06:00
|
|
|
|
2025-03-24 12:41:43 -06:00
|
|
|
currentError = alert.create();
|
|
|
|
|
|
|
|
|
|
currentError.show();
|
2024-07-24 15:27:13 -06:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|