This commit is contained in:
2025-04-14 12:22:19 +02:00
parent 40ca7dd72f
commit a7b98c93f4
2 changed files with 91 additions and 110 deletions

View File

@@ -327,7 +327,7 @@ class AstromicFormController extends FormController {
.map( .map(
((int, FormGroupStructure) s) => getFormGroupValue( ((int, FormGroupStructure) s) => getFormGroupValue(
s.$2.id, s.$2.id,
parents: { parents: <String, int>{
if (parents != null) ...parents, if (parents != null) ...parents,
formGroupID: i, formGroupID: i,
}, },
@@ -393,102 +393,78 @@ class AstromicFormController extends FormController {
return p.trim(); return p.trim();
} }
/// Remove an instance from the group void _removeGroupControllers(String composedID, List<int> indecies, List<String> fields, List<String> values, {bool switchValuesFirst = false}) {
void removeInstanceFromGroupT(String formGroupID, int indexToRemove, {Map<String, int>? parents, bool removeAll = false}) { for (int i in indecies) {
// Get the group structure with the same ID for (String fieldID in fields) {
FormGroupStructure? structure = getGroupStructure(formGroupID); String p = standeredGroupFormat(composedID, (switchValuesFirst ? (i + 1) : i).toString(), fieldID);
assert(structure != null, 'The Group $formGroupID doesn\'t seem to be found, are you sure you initialized it?'); String k = standeredGroupFormat(composedID, i.toString(), fieldID);
if (switchValuesFirst) {
// Get the prefix for subgroups if exists set(k, tryValue(p) ?? '');
String? prefix; }
if (isASubGroup(formGroupID) && parents != null) { removeController(p);
prefix = parents.entries.map((MapEntry<String, int> parentEntry) => standeredGroupFormat(parentEntry.key, parentEntry.value.toString(), null)).join('-'); }
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<String, int>? parents}) {
// Get the group structure and check it's subGroups
FormGroupStructure? structure = getGroupStructure(groupID);
if (structure!.subGroups != null && structure.subGroups!.isNotEmpty) { if (structure!.subGroups != null && structure.subGroups!.isNotEmpty) {
for (final (int, FormGroupStructure) subGroup in structure.subGroups!) { for ((int, FormGroupStructure) sg in structure.subGroups!) {
// Recursively remove any nested subgroups inside this subgroup // Get the SubGroup instances Count and prefix it.
final nestedParents = { int subgroupCount = getInstanceCount(sg.$2.id, parents: <String, int>{if (parents != null) parents.entries.first.key: parents.entries.first.value + 1});
if (parents != null) ...parents, String prefix = parents?.entries.map((MapEntry<String, int> parentEntry) => standeredGroupFormat(parentEntry.key, parentEntry.value.toString(), null)).join('-') ?? '';
structure.id: indexToRemove,
};
int subGroupInstancesCount = getInstanceCount( // Recurse through subGroups to find the latest.
subGroup.$2.id, for (int sgInstance = 0; sgInstance < subgroupCount; sgInstance++) {
parents: nestedParents, _checkSubgroups(
); sg.$2.id,
parents: <String, int>{
for (int ii = 0; ii < subGroupInstancesCount; ii++) { ...?parents,
removeInstanceFromGroupT( ...<String, int>{sg.$2.id: sgInstance}
subGroup.$2.id, },
ii,
parents: nestedParents,
removeAll: true,
); );
} }
//-- // Remove controllers for these subgroups.
// } _removeGroupControllers(prefix.isNotEmpty ? '$prefix-${sg.$2.id}' : sg.$2.id, List<int>.generate(subgroupCount, (int ii) => ii), sg.$2.fields, sg.$2.values ?? <String>[]);
}
}
// 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<String, int>? 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<String, int> parentEntry) => standeredGroupFormat(parentEntry.key, parentEntry.value.toString(), null)).join('-') ?? '';
if (indexToRemove == (targetedGroupCount - 1)) {
// Last Item in the group, Remove directly.
_checkSubgroups(targetGroupID, parents: <String, int>{...?parents, targetGroupID: indexToRemove});
_removeGroupControllers(prefix.isNotEmpty ? '$prefix-$targetGroupID' : targetGroupID, <int>[indexToRemove], targetedGroupStructure!.fields, targetedGroupStructure.values ?? <String>[]);
} else { } else {
// The actual controllers removal //TODO - Has an issue with transferring subGroups...
if (indexToRemove >= currentGroupInstances) { _checkSubgroups(targetGroupID, parents: <String, int>{...?parents, targetGroupID: indexToRemove});
// return; _removeGroupControllers(prefix.isNotEmpty ? '$prefix-$targetGroupID' : targetGroupID, <int>[indexToRemove], targetedGroupStructure!.fields, targetedGroupStructure.values ?? <String>[],
throw Exception('The index to remove is larger than the whole instances count. ($indexToRemove , $currentGroupInstances)'); switchValuesFirst: true);
} 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);
}
}
}
}
} }
} }
//!SECTION //!SECTION

