From 0c9067d142d11d4f2d2350b3d38c00bed7798546 Mon Sep 17 00:00:00 2001 From: Michael Aziz Date: Tue, 8 Apr 2025 09:44:43 +0200 Subject: [PATCH] [SYNC] --- lib/src/form/src/controller.dart | 21 ++++++----- .../models/form_group_structure.model.dart | 35 +++++++++++-------- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/lib/src/form/src/controller.dart b/lib/src/form/src/controller.dart index 92f63d8..67b2cce 100644 --- a/lib/src/form/src/controller.dart +++ b/lib/src/form/src/controller.dart @@ -70,9 +70,15 @@ class AstromicFormController extends FormController { // Add the controllers and values. for (int i = 0; i < initialCount; i++) { - for (MapEntry fieldEntry in groupStructure.fields.entries) { - String fieldID = '${groupStructure.id}-#$i-${fieldEntry.key}'; - controller(fieldID); + for (String fieldID in groupStructure.fields) { + String finalFieldID = '${groupStructure.id}-#$i-$fieldID'; + controller(finalFieldID); + } + if (groupStructure.values != null && groupStructure.values!.isNotEmpty) { + for (String valueID in groupStructure.values!.map(((String, Type) a) => a.$1).toList()) { + String finalFieldID = '${groupStructure.id}-#$i-$valueID'; + controller(finalFieldID); + } } } } @@ -120,15 +126,15 @@ class AstromicFormController extends FormController { // Get the current fields with this ID Map firstSetOfFieldsWithID = Map.fromEntries( - controllers.entries.where((MapEntry c) => RegExp(formGroupID + r'-#[\d+]-' + groupStructure!.fields.entries.first.key).hasMatch(c.key)).nonNulls.toList(), + 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.entries.where((MapEntry e) => !e.value).nonNulls.map((MapEntry ee) => ee.key).toList(); + List fieldsIDs = groupStructure!.fields.nonNulls.toList(); print('fieldIDs: $fieldsIDs'); // get the values IDs - List valuesIDs = groupStructure.fields.entries.where((MapEntry e) => e.value).nonNulls.map((MapEntry ee) => ee.key).toList(); + List<(String, Type)> valuesIDs = groupStructure.values?.nonNulls.toList() ?? <(String, Type)>[]; print('valueIDs: $valuesIDs'); // get the subGroups @@ -143,7 +149,7 @@ class AstromicFormController extends FormController { instances.add( FormGroupInstance( fields: Map.fromEntries(fieldsIDs.map((String id) => MapEntry('$formGroupID-#$i-$id', value('$formGroupID-#$i-$id'))).toList()), - values: Map.fromEntries(valuesIDs.map((String id) => MapEntry('$formGroupID-#$i-$id', getValue('$formGroupID-#$i-$id'))).toList()), + values: Map.fromEntries(valuesIDs.map(((String, Type) a) => MapEntry('$formGroupID-#$i-${a.$1}', getValue('$formGroupID-#$i-${a.$1}'))).toList()), subGroups: subValues, ), ); @@ -194,7 +200,6 @@ class AstromicFormController extends FormController { // } // } - // Map> getFormGroup(String groupID){} /// Get the field state and message of a specific field using it's ID. diff --git a/lib/src/form/src/models/form_group_structure.model.dart b/lib/src/form/src/models/form_group_structure.model.dart index f26da52..419367d 100644 --- a/lib/src/form/src/models/form_group_structure.model.dart +++ b/lib/src/form/src/models/form_group_structure.model.dart @@ -4,22 +4,26 @@ import 'package:flutter/foundation.dart'; class FormGroupStructure { final String id; - final Map fields; + final List fields; + final List<(String, Type)>? values; final List? subGroups; FormGroupStructure({ required this.id, required this.fields, + this.values, this.subGroups, }); FormGroupStructure copyWith({ String? id, - Map? fields, + List? fields, + List<(String, Type)>? values, List? subGroups, }) { return FormGroupStructure( id: id ?? this.id, fields: fields ?? this.fields, + values: values ?? this.values, subGroups: subGroups ?? this.subGroups, ); } @@ -28,6 +32,7 @@ class FormGroupStructure { return { 'id': id, 'fields': fields, + 'values': values?.map(((String, Type) x) => {'id': x.$1, 'type': x.$2}).toList(), 'subGroups': subGroups?.map((FormGroupStructure x) => x.toMap()).toList(), }; } @@ -35,14 +40,9 @@ class FormGroupStructure { factory FormGroupStructure.fromMap(Map map) { return FormGroupStructure( id: map['id'] as String, - fields: Map.from(map['fields'] as Map), - subGroups: map['subGroups'] != null - ? List.from( - (map['subGroups'] as List).map( - (int x) => FormGroupStructure.fromMap(x as Map), - ), - ) - : null, + fields: List.from(map['fields'] as List), + values: map['values'] != null ? (map['values'] as Map).entries.map((MapEntry entry) => (entry.key.toString(),entry.value.runtimeType)).toList() : null, + subGroups: map['subGroups'] != null ? (map['subGroups'] as List>).map((Map f) => FormGroupStructure.fromMap(f)).toList() : null, ); } @@ -52,18 +52,25 @@ class FormGroupStructure { @override String toString() { - return 'FormGroup(id: $id, fields: $fields, subGroups: $subGroups)'; + return 'FormGroupStructure(id: $id, fields: $fields, values: $values, subGroups: $subGroups)'; } @override bool operator ==(covariant FormGroupStructure other) { if (identical(this, other)) return true; - - return other.id == id && mapEquals(other.fields, fields) && listEquals(other.subGroups, subGroups); + + return + other.id == id && + listEquals(other.fields, fields) && + listEquals(other.values, values) && + listEquals(other.subGroups, subGroups); } @override int get hashCode { - return id.hashCode ^ fields.hashCode ^ subGroups.hashCode; + return id.hashCode ^ + fields.hashCode ^ + values.hashCode ^ + subGroups.hashCode; } }