This commit is contained in:
2025-04-08 17:04:02 +02:00
parent 57766af52b
commit ce1fe22bdf

View File

@@ -196,23 +196,21 @@ class AstromicFormController extends FormController {
} }
int getInstanceCount(String targetGroupID) { int getInstanceCount(String targetGroupID) {
String? p = getFullPathOfGroup(targetGroupID);
if (p != null && p.split('->').length > 1 && p.split('->').indexOf(targetGroupID) != 0) {
// This is a subgroup
}
FormGroupStructure? structure = getGroupStructure(targetGroupID); FormGroupStructure? structure = getGroupStructure(targetGroupID);
if (structure != null) { if (structure != null) {
String firstField = structure.fields.first; String firstField = structure.fields.first;
// return controllers.keys.where((String c) => RegExp(standeredGroupFormat(targetGroupID, r'[\d+]', firstField)).hasMatch(c)).nonNulls.toList().length; String? p = getFullPathOfGroup(targetGroupID);
return controllers.keys late RegExp pattern;
.where((String c) { if (p != null && p.split('->').length > 1 && p.split('->').indexOf(targetGroupID) != 0) {
// Match the specific group ID and field name within the group // This is a subgroup
final RegExp pattern = RegExp(r'^' + RegExp.escape(standeredGroupFormat(targetGroupID, r'[\d+]', firstField)) + r'$'); pattern = RegExp(r'^' +
return pattern.hasMatch(c); p.split('->').sublist(0, p.split('->').indexOf(targetGroupID)).map((a) => standeredGroupFormat(a, '0', '')).toList().join('') +
}) standeredGroupFormat(targetGroupID, r'[\d+]', firstField) +
.nonNulls r'$');
.toList() } else {
.length; pattern = RegExp(r'^' + standeredGroupFormat(targetGroupID, r'[\d+]', firstField) + r'$');
}
return controllers.keys.where((String c) => pattern.hasMatch(c)).nonNulls.toList().length;
} }
return 0; return 0;
} }
@@ -278,6 +276,44 @@ class AstromicFormController extends FormController {
_initializeGroupControllersRecursively(groupStructure, initialCount); _initializeGroupControllersRecursively(groupStructure, initialCount);
} }
/// Get the target formGroup Value.
FormGroupValue? getFormGroupValue(String formGroupID) {
// Get the group structure with the ID
FormGroupStructure? groupStructure = getGroupStructure(formGroupID);
// Get the current fields with this ID
int instancesCount = getInstanceCount(formGroupID);
// get the fields IDs
List<String> fieldsIDs = groupStructure!.fields.nonNulls.toList();
// get the values IDs
List<String> valuesIDs = groupStructure.values?.nonNulls.toList() ?? <String>[];
// get the subGroups
List<FormGroupValue> subValues = <FormGroupValue>[];
if (groupStructure.subGroups != null && groupStructure.subGroups!.isNotEmpty) {
subValues = groupStructure.subGroups!.map(((int, FormGroupStructure) s) => getFormGroupValue(s.$2.id)).nonNulls.toList();
}
List<FormGroupInstance> instances = <FormGroupInstance>[];
for (int i = 0; i < instancesCount; i++) {
instances.add(
FormGroupInstance(
composedID: standeredGroupFormat(formGroupID, i.toString(), ''),
fields: Map<String, String>.fromEntries(fieldsIDs.map((String id) => MapEntry<String, String>(standeredGroupFormat(formGroupID, i.toString(), id),value(standeredGroupFormat(formGroupID, i.toString(), id)))).toList()),
values: valuesIDs.isNotEmpty
? Map<String, dynamic>.fromEntries(valuesIDs.map((String id) => MapEntry<String, dynamic>(standeredGroupFormat(formGroupID, i.toString(), id), getValue<dynamic>(standeredGroupFormat(formGroupID, i.toString(), id)))).toList())
: <String, dynamic>{},
subGroups: subValues,
),
);
}
FormGroupValue groupValue = FormGroupValue(groupID: formGroupID, instancesCount: instancesCount, instances: instances);
return groupValue;
}
// _formController.initializeFormGroup( // _formController.initializeFormGroup(
// FormGroupStructure( // FormGroupStructure(
// id: 'mainGroup', // id: 'mainGroup',
@@ -514,52 +550,6 @@ class AstromicFormController extends FormController {
// ); // );
// } // }
FormGroupValue? getFormGroupValue(String formGroupID, {bool isSubGroup = false}) {
// Get the group structure with the ID
FormGroupStructure? groupStructure = getGroupStructure(formGroupID);
print('Got the structure: $groupStructure');
// Get the current fields with this ID
int instancesCount = getInstanceCount(formGroupID);
print('Got the instancesCount: $instancesCount');
// Map<String, String> firstSetOfFieldsWithID = Map<String, String>.fromEntries(
// controllers.entries.where((MapEntry<String, String> c) => RegExp(formGroupID + r'-#[\d+]-' + groupStructure!.fields.first).hasMatch(c.key)).nonNulls.toList(),
// );
// print('First set of fields: $firstSetOfFieldsWithID');
// get the fields IDs
List<String> fieldsIDs = groupStructure!.fields.nonNulls.toList();
print('fieldIDs: $fieldsIDs');
// get the values IDs
List<String> valuesIDs = groupStructure.values?.nonNulls.toList() ?? <String>[];
print('valueIDs: $valuesIDs');
// get the subGroups
List<FormGroupValue> subValues = <FormGroupValue>[];
if (groupStructure.subGroups != null && groupStructure.subGroups!.isNotEmpty) {
subValues = groupStructure.subGroups!.map(((int, FormGroupStructure) s) => getFormGroupValue(s.$2.id, isSubGroup: true)).nonNulls.toList();
}
print('subValues: $subValues');
List<FormGroupInstance> instances = <FormGroupInstance>[];
for (int i = 0; i < instancesCount; i++) {
instances.add(
FormGroupInstance(
composedID: '$formGroupID-#$i-',
fields: Map<String, String>.fromEntries(fieldsIDs.map((String id) => MapEntry<String, String>('$formGroupID-#$i-$id', value('$formGroupID-#$i-$id'))).toList()),
values: valuesIDs.isNotEmpty
? Map<String, dynamic>.fromEntries(valuesIDs.map((String a) => MapEntry<String, dynamic>('$formGroupID-#$i-$a', getValue<dynamic>('$formGroupID-#$i-$a'))).toList())
: <String, dynamic>{},
subGroups: subValues,
),
);
}
FormGroupValue groupValue = FormGroupValue(groupID: formGroupID, instancesCount: instancesCount, instances: instances);
return groupValue;
}
// void removeInstanceFromFormGroup(String formGroupID, int indexToRemove, {bool isSubGroup = false}) { // void removeInstanceFromFormGroup(String formGroupID, int indexToRemove, {bool isSubGroup = false}) {
// // Get the group structure with the same ID // // Get the group structure with the same ID
// FormGroupStructure? groupStructure; // FormGroupStructure? groupStructure;
@@ -626,7 +616,7 @@ class AstromicFormController extends FormController {
//!SECTION //!SECTION
//SECTION - Helper Methods //SECTION - Helper Methods
String standeredGroupFormat(String groupID, String groupIndex, String? secondaryID) => '$groupID-#$groupIndex-${secondaryID ?? ""}'; String standeredGroupFormat(String groupID, String groupIndex, String? secondaryID) => '$groupID-#$groupIndex${secondaryID == null ? "" : "-$secondaryID"}';
void _addInitialControllers(Map<String, (String, bool)>? initialValues) { void _addInitialControllers(Map<String, (String, bool)>? initialValues) {
if (initialValues != null) { if (initialValues != null) {