Add revert button

This commit is contained in:
Michael Mikovsky
2025-04-08 12:27:09 -06:00
parent 465b94ac7e
commit 2dfad418e3
4 changed files with 35 additions and 4 deletions
@@ -12,6 +12,7 @@ import com.ridgebotics.ridgescout.utility.FileEditor;
import com.ridgebotics.ridgescout.utility.BuiltByteParser;
import com.ridgebotics.ridgescout.utility.ByteBuilder;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
@@ -58,11 +59,18 @@ public class ScoutingDataWriter {
public static ParsedScoutingDataResult load(String filename, FieldType[][] values , TransferType[][] transferValues){
byte[] bytes = FileEditor.readFile(filename);
BuiltByteParser bbp = new BuiltByteParser(bytes);
try {
ArrayList<BuiltByteParser.parsedObject> objects = bbp.parse();
DataType[] dataTypes = new DataType[objects.size()-2];
int version = ((int)objects.get(0).get());
if(values.length <= version) {
AlertManager.addSimpleError("Field version (" +version + ") is too recent as compared to latest version (" + (values.length-1) + ")!");
throw new BuiltByteParser.byteParsingExeption();
}
// System.out.println(version);
String username = (String) objects.get(1).get();
@@ -134,7 +134,10 @@ public class FieldsFragment extends Fragment {
binding.saveButton.setOnClickListener(l -> save());
// binding.previewButton.setOnClickListener(v -> startPreview());
if(tmp_values.length > 1)
binding.revertButton.setOnClickListener(v -> revertPopup());
else
binding.revertButton.setEnabled(false);
return binding.getRoot();
@@ -259,8 +262,8 @@ public class FieldsFragment extends Fragment {
private void save(){
AlertDialog.Builder alert = new AlertDialog.Builder(getContext());
alert.setTitle("Warning!");
alert.setMessage("Changing or removing some values will result in lost data!\nBut this will create a new field version, and you can revert at any time.");
alert.setPositiveButton("OK", (dialog, which) -> {
alert.setMessage("Changing or removing some values will result in lost data! but you can revert at any time.");
alert.setNeutralButton("Save", (dialog, which) -> {
FieldType[][] currentValues = Fields.load(filename);
assert currentValues != null;
FieldType[][] newValues = new FieldType[currentValues.length+1][];
@@ -286,6 +289,23 @@ public class FieldsFragment extends Fragment {
}
public void revertPopup(){
AlertDialog.Builder alert = new AlertDialog.Builder(getContext());
alert.setTitle("Warning!");
alert.setMessage("If there is any data scouted with this version of the fields, it will cause conflicts!\nYou should know what you are doing");
alert.setNeutralButton("Revert and delete version", (dialog, which) -> {
FieldType[][] currentValues = Fields.load(filename);
assert currentValues != null;
FieldType[][] newValues = new FieldType[currentValues.length-1][];
System.arraycopy(currentValues, 0, newValues, 0, currentValues.length - 1);
if(Fields.save(filename, newValues))
AlertManager.toast("Saved");
Navigation.findNavController((Activity) getContext(), R.id.nav_host_fragment_activity_main).navigate(R.id.action_navigation_data_fields_to_navigation_settings);
});
alert.setNegativeButton("Cancel", null);
alert.setCancelable(true);
alert.create().show();
}
}
@@ -57,6 +57,7 @@ public class AlertManager {
public static void error(Exception e) {
e.printStackTrace();
// simpleErrorList.add(e.getMessage());
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
@@ -95,8 +96,10 @@ public class AlertManager {
alert.setMessage(String.join("\n", simpleErrorList));
alert.setPositiveButton("OK", (dialogInterface, i) -> {if(currentError != null){errorList.clear(); simpleErrorList.clear();}});
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)));
alert.setOnDismissListener((x) -> {if(currentError != null){errorList.clear(); simpleErrorList.clear();}});
@@ -13,7 +13,7 @@ public class BuiltByteParser {
public static final Integer stringArrayType = 4;
public static final Integer longType = 5;
public class byteParsingExeption extends Exception {
public static class byteParsingExeption extends Exception {
public byteParsingExeption() {}
public byteParsingExeption(String message) {
super(message);