[SYNC]
This commit is contained in:
@@ -147,8 +147,8 @@ class AstromicFormController extends FormController {
|
||||
if (group.id == targetGroupID) return '$currentPath${group.id}';
|
||||
|
||||
// Otherwise, check in its subgroups recursively
|
||||
for (final (FormGroupStructure, int) subGroup in group.subGroups ?? <(FormGroupStructure, int)>[]) {
|
||||
final String? subGroupPath = getFullPathOfGroup(targetGroupID, formGroups: <FormGroupStructure>[subGroup.$1], currentPath: '$currentPath${group.id}->');
|
||||
for (final (int, FormGroupStructure) subGroup in group.subGroups ?? <(int, FormGroupStructure)>[]) {
|
||||
final String? subGroupPath = getFullPathOfGroup(targetGroupID, formGroups: <FormGroupStructure>[subGroup.$2], currentPath: '$currentPath${group.id}->');
|
||||
// Return the path if found
|
||||
if (subGroupPath != null) {
|
||||
return subGroupPath;
|
||||
@@ -219,11 +219,11 @@ class AstromicFormController extends FormController {
|
||||
final String segment = pathSegments[i];
|
||||
|
||||
// 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 (subGroup != null) {
|
||||
currentGroup = subGroup.$1;
|
||||
currentGroup = subGroup.$2;
|
||||
} else {
|
||||
// If no subgroup is found at this level, 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.
|
||||
// void initializeFormGroup(FormGroupStructure groupStructure, {int initialCount = 1}) {
|
||||
// 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}) {
|
||||
assert(groupStructure.fields.isNotEmpty, '${groupStructure.id}: Group fields should NOT be empty.');
|
||||
|
||||
// Validate subgroups (if any)
|
||||
_validateSubGroups(groupStructure);
|
||||
_validateSubGroupsRecursively(groupStructure);
|
||||
|
||||
// Add structure to registry
|
||||
_formGroups.add(groupStructure);
|
||||
|
||||
// Initialize the group instances
|
||||
_initializeGroupControllers(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);
|
||||
}
|
||||
});
|
||||
_initializeGroupControllersRecursively(groupStructure, initialCount);
|
||||
}
|
||||
|
||||
// void initializeFormGroup(FormGroupStructure groupStructure, {int initialCount = 1}) {
|
||||
@@ -508,8 +508,8 @@ class AstromicFormController extends FormController {
|
||||
|
||||
// Recursively count instances in subgroups
|
||||
if (groupStructure.subGroups != null) {
|
||||
for ((FormGroupStructure, int) subGroup in groupStructure.subGroups!) {
|
||||
instanceCount += countInstancesRecursively(subGroup.$1, controllersKeys);
|
||||
for ((int, FormGroupStructure) subGroup in groupStructure.subGroups!) {
|
||||
instanceCount += countInstancesRecursively(subGroup.$2, controllersKeys);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -551,7 +551,7 @@ class AstromicFormController extends FormController {
|
||||
// get the subGroups
|
||||
List<FormGroupValue> subValues = <FormGroupValue>[];
|
||||
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');
|
||||
|
||||
@@ -639,7 +639,7 @@ class AstromicFormController extends FormController {
|
||||
//!SECTION
|
||||
|
||||
//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) {
|
||||
if (initialValues != null) {
|
||||
|
||||
@@ -6,7 +6,7 @@ class FormGroupStructure {
|
||||
final String id;
|
||||
final List<String> fields;
|
||||
final List<String>? values;
|
||||
final List<(FormGroupStructure structure, int initialCount)>? subGroups;
|
||||
final List<(int initialCount, FormGroupStructure structure)>? subGroups;
|
||||
FormGroupStructure({
|
||||
required this.id,
|
||||
required this.fields,
|
||||
@@ -18,7 +18,7 @@ class FormGroupStructure {
|
||||
String? id,
|
||||
List<String>? fields,
|
||||
List<String>? values,
|
||||
List<(FormGroupStructure structure, int initialCount)>? subGroups,
|
||||
List<(int initialCount, FormGroupStructure structure)>? subGroups,
|
||||
}) {
|
||||
return FormGroupStructure(
|
||||
id: id ?? this.id,
|
||||
@@ -33,7 +33,7 @@ class FormGroupStructure {
|
||||
'id': id,
|
||||
'fields': fields,
|
||||
'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>),
|
||||
values: map['values'] != null ? List<String>.from(map['values'] as List<String>) : 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,
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user