This commit is contained in:
2025-04-12 15:09:35 +02:00
parent e47d957f7a
commit abbe07c500

View File

@@ -121,6 +121,14 @@ class AstromicFormController extends FormController {
_hostedValueValidationStreamController.add((id, false)); _hostedValueValidationStreamController.add((id, false));
} }
/// Remove the value of a hosted state variable using it's ID.
void removeValue(String id) {
if (!_hostedValues.keys.toList().contains(id)) {
throw Exception('Value of id `$id` not found to be removed.');
}
_hostedValues.remove(id);
}
/// Validate hosted values. /// Validate hosted values.
bool validateValues(List<String> valueIDs) { bool validateValues(List<String> valueIDs) {
for (String hostedValueID in valueIDs) { for (String hostedValueID in valueIDs) {
@@ -331,35 +339,34 @@ class AstromicFormController extends FormController {
return fullPath != null && fullPath.length > 1 && fullPath.indexOf(formGroupID) != 0; return fullPath != null && fullPath.length > 1 && fullPath.indexOf(formGroupID) != 0;
} }
String addInstanceToFormGroup(String formGroupID, {Map<String, int>? parentsPrefixes}) { String addInstanceToFormGroup(String formGroupID, {Map<String, int>? parents}) {
// Get the group structure with the same ID // Get the group structure with the same ID
FormGroupStructure? structure = getGroupStructure(formGroupID); FormGroupStructure? structure = getGroupStructure(formGroupID);
assert(structure != null, 'The Group $formGroupID doesn\'t seem to be found, are you sure you initialized it?'); assert(structure != null, 'The Group $formGroupID doesn\'t seem to be found, are you sure you initialized it?');
// Get current instances of this group // Get the prefix for subgroups if exists
int currentGroupInstances = getInstanceCount(formGroupID); String? prefix;
int newGroupIndex = currentGroupInstances + 1; if (isASubGroup(formGroupID) && parents != null) {
prefix = parents.entries.map((MapEntry<String, int> parentEntry) => standeredGroupFormat(parentEntry.key, parentEntry.value.toString(), null)).join('-');
if (structure!.subGroups != null && structure.subGroups!.isNotEmpty) {
for ((int gI, FormGroupStructure gS) f in structure.subGroups!) {
addInstanceToFormGroup(formGroupID, parentsPrefixes: {
...?parentsPrefixes,
...{formGroupID: newGroupIndex},
});
}
} }
String prefixedGroup = parentsPrefixes?.entries.map((MapEntry<String, int> p) => standeredGroupFormat(p.key, p.value.toString(), null)).toList().join('-') ?? ''; // Get current instances of this group and the new index.
int currentGroupInstances = getInstanceCount(formGroupID);
int newGroupIndex = currentGroupInstances;
// Add the controllers and values. // Add the controllers and values.
for (String fieldID in structure.fields.nonNulls) { for (String fieldID in structure!.fields.nonNulls) {
controller(prefixedGroup + standeredGroupFormat(formGroupID, newGroupIndex.toString(), fieldID)); String p = (prefix != null ? '$prefix-' : '') + standeredGroupFormat(formGroupID, newGroupIndex.toString(), fieldID);
controller(p);
} }
if (structure.values != null && structure.values!.isNotEmpty) { if (structure.values != null && structure.values!.isNotEmpty) {
for (String valueID in structure.values!.nonNulls) { for (String valueID in structure.values!.nonNulls) {
controller(prefixedGroup + standeredGroupFormat(formGroupID, newGroupIndex.toString(), valueID)); String p = (prefix != null ? '$prefix-' : '') + standeredGroupFormat(formGroupID, newGroupIndex.toString(), valueID);
controller(p);
} }
} }
return prefixedGroup + standeredGroupFormat(formGroupID, newGroupIndex.toString(), ''); String p = (prefix != null ? '$prefix-' : '') + standeredGroupFormat(formGroupID, newGroupIndex.toString(), '');
return p;
} }
// void removeInstanceFromFormGroup(String formGroupID, int indexToRemove, {bool isSubGroup = false}) { // void removeInstanceFromFormGroup(String formGroupID, int indexToRemove, {bool isSubGroup = false}) {