From ce1fe22bdfd83b425d6fd8fb4e304d8dd66d0051 Mon Sep 17 00:00:00 2001 From: Michael Aziz Date: Tue, 8 Apr 2025 17:04:02 +0200 Subject: [PATCH] [SYNC] --- lib/src/form/src/controller.dart | 112 ++++++++++++++----------------- 1 file changed, 51 insertions(+), 61 deletions(-) diff --git a/lib/src/form/src/controller.dart b/lib/src/form/src/controller.dart index dec10a7..a1c938f 100644 --- a/lib/src/form/src/controller.dart +++ b/lib/src/form/src/controller.dart @@ -196,23 +196,21 @@ class AstromicFormController extends FormController { } 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); if (structure != null) { String firstField = structure.fields.first; - // return controllers.keys.where((String c) => RegExp(standeredGroupFormat(targetGroupID, r'[\d+]', firstField)).hasMatch(c)).nonNulls.toList().length; - return controllers.keys - .where((String c) { - // Match the specific group ID and field name within the group - final RegExp pattern = RegExp(r'^' + RegExp.escape(standeredGroupFormat(targetGroupID, r'[\d+]', firstField)) + r'$'); - return pattern.hasMatch(c); - }) - .nonNulls - .toList() - .length; + String? p = getFullPathOfGroup(targetGroupID); + late RegExp pattern; + if (p != null && p.split('->').length > 1 && p.split('->').indexOf(targetGroupID) != 0) { + // This is a subgroup + pattern = RegExp(r'^' + + p.split('->').sublist(0, p.split('->').indexOf(targetGroupID)).map((a) => standeredGroupFormat(a, '0', '')).toList().join('') + + standeredGroupFormat(targetGroupID, r'[\d+]', firstField) + + r'$'); + } else { + pattern = RegExp(r'^' + standeredGroupFormat(targetGroupID, r'[\d+]', firstField) + r'$'); + } + return controllers.keys.where((String c) => pattern.hasMatch(c)).nonNulls.toList().length; } return 0; } @@ -278,6 +276,44 @@ class AstromicFormController extends FormController { _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 fieldsIDs = groupStructure!.fields.nonNulls.toList(); + + // get the values IDs + List valuesIDs = groupStructure.values?.nonNulls.toList() ?? []; + + // get the subGroups + List subValues = []; + if (groupStructure.subGroups != null && groupStructure.subGroups!.isNotEmpty) { + subValues = groupStructure.subGroups!.map(((int, FormGroupStructure) s) => getFormGroupValue(s.$2.id)).nonNulls.toList(); + } + + List instances = []; + for (int i = 0; i < instancesCount; i++) { + instances.add( + FormGroupInstance( + composedID: standeredGroupFormat(formGroupID, i.toString(), ''), + fields: Map.fromEntries(fieldsIDs.map((String id) => MapEntry(standeredGroupFormat(formGroupID, i.toString(), id),value(standeredGroupFormat(formGroupID, i.toString(), id)))).toList()), + values: valuesIDs.isNotEmpty + ? Map.fromEntries(valuesIDs.map((String id) => MapEntry(standeredGroupFormat(formGroupID, i.toString(), id), getValue(standeredGroupFormat(formGroupID, i.toString(), id)))).toList()) + : {}, + subGroups: subValues, + ), + ); + } + + FormGroupValue groupValue = FormGroupValue(groupID: formGroupID, instancesCount: instancesCount, instances: instances); + return groupValue; + } + // _formController.initializeFormGroup( // FormGroupStructure( // 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 firstSetOfFieldsWithID = Map.fromEntries( - // controllers.entries.where((MapEntry c) => RegExp(formGroupID + r'-#[\d+]-' + groupStructure!.fields.first).hasMatch(c.key)).nonNulls.toList(), - // ); - - // print('First set of fields: $firstSetOfFieldsWithID'); - // get the fields IDs - List fieldsIDs = groupStructure!.fields.nonNulls.toList(); - print('fieldIDs: $fieldsIDs'); - // get the values IDs - List valuesIDs = groupStructure.values?.nonNulls.toList() ?? []; - print('valueIDs: $valuesIDs'); - - // get the subGroups - List subValues = []; - 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 instances = []; - for (int i = 0; i < instancesCount; i++) { - instances.add( - FormGroupInstance( - composedID: '$formGroupID-#$i-', - fields: Map.fromEntries(fieldsIDs.map((String id) => MapEntry('$formGroupID-#$i-$id', value('$formGroupID-#$i-$id'))).toList()), - values: valuesIDs.isNotEmpty - ? Map.fromEntries(valuesIDs.map((String a) => MapEntry('$formGroupID-#$i-$a', getValue('$formGroupID-#$i-$a'))).toList()) - : {}, - subGroups: subValues, - ), - ); - } - - FormGroupValue groupValue = FormGroupValue(groupID: formGroupID, instancesCount: instancesCount, instances: instances); - return groupValue; - } - // void removeInstanceFromFormGroup(String formGroupID, int indexToRemove, {bool isSubGroup = false}) { // // Get the group structure with the same ID // FormGroupStructure? groupStructure; @@ -626,7 +616,7 @@ class AstromicFormController extends FormController { //!SECTION //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? initialValues) { if (initialValues != null) {