diff --git a/lib/src/form/src/controller.dart b/lib/src/form/src/controller.dart index 61d3583..a6c34f5 100644 --- a/lib/src/form/src/controller.dart +++ b/lib/src/form/src/controller.dart @@ -13,6 +13,7 @@ import 'package:form_controller/form_controller.dart'; //s3 Services //s3 Models & Widgets import 'enums/enums.exports.dart'; +import 'models/form_group_structure.model.dart'; //s1 Exports /// A specialized form controller to handle form states, @@ -22,6 +23,7 @@ class AstromicFormController extends FormController { final Map fieldMessages = {}; final Map _hostedValues = {}; final Stream>? errorStream; + final List _formGroups = []; // State Stream Variables static final StreamController<(String, AstromicFieldState)> _stateStreamController = StreamController<(String id, AstromicFieldState)>.broadcast(); @@ -39,9 +41,135 @@ class AstromicFormController extends FormController { _addInitialControllers(initialValues); } + get i => null; + /// Does the controller has a field with this ID. bool hasKey(String id) => controllers.containsKey(id); + /* some-group: { + some-group-0 : null, + some-group-1 : null, + some-group-2: null, + some-group-3: { + some-group-3-a: null, + some-group-3-b: null, + some-group-3-c: null, + some-group-3-d: null, + }, + } + + */ + + // At the start of a screen? + void initializeFormGroup(FormGroupStructure group, {int initialCount = 1}) { + if (group.fields.isEmpty) { + throw Exception('Group Fields should NOT be empty.'); + } + _formGroups.add(group); + // Add the controllers and values. + for (int i = 0; i < initialCount; i++) { + for (MapEntry fieldEntry in group.fields.entries) { + String fieldID = '${group.id}-$i-${fieldEntry.key}'; + controller(fieldID); + if (fieldEntry.value) { + // Is a value field + } else { + // Is a text field + } + } + } + } + +/*{ +{ +0: { + field1: something, + field2: something else + field3: somethin another, + sub-group: { + 0: + { + sub-field1: someValue, + sub-field2: someOtherValue, + } + 1: + 2: + } + } +1: +2: +} +} +*/ + + Map> getFormGroup(String formGroupID) { + // Get the group structure with the same ID + FormGroupStructure? groupWithSameID = _formGroups.where((FormGroupStructure f) => f.id == formGroupID).nonNulls.toList().firstOrNull; + if (groupWithSameID == null) { + throw Exception('The group ID $formGroupID doesn\'t have any elements. Did you initialize the group?'); + } + + // Get the current fields with this ID + Map firstSetOfFieldsWithID = Map.fromEntries( + controllers.entries.where((MapEntry c) => RegExp(formGroupID + r'-#[\d+]-' + groupWithSameID.fields.entries.first.key).hasMatch(c.key)).nonNulls.toList(), + ); + + for (MapEntry fieldEntry in groupWithSameID.fields.entries) { + String fieldID = '$formGroupID-${firstSetOfFieldsWithID.length}-${fieldEntry.key}'; + controller(fieldID); + if (fieldEntry.value) { + // Is a value field + } else { + // Is a text field + } + } + } + + void addToFormGroup(String formGroupID) { + // Get the group structure with the same ID + FormGroupStructure? groupWithSameID = _formGroups.where((FormGroupStructure f) => f.id == formGroupID).nonNulls.toList().firstOrNull; + if (groupWithSameID == null) { + throw Exception('The group ID $formGroupID doesn\'t have any elements. Did you initialize the group?'); + } + + // Get the current fields with this ID + Map firstSetOfFieldsWithID = Map.fromEntries( + controllers.entries.where((MapEntry c) => RegExp(formGroupID + r'-#[\d+]-' + groupWithSameID.fields.entries.first.key).hasMatch(c.key)).nonNulls.toList(), + ); + + for (MapEntry fieldEntry in groupWithSameID.fields.entries) { + String fieldID = '$formGroupID-${firstSetOfFieldsWithID.length}-${fieldEntry.key}'; + controller(fieldID); + if (fieldEntry.value) { + // Is a value field + } else { + // Is a text field + } + } + } + + void removeFromFormGroup(String formGroupID, int index) { + // Get all the groups with the same ID + List groupsWithSameID = _formGroups.where((FormGroupStructure f) => f.id == formGroupID).nonNulls.toList(); + if (groupsWithSameID.isEmpty) { + throw Exception('The group ID $formGroupID doesn\'t have any elements. Did you initialize the group?'); + } + _formGroups.removeAt(groupsWithSameID.first); + for (MapEntry fieldEntry in groupsWithSameID.first.fields.entries) { + String fieldID = '$formGroupID-${groupsWithSameID.length}-${fieldEntry.key}'; + controller(fieldID); + if (fieldEntry.value) { + // Is a value field + } else { + // Is a text field + } + } + } + + void getFormGroupValues(FormGroupStructure group) {} + + // Map> getFormGroup(String groupID){} + /// Get the field state and message of a specific field using it's ID. (AstromicFieldState, String? message)? getState(String fieldId) => (fieldStates.containsKey(fieldId)) ? (fieldStates[fieldId]!, fieldMessages[fieldId]) : null; diff --git a/lib/src/form/src/form_group_wrapper.dart b/lib/src/form/src/form_group_wrapper.dart deleted file mode 100644 index f431f19..0000000 --- a/lib/src/form/src/form_group_wrapper.dart +++ /dev/null @@ -1,117 +0,0 @@ -// //s1 Imports -// //s2 Packages -// //s3 Core Packages -// import 'package:flutter/material.dart'; - -// import '../../sheet/sheet_helper.astromic.dart'; -// //s3 Internal Packages -// //s3 3rd-party Packages -// //s2 Utility -// //s3 Configs -// //s3 Misc -// //s2 Domain -// //s3 Entities -// //s3 Usecases -// //s2 Presentation -// //s3 Design -// //s3 Presenters -// //s3 Widgets -// //s1 Exports - -// class FormGroupWrapper extends StatefulWidget { -// //SECTION - Widget Arguments -// final AstromicFormController formController; -// final String identifier; -// //!SECTION -// // -// const FormGroupWrapper({ -// super.key, -// required this.formController, -// required this -// }); - -// @override -// State createState() => _FormGroupWrapperState(); -// } - -// class _FormGroupWrapperState extends State { -// // -// //SECTION - State Variables -// //s1 --State -// //s1 --State -// // -// //s1 --Controllers -// //late AstromicFormController _formController; -// //s1 --Controllers -// // -// //s1 --Constants -// //s1 --Constants -// //!SECTION - -// @override -// void initState() { -// super.initState(); -// // -// //SECTION - State Variables initializations & Listeners -// //s1 --State -// //s1 --State -// // -// //s1 --Controllers & Listeners -// // _formController = AstromicFormController(); -// //s1 --Controllers & Listeners -// // -// //s1 --Late & Async Initializers -// //s1 --Late & Async Initializers -// //!SECTION -// } - - -// @override -// void didChangeDependencies() { -// super.didChangeDependencies(); -// // -// //SECTION - State Variables initializations & Listeners -// //s1 --State -// //s1 --State -// // -// //s1 --Controllers & Listeners -// //s1 --Controllers & Listeners -// // -// //!SECTION -// } - -// //SECTION - Dumb Widgets -// //!SECTION - -// //SECTION - Stateless functions -// //!SECTION - -// //SECTION - Action Callbacks -// //!SECTION - -// @override -// Widget build(BuildContext context) { -// //SECTION - Build Setup -// //s1 --Values -// //double w = MediaQuery.of(context).size.width; -// //double h = MediaQuery.of(context).size.height; -// //s1 --Values -// // -// //s1 --Contexted Widgets -// //s1 --Contexted Widgets -// //!SECTION - -// //SECTION - Build Return -// return const Scaffold( -// body: Center(child:Text(FormGroupWrapper)), -// ); -// //!SECTION -// } - -// @override -// void dispose() { -// //SECTION - Disposable variables -// //!SECTION -// super.dispose(); -// } -// } diff --git a/lib/src/form/src/models/form_group_structure.model.dart b/lib/src/form/src/models/form_group_structure.model.dart new file mode 100644 index 0000000..f26da52 --- /dev/null +++ b/lib/src/form/src/models/form_group_structure.model.dart @@ -0,0 +1,69 @@ +import 'dart:convert'; + +import 'package:flutter/foundation.dart'; + +class FormGroupStructure { + final String id; + final Map fields; + final List? subGroups; + FormGroupStructure({ + required this.id, + required this.fields, + this.subGroups, + }); + + FormGroupStructure copyWith({ + String? id, + Map? fields, + List? subGroups, + }) { + return FormGroupStructure( + id: id ?? this.id, + fields: fields ?? this.fields, + subGroups: subGroups ?? this.subGroups, + ); + } + + Map toMap() { + return { + 'id': id, + 'fields': fields, + 'subGroups': subGroups?.map((FormGroupStructure x) => x.toMap()).toList(), + }; + } + + 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, + ); + } + + String toJson() => json.encode(toMap()); + + factory FormGroupStructure.fromJson(String source) => FormGroupStructure.fromMap(json.decode(source) as Map); + + @override + String toString() { + return 'FormGroup(id: $id, fields: $fields, 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); + } + + @override + int get hashCode { + return id.hashCode ^ fields.hashCode ^ subGroups.hashCode; + } +} diff --git a/lib/src/form/src/models/form_group_value.model.dart b/lib/src/form/src/models/form_group_value.model.dart new file mode 100644 index 0000000..b038173 --- /dev/null +++ b/lib/src/form/src/models/form_group_value.model.dart @@ -0,0 +1,3 @@ +class FromGroupValue{ + final +} \ No newline at end of file diff --git a/lib/src/form/src/models/form_row.model.dart b/lib/src/form/src/models/form_row.model.dart deleted file mode 100644 index e69de29..0000000 diff --git a/pubspec.lock b/pubspec.lock index aeb1509..325fe9b 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -165,7 +165,7 @@ packages: description: path: "." ref: master - resolved-ref: ef28b08380a54747c58c585e4326e30bcc468a51 + resolved-ref: c14b8a2ecedbf840a43452fd696bc6ca01c3aa1d url: "https://git.micazi.dev/micazi/form_controller.git" source: git version: "0.8.8+2"