From a7b98c93f41dde988ba819500e73d6ffb07a1a2b Mon Sep 17 00:00:00 2001 From: Michael Aziz Date: Mon, 14 Apr 2025 12:22:19 +0200 Subject: [PATCH] [SYNC] --- lib/src/form/src/controller.dart | 152 ++++++++++------------- lib/src/form/src/form_group_wrapper.dart | 49 ++++---- 2 files changed, 91 insertions(+), 110 deletions(-) diff --git a/lib/src/form/src/controller.dart b/lib/src/form/src/controller.dart index a09c1e5..d02f323 100644 --- a/lib/src/form/src/controller.dart +++ b/lib/src/form/src/controller.dart @@ -327,7 +327,7 @@ class AstromicFormController extends FormController { .map( ((int, FormGroupStructure) s) => getFormGroupValue( s.$2.id, - parents: { + parents: { if (parents != null) ...parents, formGroupID: i, }, @@ -393,102 +393,78 @@ class AstromicFormController extends FormController { return p.trim(); } - /// Remove an instance from the group - void removeInstanceFromGroupT(String formGroupID, int indexToRemove, {Map? parents, bool removeAll = false}) { - // 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?'); - - // Get the prefix for subgroups if exists - String? prefix; - if (isASubGroup(formGroupID) && parents != null) { - prefix = parents.entries.map((MapEntry parentEntry) => standeredGroupFormat(parentEntry.key, parentEntry.value.toString(), null)).join('-'); + void _removeGroupControllers(String composedID, List indecies, List fields, List values, {bool switchValuesFirst = false}) { + for (int i in indecies) { + for (String fieldID in fields) { + String p = standeredGroupFormat(composedID, (switchValuesFirst ? (i + 1) : i).toString(), fieldID); + String k = standeredGroupFormat(composedID, i.toString(), fieldID); + if (switchValuesFirst) { + set(k, tryValue(p) ?? ''); + } + removeController(p); + } + for (String valueID in values) { + String p = standeredGroupFormat(composedID, (switchValuesFirst ? (i + 1) : i).toString(), valueID); + String k = standeredGroupFormat(composedID, i.toString(), valueID); + if (switchValuesFirst) { + set(k, tryValue(p) ?? ''); + setValue(k, getValue(p) ?? ''); + } + removeValue(p); + set(p, ''); + removeController(p); + } } + } - // handle subgroups + void _checkSubgroups(String groupID, {Map? parents}) { + // Get the group structure and check it's subGroups + FormGroupStructure? structure = getGroupStructure(groupID); if (structure!.subGroups != null && structure.subGroups!.isNotEmpty) { - for (final (int, FormGroupStructure) subGroup in structure.subGroups!) { - // Recursively remove any nested subgroups inside this subgroup - final nestedParents = { - if (parents != null) ...parents, - structure.id: indexToRemove, - }; + for ((int, FormGroupStructure) sg in structure.subGroups!) { + // Get the SubGroup instances Count and prefix it. + int subgroupCount = getInstanceCount(sg.$2.id, parents: {if (parents != null) parents.entries.first.key: parents.entries.first.value + 1}); + String prefix = parents?.entries.map((MapEntry parentEntry) => standeredGroupFormat(parentEntry.key, parentEntry.value.toString(), null)).join('-') ?? ''; - int subGroupInstancesCount = getInstanceCount( - subGroup.$2.id, - parents: nestedParents, - ); - - for (int ii = 0; ii < subGroupInstancesCount; ii++) { - removeInstanceFromGroupT( - subGroup.$2.id, - ii, - parents: nestedParents, - removeAll: true, + // Recurse through subGroups to find the latest. + for (int sgInstance = 0; sgInstance < subgroupCount; sgInstance++) { + _checkSubgroups( + sg.$2.id, + parents: { + ...?parents, + ...{sg.$2.id: sgInstance} + }, ); } - //-- - // } + // Remove controllers for these subgroups. + _removeGroupControllers(prefix.isNotEmpty ? '$prefix-${sg.$2.id}' : sg.$2.id, List.generate(subgroupCount, (int ii) => ii), sg.$2.fields, sg.$2.values ?? []); } } - // Get current instances of this group. - int currentGroupInstances = getInstanceCount(formGroupID, parents: parents); - if (removeAll) { - for (var ina = 0; ina < currentGroupInstances; ina++) { - // Remove the last item - for (String fieldID in structure.fields) { - String p = (prefix != null ? '$prefix-' : '') + standeredGroupFormat(structure.id, ina.toString(), fieldID); - removeController(p); - } - if (structure.values != null && structure.values!.isNotEmpty) { - for (String valueID in structure.values!) { - String p = (prefix != null ? '$prefix-' : '') + standeredGroupFormat(structure.id, ina.toString(), valueID); - removeValue(p); - removeController(p); - } - } - } + } + + /// Remove an instance from the group + void removeInstanceFromGroup(String targetGroupID, int indexToRemove, {Map? parents}) { + // Get tha main targeted group's structure + FormGroupStructure? targetedGroupStructure = getGroupStructure(targetGroupID); + assert(targetedGroupStructure != null, 'The Group $targetGroupID doesn\'t seem to be found, are you sure you initialized it?'); + + // Get tha main targeted group's count + int targetedGroupCount = getInstanceCount(targetGroupID, parents: parents); + assert(indexToRemove < targetedGroupCount, 'The index to remove is larger than the whole instances count. ($indexToRemove , $targetedGroupCount)'); + + // Loop through the subGroups and remove their controllers. + + // Prefix the main targeted ID and remove it's controllers. + String prefix = parents?.entries.map((MapEntry parentEntry) => standeredGroupFormat(parentEntry.key, parentEntry.value.toString(), null)).join('-') ?? ''; + if (indexToRemove == (targetedGroupCount - 1)) { + // Last Item in the group, Remove directly. + _checkSubgroups(targetGroupID, parents: {...?parents, targetGroupID: indexToRemove}); + _removeGroupControllers(prefix.isNotEmpty ? '$prefix-$targetGroupID' : targetGroupID, [indexToRemove], targetedGroupStructure!.fields, targetedGroupStructure.values ?? []); } else { - // The actual controllers removal - if (indexToRemove >= currentGroupInstances) { - // return; - throw Exception('The index to remove is larger than the whole instances count. ($indexToRemove , $currentGroupInstances)'); - } else { - if (indexToRemove == (currentGroupInstances - 1)) { - // Remove the last item - for (String fieldID in structure.fields) { - String p = (prefix != null ? '$prefix-' : '') + standeredGroupFormat(structure.id, indexToRemove.toString(), fieldID); - removeController(p); - } - if (structure.values != null && structure.values!.isNotEmpty) { - for (String valueID in structure.values!) { - String p = (prefix != null ? '$prefix-' : '') + standeredGroupFormat(structure.id, indexToRemove.toString(), valueID); - removeValue(p); - removeController(p); - } - } - } else { - // Switch and remove - int nextIndex = indexToRemove + 1; - for (String fieldID in structure!.fields) { - String p = (prefix != null ? '$prefix-' : '') + standeredGroupFormat(structure.id, indexToRemove.toString(), fieldID); - String p2 = (prefix != null ? '$prefix-' : '') + standeredGroupFormat(structure.id, nextIndex.toString(), fieldID); - set(p, value(p2)); - removeController(p2); - } - if (structure.values != null && structure.values!.isNotEmpty) { - for (String valueID in structure.values!) { - String p = (prefix != null ? '$prefix-' : '') + standeredGroupFormat(structure.id, indexToRemove.toString(), valueID); - String p2 = (prefix != null ? '$prefix-' : '') + standeredGroupFormat(structure.id, nextIndex.toString(), valueID); - removeValue(p); - set(p, value(p2)); - setValue(p, getValue(p2)); - removeController(p2); - removeValue(p2); - } - } - } - } + //TODO - Has an issue with transferring subGroups... + _checkSubgroups(targetGroupID, parents: {...?parents, targetGroupID: indexToRemove}); + _removeGroupControllers(prefix.isNotEmpty ? '$prefix-$targetGroupID' : targetGroupID, [indexToRemove], targetedGroupStructure!.fields, targetedGroupStructure.values ?? [], + switchValuesFirst: true); } } //!SECTION diff --git a/lib/src/form/src/form_group_wrapper.dart b/lib/src/form/src/form_group_wrapper.dart index 756cefb..ae4824e 100644 --- a/lib/src/form/src/form_group_wrapper.dart +++ b/lib/src/form/src/form_group_wrapper.dart @@ -2,48 +2,43 @@ //s2 Packages //s3 Core Packages import 'package:flutter/material.dart'; - -import '../../../astromic_helpers.dart'; //s3 Internal Packages -// import 'package:astromic_elements/astromic_elements.dart'; //s3 3rd-party Packages //s2 Utility //s3 Configs -// import '../../../../../core/configs/routing/routing.config.dart'; //s3 Misc //s2 Domain //s3 Entities +import 'controller.dart'; +import 'models/models.exports.dart'; //s3 Usecases -//s2 Presentation -//s3 Design -// import '../../../../design-system/design_system.dart'; -//s3 Presenters -//s3 Widgets //s1 Exports -class FormGroupWrapper extends StatefulWidget { +class FormGroupWrapperTest extends StatefulWidget { //SECTION - Widget Arguments final AstromicFormController formController; final String groupID; final Widget Function(List children, String Function() addItem, void Function(int) removeItem) groupBuilder; final Widget Function(int index, String composedID, VoidCallback removeItem) itemBuilder; final int startLength; + final Map? parents; //!SECTION // - const FormGroupWrapper({ + const FormGroupWrapperTest({ super.key, required this.formController, required this.groupID, required this.groupBuilder, required this.itemBuilder, this.startLength = 0, + this.parents, }); @override - State createState() => _FormGroupWrapperState(); + State createState() => _FormGroupWrapperTestState(); } -class _FormGroupWrapperState extends State { +class _FormGroupWrapperTestState extends State { // //SECTION - State Variables //s1 --State @@ -66,7 +61,8 @@ class _FormGroupWrapperState extends State { //s1 --State // //s1 --Controllers & Listeners - instances = widget.formController.getFormGroupValue(widget.groupID)!.instances; + instances = widget.formController.getFormGroupValue(widget.groupID, parents: widget.parents)!.instances; + // LoggingService.log("instances: $instances"); //s1 --Controllers & Listeners // //s1 --Late & Async Initializers @@ -98,27 +94,36 @@ class _FormGroupWrapperState extends State { //SECTION - Build Return return widget.groupBuilder( // Children - List.generate(instances.length, (int i) => widget.itemBuilder(i, instances[i].composedID, () => _removeItem(i))), + List.generate( + instances.length, + (int i) => widget.itemBuilder(i, instances[i].composedID, () { + setState(() { + _removeItem(i); + instances = widget.formController.getFormGroupValue(widget.groupID, parents: widget.parents)!.instances; + }); + })), // Add Callback () { String id = ''; setState(() { - // id = widget.formController.addInstanceToFormGroup(widget.groupID); - // instances = widget.formController.getFormGroupValue(widget.groupID)!.instances; + id = widget.formController.addInstanceToFormGroup(widget.groupID, parents: widget.parents); + instances = widget.formController.getFormGroupValue(widget.groupID, parents: widget.parents)!.instances; }); return id; }, // Remove Callback - (int i) => _removeItem(i), + (int i) { + setState(() { + _removeItem(i); + instances = widget.formController.getFormGroupValue(widget.groupID, parents: widget.parents)!.instances; + }); + }, ); //!SECTION } void _removeItem(int i) { - setState(() { - // widget.formController.removeInstanceFromFormGroup(widget.groupID, i,isSubGroup: widget.isSubGroup); - // instances = widget.formController.getFormGroupValue(widget.groupID,isSubGroup: widget.isSubGroup)!.instances; - }); + widget.formController.removeInstanceFromGroup(widget.groupID, i, parents: widget.parents); } @override