This commit is contained in:
2025-04-08 15:53:15 +02:00
parent 91feadff64
commit 6bfcd81ac4
2 changed files with 51 additions and 51 deletions

View File

@@ -147,8 +147,8 @@ class AstromicFormController extends FormController {
if (group.id == targetGroupID) return '$currentPath${group.id}'; if (group.id == targetGroupID) return '$currentPath${group.id}';
// Otherwise, check in its subgroups recursively // Otherwise, check in its subgroups recursively
for (final (FormGroupStructure, int) subGroup in group.subGroups ?? <(FormGroupStructure, int)>[]) { for (final (int, FormGroupStructure) subGroup in group.subGroups ?? <(int, FormGroupStructure)>[]) {
final String? subGroupPath = getFullPathOfGroup(targetGroupID, formGroups: <FormGroupStructure>[subGroup.$1], currentPath: '$currentPath${group.id}->'); final String? subGroupPath = getFullPathOfGroup(targetGroupID, formGroups: <FormGroupStructure>[subGroup.$2], currentPath: '$currentPath${group.id}->');
// Return the path if found // Return the path if found
if (subGroupPath != null) { if (subGroupPath != null) {
return subGroupPath; return subGroupPath;
@@ -219,11 +219,11 @@ class AstromicFormController extends FormController {
final String segment = pathSegments[i]; final String segment = pathSegments[i];
// Search for the subgroup within the current group // Search for the subgroup within the current group
final (FormGroupStructure, int)? subGroup = currentGroup?.subGroups?.where(((FormGroupStructure, int) subGroup) => subGroup.$1.id == segment).firstOrNull; final (int, FormGroupStructure)? subGroup = currentGroup?.subGroups?.where(((int, FormGroupStructure) subGroup) => subGroup.$2.id == segment).firstOrNull;
// If a subgroup is found, update currentGroup to the subgroup // If a subgroup is found, update currentGroup to the subgroup
if (subGroup != null) { if (subGroup != null) {
currentGroup = subGroup.$1; currentGroup = subGroup.$2;
} else { } else {
// If no subgroup is found at this level, return null // If no subgroup is found at this level, return null
return null; return null;
@@ -272,20 +272,6 @@ class AstromicFormController extends FormController {
// } // }
// } // }
void _addGroupControllers(FormGroupStructure structure, int index, {String? parentPrefix}) {
final String baseID = parentPrefix ?? structure.id;
for (final String fieldID in structure.fields) {
final String fullID = standeredGroupFormat(baseID, index, fieldID);
controller(fullID);
}
for (final String valueID in structure.values ?? <String>[]) {
final String fullID = standeredGroupFormat(baseID, index, valueID);
controller(fullID);
}
}
/// Initialize the formGroup Structure. /// Initialize the formGroup Structure.
// void initializeFormGroup(FormGroupStructure groupStructure, {int initialCount = 1}) { // void initializeFormGroup(FormGroupStructure groupStructure, {int initialCount = 1}) {
// assert(groupStructure.fields.isNotEmpty, '${groupStructure.id}: Group fields should NOT be empty.'); // assert(groupStructure.fields.isNotEmpty, '${groupStructure.id}: Group fields should NOT be empty.');
@@ -314,52 +300,66 @@ class AstromicFormController extends FormController {
// } // }
// } // }
/// Validate subgroups recursively
void _validateSubGroupsRecursively(FormGroupStructure groupStructure) {
groupStructure.subGroups?.forEach(((int, FormGroupStructure) subGroupTuple) {
final FormGroupStructure subGroup = subGroupTuple.$2;
assert(subGroup.fields.isNotEmpty, '${subGroup.id}: Subgroup fields should NOT be empty.');
// Recursively validate subgroups of subgroups
if (subGroup.subGroups != null) {
_validateSubGroupsRecursively(subGroup);
}
});
}
/// Add controllers
void _addGroupControllers(FormGroupStructure structure, int index, {String? parentPrefix}) {
final String baseID = parentPrefix ?? structure.id;
for (final String fieldID in structure.fields) {
final String fullID = standeredGroupFormat(baseID, index, fieldID);
controller(fullID);
}
for (final String valueID in structure.values ?? <String>[]) {
final String fullID = standeredGroupFormat(baseID, index, valueID);
controller(fullID);
}
}
/// Recursively initialize controllers for the group and its subgroups
void _initializeGroupControllersRecursively(FormGroupStructure groupStructure, int initialCount, {String parentPrefix = ''}) {
// Add main group fields/values
for (int groupIndex = 0; groupIndex < initialCount; groupIndex++) {
_addGroupControllers(groupStructure, groupIndex, parentPrefix: parentPrefix.isEmpty ? null : parentPrefix);
// Recursively handle subgroups
if (groupStructure.subGroups != null && groupStructure.subGroups!.isNotEmpty) {
for (final (int subgroupInitialCount, FormGroupStructure subGroup) in groupStructure.subGroups!) {
final String subgroupPrefix =
parentPrefix.isEmpty ? standeredGroupFormat(groupStructure.id, groupIndex, subGroup.id) : standeredGroupFormat(parentPrefix, groupIndex, subGroup.id); // Add to parentPrefix only once
// Initialize subgroup controllers recursively
for (int subIndex = 0; subIndex < subgroupInitialCount; subIndex++) {
_initializeGroupControllersRecursively(subGroup, subgroupInitialCount, parentPrefix: subgroupPrefix);
}
}
}
}
}
void initializeFormGroup(FormGroupStructure groupStructure, {int initialCount = 1}) { void initializeFormGroup(FormGroupStructure groupStructure, {int initialCount = 1}) {
assert(groupStructure.fields.isNotEmpty, '${groupStructure.id}: Group fields should NOT be empty.'); assert(groupStructure.fields.isNotEmpty, '${groupStructure.id}: Group fields should NOT be empty.');
// Validate subgroups (if any) // Validate subgroups (if any)
_validateSubGroups(groupStructure); _validateSubGroupsRecursively(groupStructure);
// Add structure to registry // Add structure to registry
_formGroups.add(groupStructure); _formGroups.add(groupStructure);
// Initialize the group instances // Initialize the group instances
_initializeGroupControllers(groupStructure, initialCount); _initializeGroupControllersRecursively(groupStructure, initialCount);
}
/// Recursively initialize controllers for the group and its subgroups
void _initializeGroupControllers(FormGroupStructure groupStructure, int initialCount, {String parentPrefix = ''}) {
// Add main group fields/values
for (int groupIndex = 0; groupIndex < initialCount; groupIndex++) {
_addGroupControllers(groupStructure, groupIndex, parentPrefix: parentPrefix);
// Recursively handle subgroups
if (groupStructure.subGroups != null && groupStructure.subGroups!.isNotEmpty) {
for (final (FormGroupStructure subGroup, int subgroupInitialCount) in groupStructure.subGroups!) {
final String subgroupPrefix = parentPrefix.isEmpty
? standeredGroupFormat(groupStructure.id, groupIndex, subGroup.id)
: '$parentPrefix->${standeredGroupFormat(groupStructure.id, groupIndex, subGroup.id)}'; // Add to parentPrefix only once
// Initialize subgroup controllers recursively
for (int subIndex = 0; subIndex < subgroupInitialCount; subIndex++) {
_initializeGroupControllers(subGroup, subgroupInitialCount, parentPrefix: subgroupPrefix);
}
}
}
}
}
/// Validate subgroups recursively
void _validateSubGroups(FormGroupStructure groupStructure) {
groupStructure.subGroups?.forEach((subGroupTuple) {
final subGroup = subGroupTuple.$1;
assert(subGroup.fields.isNotEmpty, '${subGroup.id}: Subgroup fields should NOT be empty.');
// Recursively validate subgroups of subgroups
if (subGroup.subGroups != null) {
_validateSubGroups(subGroup);
}
});
} }
// void initializeFormGroup(FormGroupStructure groupStructure, {int initialCount = 1}) { // void initializeFormGroup(FormGroupStructure groupStructure, {int initialCount = 1}) {
@@ -508,8 +508,8 @@ class AstromicFormController extends FormController {
// Recursively count instances in subgroups // Recursively count instances in subgroups
if (groupStructure.subGroups != null) { if (groupStructure.subGroups != null) {
for ((FormGroupStructure, int) subGroup in groupStructure.subGroups!) { for ((int, FormGroupStructure) subGroup in groupStructure.subGroups!) {
instanceCount += countInstancesRecursively(subGroup.$1, controllersKeys); instanceCount += countInstancesRecursively(subGroup.$2, controllersKeys);
} }
} }
@@ -551,7 +551,7 @@ class AstromicFormController extends FormController {
// get the subGroups // get the subGroups
List<FormGroupValue> subValues = <FormGroupValue>[]; List<FormGroupValue> subValues = <FormGroupValue>[];
if (groupStructure.subGroups != null && groupStructure.subGroups!.isNotEmpty) { if (groupStructure.subGroups != null && groupStructure.subGroups!.isNotEmpty) {
subValues = groupStructure.subGroups!.map(((FormGroupStructure, int) s) => getFormGroupValue(s.$1.id, isSubGroup: true)).nonNulls.toList(); subValues = groupStructure.subGroups!.map(((int, FormGroupStructure) s) => getFormGroupValue(s.$2.id, isSubGroup: true)).nonNulls.toList();
} }
print('subValues: $subValues'); print('subValues: $subValues');
@@ -639,7 +639,7 @@ class AstromicFormController extends FormController {
//!SECTION //!SECTION
//SECTION - Helper Methods //SECTION - Helper Methods
String standeredGroupFormat(String groupID, int groupIndex, String? fieldID) => '$groupID-#$groupIndex-${fieldID ?? ""}'; String standeredGroupFormat(String groupID, int groupIndex, String? secondaryID) => '$groupID-#$groupIndex-${secondaryID ?? ""}';
void _addInitialControllers(Map<String, (String, bool)>? initialValues) { void _addInitialControllers(Map<String, (String, bool)>? initialValues) {
if (initialValues != null) { if (initialValues != null) {

View File

@@ -6,7 +6,7 @@ class FormGroupStructure {
final String id; final String id;
final List<String> fields; final List<String> fields;
final List<String>? values; final List<String>? values;
final List<(FormGroupStructure structure, int initialCount)>? subGroups; final List<(int initialCount, FormGroupStructure structure)>? subGroups;
FormGroupStructure({ FormGroupStructure({
required this.id, required this.id,
required this.fields, required this.fields,
@@ -18,7 +18,7 @@ class FormGroupStructure {
String? id, String? id,
List<String>? fields, List<String>? fields,
List<String>? values, List<String>? values,
List<(FormGroupStructure structure, int initialCount)>? subGroups, List<(int initialCount, FormGroupStructure structure)>? subGroups,
}) { }) {
return FormGroupStructure( return FormGroupStructure(
id: id ?? this.id, id: id ?? this.id,
@@ -33,7 +33,7 @@ class FormGroupStructure {
'id': id, 'id': id,
'fields': fields, 'fields': fields,
'values': values, 'values': values,
'subGroups': subGroups?.map(((FormGroupStructure structure, int initialCount) x) => <String, dynamic>{'structure': x.$1.toMap(), 'initialCount': x.$2}).toList(), 'subGroups': subGroups?.map(((int initialCount, FormGroupStructure structure) x) => <String, dynamic>{'structure': x.$2.toMap(), 'initialCount': x.$1}).toList(),
}; };
} }
@@ -43,7 +43,7 @@ class FormGroupStructure {
fields: List<String>.from(map['fields'] as List<String>), fields: List<String>.from(map['fields'] as List<String>),
values: map['values'] != null ? List<String>.from(map['values'] as List<String>) : null, values: map['values'] != null ? List<String>.from(map['values'] as List<String>) : null,
subGroups: map['subGroups'] != null subGroups: map['subGroups'] != null
? (map['subGroups'] as List<Map<String, dynamic>>).map((Map<String, dynamic> map) => (FormGroupStructure.fromMap(map['structure']), int.tryParse(map['initialCount']) ?? 0)).toList() ? (map['subGroups'] as List<Map<String, dynamic>>).map((Map<String, dynamic> map) => (int.tryParse(map['initialCount']) ?? 0, FormGroupStructure.fromMap(map['structure']))).toList()
: null, : null,
); );
} }