This commit is contained in:
2025-04-08 21:11:50 +02:00
parent ce1fe22bdf
commit 69f78baccf

View File

@@ -138,6 +138,9 @@ class AstromicFormController extends FormController {
//S1 - State Variables
final List<FormGroupStructure> _formGroups = <FormGroupStructure>[];
//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<String> valuesIDs = groupStructure.values?.nonNulls.toList() ?? <String>[];
List<FormGroupInstance> instances = <FormGroupInstance>[];
for (int i = 0; i < instancesCount; i++) {
// 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();
subValues = groupStructure.subGroups!
.map(((int, FormGroupStructure) s) => getFormGroupValue(s.$2.id, prefix: (prefix != null ? '$prefix-' : '') + standeredGroupFormat(formGroupID, i.toString(), null)))
.nonNulls
.toList();
}
List<FormGroupInstance> instances = <FormGroupInstance>[];
for (int i = 0; i < instancesCount; i++) {
String pr = prefix != null ? '$prefix-' : '';
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()),
composedID: pr + standeredGroupFormat(formGroupID, i.toString(), ''),
fields: Map<String, String>.fromEntries(fieldsIDs
.map((String id) => MapEntry<String, String>(pr + standeredGroupFormat(formGroupID, i.toString(), id), value(pr + 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())
? Map<String, dynamic>.fromEntries(valuesIDs
.map((String id) => MapEntry<String, dynamic>(pr + standeredGroupFormat(formGroupID, i.toString(), id), getValue<dynamic>(pr + standeredGroupFormat(formGroupID, i.toString(), id))))
.toList())
: <String, dynamic>{},
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<String>? 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<FormGroupStructure>? 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<FormGroupStructure>? a, List<FormGroupStructure>? b) => <FormGroupStructure>[...?a, ...?b])
// ?.toList() ??
// <FormGroupStructure>[],
// );
// }
String addInstanceToFormGroup(String formGroupID, {Map<String, int>? 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<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(),
// );
// }
/// Prepare the groupStructure.
// void _addFieldsToGroup(String structureID, List<String> fields, List<String>? 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<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(),
// );
// // 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');
// // 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<FormGroupInstance> instances = <FormGroupInstance>[];
// for (int index = 0; index < structure.count; index++) {
// final Map<String, dynamic> values = {};
// final Map<String, TextEditingController> 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<String, FormGroupValue> 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<FormGroupInstance> subInstances = [];
// for (int subIndex = 0; subIndex < subCount; subIndex++) {
// final Map<String, dynamic> subValues = {};
// final Map<String, TextEditingController> 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<String, int> 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