View File

@@ -2,48 +2,43 @@
//s2 Packages //s2 Packages
//s3 Core Packages //s3 Core Packages
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import '../../../astromic_helpers.dart';
//s3 Internal Packages //s3 Internal Packages
// import 'package:astromic_elements/astromic_elements.dart';
//s3 3rd-party Packages //s3 3rd-party Packages
//s2 Utility //s2 Utility
//s3 Configs //s3 Configs
// import '../../../../../core/configs/routing/routing.config.dart';
//s3 Misc //s3 Misc
//s2 Domain //s2 Domain
//s3 Entities //s3 Entities
import 'controller.dart';
import 'models/models.exports.dart';
//s3 Usecases //s3 Usecases
//s2 Presentation
//s3 Design
// import '../../../../design-system/design_system.dart';
//s3 Presenters
//s3 Widgets
//s1 Exports //s1 Exports
class FormGroupWrapper extends StatefulWidget { class FormGroupWrapperTest extends StatefulWidget {
//SECTION - Widget Arguments //SECTION - Widget Arguments
final AstromicFormController formController; final AstromicFormController formController;
final String groupID; final String groupID;
final Widget Function(List<Widget> children, String Function() addItem, void Function(int) removeItem) groupBuilder; final Widget Function(List<Widget> children, String Function() addItem, void Function(int) removeItem) groupBuilder;
final Widget Function(int index, String composedID, VoidCallback removeItem) itemBuilder; final Widget Function(int index, String composedID, VoidCallback removeItem) itemBuilder;
final int startLength; final int startLength;
final Map<String, int>? parents;
//!SECTION //!SECTION
// //
const FormGroupWrapper({ const FormGroupWrapperTest({
super.key, super.key,
required this.formController, required this.formController,
required this.groupID, required this.groupID,
required this.groupBuilder, required this.groupBuilder,
required this.itemBuilder, required this.itemBuilder,
this.startLength = 0, this.startLength = 0,
this.parents,
}); });
@override @override
State<FormGroupWrapper> createState() => _FormGroupWrapperState(); State<FormGroupWrapperTest> createState() => _FormGroupWrapperTestState();
} }
class _FormGroupWrapperState extends State<FormGroupWrapper> { class _FormGroupWrapperTestState extends State<FormGroupWrapperTest> {
// //
//SECTION - State Variables //SECTION - State Variables
//s1 --State //s1 --State
@@ -66,7 +61,8 @@ class _FormGroupWrapperState extends State<FormGroupWrapper> {
//s1 --State //s1 --State
// //
//s1 --Controllers & Listeners //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 --Controllers & Listeners
// //
//s1 --Late & Async Initializers //s1 --Late & Async Initializers
@@ -98,27 +94,36 @@ class _FormGroupWrapperState extends State<FormGroupWrapper> {
//SECTION - Build Return //SECTION - Build Return
return widget.groupBuilder( return widget.groupBuilder(
// Children // Children
List<Widget>.generate(instances.length, (int i) => widget.itemBuilder(i, instances[i].composedID, () => _removeItem(i))), List<Widget>.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 // Add Callback
() { () {
String id = ''; String id = '';
setState(() { setState(() {
// id = widget.formController.addInstanceToFormGroup(widget.groupID); id = widget.formController.addInstanceToFormGroup(widget.groupID, parents: widget.parents);
// instances = widget.formController.getFormGroupValue(widget.groupID)!.instances; instances = widget.formController.getFormGroupValue(widget.groupID, parents: widget.parents)!.instances;
}); });
return id; return id;
}, },
// Remove Callback // Remove Callback
(int i) => _removeItem(i), (int i) {
setState(() {
_removeItem(i);
instances = widget.formController.getFormGroupValue(widget.groupID, parents: widget.parents)!.instances;
});
},
); );
//!SECTION //!SECTION
} }
void _removeItem(int i) { void _removeItem(int i) {
setState(() { widget.formController.removeInstanceFromGroup(widget.groupID, i, parents: widget.parents);
// widget.formController.removeInstanceFromFormGroup(widget.groupID, i,isSubGroup: widget.isSubGroup);
// instances = widget.formController.getFormGroupValue(widget.groupID,isSubGroup: widget.isSubGroup)!.instances;
});
} }
@override @override