From 69f78baccffa60f3e5610574d59e18e44701b66f Mon Sep 17 00:00:00 2001 From: Michael Aziz Date: Tue, 8 Apr 2025 21:11:50 +0200 Subject: [PATCH] [SYNC] --- lib/src/form/src/controller.dart | 296 ++++++------------------------- 1 file changed, 54 insertions(+), 242 deletions(-) diff --git a/lib/src/form/src/controller.dart b/lib/src/form/src/controller.dart index a1c938f..26b3ab5 100644 --- a/lib/src/form/src/controller.dart +++ b/lib/src/form/src/controller.dart @@ -138,6 +138,9 @@ class AstromicFormController extends FormController { //S1 - State Variables final List _formGroups = []; //S1 - Methods + /// Does the controller has a field group with this ID. + bool hasFieldGroup(String formGroupID) => getGroupStructure(formGroupID) != null; + /// Get the structure of this groupId FormGroupStructure? getGroupStructure(String groupID) { // Get the full path of the group @@ -204,7 +207,7 @@ class AstromicFormController extends FormController { 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('') + + p.split('->').sublist(0, p.split('->').indexOf(targetGroupID)).map((String a) => standeredGroupFormat(a, '0', '')).toList().join() + standeredGroupFormat(targetGroupID, r'[\d+]', firstField) + r'$'); } else { @@ -277,7 +280,9 @@ class AstromicFormController extends FormController { } /// Get the target formGroup Value. - FormGroupValue? getFormGroupValue(String formGroupID) { + FormGroupValue? getFormGroupValue(String formGroupID, {String? prefix}) { + assert(prefix != null || _formGroups.map((FormGroupStructure f) => f.id).contains(formGroupID), 'This method is not valid for sub-groups.'); + // Get the group structure with the ID FormGroupStructure? groupStructure = getGroupStructure(formGroupID); @@ -290,20 +295,27 @@ class AstromicFormController extends FormController { // 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++) { + // get the subGroups + List subValues = []; + if (groupStructure.subGroups != null && groupStructure.subGroups!.isNotEmpty) { + subValues = groupStructure.subGroups! + .map(((int, FormGroupStructure) s) => getFormGroupValue(s.$2.id, prefix: (prefix != null ? '$prefix-' : '') + standeredGroupFormat(formGroupID, i.toString(), null))) + .nonNulls + .toList(); + } + String pr = prefix != null ? '$prefix-' : ''; 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()), + composedID: pr + standeredGroupFormat(formGroupID, i.toString(), ''), + fields: Map.fromEntries(fieldsIDs + .map((String id) => MapEntry(pr + standeredGroupFormat(formGroupID, i.toString(), id), value(pr + 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()) + ? Map.fromEntries(valuesIDs + .map((String id) => MapEntry(pr + standeredGroupFormat(formGroupID, i.toString(), id), getValue(pr + standeredGroupFormat(formGroupID, i.toString(), id)))) + .toList()) : {}, subGroups: subValues, ), @@ -314,241 +326,41 @@ class AstromicFormController extends FormController { return groupValue; } - // _formController.initializeFormGroup( - // FormGroupStructure( - // id: 'mainGroup', - // fields: [ - // 'title', - // 'description', - // ], - // values: [ - // 'media', - // ], - // subGroups: [ - // (FormGroupStructure( - // id: 'variations', - // fields: ['title'], - // values: ['media'], - // ),3), - // ], - // ), - // initialCount: 2, - // ); - /// Does the controller has a field group with this ID. - // bool hasFieldGroup(String formGroupID, {bool isSubGroup = false}) => _getGroupStructure(formGroupID, isSubGroup: isSubGroup) != null; + bool isASubGroup(String formGroupID) { + List? fullPath = getFullPathOfGroup(formGroupID)?.split('->'); + return fullPath != null && fullPath.length > 1 && fullPath.indexOf(formGroupID) != 0; + } - /// Get the formGroupStructure of this groupId - // String? findGroupPath(String groupID, List? groups) { - // if ((groups ?? _formGroups).map((FormGroupStructure a) => a.id).contains(groupID)) return groupID; - // findGroupPath( - // groupID, - // (groups ?? _formGroups) - // .map((FormGroupStructure f) => f.subGroups?.map(((FormGroupStructure, int) aa) => aa.$1).toList()) - // .toList() - // .reduce((List? a, List? b) => [...?a, ...?b]) - // ?.toList() ?? - // [], - // ); - // } + String addInstanceToFormGroup(String formGroupID, {Map? parentsPrefixes}) { + // Get the group structure with the same ID + FormGroupStructure? structure = getGroupStructure(formGroupID); + assert(structure != null, 'The Group $formGroupID doesn\'t seem to be found, are you sure you initialized it?'); - // FormGroupStructure? _getGroupStructure(String formGroupID, {bool isSubGroup = false}) { - // FormGroupStructure? groupStructure; - // if (isSubGroup) { - // FormGroupStructure? parentGroup = - // _formGroups.where((FormGroupStructure f) => (f.subGroups?.map(((FormGroupStructure, int) ss) => ss.$1.id).contains(formGroupID) ?? false)).nonNulls.toList().firstOrNull; - // if (parentGroup != null && parentGroup.subGroups != null && parentGroup.subGroups!.map(((FormGroupStructure, int) i) => i.$1.id).contains(formGroupID)) { - // assert(parentGroup.subGroups!.where(((FormGroupStructure, int) f) => f.$1.id == formGroupID).nonNulls.toList().length == 1, 'Seems there are multible subgroups with this ID!'); - // groupStructure = parentGroup.subGroups!.where(((FormGroupStructure, int) f) => f.$1.id == formGroupID).nonNulls.toList().firstOrNull?.$1; - // } - // } else { - // groupStructure = _formGroups.where((FormGroupStructure f) => f.id == formGroupID).nonNulls.toList().firstOrNull; - // } - // // - // return groupStructure; - // } + // Get current instances of this group + int currentGroupInstances = getInstanceCount(formGroupID); + int newGroupIndex = currentGroupInstances + 1; - /// Get current Instance count of this formGroup - // int formGroupInstancesLength(String formGroupID, {bool isSubGroup = false}) { - // // Get the group structure with the same ID - // FormGroupStructure? structure = _getGroupStructure(formGroupID, isSubGroup: isSubGroup); - // assert(structure != null, 'The ${isSubGroup ? "SUBGroup" : "Group"} $formGroupID doesn\'t seem to be found, are you sure you initialized it?'); + if (structure!.subGroups != null && structure.subGroups!.isNotEmpty) { + for ((int gI, FormGroupStructure gS) f in structure.subGroups!) { + addInstanceToFormGroup(formGroupID, parentsPrefixes: { + ...?parentsPrefixes, + ...{formGroupID: newGroupIndex}, + }); + } + } - // Map firstSetOfFieldsWithID = Map.fromEntries( - // controllers.entries.where((MapEntry c) => RegExp(formGroupID + r'-#[\d+]-' + groupStructure!.fields.first).hasMatch(c.key)).nonNulls.toList(), - // ); - // } - - /// Prepare the groupStructure. - // void _addFieldsToGroup(String structureID, List fields, List? values, int index) { - // for (String fieldID in fields) { - // controller(standeredGroupFormat(structureID, index, fieldID)); - // } - // if (values != null && values.isNotEmpty) { - // for (String valueID in values) { - // controller(standeredGroupFormat(structureID, index, valueID)); - // } - // } - // } - - /// Initialize the formGroup Structure. - // void initializeFormGroup(FormGroupStructure groupStructure, {int initialCount = 1}) { - // assert(groupStructure.fields.isNotEmpty, '${groupStructure.id}: Group fields should NOT be empty.'); - - // // Validate subgroups (if any) - // groupStructure.subGroups?.map(((FormGroupStructure, int) a) => a.$1).forEach(((FormGroupStructure subGroup) { - // assert(subGroup.fields.isNotEmpty, '${subGroup.id}: Subgroup fields should NOT be empty.'); - // })); - - // // Add structure to registry - // _formGroups.add(groupStructure); - - // for (int groupIndex = 0; groupIndex < initialCount; groupIndex++) { - // // Add main group fields/values - // _addGroupControllers(groupStructure, groupIndex); - - // // Add subgroup fields/values - // if (groupStructure.subGroups != null && groupStructure.subGroups!.isNotEmpty) { - // for (final (FormGroupStructure subGroup, int subgroupInitialCount) in groupStructure.subGroups!) { - // for (int subIndex = 0; subIndex < subgroupInitialCount; subIndex++) { - // final String nestedID = standeredGroupFormat(groupStructure.id, groupIndex, subGroup.id); - // _addGroupControllers(subGroup, subIndex, parentPrefix: nestedID); - // } - // } - // } - // } - // } - - // void initializeFormGroup(FormGroupStructure groupStructure, {int initialCount = 1}) { - // assert(groupStructure.fields.isNotEmpty, '$groupStructure: Group Fields should NOT be empty.'); - // if (groupStructure.subGroups != null && groupStructure.subGroups!.isNotEmpty) { - // assert(!groupStructure.subGroups!.any(((FormGroupStructure, int) a) => a.$1.fields.isEmpty), '$groupStructure: Subgroup Fields should NOT be empty.'); - // } - - // // Add the group. - // _formGroups.add(groupStructure); - - // // Add the controllers and values. - // for (int groupInstanceIndex = 0; groupInstanceIndex < initialCount; groupInstanceIndex++) { - // _addFieldsToGroup(groupStructure.id, groupStructure.fields, groupStructure.values, groupInstanceIndex); - - // // Add subGroup data if non null - // if (groupStructure.subGroups != null && groupStructure.subGroups!.isNotEmpty) { - // for ((FormGroupStructure, int) subGroupStructure in groupStructure.subGroups!) { - // for (int subgroupInstanceIndex = 0; subgroupInstanceIndex < subGroupStructure.$2; subgroupInstanceIndex++) { - // _addFieldsToGroup(standeredGroupFormat(groupStructure.id, groupInstanceIndex, subGroupStructure.$1.id), subGroupStructure.$1.fields, subGroupStructure.$1.values, subgroupInstanceIndex); - // } - // } - // } - // } - // } - - // String addInstanceToFormGroup(String formGroupID, {bool isSubGroup = false}) { - // // Get the group structure with the same ID - // FormGroupStructure? structure = _getGroupStructure(formGroupID); - // assert(structure != null, 'The ${isSubGroup ? "SUBGroup" : "Group"} $formGroupID doesn\'t seem to be found, are you sure you initialized it?'); - - // // Get the current fields with this ID - // Map firstSetOfFieldsWithID = Map.fromEntries( - // controllers.entries.where((MapEntry c) => RegExp(formGroupID + r'-#[\d+]-' + groupStructure!.fields.first).hasMatch(c.key)).nonNulls.toList(), - // ); - - // // 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'); - - // // Add the controllers and values. - - // for (String fieldID in groupStructure.fields) { - // String finalFieldID = '${groupStructure.id}-#${firstSetOfFieldsWithID.length}-$fieldID'; - // controller(finalFieldID); - // } - // if (groupStructure.values != null && groupStructure.values!.isNotEmpty) { - // for (String valueID in groupStructure.values!) { - // String finalFieldID = '${groupStructure.id}-#${firstSetOfFieldsWithID.length}-$valueID'; - // controller(finalFieldID); - // } - // } - // return '${groupStructure.id}-#${firstSetOfFieldsWithID.length}-'; - // } - -// FormGroupValue getFormGroupValue(String groupID) { -// final FormGroupStructure structure = _formGroups.firstWhere( -// (FormGroupStructure group) => group.id == groupID, -// orElse: () => throw Exception("Group '$groupID' not initialized."), -// ); - -// final List instances = []; - -// for (int index = 0; index < structure.count; index++) { -// final Map values = {}; -// final Map fields = {}; - -// // Fields (Text) -// for (final fieldID in structure.fields) { -// final fullID = standeredGroupFormat(groupID, index, fieldID); -// fields[fieldID] = controller(fullID); -// } - -// // Values (Dynamic) -// for (final valueID in structure.values ?? []) { -// final fullID = standeredGroupFormat(groupID, index, valueID); -// values[valueID] = controller(fullID).value; -// } - -// // SubGroups -// final Map subGroups = {}; - -// if (structure.subGroups != null && structure.subGroups!.isNotEmpty) { -// for ((FormGroupStructure subGroup, int subCount) in structure.subGroups!) { -// final nestedID = standeredGroupFormat(groupID, index, subGroup.id); - -// final List subInstances = []; - -// for (int subIndex = 0; subIndex < subCount; subIndex++) { -// final Map subValues = {}; -// final Map subFields = {}; - -// for (final fieldID in subGroup.fields) { -// final subFullID = standeredGroupFormat(nestedID, subIndex, fieldID); -// subFields[fieldID] = controller(subFullID); -// } - -// for (final valueID in subGroup.values ?? []) { -// final subFullID = standeredGroupFormat(nestedID, subIndex, valueID); -// subValues[valueID] = controller(subFullID).value; -// } - -// subInstances.add(FormGroupInstance( -// fields: subFields, -// values: subValues, -// subGroups: const {}, // deeper nesting unsupported (for now) -// )); -// } - -// subGroups[subGroup.id] = FormGroupValue( -// groupID: subGroup.id, -// instancesCount: subCount, -// instances: subInstances, -// ); -// } -// } - -// instances.add(FormGroupInstance( -// fields: fields, -// values: values, -// subGroups: subGroups, -// )); -// } - -// return FormGroupValue( -// groupID: groupID, -// instancesCount: structure.count, -// instances: instances, -// ); -// } + String prefixedGroup = parentsPrefixes?.entries.map((MapEntry p) => standeredGroupFormat(p.key, p.value.toString(), null)).toList().join('-') ?? ''; + // Add the controllers and values. + for (String fieldID in structure.fields.nonNulls) { + controller(prefixedGroup + standeredGroupFormat(formGroupID, newGroupIndex.toString(), fieldID)); + } + if (structure.values != null && structure.values!.isNotEmpty) { + for (String valueID in structure.values!.nonNulls) { + controller(prefixedGroup + standeredGroupFormat(formGroupID, newGroupIndex.toString(), valueID)); + } + } + return prefixedGroup + standeredGroupFormat(formGroupID, newGroupIndex.toString(), ''); + } // void removeInstanceFromFormGroup(String formGroupID, int indexToRemove, {bool isSubGroup = false}) { // // Get the group structure with the same ID