diff --git a/packages/generator_tests/pubspec.yaml b/packages/generator_tests/pubspec.yaml index d54b792f..2fc25c37 100644 --- a/packages/generator_tests/pubspec.yaml +++ b/packages/generator_tests/pubspec.yaml @@ -19,7 +19,7 @@ version: 1.0.0+1 environment: flutter: ">=3.0.0" - sdk: ">=3.8.0 <4.0.0" + sdk: ">=3.9.0 <4.0.0" resolution: workspace dependencies: @@ -45,6 +45,5 @@ dev_dependencies: flutter_lints: ^6.0.0 dependency_overrides: - analyzer: 8.2.0 - _fe_analyzer_shared: 89.0.0 - test_core: 0.6.8 \ No newline at end of file + analyzer: 9.0.0 + _fe_analyzer_shared: 92.0.0 diff --git a/packages/generator_tests/test/doc/animated_url_list_output_test.dart b/packages/generator_tests/test/doc/animated_url_list_output_test.dart index a0ff27db..f1174754 100644 --- a/packages/generator_tests/test/doc/animated_url_list_output_test.dart +++ b/packages/generator_tests/test/doc/animated_url_list_output_test.dart @@ -200,6 +200,7 @@ class _AnimatedUrlLisOFormBuilderState AnimatedUrlLisOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -242,7 +243,9 @@ class _AnimatedUrlLisOFormBuilderState @override void didUpdateWidget(covariant AnimatedUrlLisOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -278,8 +281,12 @@ final _logAnimatedUrlLisOForm = Logger.detached('AnimatedUrlLisOForm'); class AnimatedUrlLisOForm implements FormModel { - AnimatedUrlLisOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + AnimatedUrlLisOForm( + this.form, + this.path, + this._formModel, { + AnimatedUrlLisO? initialModel, + }) : _ownInitialModel = initialModel; static const String urlListControlName = "urlList"; @@ -292,8 +299,10 @@ class AnimatedUrlLisOForm final Map _disabled = {}; - @override - final Map initial; + AnimatedUrlLisO? _ownInitialModel; + + late Map _ownInitialRawValue = + AnimatedUrlLisOForm.formElements(_ownInitialModel).rawValue; String urlListControlPath() => pathBuilder(urlListControlName); @@ -554,10 +563,28 @@ class AnimatedUrlLisOForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + AnimatedUrlLisO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([AnimatedUrlLisO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = AnimatedUrlLisOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -596,55 +623,6 @@ class AnimatedUrlLisOForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -670,8 +648,12 @@ class AnimatedUrlLisOForm final _logUrlEntityOForm = Logger.detached('UrlEntityOForm'); class UrlEntityOForm implements FormModel { - UrlEntityOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + UrlEntityOForm( + this.form, + this.path, + this._formModel, { + UrlEntityO? initialModel, + }) : _ownInitialModel = initialModel; static const String labelControlName = "label"; @@ -686,8 +668,11 @@ class UrlEntityOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + UrlEntityO? _ownInitialModel; + + late Map _ownInitialRawValue = UrlEntityOForm.formElements( + _ownInitialModel, + ).rawValue; String labelControlPath() => pathBuilder(labelControlName); @@ -965,10 +950,28 @@ class UrlEntityOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + UrlEntityO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([UrlEntityO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = UrlEntityOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1007,55 +1010,6 @@ class UrlEntityOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/animated_url_list_test.dart b/packages/generator_tests/test/doc/animated_url_list_test.dart index b5b06e07..c93d8d78 100644 --- a/packages/generator_tests/test/doc/animated_url_list_test.dart +++ b/packages/generator_tests/test/doc/animated_url_list_test.dart @@ -200,6 +200,7 @@ class _AnimatedUrlListFormBuilderState AnimatedUrlListForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -242,7 +243,9 @@ class _AnimatedUrlListFormBuilderState @override void didUpdateWidget(covariant AnimatedUrlListFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -278,8 +281,12 @@ final _logAnimatedUrlListForm = Logger.detached('AnimatedUrlListForm'); class AnimatedUrlListForm implements FormModel { - AnimatedUrlListForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + AnimatedUrlListForm( + this.form, + this.path, + this._formModel, { + AnimatedUrlList? initialModel, + }) : _ownInitialModel = initialModel; static const String urlListControlName = "urlList"; @@ -292,8 +299,10 @@ class AnimatedUrlListForm final Map _disabled = {}; - @override - final Map initial; + AnimatedUrlList? _ownInitialModel; + + late Map _ownInitialRawValue = + AnimatedUrlListForm.formElements(_ownInitialModel).rawValue; String urlListControlPath() => pathBuilder(urlListControlName); @@ -553,10 +562,28 @@ class AnimatedUrlListForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + AnimatedUrlList? get initialModel { + return _ownInitialModel; + } + + void commitInitial([AnimatedUrlList? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = AnimatedUrlListForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -595,55 +622,6 @@ class AnimatedUrlListForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -669,8 +647,12 @@ class AnimatedUrlListForm final _logUrlEntityForm = Logger.detached('UrlEntityForm'); class UrlEntityForm implements FormModel { - UrlEntityForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + UrlEntityForm( + this.form, + this.path, + this._formModel, { + UrlEntity? initialModel, + }) : _ownInitialModel = initialModel; static const String labelControlName = "label"; @@ -685,8 +667,11 @@ class UrlEntityForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + UrlEntity? _ownInitialModel; + + late Map _ownInitialRawValue = UrlEntityForm.formElements( + _ownInitialModel, + ).rawValue; String labelControlPath() => pathBuilder(labelControlName); @@ -917,10 +902,26 @@ class UrlEntityForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + UrlEntity? get initialModel { + return _ownInitialModel; + } + + void commitInitial([UrlEntity? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = UrlEntityForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -959,55 +960,6 @@ class UrlEntityForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/annotateless_output_test.dart b/packages/generator_tests/test/doc/annotateless_output_test.dart index e693dbbd..dcc97f04 100644 --- a/packages/generator_tests/test/doc/annotateless_output_test.dart +++ b/packages/generator_tests/test/doc/annotateless_output_test.dart @@ -190,6 +190,7 @@ class _AnnotatelessOFormBuilderState extends State { AnnotatelessOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -232,7 +233,9 @@ class _AnnotatelessOFormBuilderState extends State { @override void didUpdateWidget(covariant AnnotatelessOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -268,8 +271,12 @@ final _logAnnotatelessOForm = Logger.detached('AnnotatelessOForm'); class AnnotatelessOForm implements FormModel { - AnnotatelessOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + AnnotatelessOForm( + this.form, + this.path, + this._formModel, { + AnnotatelessO? initialModel, + }) : _ownInitialModel = initialModel; static const String emailControlName = "email"; @@ -284,8 +291,10 @@ class AnnotatelessOForm final Map _disabled = {}; - @override - final Map initial; + AnnotatelessO? _ownInitialModel; + + late Map _ownInitialRawValue = + AnnotatelessOForm.formElements(_ownInitialModel).rawValue; String emailControlPath() => pathBuilder(emailControlName); @@ -517,10 +526,28 @@ class AnnotatelessOForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + AnnotatelessO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([AnnotatelessO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = AnnotatelessOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -559,55 +586,6 @@ class AnnotatelessOForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/annotateless_test.dart b/packages/generator_tests/test/doc/annotateless_test.dart index cb1e9426..d4bce0d2 100644 --- a/packages/generator_tests/test/doc/annotateless_test.dart +++ b/packages/generator_tests/test/doc/annotateless_test.dart @@ -187,6 +187,7 @@ class _AnnotatelessFormBuilderState extends State { AnnotatelessForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -229,7 +230,9 @@ class _AnnotatelessFormBuilderState extends State { @override void didUpdateWidget(covariant AnnotatelessFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -264,8 +267,12 @@ class _AnnotatelessFormBuilderState extends State { final _logAnnotatelessForm = Logger.detached('AnnotatelessForm'); class AnnotatelessForm implements FormModel { - AnnotatelessForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + AnnotatelessForm( + this.form, + this.path, + this._formModel, { + Annotateless? initialModel, + }) : _ownInitialModel = initialModel; static const String emailControlName = "email"; @@ -280,8 +287,11 @@ class AnnotatelessForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + Annotateless? _ownInitialModel; + + late Map _ownInitialRawValue = AnnotatelessForm.formElements( + _ownInitialModel, + ).rawValue; String emailControlPath() => pathBuilder(emailControlName); @@ -512,10 +522,28 @@ class AnnotatelessForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + Annotateless? get initialModel { + return _ownInitialModel; + } + + void commitInitial([Annotateless? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = AnnotatelessForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -554,55 +582,6 @@ class AnnotatelessForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/array_nullable_output_test.dart b/packages/generator_tests/test/doc/array_nullable_output_test.dart index 2fc6ca51..bc7becbd 100644 --- a/packages/generator_tests/test/doc/array_nullable_output_test.dart +++ b/packages/generator_tests/test/doc/array_nullable_output_test.dart @@ -202,6 +202,7 @@ class _ArrayNullableOFormBuilderState extends State { ArrayNullableOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -244,7 +245,9 @@ class _ArrayNullableOFormBuilderState extends State { @override void didUpdateWidget(covariant ArrayNullableOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -280,8 +283,12 @@ final _logArrayNullableOForm = Logger.detached('ArrayNullableOForm'); class ArrayNullableOForm implements FormModel { - ArrayNullableOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + ArrayNullableOForm( + this.form, + this.path, + this._formModel, { + ArrayNullableO? initialModel, + }) : _ownInitialModel = initialModel; static const String emailListControlName = "emailList"; @@ -302,8 +309,10 @@ class ArrayNullableOForm final Map _disabled = {}; - @override - final Map initial; + ArrayNullableO? _ownInitialModel; + + late Map _ownInitialRawValue = + ArrayNullableOForm.formElements(_ownInitialModel).rawValue; String someListControlPath() => pathBuilder(someListControlName); @@ -1044,10 +1053,28 @@ class ArrayNullableOForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + ArrayNullableO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([ArrayNullableO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = ArrayNullableOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1086,55 +1113,6 @@ class ArrayNullableOForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/array_nullable_test.dart b/packages/generator_tests/test/doc/array_nullable_test.dart index b902db2b..21956669 100644 --- a/packages/generator_tests/test/doc/array_nullable_test.dart +++ b/packages/generator_tests/test/doc/array_nullable_test.dart @@ -205,6 +205,7 @@ class _ArrayNullableFormBuilderState extends State { ArrayNullableForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -247,7 +248,9 @@ class _ArrayNullableFormBuilderState extends State { @override void didUpdateWidget(covariant ArrayNullableFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -282,8 +285,12 @@ class _ArrayNullableFormBuilderState extends State { final _logArrayNullableForm = Logger.detached('ArrayNullableForm'); class ArrayNullableForm implements FormModel { - ArrayNullableForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + ArrayNullableForm( + this.form, + this.path, + this._formModel, { + ArrayNullable? initialModel, + }) : _ownInitialModel = initialModel; static const String emailListControlName = "emailList"; @@ -306,8 +313,10 @@ class ArrayNullableForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + ArrayNullable? _ownInitialModel; + + late Map _ownInitialRawValue = + ArrayNullableForm.formElements(_ownInitialModel).rawValue; String someListControlPath() => pathBuilder(someListControlName); @@ -1131,10 +1140,28 @@ class ArrayNullableForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + ArrayNullable? get initialModel { + return _ownInitialModel; + } + + void commitInitial([ArrayNullable? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = ArrayNullableForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1173,55 +1200,6 @@ class ArrayNullableForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/create_output_test.dart b/packages/generator_tests/test/doc/create_output_test.dart index fc2ff63e..c227ea31 100644 --- a/packages/generator_tests/test/doc/create_output_test.dart +++ b/packages/generator_tests/test/doc/create_output_test.dart @@ -246,6 +246,7 @@ class _MSICreateFormBuilderState extends State { MSICreateForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -288,7 +289,9 @@ class _MSICreateFormBuilderState extends State { @override void didUpdateWidget(covariant MSICreateFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -323,8 +326,12 @@ class _MSICreateFormBuilderState extends State { final _logMSICreateForm = Logger.detached('MSICreateForm'); class MSICreateForm implements FormModel { - MSICreateForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + MSICreateForm( + this.form, + this.path, + this._formModel, { + MSICreate? initialModel, + }) : _ownInitialModel = initialModel; static const String idControlName = "id"; @@ -356,8 +363,11 @@ class MSICreateForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + MSICreate? _ownInitialModel; + + late Map _ownInitialRawValue = MSICreateForm.formElements( + _ownInitialModel, + ).rawValue; String idControlPath() => pathBuilder(idControlName); @@ -1490,10 +1500,26 @@ class MSICreateForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + MSICreate? get initialModel { + return _ownInitialModel; + } + + void commitInitial([MSICreate? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = MSICreateForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1532,55 +1558,6 @@ class MSICreateForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1663,7 +1640,8 @@ class MSICreateForm implements FormModel { final _logAddressForm = Logger.detached('AddressForm'); class AddressForm implements FormModel { - AddressForm(this.form, this.path, this._formModel) : initial = form.rawValue; + AddressForm(this.form, this.path, this._formModel, {Address? initialModel}) + : _ownInitialModel = initialModel; static const String streetControlName = "street"; @@ -1682,8 +1660,11 @@ class AddressForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + Address? _ownInitialModel; + + late Map _ownInitialRawValue = AddressForm.formElements( + _ownInitialModel, + ).rawValue; String streetControlPath() => pathBuilder(streetControlName); @@ -2179,10 +2160,26 @@ class AddressForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + Address? get initialModel { + return _ownInitialModel; + } + + void commitInitial([Address? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = AddressForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -2221,55 +2218,6 @@ class AddressForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -2319,8 +2267,12 @@ final _logPrimaryContactForm = Logger.detached('PrimaryContactForm'); class PrimaryContactForm implements FormModel { - PrimaryContactForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + PrimaryContactForm( + this.form, + this.path, + this._formModel, { + PrimaryContact? initialModel, + }) : _ownInitialModel = initialModel; static const String fullNameControlName = "fullName"; @@ -2337,8 +2289,10 @@ class PrimaryContactForm final Map _disabled = {}; - @override - final Map initial; + PrimaryContact? _ownInitialModel; + + late Map _ownInitialRawValue = + PrimaryContactForm.formElements(_ownInitialModel).rawValue; String fullNameControlPath() => pathBuilder(fullNameControlName); @@ -2727,10 +2681,28 @@ class PrimaryContactForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + PrimaryContact? get initialModel { + return _ownInitialModel; + } + + void commitInitial([PrimaryContact? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = PrimaryContactForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -2769,55 +2741,6 @@ class PrimaryContactForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -2862,8 +2785,12 @@ final _logAdminContactInformationForm = Logger.detached( class AdminContactInformationForm implements FormModel { - AdminContactInformationForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + AdminContactInformationForm( + this.form, + this.path, + this._formModel, { + AdminContactInformation? initialModel, + }) : _ownInitialModel = initialModel; static const String firstNameControlName = "firstName"; @@ -2880,8 +2807,10 @@ class AdminContactInformationForm final Map _disabled = {}; - @override - final Map initial; + AdminContactInformation? _ownInitialModel; + + late Map _ownInitialRawValue = + AdminContactInformationForm.formElements(_ownInitialModel).rawValue; String firstNameControlPath() => pathBuilder(firstNameControlName); @@ -3270,10 +3199,28 @@ class AdminContactInformationForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + AdminContactInformation? get initialModel { + return _ownInitialModel; + } + + void commitInitial([AdminContactInformation? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = AdminContactInformationForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -3312,55 +3259,6 @@ class AdminContactInformationForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/delivery_list_output_test.dart b/packages/generator_tests/test/doc/delivery_list_output_test.dart index dafd8816..be757997 100644 --- a/packages/generator_tests/test/doc/delivery_list_output_test.dart +++ b/packages/generator_tests/test/doc/delivery_list_output_test.dart @@ -243,6 +243,7 @@ class _DeliveryListOFormBuilderState extends State { DeliveryListOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -285,7 +286,9 @@ class _DeliveryListOFormBuilderState extends State { @override void didUpdateWidget(covariant DeliveryListOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -321,8 +324,12 @@ final _logDeliveryListOForm = Logger.detached('DeliveryListOForm'); class DeliveryListOForm implements FormModel { - DeliveryListOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + DeliveryListOForm( + this.form, + this.path, + this._formModel, { + DeliveryListO? initialModel, + }) : _ownInitialModel = initialModel; static const String deliveryListControlName = "deliveryList"; @@ -337,8 +344,10 @@ class DeliveryListOForm final Map _disabled = {}; - @override - final Map initial; + DeliveryListO? _ownInitialModel; + + late Map _ownInitialRawValue = + DeliveryListOForm.formElements(_ownInitialModel).rawValue; String deliveryListControlPath() => pathBuilder(deliveryListControlName); @@ -824,10 +833,28 @@ class DeliveryListOForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + DeliveryListO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([DeliveryListO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = DeliveryListOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -866,55 +893,6 @@ class DeliveryListOForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -950,8 +928,12 @@ final _logDeliveryPointOForm = Logger.detached('DeliveryPointOForm'); class DeliveryPointOForm implements FormModel { - DeliveryPointOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + DeliveryPointOForm( + this.form, + this.path, + this._formModel, { + DeliveryPointO? initialModel, + }) : _ownInitialModel = initialModel; static const String nameControlName = "name"; @@ -966,8 +948,10 @@ class DeliveryPointOForm final Map _disabled = {}; - @override - final Map initial; + DeliveryPointO? _ownInitialModel; + + late Map _ownInitialRawValue = + DeliveryPointOForm.formElements(_ownInitialModel).rawValue; String nameControlPath() => pathBuilder(nameControlName); @@ -1227,10 +1211,28 @@ class DeliveryPointOForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + DeliveryPointO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([DeliveryPointO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = DeliveryPointOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1269,55 +1271,6 @@ class DeliveryPointOForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1343,7 +1296,8 @@ class DeliveryPointOForm final _logAddressOForm = Logger.detached('AddressOForm'); class AddressOForm implements FormModel { - AddressOForm(this.form, this.path, this._formModel) : initial = form.rawValue; + AddressOForm(this.form, this.path, this._formModel, {AddressO? initialModel}) + : _ownInitialModel = initialModel; static const String streetControlName = "street"; @@ -1358,8 +1312,11 @@ class AddressOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + AddressO? _ownInitialModel; + + late Map _ownInitialRawValue = AddressOForm.formElements( + _ownInitialModel, + ).rawValue; String streetControlPath() => pathBuilder(streetControlName); @@ -1637,10 +1594,26 @@ class AddressOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + AddressO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([AddressO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = AddressOForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1679,55 +1652,6 @@ class AddressOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1760,7 +1684,8 @@ class AddressOForm implements FormModel { final _logClientOForm = Logger.detached('ClientOForm'); class ClientOForm implements FormModel { - ClientOForm(this.form, this.path, this._formModel) : initial = form.rawValue; + ClientOForm(this.form, this.path, this._formModel, {ClientO? initialModel}) + : _ownInitialModel = initialModel; static const String clientTypeControlName = "clientType"; @@ -1777,8 +1702,11 @@ class ClientOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + ClientO? _ownInitialModel; + + late Map _ownInitialRawValue = ClientOForm.formElements( + _ownInitialModel, + ).rawValue; String clientTypeControlPath() => pathBuilder(clientTypeControlName); @@ -2142,10 +2070,26 @@ class ClientOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + ClientO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([ClientO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = ClientOForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -2184,55 +2128,6 @@ class ClientOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -2699,6 +2594,7 @@ class _StandaloneDeliveryPointFormBuilderState StandaloneDeliveryPointForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -2743,7 +2639,9 @@ class _StandaloneDeliveryPointFormBuilderState @override void didUpdateWidget(covariant StandaloneDeliveryPointFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -2781,8 +2679,12 @@ final _logStandaloneDeliveryPointForm = Logger.detached( class StandaloneDeliveryPointForm implements FormModel { - StandaloneDeliveryPointForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + StandaloneDeliveryPointForm( + this.form, + this.path, + this._formModel, { + DeliveryPointO? initialModel, + }) : _ownInitialModel = initialModel; static const String nameControlName = "name"; @@ -2797,8 +2699,10 @@ class StandaloneDeliveryPointForm final Map _disabled = {}; - @override - final Map initial; + DeliveryPointO? _ownInitialModel; + + late Map _ownInitialRawValue = + StandaloneDeliveryPointForm.formElements(_ownInitialModel).rawValue; String nameControlPath() => pathBuilder(nameControlName); @@ -3058,10 +2962,28 @@ class StandaloneDeliveryPointForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + DeliveryPointO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([DeliveryPointO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = StandaloneDeliveryPointForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -3100,55 +3022,6 @@ class StandaloneDeliveryPointForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/delivery_list_test.dart b/packages/generator_tests/test/doc/delivery_list_test.dart index 592d3ae0..b2e066ce 100644 --- a/packages/generator_tests/test/doc/delivery_list_test.dart +++ b/packages/generator_tests/test/doc/delivery_list_test.dart @@ -235,6 +235,7 @@ class _DeliveryListFormBuilderState extends State { DeliveryListForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -277,7 +278,9 @@ class _DeliveryListFormBuilderState extends State { @override void didUpdateWidget(covariant DeliveryListFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -312,8 +315,12 @@ class _DeliveryListFormBuilderState extends State { final _logDeliveryListForm = Logger.detached('DeliveryListForm'); class DeliveryListForm implements FormModel { - DeliveryListForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + DeliveryListForm( + this.form, + this.path, + this._formModel, { + DeliveryList? initialModel, + }) : _ownInitialModel = initialModel; static const String deliveryListControlName = "deliveryList"; @@ -328,8 +335,11 @@ class DeliveryListForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + DeliveryList? _ownInitialModel; + + late Map _ownInitialRawValue = DeliveryListForm.formElements( + _ownInitialModel, + ).rawValue; String deliveryListControlPath() => pathBuilder(deliveryListControlName); @@ -814,10 +824,28 @@ class DeliveryListForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + DeliveryList? get initialModel { + return _ownInitialModel; + } + + void commitInitial([DeliveryList? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = DeliveryListForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -856,55 +884,6 @@ class DeliveryListForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -939,8 +918,12 @@ class DeliveryListForm implements FormModel { final _logDeliveryPointForm = Logger.detached('DeliveryPointForm'); class DeliveryPointForm implements FormModel { - DeliveryPointForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + DeliveryPointForm( + this.form, + this.path, + this._formModel, { + DeliveryPoint? initialModel, + }) : _ownInitialModel = initialModel; static const String nameControlName = "name"; @@ -955,8 +938,10 @@ class DeliveryPointForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + DeliveryPoint? _ownInitialModel; + + late Map _ownInitialRawValue = + DeliveryPointForm.formElements(_ownInitialModel).rawValue; String nameControlPath() => pathBuilder(nameControlName); @@ -1214,10 +1199,28 @@ class DeliveryPointForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + DeliveryPoint? get initialModel { + return _ownInitialModel; + } + + void commitInitial([DeliveryPoint? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = DeliveryPointForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1256,55 +1259,6 @@ class DeliveryPointForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1330,7 +1284,8 @@ class DeliveryPointForm implements FormModel { final _logAddressForm = Logger.detached('AddressForm'); class AddressForm implements FormModel { - AddressForm(this.form, this.path, this._formModel) : initial = form.rawValue; + AddressForm(this.form, this.path, this._formModel, {Address? initialModel}) + : _ownInitialModel = initialModel; static const String streetControlName = "street"; @@ -1345,8 +1300,11 @@ class AddressForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + Address? _ownInitialModel; + + late Map _ownInitialRawValue = AddressForm.formElements( + _ownInitialModel, + ).rawValue; String streetControlPath() => pathBuilder(streetControlName); @@ -1623,10 +1581,26 @@ class AddressForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + Address? get initialModel { + return _ownInitialModel; + } + + void commitInitial([Address? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = AddressForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1665,55 +1639,6 @@ class AddressForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1746,7 +1671,8 @@ class AddressForm implements FormModel { final _logClientForm = Logger.detached('ClientForm'); class ClientForm implements FormModel { - ClientForm(this.form, this.path, this._formModel) : initial = form.rawValue; + ClientForm(this.form, this.path, this._formModel, {Client? initialModel}) + : _ownInitialModel = initialModel; static const String clientTypeControlName = "clientType"; @@ -1763,8 +1689,11 @@ class ClientForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + Client? _ownInitialModel; + + late Map _ownInitialRawValue = ClientForm.formElements( + _ownInitialModel, + ).rawValue; String clientTypeControlPath() => pathBuilder(clientTypeControlName); @@ -2127,10 +2056,26 @@ class ClientForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + Client? get initialModel { + return _ownInitialModel; + } + + void commitInitial([Client? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = ClientForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -2169,55 +2114,6 @@ class ClientForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -2633,6 +2529,7 @@ class _StandaloneDeliveryPointFormBuilderState StandaloneDeliveryPointForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -2677,7 +2574,9 @@ class _StandaloneDeliveryPointFormBuilderState @override void didUpdateWidget(covariant StandaloneDeliveryPointFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -2715,8 +2614,12 @@ final _logStandaloneDeliveryPointForm = Logger.detached( class StandaloneDeliveryPointForm implements FormModel { - StandaloneDeliveryPointForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + StandaloneDeliveryPointForm( + this.form, + this.path, + this._formModel, { + DeliveryPoint? initialModel, + }) : _ownInitialModel = initialModel; static const String nameControlName = "name"; @@ -2731,8 +2634,10 @@ class StandaloneDeliveryPointForm final Map _disabled = {}; - @override - final Map initial; + DeliveryPoint? _ownInitialModel; + + late Map _ownInitialRawValue = + StandaloneDeliveryPointForm.formElements(_ownInitialModel).rawValue; String nameControlPath() => pathBuilder(nameControlName); @@ -2990,10 +2895,28 @@ class StandaloneDeliveryPointForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + DeliveryPoint? get initialModel { + return _ownInitialModel; + } + + void commitInitial([DeliveryPoint? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = StandaloneDeliveryPointForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -3032,55 +2955,6 @@ class StandaloneDeliveryPointForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/freezed_class_output_test.dart b/packages/generator_tests/test/doc/freezed_class_output_test.dart index 1d738ff2..288a9146 100644 --- a/packages/generator_tests/test/doc/freezed_class_output_test.dart +++ b/packages/generator_tests/test/doc/freezed_class_output_test.dart @@ -203,6 +203,7 @@ class _FreezedClassOFormBuilderState extends State { FreezedClassOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -245,7 +246,9 @@ class _FreezedClassOFormBuilderState extends State { @override void didUpdateWidget(covariant FreezedClassOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -281,8 +284,12 @@ final _logFreezedClassOForm = Logger.detached('FreezedClassOForm'); class FreezedClassOForm implements FormModel { - FreezedClassOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + FreezedClassOForm( + this.form, + this.path, + this._formModel, { + FreezedClassO? initialModel, + }) : _ownInitialModel = initialModel; static const String genderControlName = "gender"; @@ -311,8 +318,10 @@ class FreezedClassOForm final Map _disabled = {}; - @override - final Map initial; + FreezedClassO? _ownInitialModel; + + late Map _ownInitialRawValue = + FreezedClassOForm.formElements(_ownInitialModel).rawValue; String genderControlPath() => pathBuilder(genderControlName); @@ -1270,10 +1279,28 @@ class FreezedClassOForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + FreezedClassO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([FreezedClassO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = FreezedClassOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1312,55 +1339,6 @@ class FreezedClassOForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/freezed_class_test.dart b/packages/generator_tests/test/doc/freezed_class_test.dart index af7b64bc..8c228a56 100644 --- a/packages/generator_tests/test/doc/freezed_class_test.dart +++ b/packages/generator_tests/test/doc/freezed_class_test.dart @@ -202,6 +202,7 @@ class _FreezedClassFormBuilderState extends State { FreezedClassForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -244,7 +245,9 @@ class _FreezedClassFormBuilderState extends State { @override void didUpdateWidget(covariant FreezedClassFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -279,8 +282,12 @@ class _FreezedClassFormBuilderState extends State { final _logFreezedClassForm = Logger.detached('FreezedClassForm'); class FreezedClassForm implements FormModel { - FreezedClassForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + FreezedClassForm( + this.form, + this.path, + this._formModel, { + FreezedClass? initialModel, + }) : _ownInitialModel = initialModel; static const String genderControlName = "gender"; @@ -303,8 +310,11 @@ class FreezedClassForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + FreezedClass? _ownInitialModel; + + late Map _ownInitialRawValue = FreezedClassForm.formElements( + _ownInitialModel, + ).rawValue; String genderControlPath() => pathBuilder(genderControlName); @@ -975,10 +985,28 @@ class FreezedClassForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + FreezedClass? get initialModel { + return _ownInitialModel; + } + + void commitInitial([FreezedClass? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = FreezedClassForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1017,55 +1045,6 @@ class FreezedClassForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/generic_output_test.dart b/packages/generator_tests/test/doc/generic_output_test.dart index 8a6be633..0ac91579 100644 --- a/packages/generator_tests/test/doc/generic_output_test.dart +++ b/packages/generator_tests/test/doc/generic_output_test.dart @@ -177,6 +177,7 @@ class _TagsOFormBuilderState extends State> { TagsOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -219,7 +220,9 @@ class _TagsOFormBuilderState extends State> { @override void didUpdateWidget(covariant TagsOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -254,7 +257,8 @@ class _TagsOFormBuilderState extends State> { final _logTagsOForm = Logger.detached('TagsOForm'); class TagsOForm implements FormModel, TagsOOutput> { - TagsOForm(this.form, this.path, this._formModel) : initial = form.rawValue; + TagsOForm(this.form, this.path, this._formModel, {TagsO? initialModel}) + : _ownInitialModel = initialModel; static const String tagsControlName = "tags"; @@ -267,8 +271,11 @@ class TagsOForm implements FormModel, TagsOOutput> { final Map _disabled = {}; - @override - final Map initial; + TagsO? _ownInitialModel; + + late Map _ownInitialRawValue = TagsOForm.formElements( + _ownInitialModel, + ).rawValue; String tagsControlPath() => pathBuilder(tagsControlName); @@ -445,10 +452,26 @@ class TagsOForm implements FormModel, TagsOOutput> { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + TagsO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([TagsO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = TagsOForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -487,55 +510,6 @@ class TagsOForm implements FormModel, TagsOOutput> { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/generic_status_list_output_test.dart b/packages/generator_tests/test/doc/generic_status_list_output_test.dart index 052ceb22..0d066fc5 100644 --- a/packages/generator_tests/test/doc/generic_status_list_output_test.dart +++ b/packages/generator_tests/test/doc/generic_status_list_output_test.dart @@ -187,6 +187,7 @@ class _StatusListOFormBuilderState StatusListOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -229,7 +230,9 @@ class _StatusListOFormBuilderState @override void didUpdateWidget(covariant StatusListOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -265,8 +268,12 @@ final _logStatusListOForm = Logger.detached('StatusListOForm'); class StatusListOForm implements FormModel, StatusListOOutput> { - StatusListOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + StatusListOForm( + this.form, + this.path, + this._formModel, { + StatusListO? initialModel, + }) : _ownInitialModel = initialModel; static const String listControlName = "list"; @@ -279,8 +286,11 @@ class StatusListOForm final Map _disabled = {}; - @override - final Map initial; + StatusListO? _ownInitialModel; + + late Map _ownInitialRawValue = StatusListOForm.formElements( + _ownInitialModel, + ).rawValue; String listControlPath() => pathBuilder(listControlName); @@ -479,10 +489,28 @@ class StatusListOForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + StatusListO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([StatusListO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = StatusListOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -521,55 +549,6 @@ class StatusListOForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/generic_status_list_test.dart b/packages/generator_tests/test/doc/generic_status_list_test.dart index a8e83782..1a4c5676 100644 --- a/packages/generator_tests/test/doc/generic_status_list_test.dart +++ b/packages/generator_tests/test/doc/generic_status_list_test.dart @@ -187,6 +187,7 @@ class _StatusListFormBuilderState StatusListForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -229,7 +230,9 @@ class _StatusListFormBuilderState @override void didUpdateWidget(covariant StatusListFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -265,8 +268,12 @@ final _logStatusListForm = Logger.detached('StatusListForm'); class StatusListForm implements FormModel, StatusList> { - StatusListForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + StatusListForm( + this.form, + this.path, + this._formModel, { + StatusList? initialModel, + }) : _ownInitialModel = initialModel; static const String listControlName = "list"; @@ -279,8 +286,11 @@ class StatusListForm final Map _disabled = {}; - @override - final Map initial; + StatusList? _ownInitialModel; + + late Map _ownInitialRawValue = StatusListForm.formElements( + _ownInitialModel, + ).rawValue; String listControlPath() => pathBuilder(listControlName); @@ -478,10 +488,28 @@ class StatusListForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + StatusList? get initialModel { + return _ownInitialModel; + } + + void commitInitial([StatusList? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = StatusListForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -520,55 +548,6 @@ class StatusListForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/generic_test.dart b/packages/generator_tests/test/doc/generic_test.dart index 13d7816c..7bc6ae55 100644 --- a/packages/generator_tests/test/doc/generic_test.dart +++ b/packages/generator_tests/test/doc/generic_test.dart @@ -176,6 +176,7 @@ class _TagsFormBuilderState extends State> { TagsForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -218,7 +219,9 @@ class _TagsFormBuilderState extends State> { @override void didUpdateWidget(covariant TagsFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -253,7 +256,8 @@ class _TagsFormBuilderState extends State> { final _logTagsForm = Logger.detached('TagsForm'); class TagsForm implements FormModel, Tags> { - TagsForm(this.form, this.path, this._formModel) : initial = form.rawValue; + TagsForm(this.form, this.path, this._formModel, {Tags? initialModel}) + : _ownInitialModel = initialModel; static const String tagsControlName = "tags"; @@ -266,8 +270,11 @@ class TagsForm implements FormModel, Tags> { final Map _disabled = {}; - @override - final Map initial; + Tags? _ownInitialModel; + + late Map _ownInitialRawValue = TagsForm.formElements( + _ownInitialModel, + ).rawValue; String tagsControlPath() => pathBuilder(tagsControlName); @@ -443,10 +450,26 @@ class TagsForm implements FormModel, Tags> { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + Tags? get initialModel { + return _ownInitialModel; + } + + void commitInitial([Tags? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = TagsForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -485,55 +508,6 @@ class TagsForm implements FormModel, Tags> { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/group_output_test.dart b/packages/generator_tests/test/doc/group_output_test.dart index 34844d31..bf916d7d 100644 --- a/packages/generator_tests/test/doc/group_output_test.dart +++ b/packages/generator_tests/test/doc/group_output_test.dart @@ -220,7 +220,12 @@ class _GroupOFormBuilderState extends State { @override void initState() { - _formModel = GroupOForm(GroupOForm.formElements(widget.model), null, null); + _formModel = GroupOForm( + GroupOForm.formElements(widget.model), + null, + null, + initialModel: widget.model, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -262,7 +267,9 @@ class _GroupOFormBuilderState extends State { @override void didUpdateWidget(covariant GroupOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -297,7 +304,8 @@ class _GroupOFormBuilderState extends State { final _logGroupOForm = Logger.detached('GroupOForm'); class GroupOForm implements FormModel { - GroupOForm(this.form, this.path, this._formModel) : initial = form.rawValue; + GroupOForm(this.form, this.path, this._formModel, {GroupO? initialModel}) + : _ownInitialModel = initialModel; static const String personalControlName = "personal"; @@ -316,8 +324,11 @@ class GroupOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + GroupO? _ownInitialModel; + + late Map _ownInitialRawValue = GroupOForm.formElements( + _ownInitialModel, + ).rawValue; String personalControlPath() => pathBuilder(personalControlName); @@ -824,10 +835,26 @@ class GroupOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + GroupO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([GroupO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = GroupOForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -866,55 +893,6 @@ class GroupOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -935,8 +913,12 @@ class GroupOForm implements FormModel { final _logPersonalOForm = Logger.detached('PersonalOForm'); class PersonalOForm implements FormModel { - PersonalOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + PersonalOForm( + this.form, + this.path, + this._formModel, { + PersonalO? initialModel, + }) : _ownInitialModel = initialModel; static const String nameControlName = "name"; @@ -951,8 +933,11 @@ class PersonalOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + PersonalO? _ownInitialModel; + + late Map _ownInitialRawValue = PersonalOForm.formElements( + _ownInitialModel, + ).rawValue; String nameControlPath() => pathBuilder(nameControlName); @@ -1230,10 +1215,26 @@ class PersonalOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + PersonalO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([PersonalO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = PersonalOForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1272,55 +1273,6 @@ class PersonalOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1353,7 +1305,8 @@ class PersonalOForm implements FormModel { final _logPhoneOForm = Logger.detached('PhoneOForm'); class PhoneOForm implements FormModel { - PhoneOForm(this.form, this.path, this._formModel) : initial = form.rawValue; + PhoneOForm(this.form, this.path, this._formModel, {PhoneO? initialModel}) + : _ownInitialModel = initialModel; static const String phoneNumberControlName = "phoneNumber"; @@ -1368,8 +1321,11 @@ class PhoneOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + PhoneO? _ownInitialModel; + + late Map _ownInitialRawValue = PhoneOForm.formElements( + _ownInitialModel, + ).rawValue; String phoneNumberControlPath() => pathBuilder(phoneNumberControlName); @@ -1657,10 +1613,26 @@ class PhoneOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + PhoneO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([PhoneO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = PhoneOForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1699,55 +1671,6 @@ class PhoneOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1780,7 +1703,8 @@ class PhoneOForm implements FormModel { final _logAddressOForm = Logger.detached('AddressOForm'); class AddressOForm implements FormModel { - AddressOForm(this.form, this.path, this._formModel) : initial = form.rawValue; + AddressOForm(this.form, this.path, this._formModel, {AddressO? initialModel}) + : _ownInitialModel = initialModel; static const String streetControlName = "street"; @@ -1797,8 +1721,11 @@ class AddressOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + AddressO? _ownInitialModel; + + late Map _ownInitialRawValue = AddressOForm.formElements( + _ownInitialModel, + ).rawValue; String streetControlPath() => pathBuilder(streetControlName); @@ -2185,10 +2112,26 @@ class AddressOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + AddressO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([AddressO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = AddressOForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -2227,55 +2170,6 @@ class AddressOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/group_test.dart b/packages/generator_tests/test/doc/group_test.dart index 5befe174..6d23a1a5 100644 --- a/packages/generator_tests/test/doc/group_test.dart +++ b/packages/generator_tests/test/doc/group_test.dart @@ -217,7 +217,12 @@ class _GroupFormBuilderState extends State { @override void initState() { - _formModel = GroupForm(GroupForm.formElements(widget.model), null, null); + _formModel = GroupForm( + GroupForm.formElements(widget.model), + null, + null, + initialModel: widget.model, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -259,7 +264,9 @@ class _GroupFormBuilderState extends State { @override void didUpdateWidget(covariant GroupFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -294,7 +301,8 @@ class _GroupFormBuilderState extends State { final _logGroupForm = Logger.detached('GroupForm'); class GroupForm implements FormModel { - GroupForm(this.form, this.path, this._formModel) : initial = form.rawValue; + GroupForm(this.form, this.path, this._formModel, {Group? initialModel}) + : _ownInitialModel = initialModel; static const String personalControlName = "personal"; @@ -313,8 +321,11 @@ class GroupForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + Group? _ownInitialModel; + + late Map _ownInitialRawValue = GroupForm.formElements( + _ownInitialModel, + ).rawValue; String personalControlPath() => pathBuilder(personalControlName); @@ -817,10 +828,26 @@ class GroupForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + Group? get initialModel { + return _ownInitialModel; + } + + void commitInitial([Group? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = GroupForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -856,55 +883,6 @@ class GroupForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -925,7 +903,8 @@ class GroupForm implements FormModel { final _logPersonalForm = Logger.detached('PersonalForm'); class PersonalForm implements FormModel { - PersonalForm(this.form, this.path, this._formModel) : initial = form.rawValue; + PersonalForm(this.form, this.path, this._formModel, {Personal? initialModel}) + : _ownInitialModel = initialModel; static const String nameControlName = "name"; @@ -940,8 +919,11 @@ class PersonalForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + Personal? _ownInitialModel; + + late Map _ownInitialRawValue = PersonalForm.formElements( + _ownInitialModel, + ).rawValue; String nameControlPath() => pathBuilder(nameControlName); @@ -1218,10 +1200,26 @@ class PersonalForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + Personal? get initialModel { + return _ownInitialModel; + } + + void commitInitial([Personal? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = PersonalForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1260,55 +1258,6 @@ class PersonalForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1341,7 +1290,8 @@ class PersonalForm implements FormModel { final _logPhoneForm = Logger.detached('PhoneForm'); class PhoneForm implements FormModel { - PhoneForm(this.form, this.path, this._formModel) : initial = form.rawValue; + PhoneForm(this.form, this.path, this._formModel, {Phone? initialModel}) + : _ownInitialModel = initialModel; static const String phoneNumberControlName = "phoneNumber"; @@ -1356,8 +1306,11 @@ class PhoneForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + Phone? _ownInitialModel; + + late Map _ownInitialRawValue = PhoneForm.formElements( + _ownInitialModel, + ).rawValue; String phoneNumberControlPath() => pathBuilder(phoneNumberControlName); @@ -1641,10 +1594,26 @@ class PhoneForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + Phone? get initialModel { + return _ownInitialModel; + } + + void commitInitial([Phone? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = PhoneForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1680,55 +1649,6 @@ class PhoneForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1761,7 +1681,8 @@ class PhoneForm implements FormModel { final _logAddressForm = Logger.detached('AddressForm'); class AddressForm implements FormModel { - AddressForm(this.form, this.path, this._formModel) : initial = form.rawValue; + AddressForm(this.form, this.path, this._formModel, {Address? initialModel}) + : _ownInitialModel = initialModel; static const String streetControlName = "street"; @@ -1778,8 +1699,11 @@ class AddressForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + Address? _ownInitialModel; + + late Map _ownInitialRawValue = AddressForm.formElements( + _ownInitialModel, + ).rawValue; String streetControlPath() => pathBuilder(streetControlName); @@ -2161,10 +2085,26 @@ class AddressForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + Address? get initialModel { + return _ownInitialModel; + } + + void commitInitial([Address? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = AddressForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -2203,55 +2143,6 @@ class AddressForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/login_extended_nullable_output_test.dart b/packages/generator_tests/test/doc/login_extended_nullable_output_test.dart index 7c46276a..4e3aac5a 100644 --- a/packages/generator_tests/test/doc/login_extended_nullable_output_test.dart +++ b/packages/generator_tests/test/doc/login_extended_nullable_output_test.dart @@ -212,6 +212,7 @@ class _LoginExtendedNullableOFormBuilderState LoginExtendedNullableOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -256,7 +257,9 @@ class _LoginExtendedNullableOFormBuilderState @override void didUpdateWidget(covariant LoginExtendedNullableOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -294,8 +297,12 @@ final _logLoginExtendedNullableOForm = Logger.detached( class LoginExtendedNullableOForm implements FormModel { - LoginExtendedNullableOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + LoginExtendedNullableOForm( + this.form, + this.path, + this._formModel, { + LoginExtendedNullableO? initialModel, + }) : _ownInitialModel = initialModel; static const String emailControlName = "email"; @@ -320,8 +327,10 @@ class LoginExtendedNullableOForm final Map _disabled = {}; - @override - final Map initial; + LoginExtendedNullableO? _ownInitialModel; + + late Map _ownInitialRawValue = + LoginExtendedNullableOForm.formElements(_ownInitialModel).rawValue; String emailControlPath() => pathBuilder(emailControlName); @@ -1123,10 +1132,28 @@ class LoginExtendedNullableOForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + LoginExtendedNullableO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([LoginExtendedNullableO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = LoginExtendedNullableOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1165,55 +1192,6 @@ class LoginExtendedNullableOForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/login_extended_nullable_test.dart b/packages/generator_tests/test/doc/login_extended_nullable_test.dart index 2cf420d1..c12e114d 100644 --- a/packages/generator_tests/test/doc/login_extended_nullable_test.dart +++ b/packages/generator_tests/test/doc/login_extended_nullable_test.dart @@ -212,6 +212,7 @@ class _LoginExtendedNullableFormBuilderState LoginExtendedNullableForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -256,7 +257,9 @@ class _LoginExtendedNullableFormBuilderState @override void didUpdateWidget(covariant LoginExtendedNullableFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -294,8 +297,12 @@ final _logLoginExtendedNullableForm = Logger.detached( class LoginExtendedNullableForm implements FormModel { - LoginExtendedNullableForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + LoginExtendedNullableForm( + this.form, + this.path, + this._formModel, { + LoginExtendedNullable? initialModel, + }) : _ownInitialModel = initialModel; static const String emailControlName = "email"; @@ -320,8 +327,10 @@ class LoginExtendedNullableForm final Map _disabled = {}; - @override - final Map initial; + LoginExtendedNullable? _ownInitialModel; + + late Map _ownInitialRawValue = + LoginExtendedNullableForm.formElements(_ownInitialModel).rawValue; String emailControlPath() => pathBuilder(emailControlName); @@ -1122,10 +1131,28 @@ class LoginExtendedNullableForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + LoginExtendedNullable? get initialModel { + return _ownInitialModel; + } + + void commitInitial([LoginExtendedNullable? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = LoginExtendedNullableForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1164,55 +1191,6 @@ class LoginExtendedNullableForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/login_extended_output_test.dart b/packages/generator_tests/test/doc/login_extended_output_test.dart index 82b132bd..427c2182 100644 --- a/packages/generator_tests/test/doc/login_extended_output_test.dart +++ b/packages/generator_tests/test/doc/login_extended_output_test.dart @@ -259,6 +259,7 @@ class _LoginExtendedOFormBuilderState extends State { LoginExtendedOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -301,7 +302,9 @@ class _LoginExtendedOFormBuilderState extends State { @override void didUpdateWidget(covariant LoginExtendedOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -337,8 +340,12 @@ final _logLoginExtendedOForm = Logger.detached('LoginExtendedOForm'); class LoginExtendedOForm implements FormModel { - LoginExtendedOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + LoginExtendedOForm( + this.form, + this.path, + this._formModel, { + LoginExtendedO? initialModel, + }) : _ownInitialModel = initialModel; static const String emailControlName = "email"; @@ -369,8 +376,10 @@ class LoginExtendedOForm final Map _disabled = {}; - @override - final Map initial; + LoginExtendedO? _ownInitialModel; + + late Map _ownInitialRawValue = + LoginExtendedOForm.formElements(_ownInitialModel).rawValue; String emailControlPath() => pathBuilder(emailControlName); @@ -1296,10 +1305,28 @@ class LoginExtendedOForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + LoginExtendedO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([LoginExtendedO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = LoginExtendedOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1338,55 +1365,6 @@ class LoginExtendedOForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/login_extended_test.dart b/packages/generator_tests/test/doc/login_extended_test.dart index 4832a2ef..779145c2 100644 --- a/packages/generator_tests/test/doc/login_extended_test.dart +++ b/packages/generator_tests/test/doc/login_extended_test.dart @@ -254,6 +254,7 @@ class _LoginExtendedFormBuilderState extends State { LoginExtendedForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -296,7 +297,9 @@ class _LoginExtendedFormBuilderState extends State { @override void didUpdateWidget(covariant LoginExtendedFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -331,8 +334,12 @@ class _LoginExtendedFormBuilderState extends State { final _logLoginExtendedForm = Logger.detached('LoginExtendedForm'); class LoginExtendedForm implements FormModel { - LoginExtendedForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + LoginExtendedForm( + this.form, + this.path, + this._formModel, { + LoginExtended? initialModel, + }) : _ownInitialModel = initialModel; static const String emailControlName = "email"; @@ -361,8 +368,10 @@ class LoginExtendedForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + LoginExtended? _ownInitialModel; + + late Map _ownInitialRawValue = + LoginExtendedForm.formElements(_ownInitialModel).rawValue; String emailControlPath() => pathBuilder(emailControlName); @@ -1184,10 +1193,28 @@ class LoginExtendedForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + LoginExtended? get initialModel { + return _ownInitialModel; + } + + void commitInitial([LoginExtended? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = LoginExtendedForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1226,55 +1253,6 @@ class LoginExtendedForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/login_output_test.dart b/packages/generator_tests/test/doc/login_output_test.dart index 1e28ca45..aa9911af 100644 --- a/packages/generator_tests/test/doc/login_output_test.dart +++ b/packages/generator_tests/test/doc/login_output_test.dart @@ -208,7 +208,12 @@ class _LoginOFormBuilderState extends State { @override void initState() { - _formModel = LoginOForm(LoginOForm.formElements(widget.model), null, null); + _formModel = LoginOForm( + LoginOForm.formElements(widget.model), + null, + null, + initialModel: widget.model, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -250,7 +255,9 @@ class _LoginOFormBuilderState extends State { @override void didUpdateWidget(covariant LoginOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -285,7 +292,8 @@ class _LoginOFormBuilderState extends State { final _logLoginOForm = Logger.detached('LoginOForm'); class LoginOForm implements FormModel { - LoginOForm(this.form, this.path, this._formModel) : initial = form.rawValue; + LoginOForm(this.form, this.path, this._formModel, {LoginO? initialModel}) + : _ownInitialModel = initialModel; static const String emailControlName = "email"; @@ -300,8 +308,11 @@ class LoginOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + LoginO? _ownInitialModel; + + late Map _ownInitialRawValue = LoginOForm.formElements( + _ownInitialModel, + ).rawValue; String emailControlPath() => pathBuilder(emailControlName); @@ -580,10 +591,26 @@ class LoginOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + LoginO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([LoginO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = LoginOForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -622,55 +649,6 @@ class LoginOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/login_test.dart b/packages/generator_tests/test/doc/login_test.dart index 9261b6b7..0e49fc29 100644 --- a/packages/generator_tests/test/doc/login_test.dart +++ b/packages/generator_tests/test/doc/login_test.dart @@ -192,7 +192,12 @@ class _LoginFormBuilderState extends State { @override void initState() { - _formModel = LoginForm(LoginForm.formElements(widget.model), null, null); + _formModel = LoginForm( + LoginForm.formElements(widget.model), + null, + null, + initialModel: widget.model, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -234,7 +239,9 @@ class _LoginFormBuilderState extends State { @override void didUpdateWidget(covariant LoginFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -269,7 +276,8 @@ class _LoginFormBuilderState extends State { final _logLoginForm = Logger.detached('LoginForm'); class LoginForm implements FormModel { - LoginForm(this.form, this.path, this._formModel) : initial = form.rawValue; + LoginForm(this.form, this.path, this._formModel, {Login? initialModel}) + : _ownInitialModel = initialModel; static const String emailControlName = "email"; @@ -284,8 +292,11 @@ class LoginForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + Login? _ownInitialModel; + + late Map _ownInitialRawValue = LoginForm.formElements( + _ownInitialModel, + ).rawValue; String emailControlPath() => pathBuilder(emailControlName); @@ -539,10 +550,26 @@ class LoginForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + Login? get initialModel { + return _ownInitialModel; + } + + void commitInitial([Login? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = LoginForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -578,55 +605,6 @@ class LoginForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/mailing_list_output_test.dart b/packages/generator_tests/test/doc/mailing_list_output_test.dart index b27181e1..4ff2bb95 100644 --- a/packages/generator_tests/test/doc/mailing_list_output_test.dart +++ b/packages/generator_tests/test/doc/mailing_list_output_test.dart @@ -198,6 +198,7 @@ class _MailingListOFormBuilderState extends State { MailingListOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -240,7 +241,9 @@ class _MailingListOFormBuilderState extends State { @override void didUpdateWidget(covariant MailingListOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -275,8 +278,12 @@ class _MailingListOFormBuilderState extends State { final _logMailingListOForm = Logger.detached('MailingListOForm'); class MailingListOForm implements FormModel { - MailingListOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + MailingListOForm( + this.form, + this.path, + this._formModel, { + MailingListO? initialModel, + }) : _ownInitialModel = initialModel; static const String emailListControlName = "emailList"; @@ -289,8 +296,11 @@ class MailingListOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + MailingListO? _ownInitialModel; + + late Map _ownInitialRawValue = MailingListOForm.formElements( + _ownInitialModel, + ).rawValue; String emailListControlPath() => pathBuilder(emailListControlName); @@ -491,10 +501,28 @@ class MailingListOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + MailingListO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([MailingListO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = MailingListOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -533,55 +561,6 @@ class MailingListOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/mailing_list_test.dart b/packages/generator_tests/test/doc/mailing_list_test.dart index 9c4b1fd1..5028a7c4 100644 --- a/packages/generator_tests/test/doc/mailing_list_test.dart +++ b/packages/generator_tests/test/doc/mailing_list_test.dart @@ -196,6 +196,7 @@ class _MailingListFormBuilderState extends State { MailingListForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -238,7 +239,9 @@ class _MailingListFormBuilderState extends State { @override void didUpdateWidget(covariant MailingListFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -273,8 +276,12 @@ class _MailingListFormBuilderState extends State { final _logMailingListForm = Logger.detached('MailingListForm'); class MailingListForm implements FormModel { - MailingListForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + MailingListForm( + this.form, + this.path, + this._formModel, { + MailingList? initialModel, + }) : _ownInitialModel = initialModel; static const String emailListControlName = "emailList"; @@ -287,8 +294,11 @@ class MailingListForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + MailingList? _ownInitialModel; + + late Map _ownInitialRawValue = MailingListForm.formElements( + _ownInitialModel, + ).rawValue; String emailListControlPath() => pathBuilder(emailListControlName); @@ -488,10 +498,28 @@ class MailingListForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + MailingList? get initialModel { + return _ownInitialModel; + } + + void commitInitial([MailingList? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = MailingListForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -530,55 +558,6 @@ class MailingListForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/nested_generics_output_test.dart b/packages/generator_tests/test/doc/nested_generics_output_test.dart index 144b90a3..981b04af 100644 --- a/packages/generator_tests/test/doc/nested_generics_output_test.dart +++ b/packages/generator_tests/test/doc/nested_generics_output_test.dart @@ -232,6 +232,7 @@ class _ProductDetailsOFormBuilderState

ProductDetailsOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -274,7 +275,9 @@ class _ProductDetailsOFormBuilderState

@override void didUpdateWidget(covariant ProductDetailsOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -310,8 +313,12 @@ final _logProductDetailsOForm = Logger.detached('ProductDetailsOForm'); class ProductDetailsOForm

implements FormModel, ProductDetailsOOutput> { - ProductDetailsOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + ProductDetailsOForm( + this.form, + this.path, + this._formModel, { + ProductDetailsO? initialModel, + }) : _ownInitialModel = initialModel; static const String descriptionControlName = "description"; @@ -326,8 +333,10 @@ class ProductDetailsOForm

final Map _disabled = {}; - @override - final Map initial; + ProductDetailsO? _ownInitialModel; + + late Map _ownInitialRawValue = + ProductDetailsOForm.formElements(_ownInitialModel).rawValue; String descriptionControlPath() => pathBuilder(descriptionControlName); @@ -612,10 +621,28 @@ class ProductDetailsOForm

bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + ProductDetailsO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([ProductDetailsO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = ProductDetailsOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -654,55 +681,6 @@ class ProductDetailsOForm

emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -731,7 +709,8 @@ final _logIdOForm = Logger.detached('IdOForm'); class IdOForm

implements FormModel, IdOOutput> { - IdOForm(this.form, this.path, this._formModel) : initial = form.rawValue; + IdOForm(this.form, this.path, this._formModel, {IdO? initialModel}) + : _ownInitialModel = initialModel; static const String companyNameControlName = "companyName"; @@ -746,8 +725,11 @@ class IdOForm

final Map _disabled = {}; - @override - final Map initial; + IdO? _ownInitialModel; + + late Map _ownInitialRawValue = IdOForm.formElements( + _ownInitialModel, + ).rawValue; String companyNameControlPath() => pathBuilder(companyNameControlName); @@ -1027,10 +1009,26 @@ class IdOForm

bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + IdO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([IdO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = IdOForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1069,55 +1067,6 @@ class IdOForm

emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1556,6 +1505,7 @@ class _IdOFormBuilderState

IdOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -1598,7 +1548,9 @@ class _IdOFormBuilderState

@override void didUpdateWidget(covariant IdOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); diff --git a/packages/generator_tests/test/doc/nested_generics_test.dart b/packages/generator_tests/test/doc/nested_generics_test.dart index 94116b23..1dca76f8 100644 --- a/packages/generator_tests/test/doc/nested_generics_test.dart +++ b/packages/generator_tests/test/doc/nested_generics_test.dart @@ -229,6 +229,7 @@ class _ProductDetailsFormBuilderState

ProductDetailsForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -271,7 +272,9 @@ class _ProductDetailsFormBuilderState

@override void didUpdateWidget(covariant ProductDetailsFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -307,8 +310,12 @@ final _logProductDetailsForm = Logger.detached('ProductDetailsForm'); class ProductDetailsForm

implements FormModel, ProductDetails> { - ProductDetailsForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + ProductDetailsForm( + this.form, + this.path, + this._formModel, { + ProductDetails? initialModel, + }) : _ownInitialModel = initialModel; static const String descriptionControlName = "description"; @@ -323,8 +330,10 @@ class ProductDetailsForm

final Map _disabled = {}; - @override - final Map initial; + ProductDetails? _ownInitialModel; + + late Map _ownInitialRawValue = + ProductDetailsForm.formElements(_ownInitialModel).rawValue; String descriptionControlPath() => pathBuilder(descriptionControlName); @@ -605,10 +614,28 @@ class ProductDetailsForm

bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + ProductDetails? get initialModel { + return _ownInitialModel; + } + + void commitInitial([ProductDetails? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = ProductDetailsForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -647,55 +674,6 @@ class ProductDetailsForm

emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -724,7 +702,8 @@ final _logIdForm = Logger.detached('IdForm'); class IdForm

implements FormModel, Id> { - IdForm(this.form, this.path, this._formModel) : initial = form.rawValue; + IdForm(this.form, this.path, this._formModel, {Id? initialModel}) + : _ownInitialModel = initialModel; static const String companyNameControlName = "companyName"; @@ -739,8 +718,11 @@ class IdForm

final Map _disabled = {}; - @override - final Map initial; + Id? _ownInitialModel; + + late Map _ownInitialRawValue = IdForm.formElements( + _ownInitialModel, + ).rawValue; String companyNameControlPath() => pathBuilder(companyNameControlName); @@ -1019,10 +1001,26 @@ class IdForm

bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + Id? get initialModel { + return _ownInitialModel; + } + + void commitInitial([Id? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = IdForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1061,55 +1059,6 @@ class IdForm

emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1525,6 +1474,7 @@ class _IdFormBuilderState

IdForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -1567,7 +1517,9 @@ class _IdFormBuilderState

@override void didUpdateWidget(covariant IdFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); diff --git a/packages/generator_tests/test/doc/nested_output.test.dart b/packages/generator_tests/test/doc/nested_output.test.dart index 27d4fc33..c6136582 100644 --- a/packages/generator_tests/test/doc/nested_output.test.dart +++ b/packages/generator_tests/test/doc/nested_output.test.dart @@ -209,6 +209,7 @@ class _SubGroupOFormBuilderState extends State { SubGroupOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -251,7 +252,9 @@ class _SubGroupOFormBuilderState extends State { @override void didUpdateWidget(covariant SubGroupOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -286,8 +289,12 @@ class _SubGroupOFormBuilderState extends State { final _logSubGroupOForm = Logger.detached('SubGroupOForm'); class SubGroupOForm implements FormModel { - SubGroupOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + SubGroupOForm( + this.form, + this.path, + this._formModel, { + SubGroupO? initialModel, + }) : _ownInitialModel = initialModel; static const String idControlName = "id"; @@ -300,8 +307,11 @@ class SubGroupOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + SubGroupO? _ownInitialModel; + + late Map _ownInitialRawValue = SubGroupOForm.formElements( + _ownInitialModel, + ).rawValue; String idControlPath() => pathBuilder(idControlName); @@ -452,10 +462,26 @@ class SubGroupOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + SubGroupO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([SubGroupO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = SubGroupOForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -494,55 +520,6 @@ class SubGroupOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -946,7 +923,12 @@ class _GroupOFormBuilderState extends State { @override void initState() { - _formModel = GroupOForm(GroupOForm.formElements(widget.model), null, null); + _formModel = GroupOForm( + GroupOForm.formElements(widget.model), + null, + null, + initialModel: widget.model, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -988,7 +970,9 @@ class _GroupOFormBuilderState extends State { @override void didUpdateWidget(covariant GroupOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -1023,7 +1007,8 @@ class _GroupOFormBuilderState extends State { final _logGroupOForm = Logger.detached('GroupOForm'); class GroupOForm implements FormModel { - GroupOForm(this.form, this.path, this._formModel) : initial = form.rawValue; + GroupOForm(this.form, this.path, this._formModel, {GroupO? initialModel}) + : _ownInitialModel = initialModel; static const String idControlName = "id"; @@ -1038,8 +1023,11 @@ class GroupOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + GroupO? _ownInitialModel; + + late Map _ownInitialRawValue = GroupOForm.formElements( + _ownInitialModel, + ).rawValue; String idControlPath() => pathBuilder(idControlName); @@ -1381,10 +1369,26 @@ class GroupOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + GroupO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([GroupO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = GroupOForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1423,55 +1427,6 @@ class GroupOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1857,6 +1812,7 @@ class _NestedOFormBuilderState extends State { NestedOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -1899,7 +1855,9 @@ class _NestedOFormBuilderState extends State { @override void didUpdateWidget(covariant NestedOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -1934,7 +1892,8 @@ class _NestedOFormBuilderState extends State { final _logNestedOForm = Logger.detached('NestedOForm'); class NestedOForm implements FormModel { - NestedOForm(this.form, this.path, this._formModel) : initial = form.rawValue; + NestedOForm(this.form, this.path, this._formModel, {NestedO? initialModel}) + : _ownInitialModel = initialModel; static const String groupListControlName = "groupList"; @@ -1947,8 +1906,11 @@ class NestedOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + NestedO? _ownInitialModel; + + late Map _ownInitialRawValue = NestedOForm.formElements( + _ownInitialModel, + ).rawValue; String groupListControlPath() => pathBuilder(groupListControlName); @@ -2209,10 +2171,26 @@ class NestedOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + NestedO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([NestedO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = NestedOForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -2251,55 +2229,6 @@ class NestedOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/profile_output_test.dart b/packages/generator_tests/test/doc/profile_output_test.dart index a7c85b44..69e06450 100644 --- a/packages/generator_tests/test/doc/profile_output_test.dart +++ b/packages/generator_tests/test/doc/profile_output_test.dart @@ -430,6 +430,7 @@ class _ProfileOFormBuilderState extends State { ProfileOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -472,7 +473,9 @@ class _ProfileOFormBuilderState extends State { @override void didUpdateWidget(covariant ProfileOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -507,7 +510,8 @@ class _ProfileOFormBuilderState extends State { final _logProfileOForm = Logger.detached('ProfileOForm'); class ProfileOForm implements FormModel { - ProfileOForm(this.form, this.path, this._formModel) : initial = form.rawValue; + ProfileOForm(this.form, this.path, this._formModel, {ProfileO? initialModel}) + : _ownInitialModel = initialModel; static const String idControlName = "id"; @@ -538,8 +542,11 @@ class ProfileOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + ProfileO? _ownInitialModel; + + late Map _ownInitialRawValue = ProfileOForm.formElements( + _ownInitialModel, + ).rawValue; String idControlPath() => pathBuilder(idControlName); @@ -1440,10 +1447,26 @@ class ProfileOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + ProfileO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([ProfileO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = ProfileOForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1482,55 +1505,6 @@ class ProfileOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1611,8 +1585,12 @@ final _logIncidenceFilterOForm = Logger.detached('IncidenceFilterOForm'); class IncidenceFilterOForm implements FormModel { - IncidenceFilterOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + IncidenceFilterOForm( + this.form, + this.path, + this._formModel, { + IncidenceFilterO? initialModel, + }) : _ownInitialModel = initialModel; static const String isMobilityEnabledControlName = "isMobilityEnabled"; @@ -1635,8 +1613,10 @@ class IncidenceFilterOForm final Map _disabled = {}; - @override - final Map initial; + IncidenceFilterO? _ownInitialModel; + + late Map _ownInitialRawValue = + IncidenceFilterOForm.formElements(_ownInitialModel).rawValue; String isMobilityEnabledControlPath() => pathBuilder(isMobilityEnabledControlName); @@ -2211,10 +2191,28 @@ class IncidenceFilterOForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + IncidenceFilterO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([IncidenceFilterO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = IncidenceFilterOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -2253,55 +2251,6 @@ class IncidenceFilterOForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -2368,8 +2317,12 @@ final _logThresholdSettingOForm = Logger.detached('ThresholdSettingOForm'); class ThresholdSettingOForm implements FormModel { - ThresholdSettingOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + ThresholdSettingOForm( + this.form, + this.path, + this._formModel, { + ThresholdSettingO? initialModel, + }) : _ownInitialModel = initialModel; static const String isEnabledControlName = "isEnabled"; @@ -2384,8 +2337,10 @@ class ThresholdSettingOForm final Map _disabled = {}; - @override - final Map initial; + ThresholdSettingO? _ownInitialModel; + + late Map _ownInitialRawValue = + ThresholdSettingOForm.formElements(_ownInitialModel).rawValue; String isEnabledControlPath() => pathBuilder(isEnabledControlName); @@ -2623,10 +2578,28 @@ class ThresholdSettingOForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + ThresholdSettingO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([ThresholdSettingO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = ThresholdSettingOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -2665,55 +2638,6 @@ class ThresholdSettingOForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -2748,8 +2672,12 @@ final _logTimerSettingOForm = Logger.detached('TimerSettingOForm'); class TimerSettingOForm implements FormModel { - TimerSettingOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + TimerSettingOForm( + this.form, + this.path, + this._formModel, { + TimerSettingO? initialModel, + }) : _ownInitialModel = initialModel; static const String isEnabledControlName = "isEnabled"; @@ -2764,8 +2692,10 @@ class TimerSettingOForm final Map _disabled = {}; - @override - final Map initial; + TimerSettingO? _ownInitialModel; + + late Map _ownInitialRawValue = + TimerSettingOForm.formElements(_ownInitialModel).rawValue; String isEnabledControlPath() => pathBuilder(isEnabledControlName); @@ -2997,10 +2927,28 @@ class TimerSettingOForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + TimerSettingO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([TimerSettingO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = TimerSettingOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -3039,55 +2987,6 @@ class TimerSettingOForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/profile_test.dart b/packages/generator_tests/test/doc/profile_test.dart index b42210aa..d7a82f03 100644 --- a/packages/generator_tests/test/doc/profile_test.dart +++ b/packages/generator_tests/test/doc/profile_test.dart @@ -424,6 +424,7 @@ class _ProfileFormBuilderState extends State { ProfileForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -466,7 +467,9 @@ class _ProfileFormBuilderState extends State { @override void didUpdateWidget(covariant ProfileFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -501,7 +504,8 @@ class _ProfileFormBuilderState extends State { final _logProfileForm = Logger.detached('ProfileForm'); class ProfileForm implements FormModel { - ProfileForm(this.form, this.path, this._formModel) : initial = form.rawValue; + ProfileForm(this.form, this.path, this._formModel, {Profile? initialModel}) + : _ownInitialModel = initialModel; static const String idControlName = "id"; @@ -532,8 +536,11 @@ class ProfileForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + Profile? _ownInitialModel; + + late Map _ownInitialRawValue = ProfileForm.formElements( + _ownInitialModel, + ).rawValue; String idControlPath() => pathBuilder(idControlName); @@ -1433,10 +1440,26 @@ class ProfileForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + Profile? get initialModel { + return _ownInitialModel; + } + + void commitInitial([Profile? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = ProfileForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1475,55 +1498,6 @@ class ProfileForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1604,8 +1578,12 @@ final _logIncidenceFilterForm = Logger.detached('IncidenceFilterForm'); class IncidenceFilterForm implements FormModel { - IncidenceFilterForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + IncidenceFilterForm( + this.form, + this.path, + this._formModel, { + IncidenceFilter? initialModel, + }) : _ownInitialModel = initialModel; static const String isMobilityEnabledControlName = "isMobilityEnabled"; @@ -1628,8 +1606,10 @@ class IncidenceFilterForm final Map _disabled = {}; - @override - final Map initial; + IncidenceFilter? _ownInitialModel; + + late Map _ownInitialRawValue = + IncidenceFilterForm.formElements(_ownInitialModel).rawValue; String isMobilityEnabledControlPath() => pathBuilder(isMobilityEnabledControlName); @@ -2203,10 +2183,28 @@ class IncidenceFilterForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + IncidenceFilter? get initialModel { + return _ownInitialModel; + } + + void commitInitial([IncidenceFilter? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = IncidenceFilterForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -2245,55 +2243,6 @@ class IncidenceFilterForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -2359,8 +2308,12 @@ final _logThresholdSettingForm = Logger.detached('ThresholdSettingForm'); class ThresholdSettingForm implements FormModel { - ThresholdSettingForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + ThresholdSettingForm( + this.form, + this.path, + this._formModel, { + ThresholdSetting? initialModel, + }) : _ownInitialModel = initialModel; static const String isEnabledControlName = "isEnabled"; @@ -2375,8 +2328,10 @@ class ThresholdSettingForm final Map _disabled = {}; - @override - final Map initial; + ThresholdSetting? _ownInitialModel; + + late Map _ownInitialRawValue = + ThresholdSettingForm.formElements(_ownInitialModel).rawValue; String isEnabledControlPath() => pathBuilder(isEnabledControlName); @@ -2610,10 +2565,28 @@ class ThresholdSettingForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + ThresholdSetting? get initialModel { + return _ownInitialModel; + } + + void commitInitial([ThresholdSetting? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = ThresholdSettingForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -2652,55 +2625,6 @@ class ThresholdSettingForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -2734,8 +2658,12 @@ class ThresholdSettingForm final _logTimerSettingForm = Logger.detached('TimerSettingForm'); class TimerSettingForm implements FormModel { - TimerSettingForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + TimerSettingForm( + this.form, + this.path, + this._formModel, { + TimerSetting? initialModel, + }) : _ownInitialModel = initialModel; static const String isEnabledControlName = "isEnabled"; @@ -2750,8 +2678,11 @@ class TimerSettingForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + TimerSetting? _ownInitialModel; + + late Map _ownInitialRawValue = TimerSettingForm.formElements( + _ownInitialModel, + ).rawValue; String isEnabledControlPath() => pathBuilder(isEnabledControlName); @@ -2982,10 +2913,28 @@ class TimerSettingForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + TimerSetting? get initialModel { + return _ownInitialModel; + } + + void commitInitial([TimerSetting? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = TimerSettingForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -3024,55 +2973,6 @@ class TimerSettingForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/recursive_test.dart b/packages/generator_tests/test/doc/recursive_test.dart index 07eb2209..58e3b227 100644 --- a/packages/generator_tests/test/doc/recursive_test.dart +++ b/packages/generator_tests/test/doc/recursive_test.dart @@ -203,6 +203,7 @@ class _SecuredAreaFormBuilderState extends State { SecuredAreaForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -245,7 +246,9 @@ class _SecuredAreaFormBuilderState extends State { @override void didUpdateWidget(covariant SecuredAreaFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -280,8 +283,12 @@ class _SecuredAreaFormBuilderState extends State { final _logSecuredAreaForm = Logger.detached('SecuredAreaForm'); class SecuredAreaForm implements FormModel { - SecuredAreaForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + SecuredAreaForm( + this.form, + this.path, + this._formModel, { + SecuredArea? initialModel, + }) : _ownInitialModel = initialModel; static const String idControlName = "id"; @@ -300,8 +307,11 @@ class SecuredAreaForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + SecuredArea? _ownInitialModel; + + late Map _ownInitialRawValue = SecuredAreaForm.formElements( + _ownInitialModel, + ).rawValue; String idControlPath() => pathBuilder(idControlName); @@ -892,10 +902,28 @@ class SecuredAreaForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + SecuredArea? get initialModel { + return _ownInitialModel; + } + + void commitInitial([SecuredArea? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = SecuredAreaForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -934,55 +962,6 @@ class SecuredAreaForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1022,8 +1001,12 @@ class SecuredAreaForm implements FormModel { final _logParcelSystemForm = Logger.detached('ParcelSystemForm'); class ParcelSystemForm implements FormModel { - ParcelSystemForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + ParcelSystemForm( + this.form, + this.path, + this._formModel, { + ParcelSystem? initialModel, + }) : _ownInitialModel = initialModel; static const String hasParcelSystemControlName = "hasParcelSystem"; @@ -1038,8 +1021,11 @@ class ParcelSystemForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + ParcelSystem? _ownInitialModel; + + late Map _ownInitialRawValue = ParcelSystemForm.formElements( + _ownInitialModel, + ).rawValue; String hasParcelSystemControlPath() => pathBuilder(hasParcelSystemControlName); @@ -1280,10 +1266,28 @@ class ParcelSystemForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + ParcelSystem? get initialModel { + return _ownInitialModel; + } + + void commitInitial([ParcelSystem? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = ParcelSystemForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1322,55 +1326,6 @@ class ParcelSystemForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1397,8 +1352,12 @@ final _logParcelSystemDataForm = Logger.detached('ParcelSystemDataForm'); class ParcelSystemDataForm implements FormModel { - ParcelSystemDataForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + ParcelSystemDataForm( + this.form, + this.path, + this._formModel, { + ParcelSystemData? initialModel, + }) : _ownInitialModel = initialModel; static const String idControlName = "id"; @@ -1411,8 +1370,10 @@ class ParcelSystemDataForm final Map _disabled = {}; - @override - final Map initial; + ParcelSystemData? _ownInitialModel; + + late Map _ownInitialRawValue = + ParcelSystemDataForm.formElements(_ownInitialModel).rawValue; String idControlPath() => pathBuilder(idControlName); @@ -1585,10 +1546,28 @@ class ParcelSystemDataForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + ParcelSystemData? get initialModel { + return _ownInitialModel; + } + + void commitInitial([ParcelSystemData? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = ParcelSystemDataForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1627,55 +1606,6 @@ class ParcelSystemDataForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/renamed_basic_output_test.dart b/packages/generator_tests/test/doc/renamed_basic_output_test.dart index 233296a7..f8bcaf60 100644 --- a/packages/generator_tests/test/doc/renamed_basic_output_test.dart +++ b/packages/generator_tests/test/doc/renamed_basic_output_test.dart @@ -195,6 +195,7 @@ class _SomeWiredNameFormBuilderState extends State { SomeWiredNameForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -237,7 +238,9 @@ class _SomeWiredNameFormBuilderState extends State { @override void didUpdateWidget(covariant SomeWiredNameFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -273,8 +276,12 @@ final _logSomeWiredNameForm = Logger.detached('SomeWiredNameForm'); class SomeWiredNameForm implements FormModel { - SomeWiredNameForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + SomeWiredNameForm( + this.form, + this.path, + this._formModel, { + RenamedBasicO? initialModel, + }) : _ownInitialModel = initialModel; static const String emailControlName = "email"; @@ -289,8 +296,10 @@ class SomeWiredNameForm final Map _disabled = {}; - @override - final Map initial; + RenamedBasicO? _ownInitialModel; + + late Map _ownInitialRawValue = + SomeWiredNameForm.formElements(_ownInitialModel).rawValue; String emailControlPath() => pathBuilder(emailControlName); @@ -569,10 +578,28 @@ class SomeWiredNameForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + RenamedBasicO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([RenamedBasicO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = SomeWiredNameForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -611,55 +638,6 @@ class SomeWiredNameForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/renamed_basic_test.dart b/packages/generator_tests/test/doc/renamed_basic_test.dart index ccb04a7a..dc8de44a 100644 --- a/packages/generator_tests/test/doc/renamed_basic_test.dart +++ b/packages/generator_tests/test/doc/renamed_basic_test.dart @@ -195,6 +195,7 @@ class _SomeWiredNameFormBuilderState extends State { SomeWiredNameForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -237,7 +238,9 @@ class _SomeWiredNameFormBuilderState extends State { @override void didUpdateWidget(covariant SomeWiredNameFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -272,8 +275,12 @@ class _SomeWiredNameFormBuilderState extends State { final _logSomeWiredNameForm = Logger.detached('SomeWiredNameForm'); class SomeWiredNameForm implements FormModel { - SomeWiredNameForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + SomeWiredNameForm( + this.form, + this.path, + this._formModel, { + RenamedBasic? initialModel, + }) : _ownInitialModel = initialModel; static const String emailControlName = "email"; @@ -288,8 +295,10 @@ class SomeWiredNameForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + RenamedBasic? _ownInitialModel; + + late Map _ownInitialRawValue = + SomeWiredNameForm.formElements(_ownInitialModel).rawValue; String emailControlPath() => pathBuilder(emailControlName); @@ -520,10 +529,28 @@ class SomeWiredNameForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + RenamedBasic? get initialModel { + return _ownInitialModel; + } + + void commitInitial([RenamedBasic? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = SomeWiredNameForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -562,55 +589,6 @@ class SomeWiredNameForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/user_profile_output_test.dart b/packages/generator_tests/test/doc/user_profile_output_test.dart index 6e1dfe4a..fd4223d8 100644 --- a/packages/generator_tests/test/doc/user_profile_output_test.dart +++ b/packages/generator_tests/test/doc/user_profile_output_test.dart @@ -221,6 +221,7 @@ class _UserProfileOFormBuilderState extends State { UserProfileOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -263,7 +264,9 @@ class _UserProfileOFormBuilderState extends State { @override void didUpdateWidget(covariant UserProfileOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -298,8 +301,12 @@ class _UserProfileOFormBuilderState extends State { final _logUserProfileOForm = Logger.detached('UserProfileOForm'); class UserProfileOForm implements FormModel { - UserProfileOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + UserProfileOForm( + this.form, + this.path, + this._formModel, { + UserProfileO? initialModel, + }) : _ownInitialModel = initialModel; static const String idControlName = "id"; @@ -320,8 +327,11 @@ class UserProfileOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + UserProfileO? _ownInitialModel; + + late Map _ownInitialRawValue = UserProfileOForm.formElements( + _ownInitialModel, + ).rawValue; String idControlPath() => pathBuilder(idControlName); @@ -871,10 +881,28 @@ class UserProfileOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + UserProfileO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([UserProfileO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = UserProfileOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -913,55 +941,6 @@ class UserProfileOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1004,7 +983,8 @@ class UserProfileOForm implements FormModel { final _logAddressOForm = Logger.detached('AddressOForm'); class AddressOForm implements FormModel { - AddressOForm(this.form, this.path, this._formModel) : initial = form.rawValue; + AddressOForm(this.form, this.path, this._formModel, {AddressO? initialModel}) + : _ownInitialModel = initialModel; static const String streetControlName = "street"; @@ -1021,8 +1001,11 @@ class AddressOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + AddressO? _ownInitialModel; + + late Map _ownInitialRawValue = AddressOForm.formElements( + _ownInitialModel, + ).rawValue; String streetControlPath() => pathBuilder(streetControlName); @@ -1409,10 +1392,26 @@ class AddressOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + AddressO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([AddressO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = AddressOForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1451,55 +1450,6 @@ class AddressOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/generator_tests/test/doc/user_profile_test.dart b/packages/generator_tests/test/doc/user_profile_test.dart index 42a9a8e8..7e15ab46 100644 --- a/packages/generator_tests/test/doc/user_profile_test.dart +++ b/packages/generator_tests/test/doc/user_profile_test.dart @@ -219,6 +219,7 @@ class _UserProfileFormBuilderState extends State { UserProfileForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -261,7 +262,9 @@ class _UserProfileFormBuilderState extends State { @override void didUpdateWidget(covariant UserProfileFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -296,8 +299,12 @@ class _UserProfileFormBuilderState extends State { final _logUserProfileForm = Logger.detached('UserProfileForm'); class UserProfileForm implements FormModel { - UserProfileForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + UserProfileForm( + this.form, + this.path, + this._formModel, { + UserProfile? initialModel, + }) : _ownInitialModel = initialModel; static const String idControlName = "id"; @@ -318,8 +325,11 @@ class UserProfileForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + UserProfile? _ownInitialModel; + + late Map _ownInitialRawValue = UserProfileForm.formElements( + _ownInitialModel, + ).rawValue; String idControlPath() => pathBuilder(idControlName); @@ -820,10 +830,28 @@ class UserProfileForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + UserProfile? get initialModel { + return _ownInitialModel; + } + + void commitInitial([UserProfile? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = UserProfileForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -862,55 +890,6 @@ class UserProfileForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -953,7 +932,8 @@ class UserProfileForm implements FormModel { final _logAddressForm = Logger.detached('AddressForm'); class AddressForm implements FormModel { - AddressForm(this.form, this.path, this._formModel) : initial = form.rawValue; + AddressForm(this.form, this.path, this._formModel, {Address? initialModel}) + : _ownInitialModel = initialModel; static const String streetControlName = "street"; @@ -970,8 +950,11 @@ class AddressForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + Address? _ownInitialModel; + + late Map _ownInitialRawValue = AddressForm.formElements( + _ownInitialModel, + ).rawValue; String streetControlPath() => pathBuilder(streetControlName); @@ -1353,10 +1336,26 @@ class AddressForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + Address? get initialModel { + return _ownInitialModel; + } + + void commitInitial([Address? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = AddressForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1395,55 +1394,6 @@ class AddressForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_annotations/CHANGELOG.md b/packages/reactive_forms_annotations/CHANGELOG.md index 38600dbe..077f68f0 100644 --- a/packages/reactive_forms_annotations/CHANGELOG.md +++ b/packages/reactive_forms_annotations/CHANGELOG.md @@ -1,3 +1,8 @@ +## [7.5.0-beta9] + +* require Dart 3.9 +* align beta release with `reactive_forms_generator` + ## [8.0.0] * Rf18 diff --git a/packages/reactive_forms_annotations/lib/src/form_model.dart b/packages/reactive_forms_annotations/lib/src/form_model.dart index 0c718912..f0528d4f 100644 --- a/packages/reactive_forms_annotations/lib/src/form_model.dart +++ b/packages/reactive_forms_annotations/lib/src/form_model.dart @@ -12,13 +12,13 @@ abstract class FormModel { final String? path; - final Map? initial = null; - @protected TModelOutput get model; TModel get rawModel; + Map get initialRawValue; + void submit({ required void Function(TModelOutput model) onValid, void Function()? onNotValid, @@ -44,7 +44,22 @@ abstract class FormModel { reset({TModel? value, bool updateParent = true, bool emitEvent = true}); - void updateInitial(Map? value, String? path); - void toggleDisabled({bool updateParent = true, bool emitEvent = true}); + + static Object? sliceByPath(Object? root, String? path) { + if (path == null || path.isEmpty) return root; + Object? current = root; + for (final key in path.split('.')) { + if (current is Map) { + current = current[key]; + } else if (current is List) { + final idx = int.tryParse(key); + if (idx == null || idx < 0 || idx >= current.length) return null; + current = current[idx]; + } else { + return null; + } + } + return current; + } } diff --git a/packages/reactive_forms_annotations/pubspec.yaml b/packages/reactive_forms_annotations/pubspec.yaml index 42fc5e3e..9797eb5c 100644 --- a/packages/reactive_forms_annotations/pubspec.yaml +++ b/packages/reactive_forms_annotations/pubspec.yaml @@ -2,10 +2,10 @@ name: reactive_forms_annotations description: Annotations for reactive_forms_generator repository: https://github.com/artflutter/reactive_forms_generator -version: 7.5.0-beta-1 +version: 7.5.0-beta9 environment: - sdk: ">=3.8.0 <4.0.0" + sdk: ">=3.9.0 <4.0.0" resolution: workspace dependencies: @@ -16,4 +16,4 @@ dependencies: sdk: flutter dev_dependencies: - flutter_lints: ^6.0.0 \ No newline at end of file + flutter_lints: ^6.0.0 diff --git a/packages/reactive_forms_generator/CHANGELOG.md b/packages/reactive_forms_generator/CHANGELOG.md index 44cd59eb..b5a7dab8 100644 --- a/packages/reactive_forms_generator/CHANGELOG.md +++ b/packages/reactive_forms_generator/CHANGELOG.md @@ -1,3 +1,12 @@ +## [7.5.0-beta9] + +* analyzer 9 support +* require Dart 3.9 + +## [7.5.0-beta2] + +* fix hasChanges (backport of #210) + ## [7.5.0-beta1] * properly handle missing controls in form diff --git a/packages/reactive_forms_generator/example/android/app/build.gradle b/packages/reactive_forms_generator/example/android/app/build.gradle index 118ee1d9..deca80b7 100644 --- a/packages/reactive_forms_generator/example/android/app/build.gradle +++ b/packages/reactive_forms_generator/example/android/app/build.gradle @@ -28,12 +28,12 @@ android { ndkVersion flutter.ndkVersion compileOptions { - sourceCompatibility JavaVersion.VERSION_1_8 - targetCompatibility JavaVersion.VERSION_1_8 + sourceCompatibility JavaVersion.VERSION_17 + targetCompatibility JavaVersion.VERSION_17 } kotlinOptions { - jvmTarget = '1.8' + jvmTarget = '17' } sourceSets { diff --git a/packages/reactive_forms_generator/example/android/build.gradle b/packages/reactive_forms_generator/example/android/build.gradle index e83fb5da..bc157bd1 100644 --- a/packages/reactive_forms_generator/example/android/build.gradle +++ b/packages/reactive_forms_generator/example/android/build.gradle @@ -1,15 +1,3 @@ -buildscript { - ext.kotlin_version = '1.7.10' - repositories { - google() - mavenCentral() - } - - dependencies { - classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" - } -} - allprojects { repositories { google() diff --git a/packages/reactive_forms_generator/example/android/gradle/wrapper/gradle-wrapper.properties b/packages/reactive_forms_generator/example/android/gradle/wrapper/gradle-wrapper.properties index 3c472b99..3c85cfe0 100644 --- a/packages/reactive_forms_generator/example/android/gradle/wrapper/gradle-wrapper.properties +++ b/packages/reactive_forms_generator/example/android/gradle/wrapper/gradle-wrapper.properties @@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-all.zip diff --git a/packages/reactive_forms_generator/example/android/settings.gradle b/packages/reactive_forms_generator/example/android/settings.gradle index 7cd71285..5ddb9295 100644 --- a/packages/reactive_forms_generator/example/android/settings.gradle +++ b/packages/reactive_forms_generator/example/android/settings.gradle @@ -23,7 +23,8 @@ pluginManagement { plugins { id "dev.flutter.flutter-plugin-loader" version "1.0.0" - id "com.android.application" version "7.3.0" apply false + id "com.android.application" version "8.3.0" apply false + id "org.jetbrains.kotlin.android" version "2.1.0" apply false } include ":app" diff --git a/packages/reactive_forms_generator/example/lib/docs/animated_url_list/animated_url_list.gform.dart b/packages/reactive_forms_generator/example/lib/docs/animated_url_list/animated_url_list.gform.dart index ca81c12d..b95339c4 100644 --- a/packages/reactive_forms_generator/example/lib/docs/animated_url_list/animated_url_list.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/animated_url_list/animated_url_list.gform.dart @@ -150,6 +150,7 @@ class _AnimatedUrlListFormBuilderState AnimatedUrlListForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -192,7 +193,9 @@ class _AnimatedUrlListFormBuilderState @override void didUpdateWidget(covariant AnimatedUrlListFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -228,8 +231,12 @@ final _logAnimatedUrlListForm = Logger.detached('AnimatedUrlListForm'); class AnimatedUrlListForm implements FormModel { - AnimatedUrlListForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + AnimatedUrlListForm( + this.form, + this.path, + this._formModel, { + AnimatedUrlList? initialModel, + }) : _ownInitialModel = initialModel; static const String urlListControlName = "urlList"; @@ -242,8 +249,10 @@ class AnimatedUrlListForm final Map _disabled = {}; - @override - final Map initial; + AnimatedUrlList? _ownInitialModel; + + late Map _ownInitialRawValue = + AnimatedUrlListForm.formElements(_ownInitialModel).rawValue; String urlListControlPath() => pathBuilder(urlListControlName); @@ -503,10 +512,28 @@ class AnimatedUrlListForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + AnimatedUrlList? get initialModel { + return _ownInitialModel; + } + + void commitInitial([AnimatedUrlList? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = AnimatedUrlListForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -545,55 +572,6 @@ class AnimatedUrlListForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -619,8 +597,12 @@ class AnimatedUrlListForm final _logUrlEntityForm = Logger.detached('UrlEntityForm'); class UrlEntityForm implements FormModel { - UrlEntityForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + UrlEntityForm( + this.form, + this.path, + this._formModel, { + UrlEntity? initialModel, + }) : _ownInitialModel = initialModel; static const String labelControlName = "label"; @@ -635,8 +617,11 @@ class UrlEntityForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + UrlEntity? _ownInitialModel; + + late Map _ownInitialRawValue = UrlEntityForm.formElements( + _ownInitialModel, + ).rawValue; String labelControlPath() => pathBuilder(labelControlName); @@ -867,10 +852,26 @@ class UrlEntityForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + UrlEntity? get initialModel { + return _ownInitialModel; + } + + void commitInitial([UrlEntity? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = UrlEntityForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -909,55 +910,6 @@ class UrlEntityForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/animated_url_list/animated_url_list_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/animated_url_list/animated_url_list_output.gform.dart index 200ffa9f..6e8faa03 100644 --- a/packages/reactive_forms_generator/example/lib/docs/animated_url_list/animated_url_list_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/animated_url_list/animated_url_list_output.gform.dart @@ -150,6 +150,7 @@ class _AnimatedUrlLisOFormBuilderState AnimatedUrlLisOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -192,7 +193,9 @@ class _AnimatedUrlLisOFormBuilderState @override void didUpdateWidget(covariant AnimatedUrlLisOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -228,8 +231,12 @@ final _logAnimatedUrlLisOForm = Logger.detached('AnimatedUrlLisOForm'); class AnimatedUrlLisOForm implements FormModel { - AnimatedUrlLisOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + AnimatedUrlLisOForm( + this.form, + this.path, + this._formModel, { + AnimatedUrlLisO? initialModel, + }) : _ownInitialModel = initialModel; static const String urlListControlName = "urlList"; @@ -242,8 +249,10 @@ class AnimatedUrlLisOForm final Map _disabled = {}; - @override - final Map initial; + AnimatedUrlLisO? _ownInitialModel; + + late Map _ownInitialRawValue = + AnimatedUrlLisOForm.formElements(_ownInitialModel).rawValue; String urlListControlPath() => pathBuilder(urlListControlName); @@ -504,10 +513,28 @@ class AnimatedUrlLisOForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + AnimatedUrlLisO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([AnimatedUrlLisO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = AnimatedUrlLisOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -546,55 +573,6 @@ class AnimatedUrlLisOForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -620,8 +598,12 @@ class AnimatedUrlLisOForm final _logUrlEntityOForm = Logger.detached('UrlEntityOForm'); class UrlEntityOForm implements FormModel { - UrlEntityOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + UrlEntityOForm( + this.form, + this.path, + this._formModel, { + UrlEntityO? initialModel, + }) : _ownInitialModel = initialModel; static const String labelControlName = "label"; @@ -636,8 +618,11 @@ class UrlEntityOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + UrlEntityO? _ownInitialModel; + + late Map _ownInitialRawValue = UrlEntityOForm.formElements( + _ownInitialModel, + ).rawValue; String labelControlPath() => pathBuilder(labelControlName); @@ -915,10 +900,28 @@ class UrlEntityOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + UrlEntityO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([UrlEntityO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = UrlEntityOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -957,55 +960,6 @@ class UrlEntityOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1038,7 +992,6 @@ class UrlEntityOForm implements FormModel { @Rf(output: true) class AnimatedUrlLisOOutput { final List urlList; - AnimatedUrlLisOOutput({@RfArray() required this.urlList}); } @@ -1046,7 +999,6 @@ class AnimatedUrlLisOOutput { class UrlEntityOOutput { final String label; final String url; - UrlEntityOOutput({ @RfControl(validators: [RequiredValidator()]) required this.label, @RfControl(validators: [RequiredValidator()]) required this.url, diff --git a/packages/reactive_forms_generator/example/lib/docs/annotateless/annotateless.gform.dart b/packages/reactive_forms_generator/example/lib/docs/annotateless/annotateless.gform.dart index 6d3babe7..926c9561 100644 --- a/packages/reactive_forms_generator/example/lib/docs/annotateless/annotateless.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/annotateless/annotateless.gform.dart @@ -149,6 +149,7 @@ class _AnnotatelessFormBuilderState extends State { AnnotatelessForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -191,7 +192,9 @@ class _AnnotatelessFormBuilderState extends State { @override void didUpdateWidget(covariant AnnotatelessFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -226,8 +229,12 @@ class _AnnotatelessFormBuilderState extends State { final _logAnnotatelessForm = Logger.detached('AnnotatelessForm'); class AnnotatelessForm implements FormModel { - AnnotatelessForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + AnnotatelessForm( + this.form, + this.path, + this._formModel, { + Annotateless? initialModel, + }) : _ownInitialModel = initialModel; static const String emailControlName = "email"; @@ -242,8 +249,11 @@ class AnnotatelessForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + Annotateless? _ownInitialModel; + + late Map _ownInitialRawValue = AnnotatelessForm.formElements( + _ownInitialModel, + ).rawValue; String emailControlPath() => pathBuilder(emailControlName); @@ -474,10 +484,28 @@ class AnnotatelessForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + Annotateless? get initialModel { + return _ownInitialModel; + } + + void commitInitial([Annotateless? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = AnnotatelessForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -516,55 +544,6 @@ class AnnotatelessForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/annotateless/annotateless_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/annotateless/annotateless_output.gform.dart index b7f6707a..4375e618 100644 --- a/packages/reactive_forms_generator/example/lib/docs/annotateless/annotateless_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/annotateless/annotateless_output.gform.dart @@ -149,6 +149,7 @@ class _AnnotatelessOFormBuilderState extends State { AnnotatelessOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -191,7 +192,9 @@ class _AnnotatelessOFormBuilderState extends State { @override void didUpdateWidget(covariant AnnotatelessOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -227,8 +230,12 @@ final _logAnnotatelessOForm = Logger.detached('AnnotatelessOForm'); class AnnotatelessOForm implements FormModel { - AnnotatelessOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + AnnotatelessOForm( + this.form, + this.path, + this._formModel, { + AnnotatelessO? initialModel, + }) : _ownInitialModel = initialModel; static const String emailControlName = "email"; @@ -243,8 +250,10 @@ class AnnotatelessOForm final Map _disabled = {}; - @override - final Map initial; + AnnotatelessO? _ownInitialModel; + + late Map _ownInitialRawValue = + AnnotatelessOForm.formElements(_ownInitialModel).rawValue; String emailControlPath() => pathBuilder(emailControlName); @@ -476,10 +485,28 @@ class AnnotatelessOForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + AnnotatelessO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([AnnotatelessO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = AnnotatelessOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -518,55 +545,6 @@ class AnnotatelessOForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/array_nullable/array_nullable.gform.dart b/packages/reactive_forms_generator/example/lib/docs/array_nullable/array_nullable.gform.dart index 79c4d7fd..530cebc0 100644 --- a/packages/reactive_forms_generator/example/lib/docs/array_nullable/array_nullable.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/array_nullable/array_nullable.gform.dart @@ -149,6 +149,7 @@ class _ArrayNullableFormBuilderState extends State { ArrayNullableForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -191,7 +192,9 @@ class _ArrayNullableFormBuilderState extends State { @override void didUpdateWidget(covariant ArrayNullableFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -226,8 +229,12 @@ class _ArrayNullableFormBuilderState extends State { final _logArrayNullableForm = Logger.detached('ArrayNullableForm'); class ArrayNullableForm implements FormModel { - ArrayNullableForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + ArrayNullableForm( + this.form, + this.path, + this._formModel, { + ArrayNullable? initialModel, + }) : _ownInitialModel = initialModel; static const String emailListControlName = "emailList"; @@ -250,8 +257,10 @@ class ArrayNullableForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + ArrayNullable? _ownInitialModel; + + late Map _ownInitialRawValue = + ArrayNullableForm.formElements(_ownInitialModel).rawValue; String someListControlPath() => pathBuilder(someListControlName); @@ -1075,10 +1084,28 @@ class ArrayNullableForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + ArrayNullable? get initialModel { + return _ownInitialModel; + } + + void commitInitial([ArrayNullable? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = ArrayNullableForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1117,55 +1144,6 @@ class ArrayNullableForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/array_nullable/array_nullable_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/array_nullable/array_nullable_output.gform.dart index 7abb7680..c7fb5495 100644 --- a/packages/reactive_forms_generator/example/lib/docs/array_nullable/array_nullable_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/array_nullable/array_nullable_output.gform.dart @@ -149,6 +149,7 @@ class _ArrayNullableOFormBuilderState extends State { ArrayNullableOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -191,7 +192,9 @@ class _ArrayNullableOFormBuilderState extends State { @override void didUpdateWidget(covariant ArrayNullableOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -227,8 +230,12 @@ final _logArrayNullableOForm = Logger.detached('ArrayNullableOForm'); class ArrayNullableOForm implements FormModel { - ArrayNullableOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + ArrayNullableOForm( + this.form, + this.path, + this._formModel, { + ArrayNullableO? initialModel, + }) : _ownInitialModel = initialModel; static const String emailListControlName = "emailList"; @@ -249,8 +256,10 @@ class ArrayNullableOForm final Map _disabled = {}; - @override - final Map initial; + ArrayNullableO? _ownInitialModel; + + late Map _ownInitialRawValue = + ArrayNullableOForm.formElements(_ownInitialModel).rawValue; String someListControlPath() => pathBuilder(someListControlName); @@ -991,10 +1000,28 @@ class ArrayNullableOForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + ArrayNullableO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([ArrayNullableO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = ArrayNullableOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1033,55 +1060,6 @@ class ArrayNullableOForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/create/create_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/create/create_output.gform.dart index 194141d9..6c2bb11d 100644 --- a/packages/reactive_forms_generator/example/lib/docs/create/create_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/create/create_output.gform.dart @@ -144,6 +144,7 @@ class _MSICreateFormBuilderState extends State { MSICreateForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -186,7 +187,9 @@ class _MSICreateFormBuilderState extends State { @override void didUpdateWidget(covariant MSICreateFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -221,8 +224,12 @@ class _MSICreateFormBuilderState extends State { final _logMSICreateForm = Logger.detached('MSICreateForm'); class MSICreateForm implements FormModel { - MSICreateForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + MSICreateForm( + this.form, + this.path, + this._formModel, { + MSICreate? initialModel, + }) : _ownInitialModel = initialModel; static const String idControlName = "id"; @@ -254,8 +261,11 @@ class MSICreateForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + MSICreate? _ownInitialModel; + + late Map _ownInitialRawValue = MSICreateForm.formElements( + _ownInitialModel, + ).rawValue; String idControlPath() => pathBuilder(idControlName); @@ -1388,10 +1398,26 @@ class MSICreateForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + MSICreate? get initialModel { + return _ownInitialModel; + } + + void commitInitial([MSICreate? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = MSICreateForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1430,55 +1456,6 @@ class MSICreateForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1561,7 +1538,8 @@ class MSICreateForm implements FormModel { final _logAddressForm = Logger.detached('AddressForm'); class AddressForm implements FormModel { - AddressForm(this.form, this.path, this._formModel) : initial = form.rawValue; + AddressForm(this.form, this.path, this._formModel, {Address? initialModel}) + : _ownInitialModel = initialModel; static const String streetControlName = "street"; @@ -1580,8 +1558,11 @@ class AddressForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + Address? _ownInitialModel; + + late Map _ownInitialRawValue = AddressForm.formElements( + _ownInitialModel, + ).rawValue; String streetControlPath() => pathBuilder(streetControlName); @@ -2077,10 +2058,26 @@ class AddressForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + Address? get initialModel { + return _ownInitialModel; + } + + void commitInitial([Address? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = AddressForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -2119,55 +2116,6 @@ class AddressForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -2217,8 +2165,12 @@ final _logPrimaryContactForm = Logger.detached('PrimaryContactForm'); class PrimaryContactForm implements FormModel { - PrimaryContactForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + PrimaryContactForm( + this.form, + this.path, + this._formModel, { + PrimaryContact? initialModel, + }) : _ownInitialModel = initialModel; static const String fullNameControlName = "fullName"; @@ -2235,8 +2187,10 @@ class PrimaryContactForm final Map _disabled = {}; - @override - final Map initial; + PrimaryContact? _ownInitialModel; + + late Map _ownInitialRawValue = + PrimaryContactForm.formElements(_ownInitialModel).rawValue; String fullNameControlPath() => pathBuilder(fullNameControlName); @@ -2625,10 +2579,28 @@ class PrimaryContactForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + PrimaryContact? get initialModel { + return _ownInitialModel; + } + + void commitInitial([PrimaryContact? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = PrimaryContactForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -2667,55 +2639,6 @@ class PrimaryContactForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -2760,8 +2683,12 @@ final _logAdminContactInformationForm = Logger.detached( class AdminContactInformationForm implements FormModel { - AdminContactInformationForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + AdminContactInformationForm( + this.form, + this.path, + this._formModel, { + AdminContactInformation? initialModel, + }) : _ownInitialModel = initialModel; static const String firstNameControlName = "firstName"; @@ -2778,8 +2705,10 @@ class AdminContactInformationForm final Map _disabled = {}; - @override - final Map initial; + AdminContactInformation? _ownInitialModel; + + late Map _ownInitialRawValue = + AdminContactInformationForm.formElements(_ownInitialModel).rawValue; String firstNameControlPath() => pathBuilder(firstNameControlName); @@ -3168,10 +3097,28 @@ class AdminContactInformationForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + AdminContactInformation? get initialModel { + return _ownInitialModel; + } + + void commitInitial([AdminContactInformation? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = AdminContactInformationForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -3210,55 +3157,6 @@ class AdminContactInformationForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/delivery_list/delivery_list.gform.dart b/packages/reactive_forms_generator/example/lib/docs/delivery_list/delivery_list.gform.dart index 653ff1c9..3567b91d 100644 --- a/packages/reactive_forms_generator/example/lib/docs/delivery_list/delivery_list.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/delivery_list/delivery_list.gform.dart @@ -149,6 +149,7 @@ class _DeliveryListFormBuilderState extends State { DeliveryListForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -191,7 +192,9 @@ class _DeliveryListFormBuilderState extends State { @override void didUpdateWidget(covariant DeliveryListFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -226,8 +229,12 @@ class _DeliveryListFormBuilderState extends State { final _logDeliveryListForm = Logger.detached('DeliveryListForm'); class DeliveryListForm implements FormModel { - DeliveryListForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + DeliveryListForm( + this.form, + this.path, + this._formModel, { + DeliveryList? initialModel, + }) : _ownInitialModel = initialModel; static const String deliveryListControlName = "deliveryList"; @@ -242,8 +249,11 @@ class DeliveryListForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + DeliveryList? _ownInitialModel; + + late Map _ownInitialRawValue = DeliveryListForm.formElements( + _ownInitialModel, + ).rawValue; String deliveryListControlPath() => pathBuilder(deliveryListControlName); @@ -728,10 +738,28 @@ class DeliveryListForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + DeliveryList? get initialModel { + return _ownInitialModel; + } + + void commitInitial([DeliveryList? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = DeliveryListForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -770,55 +798,6 @@ class DeliveryListForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -853,8 +832,12 @@ class DeliveryListForm implements FormModel { final _logDeliveryPointForm = Logger.detached('DeliveryPointForm'); class DeliveryPointForm implements FormModel { - DeliveryPointForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + DeliveryPointForm( + this.form, + this.path, + this._formModel, { + DeliveryPoint? initialModel, + }) : _ownInitialModel = initialModel; static const String nameControlName = "name"; @@ -869,8 +852,10 @@ class DeliveryPointForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + DeliveryPoint? _ownInitialModel; + + late Map _ownInitialRawValue = + DeliveryPointForm.formElements(_ownInitialModel).rawValue; String nameControlPath() => pathBuilder(nameControlName); @@ -1128,10 +1113,28 @@ class DeliveryPointForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + DeliveryPoint? get initialModel { + return _ownInitialModel; + } + + void commitInitial([DeliveryPoint? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = DeliveryPointForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1170,55 +1173,6 @@ class DeliveryPointForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1244,7 +1198,8 @@ class DeliveryPointForm implements FormModel { final _logAddressForm = Logger.detached('AddressForm'); class AddressForm implements FormModel { - AddressForm(this.form, this.path, this._formModel) : initial = form.rawValue; + AddressForm(this.form, this.path, this._formModel, {Address? initialModel}) + : _ownInitialModel = initialModel; static const String streetControlName = "street"; @@ -1259,8 +1214,11 @@ class AddressForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + Address? _ownInitialModel; + + late Map _ownInitialRawValue = AddressForm.formElements( + _ownInitialModel, + ).rawValue; String streetControlPath() => pathBuilder(streetControlName); @@ -1537,10 +1495,26 @@ class AddressForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + Address? get initialModel { + return _ownInitialModel; + } + + void commitInitial([Address? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = AddressForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1579,55 +1553,6 @@ class AddressForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1660,7 +1585,8 @@ class AddressForm implements FormModel { final _logClientForm = Logger.detached('ClientForm'); class ClientForm implements FormModel { - ClientForm(this.form, this.path, this._formModel) : initial = form.rawValue; + ClientForm(this.form, this.path, this._formModel, {Client? initialModel}) + : _ownInitialModel = initialModel; static const String clientTypeControlName = "clientType"; @@ -1677,8 +1603,11 @@ class ClientForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + Client? _ownInitialModel; + + late Map _ownInitialRawValue = ClientForm.formElements( + _ownInitialModel, + ).rawValue; String clientTypeControlPath() => pathBuilder(clientTypeControlName); @@ -2041,10 +1970,26 @@ class ClientForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + Client? get initialModel { + return _ownInitialModel; + } + + void commitInitial([Client? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = ClientForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -2083,55 +2028,6 @@ class ClientForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -2547,6 +2443,7 @@ class _StandaloneDeliveryPointFormBuilderState StandaloneDeliveryPointForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -2591,7 +2488,9 @@ class _StandaloneDeliveryPointFormBuilderState @override void didUpdateWidget(covariant StandaloneDeliveryPointFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -2629,8 +2528,12 @@ final _logStandaloneDeliveryPointForm = Logger.detached( class StandaloneDeliveryPointForm implements FormModel { - StandaloneDeliveryPointForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + StandaloneDeliveryPointForm( + this.form, + this.path, + this._formModel, { + DeliveryPoint? initialModel, + }) : _ownInitialModel = initialModel; static const String nameControlName = "name"; @@ -2645,8 +2548,10 @@ class StandaloneDeliveryPointForm final Map _disabled = {}; - @override - final Map initial; + DeliveryPoint? _ownInitialModel; + + late Map _ownInitialRawValue = + StandaloneDeliveryPointForm.formElements(_ownInitialModel).rawValue; String nameControlPath() => pathBuilder(nameControlName); @@ -2904,10 +2809,28 @@ class StandaloneDeliveryPointForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + DeliveryPoint? get initialModel { + return _ownInitialModel; + } + + void commitInitial([DeliveryPoint? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = StandaloneDeliveryPointForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -2946,55 +2869,6 @@ class StandaloneDeliveryPointForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/delivery_list/delivery_list_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/delivery_list/delivery_list_output.gform.dart index 81275f12..927f2e19 100644 --- a/packages/reactive_forms_generator/example/lib/docs/delivery_list/delivery_list_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/delivery_list/delivery_list_output.gform.dart @@ -149,6 +149,7 @@ class _DeliveryListOFormBuilderState extends State { DeliveryListOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -191,7 +192,9 @@ class _DeliveryListOFormBuilderState extends State { @override void didUpdateWidget(covariant DeliveryListOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -227,8 +230,12 @@ final _logDeliveryListOForm = Logger.detached('DeliveryListOForm'); class DeliveryListOForm implements FormModel { - DeliveryListOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + DeliveryListOForm( + this.form, + this.path, + this._formModel, { + DeliveryListO? initialModel, + }) : _ownInitialModel = initialModel; static const String deliveryListControlName = "deliveryList"; @@ -243,8 +250,10 @@ class DeliveryListOForm final Map _disabled = {}; - @override - final Map initial; + DeliveryListO? _ownInitialModel; + + late Map _ownInitialRawValue = + DeliveryListOForm.formElements(_ownInitialModel).rawValue; String deliveryListControlPath() => pathBuilder(deliveryListControlName); @@ -730,10 +739,28 @@ class DeliveryListOForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + DeliveryListO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([DeliveryListO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = DeliveryListOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -772,55 +799,6 @@ class DeliveryListOForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -856,8 +834,12 @@ final _logDeliveryPointOForm = Logger.detached('DeliveryPointOForm'); class DeliveryPointOForm implements FormModel { - DeliveryPointOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + DeliveryPointOForm( + this.form, + this.path, + this._formModel, { + DeliveryPointO? initialModel, + }) : _ownInitialModel = initialModel; static const String nameControlName = "name"; @@ -872,8 +854,10 @@ class DeliveryPointOForm final Map _disabled = {}; - @override - final Map initial; + DeliveryPointO? _ownInitialModel; + + late Map _ownInitialRawValue = + DeliveryPointOForm.formElements(_ownInitialModel).rawValue; String nameControlPath() => pathBuilder(nameControlName); @@ -1133,10 +1117,28 @@ class DeliveryPointOForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + DeliveryPointO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([DeliveryPointO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = DeliveryPointOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1175,55 +1177,6 @@ class DeliveryPointOForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1249,7 +1202,8 @@ class DeliveryPointOForm final _logAddressOForm = Logger.detached('AddressOForm'); class AddressOForm implements FormModel { - AddressOForm(this.form, this.path, this._formModel) : initial = form.rawValue; + AddressOForm(this.form, this.path, this._formModel, {AddressO? initialModel}) + : _ownInitialModel = initialModel; static const String streetControlName = "street"; @@ -1264,8 +1218,11 @@ class AddressOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + AddressO? _ownInitialModel; + + late Map _ownInitialRawValue = AddressOForm.formElements( + _ownInitialModel, + ).rawValue; String streetControlPath() => pathBuilder(streetControlName); @@ -1543,10 +1500,26 @@ class AddressOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + AddressO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([AddressO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = AddressOForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1585,55 +1558,6 @@ class AddressOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1666,7 +1590,8 @@ class AddressOForm implements FormModel { final _logClientOForm = Logger.detached('ClientOForm'); class ClientOForm implements FormModel { - ClientOForm(this.form, this.path, this._formModel) : initial = form.rawValue; + ClientOForm(this.form, this.path, this._formModel, {ClientO? initialModel}) + : _ownInitialModel = initialModel; static const String clientTypeControlName = "clientType"; @@ -1683,8 +1608,11 @@ class ClientOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + ClientO? _ownInitialModel; + + late Map _ownInitialRawValue = ClientOForm.formElements( + _ownInitialModel, + ).rawValue; String clientTypeControlPath() => pathBuilder(clientTypeControlName); @@ -2048,10 +1976,26 @@ class ClientOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + ClientO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([ClientO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = ClientOForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -2090,55 +2034,6 @@ class ClientOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -2605,6 +2500,7 @@ class _StandaloneDeliveryPointFormBuilderState StandaloneDeliveryPointForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -2649,7 +2545,9 @@ class _StandaloneDeliveryPointFormBuilderState @override void didUpdateWidget(covariant StandaloneDeliveryPointFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -2687,8 +2585,12 @@ final _logStandaloneDeliveryPointForm = Logger.detached( class StandaloneDeliveryPointForm implements FormModel { - StandaloneDeliveryPointForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + StandaloneDeliveryPointForm( + this.form, + this.path, + this._formModel, { + DeliveryPointO? initialModel, + }) : _ownInitialModel = initialModel; static const String nameControlName = "name"; @@ -2703,8 +2605,10 @@ class StandaloneDeliveryPointForm final Map _disabled = {}; - @override - final Map initial; + DeliveryPointO? _ownInitialModel; + + late Map _ownInitialRawValue = + StandaloneDeliveryPointForm.formElements(_ownInitialModel).rawValue; String nameControlPath() => pathBuilder(nameControlName); @@ -2964,10 +2868,28 @@ class StandaloneDeliveryPointForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + DeliveryPointO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([DeliveryPointO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = StandaloneDeliveryPointForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -3006,55 +2928,6 @@ class StandaloneDeliveryPointForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/delivery_list/delivery_route_form.dart b/packages/reactive_forms_generator/example/lib/docs/delivery_list/delivery_route_form.dart index b5c61fa5..2c4b102e 100644 --- a/packages/reactive_forms_generator/example/lib/docs/delivery_list/delivery_route_form.dart +++ b/packages/reactive_forms_generator/example/lib/docs/delivery_list/delivery_route_form.dart @@ -2,6 +2,7 @@ import 'package:example/docs/delivery_list/delivery_list.dart'; import 'package:example/docs/delivery_list/labels.dart'; import 'package:example/docs/delivery_list/mocks.dart'; import 'package:example/sample_screen.dart'; +import 'package:example/widgets/dirty_badge.dart'; import 'package:flutter/material.dart' hide ProgressIndicator; import 'package:reactive_forms/reactive_forms.dart'; @@ -17,9 +18,18 @@ class DeliveryListFormWidget extends StatelessWidget { body: DeliveryListFormBuilder( model: const DeliveryList(), builder: (context, formModel, child) { - return Column( + return StreamBuilder( + stream: formModel.form.valueChanges, + builder: (context, _) => Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ + Padding( + padding: const EdgeInsets.only(bottom: 8), + child: DirtyBadge( + changed: formModel.hasChanged, + label: 'form', + ), + ), Row( children: [ Expanded( @@ -29,8 +39,17 @@ class DeliveryListFormWidget extends StatelessWidget { >( extended: formModel.deliveryListExtendedControl, itemBuilder: (context, i, formItem, _) { + final itemForm = + formModel.deliveryListDeliveryPointForm[i]; return Column( children: [ + Padding( + padding: const EdgeInsets.only(top: 4), + child: DirtyBadge( + changed: itemForm.hasChanged, + label: 'item $i', + ), + ), Row( children: [ Expanded( @@ -64,6 +83,13 @@ class DeliveryListFormWidget extends StatelessWidget { ), ], ), + Padding( + padding: const EdgeInsets.only(top: 4), + child: DirtyBadge( + changed: itemForm.addressForm.hasChanged, + label: 'address $i', + ), + ), ReactiveTextField( key: street.itemIndexKey(i), formControl: @@ -255,6 +281,7 @@ class DeliveryListFormWidget extends StatelessWidget { ], ), ], + ), ); }, ), diff --git a/packages/reactive_forms_generator/example/lib/docs/freezed/freezed_class.gform.dart b/packages/reactive_forms_generator/example/lib/docs/freezed/freezed_class.gform.dart index 75b6174a..bd58a33f 100644 --- a/packages/reactive_forms_generator/example/lib/docs/freezed/freezed_class.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/freezed/freezed_class.gform.dart @@ -149,6 +149,7 @@ class _FreezedClassFormBuilderState extends State { FreezedClassForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -191,7 +192,9 @@ class _FreezedClassFormBuilderState extends State { @override void didUpdateWidget(covariant FreezedClassFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -226,8 +229,12 @@ class _FreezedClassFormBuilderState extends State { final _logFreezedClassForm = Logger.detached('FreezedClassForm'); class FreezedClassForm implements FormModel { - FreezedClassForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + FreezedClassForm( + this.form, + this.path, + this._formModel, { + FreezedClass? initialModel, + }) : _ownInitialModel = initialModel; static const String genderControlName = "gender"; @@ -250,8 +257,11 @@ class FreezedClassForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + FreezedClass? _ownInitialModel; + + late Map _ownInitialRawValue = FreezedClassForm.formElements( + _ownInitialModel, + ).rawValue; String genderControlPath() => pathBuilder(genderControlName); @@ -922,10 +932,28 @@ class FreezedClassForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + FreezedClass? get initialModel { + return _ownInitialModel; + } + + void commitInitial([FreezedClass? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = FreezedClassForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -964,55 +992,6 @@ class FreezedClassForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/freezed/freezed_class_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/freezed/freezed_class_output.gform.dart index 412dd485..c6b59119 100644 --- a/packages/reactive_forms_generator/example/lib/docs/freezed/freezed_class_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/freezed/freezed_class_output.gform.dart @@ -149,6 +149,7 @@ class _FreezedClassOFormBuilderState extends State { FreezedClassOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -191,7 +192,9 @@ class _FreezedClassOFormBuilderState extends State { @override void didUpdateWidget(covariant FreezedClassOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -227,8 +230,12 @@ final _logFreezedClassOForm = Logger.detached('FreezedClassOForm'); class FreezedClassOForm implements FormModel { - FreezedClassOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + FreezedClassOForm( + this.form, + this.path, + this._formModel, { + FreezedClassO? initialModel, + }) : _ownInitialModel = initialModel; static const String genderControlName = "gender"; @@ -257,8 +264,10 @@ class FreezedClassOForm final Map _disabled = {}; - @override - final Map initial; + FreezedClassO? _ownInitialModel; + + late Map _ownInitialRawValue = + FreezedClassOForm.formElements(_ownInitialModel).rawValue; String genderControlPath() => pathBuilder(genderControlName); @@ -1216,10 +1225,28 @@ class FreezedClassOForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + FreezedClassO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([FreezedClassO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = FreezedClassOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1258,55 +1285,6 @@ class FreezedClassOForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/freezed2/test.gform.dart b/packages/reactive_forms_generator/example/lib/docs/freezed2/test.gform.dart index 72a1bb1f..9dc80a18 100644 --- a/packages/reactive_forms_generator/example/lib/docs/freezed2/test.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/freezed2/test.gform.dart @@ -126,7 +126,12 @@ class _TestFormBuilderState extends State { @override void initState() { - _formModel = TestForm(TestForm.formElements(widget.model), null, null); + _formModel = TestForm( + TestForm.formElements(widget.model), + null, + null, + initialModel: widget.model, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -168,7 +173,9 @@ class _TestFormBuilderState extends State { @override void didUpdateWidget(covariant TestFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -203,7 +210,8 @@ class _TestFormBuilderState extends State { final _logTestForm = Logger.detached('TestForm'); class TestForm implements FormModel { - TestForm(this.form, this.path, this._formModel) : initial = form.rawValue; + TestForm(this.form, this.path, this._formModel, {Test? initialModel}) + : _ownInitialModel = initialModel; static const String titleControlName = "title"; @@ -218,8 +226,11 @@ class TestForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + Test? _ownInitialModel; + + late Map _ownInitialRawValue = TestForm.formElements( + _ownInitialModel, + ).rawValue; String titleControlPath() => pathBuilder(titleControlName); @@ -475,10 +486,26 @@ class TestForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + Test? get initialModel { + return _ownInitialModel; + } + + void commitInitial([Test? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = TestForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -514,55 +541,6 @@ class TestForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/generic/generic.gform.dart b/packages/reactive_forms_generator/example/lib/docs/generic/generic.gform.dart index e24b0e61..3dbee416 100644 --- a/packages/reactive_forms_generator/example/lib/docs/generic/generic.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/generic/generic.gform.dart @@ -140,6 +140,7 @@ class _TagsFormBuilderState extends State> { TagsForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -182,7 +183,9 @@ class _TagsFormBuilderState extends State> { @override void didUpdateWidget(covariant TagsFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -217,7 +220,8 @@ class _TagsFormBuilderState extends State> { final _logTagsForm = Logger.detached('TagsForm'); class TagsForm implements FormModel, Tags> { - TagsForm(this.form, this.path, this._formModel) : initial = form.rawValue; + TagsForm(this.form, this.path, this._formModel, {Tags? initialModel}) + : _ownInitialModel = initialModel; static const String tagsControlName = "tags"; @@ -230,8 +234,11 @@ class TagsForm implements FormModel, Tags> { final Map _disabled = {}; - @override - final Map initial; + Tags? _ownInitialModel; + + late Map _ownInitialRawValue = TagsForm.formElements( + _ownInitialModel, + ).rawValue; String tagsControlPath() => pathBuilder(tagsControlName); @@ -407,10 +414,26 @@ class TagsForm implements FormModel, Tags> { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + Tags? get initialModel { + return _ownInitialModel; + } + + void commitInitial([Tags? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = TagsForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -449,55 +472,6 @@ class TagsForm implements FormModel, Tags> { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/generic/generic_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/generic/generic_output.gform.dart index 1704a8e8..ffe49ca4 100644 --- a/packages/reactive_forms_generator/example/lib/docs/generic/generic_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/generic/generic_output.gform.dart @@ -141,6 +141,7 @@ class _TagsOFormBuilderState extends State> { TagsOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -183,7 +184,9 @@ class _TagsOFormBuilderState extends State> { @override void didUpdateWidget(covariant TagsOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -218,7 +221,8 @@ class _TagsOFormBuilderState extends State> { final _logTagsOForm = Logger.detached('TagsOForm'); class TagsOForm implements FormModel, TagsOOutput> { - TagsOForm(this.form, this.path, this._formModel) : initial = form.rawValue; + TagsOForm(this.form, this.path, this._formModel, {TagsO? initialModel}) + : _ownInitialModel = initialModel; static const String tagsControlName = "tags"; @@ -231,8 +235,11 @@ class TagsOForm implements FormModel, TagsOOutput> { final Map _disabled = {}; - @override - final Map initial; + TagsO? _ownInitialModel; + + late Map _ownInitialRawValue = TagsOForm.formElements( + _ownInitialModel, + ).rawValue; String tagsControlPath() => pathBuilder(tagsControlName); @@ -409,10 +416,26 @@ class TagsOForm implements FormModel, TagsOOutput> { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + TagsO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([TagsO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = TagsOForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -451,55 +474,6 @@ class TagsOForm implements FormModel, TagsOOutput> { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/generic_status_list/generic_status_list.gform.dart b/packages/reactive_forms_generator/example/lib/docs/generic_status_list/generic_status_list.gform.dart index 78c42dff..6b8ccd71 100644 --- a/packages/reactive_forms_generator/example/lib/docs/generic_status_list/generic_status_list.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/generic_status_list/generic_status_list.gform.dart @@ -154,6 +154,7 @@ class _StatusListFormBuilderState StatusListForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -196,7 +197,9 @@ class _StatusListFormBuilderState @override void didUpdateWidget(covariant StatusListFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -232,8 +235,12 @@ final _logStatusListForm = Logger.detached('StatusListForm'); class StatusListForm implements FormModel, StatusList> { - StatusListForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + StatusListForm( + this.form, + this.path, + this._formModel, { + StatusList? initialModel, + }) : _ownInitialModel = initialModel; static const String listControlName = "list"; @@ -246,8 +253,11 @@ class StatusListForm final Map _disabled = {}; - @override - final Map initial; + StatusList? _ownInitialModel; + + late Map _ownInitialRawValue = StatusListForm.formElements( + _ownInitialModel, + ).rawValue; String listControlPath() => pathBuilder(listControlName); @@ -445,10 +455,28 @@ class StatusListForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + StatusList? get initialModel { + return _ownInitialModel; + } + + void commitInitial([StatusList? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = StatusListForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -487,55 +515,6 @@ class StatusListForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/generic_status_list/generic_status_list_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/generic_status_list/generic_status_list_output.gform.dart index d51f8812..830a95c3 100644 --- a/packages/reactive_forms_generator/example/lib/docs/generic_status_list/generic_status_list_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/generic_status_list/generic_status_list_output.gform.dart @@ -154,6 +154,7 @@ class _StatusListOFormBuilderState StatusListOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -196,7 +197,9 @@ class _StatusListOFormBuilderState @override void didUpdateWidget(covariant StatusListOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -232,8 +235,12 @@ final _logStatusListOForm = Logger.detached('StatusListOForm'); class StatusListOForm implements FormModel, StatusListOOutput> { - StatusListOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + StatusListOForm( + this.form, + this.path, + this._formModel, { + StatusListO? initialModel, + }) : _ownInitialModel = initialModel; static const String listControlName = "list"; @@ -246,8 +253,11 @@ class StatusListOForm final Map _disabled = {}; - @override - final Map initial; + StatusListO? _ownInitialModel; + + late Map _ownInitialRawValue = StatusListOForm.formElements( + _ownInitialModel, + ).rawValue; String listControlPath() => pathBuilder(listControlName); @@ -446,10 +456,28 @@ class StatusListOForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + StatusListO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([StatusListO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = StatusListOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -488,55 +516,6 @@ class StatusListOForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/group/group.gform.dart b/packages/reactive_forms_generator/example/lib/docs/group/group.gform.dart index d24cada0..e75e0f2b 100644 --- a/packages/reactive_forms_generator/example/lib/docs/group/group.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/group/group.gform.dart @@ -134,7 +134,12 @@ class _GroupFormBuilderState extends State { @override void initState() { - _formModel = GroupForm(GroupForm.formElements(widget.model), null, null); + _formModel = GroupForm( + GroupForm.formElements(widget.model), + null, + null, + initialModel: widget.model, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -176,7 +181,9 @@ class _GroupFormBuilderState extends State { @override void didUpdateWidget(covariant GroupFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -211,7 +218,8 @@ class _GroupFormBuilderState extends State { final _logGroupForm = Logger.detached('GroupForm'); class GroupForm implements FormModel { - GroupForm(this.form, this.path, this._formModel) : initial = form.rawValue; + GroupForm(this.form, this.path, this._formModel, {Group? initialModel}) + : _ownInitialModel = initialModel; static const String personalControlName = "personal"; @@ -230,8 +238,11 @@ class GroupForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + Group? _ownInitialModel; + + late Map _ownInitialRawValue = GroupForm.formElements( + _ownInitialModel, + ).rawValue; String personalControlPath() => pathBuilder(personalControlName); @@ -734,10 +745,26 @@ class GroupForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + Group? get initialModel { + return _ownInitialModel; + } + + void commitInitial([Group? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = GroupForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -773,55 +800,6 @@ class GroupForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -842,7 +820,8 @@ class GroupForm implements FormModel { final _logPersonalForm = Logger.detached('PersonalForm'); class PersonalForm implements FormModel { - PersonalForm(this.form, this.path, this._formModel) : initial = form.rawValue; + PersonalForm(this.form, this.path, this._formModel, {Personal? initialModel}) + : _ownInitialModel = initialModel; static const String nameControlName = "name"; @@ -857,8 +836,11 @@ class PersonalForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + Personal? _ownInitialModel; + + late Map _ownInitialRawValue = PersonalForm.formElements( + _ownInitialModel, + ).rawValue; String nameControlPath() => pathBuilder(nameControlName); @@ -1135,10 +1117,26 @@ class PersonalForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + Personal? get initialModel { + return _ownInitialModel; + } + + void commitInitial([Personal? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = PersonalForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1177,55 +1175,6 @@ class PersonalForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1258,7 +1207,8 @@ class PersonalForm implements FormModel { final _logPhoneForm = Logger.detached('PhoneForm'); class PhoneForm implements FormModel { - PhoneForm(this.form, this.path, this._formModel) : initial = form.rawValue; + PhoneForm(this.form, this.path, this._formModel, {Phone? initialModel}) + : _ownInitialModel = initialModel; static const String phoneNumberControlName = "phoneNumber"; @@ -1273,8 +1223,11 @@ class PhoneForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + Phone? _ownInitialModel; + + late Map _ownInitialRawValue = PhoneForm.formElements( + _ownInitialModel, + ).rawValue; String phoneNumberControlPath() => pathBuilder(phoneNumberControlName); @@ -1558,10 +1511,26 @@ class PhoneForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + Phone? get initialModel { + return _ownInitialModel; + } + + void commitInitial([Phone? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = PhoneForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1597,55 +1566,6 @@ class PhoneForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1678,7 +1598,8 @@ class PhoneForm implements FormModel { final _logAddressForm = Logger.detached('AddressForm'); class AddressForm implements FormModel { - AddressForm(this.form, this.path, this._formModel) : initial = form.rawValue; + AddressForm(this.form, this.path, this._formModel, {Address? initialModel}) + : _ownInitialModel = initialModel; static const String streetControlName = "street"; @@ -1695,8 +1616,11 @@ class AddressForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + Address? _ownInitialModel; + + late Map _ownInitialRawValue = AddressForm.formElements( + _ownInitialModel, + ).rawValue; String streetControlPath() => pathBuilder(streetControlName); @@ -2078,10 +2002,26 @@ class AddressForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + Address? get initialModel { + return _ownInitialModel; + } + + void commitInitial([Address? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = AddressForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -2120,55 +2060,6 @@ class AddressForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/group/group_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/group/group_output.gform.dart index 39b6120b..7683d40f 100644 --- a/packages/reactive_forms_generator/example/lib/docs/group/group_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/group/group_output.gform.dart @@ -137,7 +137,12 @@ class _GroupOFormBuilderState extends State { @override void initState() { - _formModel = GroupOForm(GroupOForm.formElements(widget.model), null, null); + _formModel = GroupOForm( + GroupOForm.formElements(widget.model), + null, + null, + initialModel: widget.model, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -179,7 +184,9 @@ class _GroupOFormBuilderState extends State { @override void didUpdateWidget(covariant GroupOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -214,7 +221,8 @@ class _GroupOFormBuilderState extends State { final _logGroupOForm = Logger.detached('GroupOForm'); class GroupOForm implements FormModel { - GroupOForm(this.form, this.path, this._formModel) : initial = form.rawValue; + GroupOForm(this.form, this.path, this._formModel, {GroupO? initialModel}) + : _ownInitialModel = initialModel; static const String personalControlName = "personal"; @@ -233,8 +241,11 @@ class GroupOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + GroupO? _ownInitialModel; + + late Map _ownInitialRawValue = GroupOForm.formElements( + _ownInitialModel, + ).rawValue; String personalControlPath() => pathBuilder(personalControlName); @@ -741,10 +752,26 @@ class GroupOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + GroupO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([GroupO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = GroupOForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -783,55 +810,6 @@ class GroupOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -852,8 +830,12 @@ class GroupOForm implements FormModel { final _logPersonalOForm = Logger.detached('PersonalOForm'); class PersonalOForm implements FormModel { - PersonalOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + PersonalOForm( + this.form, + this.path, + this._formModel, { + PersonalO? initialModel, + }) : _ownInitialModel = initialModel; static const String nameControlName = "name"; @@ -868,8 +850,11 @@ class PersonalOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + PersonalO? _ownInitialModel; + + late Map _ownInitialRawValue = PersonalOForm.formElements( + _ownInitialModel, + ).rawValue; String nameControlPath() => pathBuilder(nameControlName); @@ -1147,10 +1132,26 @@ class PersonalOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + PersonalO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([PersonalO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = PersonalOForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1189,55 +1190,6 @@ class PersonalOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1270,7 +1222,8 @@ class PersonalOForm implements FormModel { final _logPhoneOForm = Logger.detached('PhoneOForm'); class PhoneOForm implements FormModel { - PhoneOForm(this.form, this.path, this._formModel) : initial = form.rawValue; + PhoneOForm(this.form, this.path, this._formModel, {PhoneO? initialModel}) + : _ownInitialModel = initialModel; static const String phoneNumberControlName = "phoneNumber"; @@ -1285,8 +1238,11 @@ class PhoneOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + PhoneO? _ownInitialModel; + + late Map _ownInitialRawValue = PhoneOForm.formElements( + _ownInitialModel, + ).rawValue; String phoneNumberControlPath() => pathBuilder(phoneNumberControlName); @@ -1574,10 +1530,26 @@ class PhoneOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + PhoneO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([PhoneO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = PhoneOForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1616,55 +1588,6 @@ class PhoneOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1697,7 +1620,8 @@ class PhoneOForm implements FormModel { final _logAddressOForm = Logger.detached('AddressOForm'); class AddressOForm implements FormModel { - AddressOForm(this.form, this.path, this._formModel) : initial = form.rawValue; + AddressOForm(this.form, this.path, this._formModel, {AddressO? initialModel}) + : _ownInitialModel = initialModel; static const String streetControlName = "street"; @@ -1714,8 +1638,11 @@ class AddressOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + AddressO? _ownInitialModel; + + late Map _ownInitialRawValue = AddressOForm.formElements( + _ownInitialModel, + ).rawValue; String streetControlPath() => pathBuilder(streetControlName); @@ -2102,10 +2029,26 @@ class AddressOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + AddressO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([AddressO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = AddressOForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -2144,55 +2087,6 @@ class AddressOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/login/login.gform.dart b/packages/reactive_forms_generator/example/lib/docs/login/login.gform.dart index 5a7aa425..bd16d3f0 100644 --- a/packages/reactive_forms_generator/example/lib/docs/login/login.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/login/login.gform.dart @@ -134,7 +134,12 @@ class _LoginFormBuilderState extends State { @override void initState() { - _formModel = LoginForm(LoginForm.formElements(widget.model), null, null); + _formModel = LoginForm( + LoginForm.formElements(widget.model), + null, + null, + initialModel: widget.model, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -176,7 +181,9 @@ class _LoginFormBuilderState extends State { @override void didUpdateWidget(covariant LoginFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -211,7 +218,8 @@ class _LoginFormBuilderState extends State { final _logLoginForm = Logger.detached('LoginForm'); class LoginForm implements FormModel { - LoginForm(this.form, this.path, this._formModel) : initial = form.rawValue; + LoginForm(this.form, this.path, this._formModel, {Login? initialModel}) + : _ownInitialModel = initialModel; static const String emailControlName = "email"; @@ -226,8 +234,11 @@ class LoginForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + Login? _ownInitialModel; + + late Map _ownInitialRawValue = LoginForm.formElements( + _ownInitialModel, + ).rawValue; String emailControlPath() => pathBuilder(emailControlName); @@ -481,10 +492,26 @@ class LoginForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + Login? get initialModel { + return _ownInitialModel; + } + + void commitInitial([Login? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = LoginForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -520,55 +547,6 @@ class LoginForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/login/login_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/login/login_output.gform.dart index 9fde7b4f..d8d04525 100644 --- a/packages/reactive_forms_generator/example/lib/docs/login/login_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/login/login_output.gform.dart @@ -137,7 +137,12 @@ class _LoginOFormBuilderState extends State { @override void initState() { - _formModel = LoginOForm(LoginOForm.formElements(widget.model), null, null); + _formModel = LoginOForm( + LoginOForm.formElements(widget.model), + null, + null, + initialModel: widget.model, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -179,7 +184,9 @@ class _LoginOFormBuilderState extends State { @override void didUpdateWidget(covariant LoginOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -214,7 +221,8 @@ class _LoginOFormBuilderState extends State { final _logLoginOForm = Logger.detached('LoginOForm'); class LoginOForm implements FormModel { - LoginOForm(this.form, this.path, this._formModel) : initial = form.rawValue; + LoginOForm(this.form, this.path, this._formModel, {LoginO? initialModel}) + : _ownInitialModel = initialModel; static const String emailControlName = "email"; @@ -229,8 +237,11 @@ class LoginOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + LoginO? _ownInitialModel; + + late Map _ownInitialRawValue = LoginOForm.formElements( + _ownInitialModel, + ).rawValue; String emailControlPath() => pathBuilder(emailControlName); @@ -509,10 +520,26 @@ class LoginOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + LoginO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([LoginO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = LoginOForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -551,55 +578,6 @@ class LoginOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/login_extended/login_extended.gform.dart b/packages/reactive_forms_generator/example/lib/docs/login_extended/login_extended.gform.dart index 8caf2c29..3bc5a651 100644 --- a/packages/reactive_forms_generator/example/lib/docs/login_extended/login_extended.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/login_extended/login_extended.gform.dart @@ -149,6 +149,7 @@ class _LoginExtendedFormBuilderState extends State { LoginExtendedForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -191,7 +192,9 @@ class _LoginExtendedFormBuilderState extends State { @override void didUpdateWidget(covariant LoginExtendedFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -226,8 +229,12 @@ class _LoginExtendedFormBuilderState extends State { final _logLoginExtendedForm = Logger.detached('LoginExtendedForm'); class LoginExtendedForm implements FormModel { - LoginExtendedForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + LoginExtendedForm( + this.form, + this.path, + this._formModel, { + LoginExtended? initialModel, + }) : _ownInitialModel = initialModel; static const String emailControlName = "email"; @@ -256,8 +263,10 @@ class LoginExtendedForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + LoginExtended? _ownInitialModel; + + late Map _ownInitialRawValue = + LoginExtendedForm.formElements(_ownInitialModel).rawValue; String emailControlPath() => pathBuilder(emailControlName); @@ -1079,10 +1088,28 @@ class LoginExtendedForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + LoginExtended? get initialModel { + return _ownInitialModel; + } + + void commitInitial([LoginExtended? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = LoginExtendedForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1121,55 +1148,6 @@ class LoginExtendedForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/login_extended/login_extended_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/login_extended/login_extended_output.gform.dart index 8bbf5502..fba8f6be 100644 --- a/packages/reactive_forms_generator/example/lib/docs/login_extended/login_extended_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/login_extended/login_extended_output.gform.dart @@ -149,6 +149,7 @@ class _LoginExtendedOFormBuilderState extends State { LoginExtendedOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -191,7 +192,9 @@ class _LoginExtendedOFormBuilderState extends State { @override void didUpdateWidget(covariant LoginExtendedOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -227,8 +230,12 @@ final _logLoginExtendedOForm = Logger.detached('LoginExtendedOForm'); class LoginExtendedOForm implements FormModel { - LoginExtendedOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + LoginExtendedOForm( + this.form, + this.path, + this._formModel, { + LoginExtendedO? initialModel, + }) : _ownInitialModel = initialModel; static const String emailControlName = "email"; @@ -259,8 +266,10 @@ class LoginExtendedOForm final Map _disabled = {}; - @override - final Map initial; + LoginExtendedO? _ownInitialModel; + + late Map _ownInitialRawValue = + LoginExtendedOForm.formElements(_ownInitialModel).rawValue; String emailControlPath() => pathBuilder(emailControlName); @@ -1186,10 +1195,28 @@ class LoginExtendedOForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + LoginExtendedO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([LoginExtendedO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = LoginExtendedOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1228,55 +1255,6 @@ class LoginExtendedOForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/login_extended_nullable/login_extended_nullable.gform.dart b/packages/reactive_forms_generator/example/lib/docs/login_extended_nullable/login_extended_nullable.gform.dart index b7609e1a..d2059f13 100644 --- a/packages/reactive_forms_generator/example/lib/docs/login_extended_nullable/login_extended_nullable.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/login_extended_nullable/login_extended_nullable.gform.dart @@ -157,6 +157,7 @@ class _LoginExtendedNullableFormBuilderState LoginExtendedNullableForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -201,7 +202,9 @@ class _LoginExtendedNullableFormBuilderState @override void didUpdateWidget(covariant LoginExtendedNullableFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -239,8 +242,12 @@ final _logLoginExtendedNullableForm = Logger.detached( class LoginExtendedNullableForm implements FormModel { - LoginExtendedNullableForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + LoginExtendedNullableForm( + this.form, + this.path, + this._formModel, { + LoginExtendedNullable? initialModel, + }) : _ownInitialModel = initialModel; static const String emailControlName = "email"; @@ -265,8 +272,10 @@ class LoginExtendedNullableForm final Map _disabled = {}; - @override - final Map initial; + LoginExtendedNullable? _ownInitialModel; + + late Map _ownInitialRawValue = + LoginExtendedNullableForm.formElements(_ownInitialModel).rawValue; String emailControlPath() => pathBuilder(emailControlName); @@ -1067,10 +1076,28 @@ class LoginExtendedNullableForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + LoginExtendedNullable? get initialModel { + return _ownInitialModel; + } + + void commitInitial([LoginExtendedNullable? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = LoginExtendedNullableForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1109,55 +1136,6 @@ class LoginExtendedNullableForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/login_extended_nullable/login_extended_nullable_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/login_extended_nullable/login_extended_nullable_output.gform.dart index f65bedf4..e9c548f0 100644 --- a/packages/reactive_forms_generator/example/lib/docs/login_extended_nullable/login_extended_nullable_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/login_extended_nullable/login_extended_nullable_output.gform.dart @@ -157,6 +157,7 @@ class _LoginExtendedNullableOFormBuilderState LoginExtendedNullableOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -201,7 +202,9 @@ class _LoginExtendedNullableOFormBuilderState @override void didUpdateWidget(covariant LoginExtendedNullableOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -239,8 +242,12 @@ final _logLoginExtendedNullableOForm = Logger.detached( class LoginExtendedNullableOForm implements FormModel { - LoginExtendedNullableOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + LoginExtendedNullableOForm( + this.form, + this.path, + this._formModel, { + LoginExtendedNullableO? initialModel, + }) : _ownInitialModel = initialModel; static const String emailControlName = "email"; @@ -265,8 +272,10 @@ class LoginExtendedNullableOForm final Map _disabled = {}; - @override - final Map initial; + LoginExtendedNullableO? _ownInitialModel; + + late Map _ownInitialRawValue = + LoginExtendedNullableOForm.formElements(_ownInitialModel).rawValue; String emailControlPath() => pathBuilder(emailControlName); @@ -1068,10 +1077,28 @@ class LoginExtendedNullableOForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + LoginExtendedNullableO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([LoginExtendedNullableO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = LoginExtendedNullableOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1110,55 +1137,6 @@ class LoginExtendedNullableOForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/mailing_list/mailing_list.gform.dart b/packages/reactive_forms_generator/example/lib/docs/mailing_list/mailing_list.gform.dart index ad61f703..99986fa0 100644 --- a/packages/reactive_forms_generator/example/lib/docs/mailing_list/mailing_list.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/mailing_list/mailing_list.gform.dart @@ -147,6 +147,7 @@ class _MailingListFormBuilderState extends State { MailingListForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -189,7 +190,9 @@ class _MailingListFormBuilderState extends State { @override void didUpdateWidget(covariant MailingListFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -224,8 +227,12 @@ class _MailingListFormBuilderState extends State { final _logMailingListForm = Logger.detached('MailingListForm'); class MailingListForm implements FormModel { - MailingListForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + MailingListForm( + this.form, + this.path, + this._formModel, { + MailingList? initialModel, + }) : _ownInitialModel = initialModel; static const String emailListControlName = "emailList"; @@ -238,8 +245,11 @@ class MailingListForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + MailingList? _ownInitialModel; + + late Map _ownInitialRawValue = MailingListForm.formElements( + _ownInitialModel, + ).rawValue; String emailListControlPath() => pathBuilder(emailListControlName); @@ -439,10 +449,28 @@ class MailingListForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + MailingList? get initialModel { + return _ownInitialModel; + } + + void commitInitial([MailingList? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = MailingListForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -481,55 +509,6 @@ class MailingListForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/mailing_list/mailing_list_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/mailing_list/mailing_list_output.gform.dart index 31ba0b4e..3f2507a2 100644 --- a/packages/reactive_forms_generator/example/lib/docs/mailing_list/mailing_list_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/mailing_list/mailing_list_output.gform.dart @@ -149,6 +149,7 @@ class _MailingListOFormBuilderState extends State { MailingListOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -191,7 +192,9 @@ class _MailingListOFormBuilderState extends State { @override void didUpdateWidget(covariant MailingListOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -226,8 +229,12 @@ class _MailingListOFormBuilderState extends State { final _logMailingListOForm = Logger.detached('MailingListOForm'); class MailingListOForm implements FormModel { - MailingListOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + MailingListOForm( + this.form, + this.path, + this._formModel, { + MailingListO? initialModel, + }) : _ownInitialModel = initialModel; static const String emailListControlName = "emailList"; @@ -240,8 +247,11 @@ class MailingListOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + MailingListO? _ownInitialModel; + + late Map _ownInitialRawValue = MailingListOForm.formElements( + _ownInitialModel, + ).rawValue; String emailListControlPath() => pathBuilder(emailListControlName); @@ -442,10 +452,28 @@ class MailingListOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + MailingListO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([MailingListO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = MailingListOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -484,55 +512,6 @@ class MailingListOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/model_extends/model_extends.gform.dart b/packages/reactive_forms_generator/example/lib/docs/model_extends/model_extends.gform.dart index 31a712f7..18a63f9e 100644 --- a/packages/reactive_forms_generator/example/lib/docs/model_extends/model_extends.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/model_extends/model_extends.gform.dart @@ -149,6 +149,7 @@ class _ModelExtendsFormBuilderState extends State { ModelExtendsForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -191,7 +192,9 @@ class _ModelExtendsFormBuilderState extends State { @override void didUpdateWidget(covariant ModelExtendsFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -226,8 +229,12 @@ class _ModelExtendsFormBuilderState extends State { final _logModelExtendsForm = Logger.detached('ModelExtendsForm'); class ModelExtendsForm implements FormModel { - ModelExtendsForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + ModelExtendsForm( + this.form, + this.path, + this._formModel, { + ModelExtends? initialModel, + }) : _ownInitialModel = initialModel; static const String emailControlName = "email"; @@ -242,8 +249,11 @@ class ModelExtendsForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + ModelExtends? _ownInitialModel; + + late Map _ownInitialRawValue = ModelExtendsForm.formElements( + _ownInitialModel, + ).rawValue; String emailControlPath() => pathBuilder(emailControlName); @@ -474,10 +484,28 @@ class ModelExtendsForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + ModelExtends? get initialModel { + return _ownInitialModel; + } + + void commitInitial([ModelExtends? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = ModelExtendsForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -516,55 +544,6 @@ class ModelExtendsForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/model_implements/model_implements.gform.dart b/packages/reactive_forms_generator/example/lib/docs/model_implements/model_implements.gform.dart index a42d02d0..40675942 100644 --- a/packages/reactive_forms_generator/example/lib/docs/model_implements/model_implements.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/model_implements/model_implements.gform.dart @@ -150,6 +150,7 @@ class _ModelImplementsFormBuilderState ModelImplementsForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -192,7 +193,9 @@ class _ModelImplementsFormBuilderState @override void didUpdateWidget(covariant ModelImplementsFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -228,8 +231,12 @@ final _logModelImplementsForm = Logger.detached('ModelImplementsForm'); class ModelImplementsForm implements FormModel { - ModelImplementsForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + ModelImplementsForm( + this.form, + this.path, + this._formModel, { + ModelImplements? initialModel, + }) : _ownInitialModel = initialModel; static const String emailControlName = "email"; @@ -244,8 +251,10 @@ class ModelImplementsForm final Map _disabled = {}; - @override - final Map initial; + ModelImplements? _ownInitialModel; + + late Map _ownInitialRawValue = + ModelImplementsForm.formElements(_ownInitialModel).rawValue; String emailControlPath() => pathBuilder(emailControlName); @@ -476,10 +485,28 @@ class ModelImplementsForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + ModelImplements? get initialModel { + return _ownInitialModel; + } + + void commitInitial([ModelImplements? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = ModelImplementsForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -518,55 +545,6 @@ class ModelImplementsForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/nested/nested.gform.dart b/packages/reactive_forms_generator/example/lib/docs/nested/nested.gform.dart index cc25cab8..d9f150e2 100644 --- a/packages/reactive_forms_generator/example/lib/docs/nested/nested.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/nested/nested.gform.dart @@ -144,6 +144,7 @@ class _SubGroupFormBuilderState extends State { SubGroupForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -186,7 +187,9 @@ class _SubGroupFormBuilderState extends State { @override void didUpdateWidget(covariant SubGroupFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -221,7 +224,8 @@ class _SubGroupFormBuilderState extends State { final _logSubGroupForm = Logger.detached('SubGroupForm'); class SubGroupForm implements FormModel { - SubGroupForm(this.form, this.path, this._formModel) : initial = form.rawValue; + SubGroupForm(this.form, this.path, this._formModel, {SubGroup? initialModel}) + : _ownInitialModel = initialModel; static const String idControlName = "id"; @@ -234,8 +238,11 @@ class SubGroupForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + SubGroup? _ownInitialModel; + + late Map _ownInitialRawValue = SubGroupForm.formElements( + _ownInitialModel, + ).rawValue; String idControlPath() => pathBuilder(idControlName); @@ -385,10 +392,26 @@ class SubGroupForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + SubGroup? get initialModel { + return _ownInitialModel; + } + + void commitInitial([SubGroup? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = SubGroupForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -427,55 +450,6 @@ class SubGroupForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -843,7 +817,12 @@ class _GroupFormBuilderState extends State { @override void initState() { - _formModel = GroupForm(GroupForm.formElements(widget.model), null, null); + _formModel = GroupForm( + GroupForm.formElements(widget.model), + null, + null, + initialModel: widget.model, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -885,7 +864,9 @@ class _GroupFormBuilderState extends State { @override void didUpdateWidget(covariant GroupFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -920,7 +901,8 @@ class _GroupFormBuilderState extends State { final _logGroupForm = Logger.detached('GroupForm'); class GroupForm implements FormModel { - GroupForm(this.form, this.path, this._formModel) : initial = form.rawValue; + GroupForm(this.form, this.path, this._formModel, {Group? initialModel}) + : _ownInitialModel = initialModel; static const String idControlName = "id"; @@ -935,8 +917,11 @@ class GroupForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + Group? _ownInitialModel; + + late Map _ownInitialRawValue = GroupForm.formElements( + _ownInitialModel, + ).rawValue; String idControlPath() => pathBuilder(idControlName); @@ -1277,10 +1262,26 @@ class GroupForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + Group? get initialModel { + return _ownInitialModel; + } + + void commitInitial([Group? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = GroupForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1316,55 +1317,6 @@ class GroupForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1743,7 +1695,12 @@ class _NestedFormBuilderState extends State { @override void initState() { - _formModel = NestedForm(NestedForm.formElements(widget.model), null, null); + _formModel = NestedForm( + NestedForm.formElements(widget.model), + null, + null, + initialModel: widget.model, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -1785,7 +1742,9 @@ class _NestedFormBuilderState extends State { @override void didUpdateWidget(covariant NestedFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -1820,7 +1779,8 @@ class _NestedFormBuilderState extends State { final _logNestedForm = Logger.detached('NestedForm'); class NestedForm implements FormModel { - NestedForm(this.form, this.path, this._formModel) : initial = form.rawValue; + NestedForm(this.form, this.path, this._formModel, {Nested? initialModel}) + : _ownInitialModel = initialModel; static const String groupListControlName = "groupList"; @@ -1833,8 +1793,11 @@ class NestedForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + Nested? _ownInitialModel; + + late Map _ownInitialRawValue = NestedForm.formElements( + _ownInitialModel, + ).rawValue; String groupListControlPath() => pathBuilder(groupListControlName); @@ -2094,10 +2057,26 @@ class NestedForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + Nested? get initialModel { + return _ownInitialModel; + } + + void commitInitial([Nested? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = NestedForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -2136,55 +2115,6 @@ class NestedForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/nested/nested_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/nested/nested_output.gform.dart index fa6215bc..43031fd3 100644 --- a/packages/reactive_forms_generator/example/lib/docs/nested/nested_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/nested/nested_output.gform.dart @@ -144,6 +144,7 @@ class _SubGroupOFormBuilderState extends State { SubGroupOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -186,7 +187,9 @@ class _SubGroupOFormBuilderState extends State { @override void didUpdateWidget(covariant SubGroupOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -221,8 +224,12 @@ class _SubGroupOFormBuilderState extends State { final _logSubGroupOForm = Logger.detached('SubGroupOForm'); class SubGroupOForm implements FormModel { - SubGroupOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + SubGroupOForm( + this.form, + this.path, + this._formModel, { + SubGroupO? initialModel, + }) : _ownInitialModel = initialModel; static const String idControlName = "id"; @@ -235,8 +242,11 @@ class SubGroupOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + SubGroupO? _ownInitialModel; + + late Map _ownInitialRawValue = SubGroupOForm.formElements( + _ownInitialModel, + ).rawValue; String idControlPath() => pathBuilder(idControlName); @@ -387,10 +397,26 @@ class SubGroupOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + SubGroupO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([SubGroupO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = SubGroupOForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -429,55 +455,6 @@ class SubGroupOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -881,7 +858,12 @@ class _GroupOFormBuilderState extends State { @override void initState() { - _formModel = GroupOForm(GroupOForm.formElements(widget.model), null, null); + _formModel = GroupOForm( + GroupOForm.formElements(widget.model), + null, + null, + initialModel: widget.model, + ); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -923,7 +905,9 @@ class _GroupOFormBuilderState extends State { @override void didUpdateWidget(covariant GroupOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -958,7 +942,8 @@ class _GroupOFormBuilderState extends State { final _logGroupOForm = Logger.detached('GroupOForm'); class GroupOForm implements FormModel { - GroupOForm(this.form, this.path, this._formModel) : initial = form.rawValue; + GroupOForm(this.form, this.path, this._formModel, {GroupO? initialModel}) + : _ownInitialModel = initialModel; static const String idControlName = "id"; @@ -973,8 +958,11 @@ class GroupOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + GroupO? _ownInitialModel; + + late Map _ownInitialRawValue = GroupOForm.formElements( + _ownInitialModel, + ).rawValue; String idControlPath() => pathBuilder(idControlName); @@ -1316,10 +1304,26 @@ class GroupOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + GroupO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([GroupO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = GroupOForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1358,55 +1362,6 @@ class GroupOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1792,6 +1747,7 @@ class _NestedOFormBuilderState extends State { NestedOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -1834,7 +1790,9 @@ class _NestedOFormBuilderState extends State { @override void didUpdateWidget(covariant NestedOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -1869,7 +1827,8 @@ class _NestedOFormBuilderState extends State { final _logNestedOForm = Logger.detached('NestedOForm'); class NestedOForm implements FormModel { - NestedOForm(this.form, this.path, this._formModel) : initial = form.rawValue; + NestedOForm(this.form, this.path, this._formModel, {NestedO? initialModel}) + : _ownInitialModel = initialModel; static const String groupListControlName = "groupList"; @@ -1882,8 +1841,11 @@ class NestedOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + NestedO? _ownInitialModel; + + late Map _ownInitialRawValue = NestedOForm.formElements( + _ownInitialModel, + ).rawValue; String groupListControlPath() => pathBuilder(groupListControlName); @@ -2144,10 +2106,26 @@ class NestedOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + NestedO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([NestedO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = NestedOForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -2186,55 +2164,6 @@ class NestedOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/nested_generics/nested_generics.gform.dart b/packages/reactive_forms_generator/example/lib/docs/nested_generics/nested_generics.gform.dart index d027d22d..6b047c3f 100644 --- a/packages/reactive_forms_generator/example/lib/docs/nested_generics/nested_generics.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/nested_generics/nested_generics.gform.dart @@ -159,6 +159,7 @@ class _ProductDetailsFormBuilderState

ProductDetailsForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -201,7 +202,9 @@ class _ProductDetailsFormBuilderState

@override void didUpdateWidget(covariant ProductDetailsFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -237,8 +240,12 @@ final _logProductDetailsForm = Logger.detached('ProductDetailsForm'); class ProductDetailsForm

implements FormModel, ProductDetails> { - ProductDetailsForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + ProductDetailsForm( + this.form, + this.path, + this._formModel, { + ProductDetails? initialModel, + }) : _ownInitialModel = initialModel; static const String descriptionControlName = "description"; @@ -253,8 +260,10 @@ class ProductDetailsForm

final Map _disabled = {}; - @override - final Map initial; + ProductDetails? _ownInitialModel; + + late Map _ownInitialRawValue = + ProductDetailsForm.formElements(_ownInitialModel).rawValue; String descriptionControlPath() => pathBuilder(descriptionControlName); @@ -535,10 +544,28 @@ class ProductDetailsForm

bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + ProductDetails? get initialModel { + return _ownInitialModel; + } + + void commitInitial([ProductDetails? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = ProductDetailsForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -577,55 +604,6 @@ class ProductDetailsForm

emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -654,7 +632,8 @@ final _logIdForm = Logger.detached('IdForm'); class IdForm

implements FormModel, Id> { - IdForm(this.form, this.path, this._formModel) : initial = form.rawValue; + IdForm(this.form, this.path, this._formModel, {Id? initialModel}) + : _ownInitialModel = initialModel; static const String companyNameControlName = "companyName"; @@ -669,8 +648,11 @@ class IdForm

final Map _disabled = {}; - @override - final Map initial; + Id? _ownInitialModel; + + late Map _ownInitialRawValue = IdForm.formElements( + _ownInitialModel, + ).rawValue; String companyNameControlPath() => pathBuilder(companyNameControlName); @@ -949,10 +931,26 @@ class IdForm

bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + Id? get initialModel { + return _ownInitialModel; + } + + void commitInitial([Id? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = IdForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -991,55 +989,6 @@ class IdForm

emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1455,6 +1404,7 @@ class _IdFormBuilderState

IdForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -1497,7 +1447,9 @@ class _IdFormBuilderState

@override void didUpdateWidget(covariant IdFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); diff --git a/packages/reactive_forms_generator/example/lib/docs/nested_generics/nested_generics_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/nested_generics/nested_generics_output.gform.dart index 0fef5d7f..b2238806 100644 --- a/packages/reactive_forms_generator/example/lib/docs/nested_generics/nested_generics_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/nested_generics/nested_generics_output.gform.dart @@ -162,6 +162,7 @@ class _ProductDetailsOFormBuilderState

ProductDetailsOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -204,7 +205,9 @@ class _ProductDetailsOFormBuilderState

@override void didUpdateWidget(covariant ProductDetailsOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -240,8 +243,12 @@ final _logProductDetailsOForm = Logger.detached('ProductDetailsOForm'); class ProductDetailsOForm

implements FormModel, ProductDetailsOOutput> { - ProductDetailsOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + ProductDetailsOForm( + this.form, + this.path, + this._formModel, { + ProductDetailsO? initialModel, + }) : _ownInitialModel = initialModel; static const String descriptionControlName = "description"; @@ -256,8 +263,10 @@ class ProductDetailsOForm

final Map _disabled = {}; - @override - final Map initial; + ProductDetailsO? _ownInitialModel; + + late Map _ownInitialRawValue = + ProductDetailsOForm.formElements(_ownInitialModel).rawValue; String descriptionControlPath() => pathBuilder(descriptionControlName); @@ -542,10 +551,28 @@ class ProductDetailsOForm

bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + ProductDetailsO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([ProductDetailsO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = ProductDetailsOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -584,55 +611,6 @@ class ProductDetailsOForm

emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -661,7 +639,8 @@ final _logIdOForm = Logger.detached('IdOForm'); class IdOForm

implements FormModel, IdOOutput> { - IdOForm(this.form, this.path, this._formModel) : initial = form.rawValue; + IdOForm(this.form, this.path, this._formModel, {IdO? initialModel}) + : _ownInitialModel = initialModel; static const String companyNameControlName = "companyName"; @@ -676,8 +655,11 @@ class IdOForm

final Map _disabled = {}; - @override - final Map initial; + IdO? _ownInitialModel; + + late Map _ownInitialRawValue = IdOForm.formElements( + _ownInitialModel, + ).rawValue; String companyNameControlPath() => pathBuilder(companyNameControlName); @@ -957,10 +939,26 @@ class IdOForm

bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + IdO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([IdO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = IdOForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -999,55 +997,6 @@ class IdOForm

emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1486,6 +1435,7 @@ class _IdOFormBuilderState

IdOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -1528,7 +1478,9 @@ class _IdOFormBuilderState

@override void didUpdateWidget(covariant IdOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); diff --git a/packages/reactive_forms_generator/example/lib/docs/profile/profile.gform.dart b/packages/reactive_forms_generator/example/lib/docs/profile/profile.gform.dart index d281b721..5b5d9c1e 100644 --- a/packages/reactive_forms_generator/example/lib/docs/profile/profile.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/profile/profile.gform.dart @@ -143,6 +143,7 @@ class _ProfileFormBuilderState extends State { ProfileForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -185,7 +186,9 @@ class _ProfileFormBuilderState extends State { @override void didUpdateWidget(covariant ProfileFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -220,7 +223,8 @@ class _ProfileFormBuilderState extends State { final _logProfileForm = Logger.detached('ProfileForm'); class ProfileForm implements FormModel { - ProfileForm(this.form, this.path, this._formModel) : initial = form.rawValue; + ProfileForm(this.form, this.path, this._formModel, {Profile? initialModel}) + : _ownInitialModel = initialModel; static const String idControlName = "id"; @@ -251,8 +255,11 @@ class ProfileForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + Profile? _ownInitialModel; + + late Map _ownInitialRawValue = ProfileForm.formElements( + _ownInitialModel, + ).rawValue; String idControlPath() => pathBuilder(idControlName); @@ -1152,10 +1159,26 @@ class ProfileForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + Profile? get initialModel { + return _ownInitialModel; + } + + void commitInitial([Profile? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = ProfileForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1194,55 +1217,6 @@ class ProfileForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1323,8 +1297,12 @@ final _logIncidenceFilterForm = Logger.detached('IncidenceFilterForm'); class IncidenceFilterForm implements FormModel { - IncidenceFilterForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + IncidenceFilterForm( + this.form, + this.path, + this._formModel, { + IncidenceFilter? initialModel, + }) : _ownInitialModel = initialModel; static const String isMobilityEnabledControlName = "isMobilityEnabled"; @@ -1347,8 +1325,10 @@ class IncidenceFilterForm final Map _disabled = {}; - @override - final Map initial; + IncidenceFilter? _ownInitialModel; + + late Map _ownInitialRawValue = + IncidenceFilterForm.formElements(_ownInitialModel).rawValue; String isMobilityEnabledControlPath() => pathBuilder(isMobilityEnabledControlName); @@ -1922,10 +1902,28 @@ class IncidenceFilterForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + IncidenceFilter? get initialModel { + return _ownInitialModel; + } + + void commitInitial([IncidenceFilter? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = IncidenceFilterForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1964,55 +1962,6 @@ class IncidenceFilterForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -2078,8 +2027,12 @@ final _logThresholdSettingForm = Logger.detached('ThresholdSettingForm'); class ThresholdSettingForm implements FormModel { - ThresholdSettingForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + ThresholdSettingForm( + this.form, + this.path, + this._formModel, { + ThresholdSetting? initialModel, + }) : _ownInitialModel = initialModel; static const String isEnabledControlName = "isEnabled"; @@ -2094,8 +2047,10 @@ class ThresholdSettingForm final Map _disabled = {}; - @override - final Map initial; + ThresholdSetting? _ownInitialModel; + + late Map _ownInitialRawValue = + ThresholdSettingForm.formElements(_ownInitialModel).rawValue; String isEnabledControlPath() => pathBuilder(isEnabledControlName); @@ -2329,10 +2284,28 @@ class ThresholdSettingForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + ThresholdSetting? get initialModel { + return _ownInitialModel; + } + + void commitInitial([ThresholdSetting? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = ThresholdSettingForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -2371,55 +2344,6 @@ class ThresholdSettingForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -2453,8 +2377,12 @@ class ThresholdSettingForm final _logTimerSettingForm = Logger.detached('TimerSettingForm'); class TimerSettingForm implements FormModel { - TimerSettingForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + TimerSettingForm( + this.form, + this.path, + this._formModel, { + TimerSetting? initialModel, + }) : _ownInitialModel = initialModel; static const String isEnabledControlName = "isEnabled"; @@ -2469,8 +2397,11 @@ class TimerSettingForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + TimerSetting? _ownInitialModel; + + late Map _ownInitialRawValue = TimerSettingForm.formElements( + _ownInitialModel, + ).rawValue; String isEnabledControlPath() => pathBuilder(isEnabledControlName); @@ -2701,10 +2632,28 @@ class TimerSettingForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + TimerSetting? get initialModel { + return _ownInitialModel; + } + + void commitInitial([TimerSetting? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = TimerSettingForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -2743,55 +2692,6 @@ class TimerSettingForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/profile/profile_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/profile/profile_output.gform.dart index 00d4c835..29d79317 100644 --- a/packages/reactive_forms_generator/example/lib/docs/profile/profile_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/profile/profile_output.gform.dart @@ -144,6 +144,7 @@ class _ProfileOFormBuilderState extends State { ProfileOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -186,7 +187,9 @@ class _ProfileOFormBuilderState extends State { @override void didUpdateWidget(covariant ProfileOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -221,7 +224,8 @@ class _ProfileOFormBuilderState extends State { final _logProfileOForm = Logger.detached('ProfileOForm'); class ProfileOForm implements FormModel { - ProfileOForm(this.form, this.path, this._formModel) : initial = form.rawValue; + ProfileOForm(this.form, this.path, this._formModel, {ProfileO? initialModel}) + : _ownInitialModel = initialModel; static const String idControlName = "id"; @@ -252,8 +256,11 @@ class ProfileOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + ProfileO? _ownInitialModel; + + late Map _ownInitialRawValue = ProfileOForm.formElements( + _ownInitialModel, + ).rawValue; String idControlPath() => pathBuilder(idControlName); @@ -1154,10 +1161,26 @@ class ProfileOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + ProfileO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([ProfileO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = ProfileOForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1196,55 +1219,6 @@ class ProfileOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1325,8 +1299,12 @@ final _logIncidenceFilterOForm = Logger.detached('IncidenceFilterOForm'); class IncidenceFilterOForm implements FormModel { - IncidenceFilterOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + IncidenceFilterOForm( + this.form, + this.path, + this._formModel, { + IncidenceFilterO? initialModel, + }) : _ownInitialModel = initialModel; static const String isMobilityEnabledControlName = "isMobilityEnabled"; @@ -1349,8 +1327,10 @@ class IncidenceFilterOForm final Map _disabled = {}; - @override - final Map initial; + IncidenceFilterO? _ownInitialModel; + + late Map _ownInitialRawValue = + IncidenceFilterOForm.formElements(_ownInitialModel).rawValue; String isMobilityEnabledControlPath() => pathBuilder(isMobilityEnabledControlName); @@ -1925,10 +1905,28 @@ class IncidenceFilterOForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + IncidenceFilterO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([IncidenceFilterO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = IncidenceFilterOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1967,55 +1965,6 @@ class IncidenceFilterOForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -2082,8 +2031,12 @@ final _logThresholdSettingOForm = Logger.detached('ThresholdSettingOForm'); class ThresholdSettingOForm implements FormModel { - ThresholdSettingOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + ThresholdSettingOForm( + this.form, + this.path, + this._formModel, { + ThresholdSettingO? initialModel, + }) : _ownInitialModel = initialModel; static const String isEnabledControlName = "isEnabled"; @@ -2098,8 +2051,10 @@ class ThresholdSettingOForm final Map _disabled = {}; - @override - final Map initial; + ThresholdSettingO? _ownInitialModel; + + late Map _ownInitialRawValue = + ThresholdSettingOForm.formElements(_ownInitialModel).rawValue; String isEnabledControlPath() => pathBuilder(isEnabledControlName); @@ -2337,10 +2292,28 @@ class ThresholdSettingOForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + ThresholdSettingO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([ThresholdSettingO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = ThresholdSettingOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -2379,55 +2352,6 @@ class ThresholdSettingOForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -2462,8 +2386,12 @@ final _logTimerSettingOForm = Logger.detached('TimerSettingOForm'); class TimerSettingOForm implements FormModel { - TimerSettingOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + TimerSettingOForm( + this.form, + this.path, + this._formModel, { + TimerSettingO? initialModel, + }) : _ownInitialModel = initialModel; static const String isEnabledControlName = "isEnabled"; @@ -2478,8 +2406,10 @@ class TimerSettingOForm final Map _disabled = {}; - @override - final Map initial; + TimerSettingO? _ownInitialModel; + + late Map _ownInitialRawValue = + TimerSettingOForm.formElements(_ownInitialModel).rawValue; String isEnabledControlPath() => pathBuilder(isEnabledControlName); @@ -2711,10 +2641,28 @@ class TimerSettingOForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + TimerSettingO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([TimerSettingO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = TimerSettingOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -2753,55 +2701,6 @@ class TimerSettingOForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/recursive/recursive.gform.dart b/packages/reactive_forms_generator/example/lib/docs/recursive/recursive.gform.dart index ae9d87e2..90c42837 100644 --- a/packages/reactive_forms_generator/example/lib/docs/recursive/recursive.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/recursive/recursive.gform.dart @@ -147,6 +147,7 @@ class _SecuredAreaFormBuilderState extends State { SecuredAreaForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -189,7 +190,9 @@ class _SecuredAreaFormBuilderState extends State { @override void didUpdateWidget(covariant SecuredAreaFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -224,8 +227,12 @@ class _SecuredAreaFormBuilderState extends State { final _logSecuredAreaForm = Logger.detached('SecuredAreaForm'); class SecuredAreaForm implements FormModel { - SecuredAreaForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + SecuredAreaForm( + this.form, + this.path, + this._formModel, { + SecuredArea? initialModel, + }) : _ownInitialModel = initialModel; static const String idControlName = "id"; @@ -244,8 +251,11 @@ class SecuredAreaForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + SecuredArea? _ownInitialModel; + + late Map _ownInitialRawValue = SecuredAreaForm.formElements( + _ownInitialModel, + ).rawValue; String idControlPath() => pathBuilder(idControlName); @@ -836,10 +846,28 @@ class SecuredAreaForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + SecuredArea? get initialModel { + return _ownInitialModel; + } + + void commitInitial([SecuredArea? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = SecuredAreaForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -878,55 +906,6 @@ class SecuredAreaForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -966,8 +945,12 @@ class SecuredAreaForm implements FormModel { final _logParcelSystemForm = Logger.detached('ParcelSystemForm'); class ParcelSystemForm implements FormModel { - ParcelSystemForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + ParcelSystemForm( + this.form, + this.path, + this._formModel, { + ParcelSystem? initialModel, + }) : _ownInitialModel = initialModel; static const String hasParcelSystemControlName = "hasParcelSystem"; @@ -982,8 +965,11 @@ class ParcelSystemForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + ParcelSystem? _ownInitialModel; + + late Map _ownInitialRawValue = ParcelSystemForm.formElements( + _ownInitialModel, + ).rawValue; String hasParcelSystemControlPath() => pathBuilder(hasParcelSystemControlName); @@ -1224,10 +1210,28 @@ class ParcelSystemForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + ParcelSystem? get initialModel { + return _ownInitialModel; + } + + void commitInitial([ParcelSystem? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = ParcelSystemForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1266,55 +1270,6 @@ class ParcelSystemForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -1341,8 +1296,12 @@ final _logParcelSystemDataForm = Logger.detached('ParcelSystemDataForm'); class ParcelSystemDataForm implements FormModel { - ParcelSystemDataForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + ParcelSystemDataForm( + this.form, + this.path, + this._formModel, { + ParcelSystemData? initialModel, + }) : _ownInitialModel = initialModel; static const String idControlName = "id"; @@ -1355,8 +1314,10 @@ class ParcelSystemDataForm final Map _disabled = {}; - @override - final Map initial; + ParcelSystemData? _ownInitialModel; + + late Map _ownInitialRawValue = + ParcelSystemDataForm.formElements(_ownInitialModel).rawValue; String idControlPath() => pathBuilder(idControlName); @@ -1529,10 +1490,28 @@ class ParcelSystemDataForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + ParcelSystemData? get initialModel { + return _ownInitialModel; + } + + void commitInitial([ParcelSystemData? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = ParcelSystemDataForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1571,55 +1550,6 @@ class ParcelSystemDataForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/renamed_basic/renamed_basic.gform.dart b/packages/reactive_forms_generator/example/lib/docs/renamed_basic/renamed_basic.gform.dart index b4496240..7c512cdb 100644 --- a/packages/reactive_forms_generator/example/lib/docs/renamed_basic/renamed_basic.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/renamed_basic/renamed_basic.gform.dart @@ -149,6 +149,7 @@ class _SomeWiredNameFormBuilderState extends State { SomeWiredNameForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -191,7 +192,9 @@ class _SomeWiredNameFormBuilderState extends State { @override void didUpdateWidget(covariant SomeWiredNameFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -226,8 +229,12 @@ class _SomeWiredNameFormBuilderState extends State { final _logSomeWiredNameForm = Logger.detached('SomeWiredNameForm'); class SomeWiredNameForm implements FormModel { - SomeWiredNameForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + SomeWiredNameForm( + this.form, + this.path, + this._formModel, { + RenamedBasic? initialModel, + }) : _ownInitialModel = initialModel; static const String emailControlName = "email"; @@ -242,8 +249,10 @@ class SomeWiredNameForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + RenamedBasic? _ownInitialModel; + + late Map _ownInitialRawValue = + SomeWiredNameForm.formElements(_ownInitialModel).rawValue; String emailControlPath() => pathBuilder(emailControlName); @@ -474,10 +483,28 @@ class SomeWiredNameForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + RenamedBasic? get initialModel { + return _ownInitialModel; + } + + void commitInitial([RenamedBasic? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = SomeWiredNameForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -516,55 +543,6 @@ class SomeWiredNameForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/renamed_basic/renamed_basic_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/renamed_basic/renamed_basic_output.gform.dart index f06e64ca..cfd48879 100644 --- a/packages/reactive_forms_generator/example/lib/docs/renamed_basic/renamed_basic_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/renamed_basic/renamed_basic_output.gform.dart @@ -149,6 +149,7 @@ class _SomeWiredNameFormBuilderState extends State { SomeWiredNameForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -191,7 +192,9 @@ class _SomeWiredNameFormBuilderState extends State { @override void didUpdateWidget(covariant SomeWiredNameFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -227,8 +230,12 @@ final _logSomeWiredNameForm = Logger.detached('SomeWiredNameForm'); class SomeWiredNameForm implements FormModel { - SomeWiredNameForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + SomeWiredNameForm( + this.form, + this.path, + this._formModel, { + RenamedBasicO? initialModel, + }) : _ownInitialModel = initialModel; static const String emailControlName = "email"; @@ -243,8 +250,10 @@ class SomeWiredNameForm final Map _disabled = {}; - @override - final Map initial; + RenamedBasicO? _ownInitialModel; + + late Map _ownInitialRawValue = + SomeWiredNameForm.formElements(_ownInitialModel).rawValue; String emailControlPath() => pathBuilder(emailControlName); @@ -523,10 +532,28 @@ class SomeWiredNameForm bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + RenamedBasicO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([RenamedBasicO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = SomeWiredNameForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -565,55 +592,6 @@ class SomeWiredNameForm emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/user_profile/user_profile.gform.dart b/packages/reactive_forms_generator/example/lib/docs/user_profile/user_profile.gform.dart index e6fc5097..d801a2e9 100644 --- a/packages/reactive_forms_generator/example/lib/docs/user_profile/user_profile.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/user_profile/user_profile.gform.dart @@ -147,6 +147,7 @@ class _UserProfileFormBuilderState extends State { UserProfileForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -189,7 +190,9 @@ class _UserProfileFormBuilderState extends State { @override void didUpdateWidget(covariant UserProfileFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -224,8 +227,12 @@ class _UserProfileFormBuilderState extends State { final _logUserProfileForm = Logger.detached('UserProfileForm'); class UserProfileForm implements FormModel { - UserProfileForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + UserProfileForm( + this.form, + this.path, + this._formModel, { + UserProfile? initialModel, + }) : _ownInitialModel = initialModel; static const String idControlName = "id"; @@ -246,8 +253,11 @@ class UserProfileForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + UserProfile? _ownInitialModel; + + late Map _ownInitialRawValue = UserProfileForm.formElements( + _ownInitialModel, + ).rawValue; String idControlPath() => pathBuilder(idControlName); @@ -748,10 +758,28 @@ class UserProfileForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + UserProfile? get initialModel { + return _ownInitialModel; + } + + void commitInitial([UserProfile? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = UserProfileForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -790,55 +818,6 @@ class UserProfileForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -881,7 +860,8 @@ class UserProfileForm implements FormModel { final _logAddressForm = Logger.detached('AddressForm'); class AddressForm implements FormModel { - AddressForm(this.form, this.path, this._formModel) : initial = form.rawValue; + AddressForm(this.form, this.path, this._formModel, {Address? initialModel}) + : _ownInitialModel = initialModel; static const String streetControlName = "street"; @@ -898,8 +878,11 @@ class AddressForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + Address? _ownInitialModel; + + late Map _ownInitialRawValue = AddressForm.formElements( + _ownInitialModel, + ).rawValue; String streetControlPath() => pathBuilder(streetControlName); @@ -1281,10 +1264,26 @@ class AddressForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + Address? get initialModel { + return _ownInitialModel; + } + + void commitInitial([Address? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = AddressForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1323,55 +1322,6 @@ class AddressForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/docs/user_profile/user_profile_output.gform.dart b/packages/reactive_forms_generator/example/lib/docs/user_profile/user_profile_output.gform.dart index 0820b57e..53f92241 100644 --- a/packages/reactive_forms_generator/example/lib/docs/user_profile/user_profile_output.gform.dart +++ b/packages/reactive_forms_generator/example/lib/docs/user_profile/user_profile_output.gform.dart @@ -149,6 +149,7 @@ class _UserProfileOFormBuilderState extends State { UserProfileOForm.formElements(widget.model), null, null, + initialModel: widget.model, ); if (_formModel.form.disabled) { @@ -191,7 +192,9 @@ class _UserProfileOFormBuilderState extends State { @override void didUpdateWidget(covariant UserProfileOFormBuilder oldWidget) { if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } super.didUpdateWidget(oldWidget); @@ -226,8 +229,12 @@ class _UserProfileOFormBuilderState extends State { final _logUserProfileOForm = Logger.detached('UserProfileOForm'); class UserProfileOForm implements FormModel { - UserProfileOForm(this.form, this.path, this._formModel) - : initial = form.rawValue; + UserProfileOForm( + this.form, + this.path, + this._formModel, { + UserProfileO? initialModel, + }) : _ownInitialModel = initialModel; static const String idControlName = "id"; @@ -248,8 +255,11 @@ class UserProfileOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + UserProfileO? _ownInitialModel; + + late Map _ownInitialRawValue = UserProfileOForm.formElements( + _ownInitialModel, + ).rawValue; String idControlPath() => pathBuilder(idControlName); @@ -799,10 +809,28 @@ class UserProfileOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + UserProfileO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([UserProfileO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = UserProfileOForm.formElements( + _ownInitialModel, + ).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -841,55 +869,6 @@ class UserProfileOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); @@ -932,7 +911,8 @@ class UserProfileOForm implements FormModel { final _logAddressOForm = Logger.detached('AddressOForm'); class AddressOForm implements FormModel { - AddressOForm(this.form, this.path, this._formModel) : initial = form.rawValue; + AddressOForm(this.form, this.path, this._formModel, {AddressO? initialModel}) + : _ownInitialModel = initialModel; static const String streetControlName = "street"; @@ -949,8 +929,11 @@ class AddressOForm implements FormModel { final Map _disabled = {}; - @override - final Map initial; + AddressO? _ownInitialModel; + + late Map _ownInitialRawValue = AddressOForm.formElements( + _ownInitialModel, + ).rawValue; String streetControlPath() => pathBuilder(streetControlName); @@ -1337,10 +1320,26 @@ class AddressOForm implements FormModel { bool get hasChanged { return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); } + @override + Map get initialRawValue { + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + } + + AddressO? get initialModel { + return _ownInitialModel; + } + + void commitInitial([AddressO? newModel]) { + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = AddressOForm.formElements(_ownInitialModel).rawValue; + } + @override FormGroup get currentForm { return path == null ? form : form.control(path!) as FormGroup; @@ -1379,55 +1378,6 @@ class AddressOForm implements FormModel { emitEvent: emitEvent, ); - @override - void updateInitial(Map? value, String? path) { - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } - } - String pathBuilder(String? pathItem) => [path, pathItem].whereType().join("."); diff --git a/packages/reactive_forms_generator/example/lib/widgets/dirty_badge.dart b/packages/reactive_forms_generator/example/lib/widgets/dirty_badge.dart new file mode 100644 index 00000000..743e9b1b --- /dev/null +++ b/packages/reactive_forms_generator/example/lib/widgets/dirty_badge.dart @@ -0,0 +1,28 @@ +import 'package:flutter/material.dart'; + +class DirtyBadge extends StatelessWidget { + const DirtyBadge({super.key, required this.changed, this.label}); + + final bool changed; + final String? label; + + @override + Widget build(BuildContext context) { + final color = changed ? Colors.orange.shade700 : Colors.grey.shade400; + final label = this.label; + return Row( + mainAxisSize: MainAxisSize.min, + children: [ + Icon( + changed ? Icons.circle : Icons.circle_outlined, + size: 10, + color: color, + ), + if (label != null) ...[ + const SizedBox(width: 4), + Text(label, style: TextStyle(color: color, fontSize: 11)), + ], + ], + ); + } +} diff --git a/packages/reactive_forms_generator/example/pubspec.yaml b/packages/reactive_forms_generator/example/pubspec.yaml index f439a447..6ebcbfeb 100644 --- a/packages/reactive_forms_generator/example/pubspec.yaml +++ b/packages/reactive_forms_generator/example/pubspec.yaml @@ -4,7 +4,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev version: 1.0.0+1 environment: - sdk: ^3.8.0 + sdk: ^3.9.0 flutter: ">=3.7.0" resolution: workspace @@ -30,4 +30,4 @@ dev_dependencies: build_runner: ^2.7.0 freezed: ^3.2.0 json_serializable: ^6.9.5 - flutter_lints: ^6.0.0 \ No newline at end of file + flutter_lints: ^6.0.0 diff --git a/packages/reactive_forms_generator/example/test/doc/delivery_list/delivery_list_dirty_test.dart b/packages/reactive_forms_generator/example/test/doc/delivery_list/delivery_list_dirty_test.dart new file mode 100644 index 00000000..66dadc00 --- /dev/null +++ b/packages/reactive_forms_generator/example/test/doc/delivery_list/delivery_list_dirty_test.dart @@ -0,0 +1,215 @@ +import 'package:example/docs/delivery_list/delivery_list.dart'; +import 'package:flutter_test/flutter_test.dart'; + +DeliveryListForm buildForm(DeliveryList model) => DeliveryListForm( + DeliveryListForm.formElements(model), + null, + null, + initialModel: model, +); + +const seed = DeliveryList( + deliveryList: [ + DeliveryPoint(name: 'n1', address: Address(street: 's1', city: 'c1')), + DeliveryPoint(name: 'n2', address: Address(street: 's2', city: 'c2')), + ], + clientList: [], +); + +void main() { + group('DeliveryList hasChanged semantics', () { + test('fresh form is clean at every level', () { + final f = buildForm(seed); + + expect(f.hasChanged, isFalse); + expect(f.deliveryListDeliveryPointForm[0].hasChanged, isFalse); + expect( + f.deliveryListDeliveryPointForm[0].addressForm.hasChanged, + isFalse, + ); + expect(f.deliveryListDeliveryPointForm[1].hasChanged, isFalse); + }); + + test('editing a nested street flips root + item + address', () { + final f = buildForm(seed); + + f.deliveryListDeliveryPointForm[0].addressForm.streetControl.updateValue( + 'EDITED', + ); + + expect(f.hasChanged, isTrue); + expect(f.deliveryListDeliveryPointForm[0].hasChanged, isTrue); + expect( + f.deliveryListDeliveryPointForm[0].addressForm.hasChanged, + isTrue, + ); + expect(f.deliveryListDeliveryPointForm[1].hasChanged, isFalse); + expect( + f.deliveryListDeliveryPointForm[1].addressForm.hasChanged, + isFalse, + ); + }); + + test('restoring the edited value returns to clean', () { + final f = buildForm(seed); + final address = f.deliveryListDeliveryPointForm[0].addressForm; + + address.streetControl.updateValue('EDITED'); + expect(f.hasChanged, isTrue); + + address.streetControl.updateValue('s1'); + expect(f.hasChanged, isFalse); + expect(address.hasChanged, isFalse); + }); + + test('clearing a nullable String control (null seed) returns to clean', () { + final f = buildForm( + const DeliveryList( + deliveryList: [DeliveryPoint(name: 'x')], // address null + ), + ); + final address = f.deliveryListDeliveryPointForm[0].addressForm; + + expect(f.hasChanged, isFalse); + + address.streetControl.updateValue('X'); + expect(f.hasChanged, isTrue); + + address.streetControl.updateValue(null); + expect( + f.hasChanged, + isFalse, + reason: + 'restoring a nullable String to its seeded null must flip back ' + 'to clean', + ); + }); + + test('toggling disabled is not a value change', () { + final f = buildForm(seed); + + f.deliveryListDeliveryPointForm[0].toggleDisabled(); + expect(f.hasChanged, isFalse); + + f.deliveryListDeliveryPointForm[0].toggleDisabled(); + expect(f.hasChanged, isFalse); + }); + + test('adding an item flips root dirty (structural change)', () { + final f = buildForm(seed); + + f.addDeliveryListItem( + const DeliveryPoint( + name: 'n3', + address: Address(street: 's3', city: 'c3'), + ), + ); + + expect(f.hasChanged, isTrue); + // The two pre-existing items must stay clean. + expect(f.deliveryListDeliveryPointForm[0].hasChanged, isFalse); + expect(f.deliveryListDeliveryPointForm[1].hasChanged, isFalse); + // The new item has no baseline slice, so it is dirty. + expect(f.deliveryListDeliveryPointForm[2].hasChanged, isTrue); + }); + + test('adding 3 empty items flips root dirty', () { + final f = buildForm(seed); + + for (var i = 0; i < 3; i++) { + f.addDeliveryListItem(const DeliveryPoint()); + } + + expect(f.hasChanged, isTrue); + }); + + test('removing the last item flips root dirty', () { + final f = buildForm(seed); + + f.removeDeliveryListItemAtIndex(1); + + expect(f.hasChanged, isTrue); + expect(f.deliveryListDeliveryPointForm[0].hasChanged, isFalse); + }); + + test('commitInitial after edit returns everything to clean', () { + final f = buildForm(seed); + + f.deliveryListDeliveryPointForm[0].nameControl.updateValue('edited'); + f.addDeliveryListItem( + const DeliveryPoint(name: 'n3', address: Address(street: 's3')), + ); + + expect(f.hasChanged, isTrue); + + f.commitInitial(); + + expect(f.hasChanged, isFalse); + expect(f.deliveryListDeliveryPointForm[0].hasChanged, isFalse); + expect(f.deliveryListDeliveryPointForm[1].hasChanged, isFalse); + expect(f.deliveryListDeliveryPointForm[2].hasChanged, isFalse); + }); + + test('equalsTo at root compares against arbitrary model', () { + final f = buildForm(seed); + + expect(f.equalsTo(seed), isTrue); + expect( + f.equalsTo( + const DeliveryList( + deliveryList: [DeliveryPoint(name: 'other')], + clientList: [], + ), + ), + isFalse, + ); + }); + + test('equalsTo on a nested item compares only its slice', () { + final f = buildForm(seed); + final i0 = f.deliveryListDeliveryPointForm[0]; + + expect( + i0.equalsTo( + const DeliveryPoint( + name: 'n1', + address: Address(street: 's1', city: 'c1'), + ), + ), + isTrue, + ); + + f.deliveryListDeliveryPointForm[0].nameControl.updateValue('mutated'); + // Re-read nested form after mutation. + final i0After = f.deliveryListDeliveryPointForm[0]; + expect( + i0After.equalsTo( + const DeliveryPoint( + name: 'n1', + address: Address(street: 's1', city: 'c1'), + ), + ), + isFalse, + ); + }); + + test('initialModel getter exposes the typed baseline on the root', () { + final f = buildForm(seed); + expect(f.initialModel, equals(seed)); + + f.deliveryListDeliveryPointForm[0].nameControl.updateValue('mutated'); + expect( + f.initialModel, + equals(seed), + reason: 'initialModel must stay frozen across edits', + ); + + f.commitInitial(); + expect( + f.initialModel?.deliveryList.first.name, + equals('mutated'), + reason: 'commitInitial must adopt the current rawModel', + ); + }); + }); +} diff --git a/packages/reactive_forms_generator/lib/src/extensions.dart b/packages/reactive_forms_generator/lib/src/extensions.dart index 2485191d..131f7cea 100644 --- a/packages/reactive_forms_generator/lib/src/extensions.dart +++ b/packages/reactive_forms_generator/lib/src/extensions.dart @@ -176,10 +176,15 @@ extension ParameterElementExt on FormalParameterElement { final typeParameter = typeArguments.first; - return (typeParameter.element is ClassElement || - typeParameter.element is EnumElement || - typeParameter.element is TypeDefiningElement) && - !typeParameter.element!.hasRfGroupAnnotation; + final element = typeParameter.element; + + return element != null && + (element is ClassElement || + element is EnumElement || + element is TypeAliasElement || + element is TypeParameterElement || + element is ExtensionTypeElement) && + !element.hasRfGroupAnnotation; } bool get isFormControl { diff --git a/packages/reactive_forms_generator/lib/src/form_generator.dart b/packages/reactive_forms_generator/lib/src/form_generator.dart index 6b2226cc..16068404 100644 --- a/packages/reactive_forms_generator/lib/src/form_generator.dart +++ b/packages/reactive_forms_generator/lib/src/form_generator.dart @@ -638,8 +638,8 @@ class FormGenerator { Code get logging => Code("final $log = Logger.detached('$classNameFull');"); - Constructor get _constructor => Constructor( - (b) => b + Constructor get _constructor => Constructor((b) { + b ..requiredParameters.addAll([ Parameter( (b) => b @@ -657,8 +657,16 @@ class FormGenerator { ..toThis = true, ), ]) - ..initializers.add(const Code('initial = form.rawValue')), - ); + ..optionalParameters.add( + Parameter( + (p) => p + ..name = 'initialModel' + ..named = true + ..type = Reference('${element.fullTypeName}?'), + ), + ) + ..initializers.add(const Code('_ownInitialModel = initialModel')); + }); String get _modelDisplayTypeNonNullable { String displayType = @@ -799,9 +807,16 @@ class FormGenerator { ), Field( (b) => b - ..name = 'initial' - ..annotations.add(const CodeExpression(Code('override'))) - ..modifier = FieldModifier.final$ + ..name = '_ownInitialModel' + ..type = Reference('${element.fullTypeName}?'), + ), + Field( + (b) => b + ..name = '_ownInitialRawValue' + ..late = true + ..assignment = Code( + '$className.formElements(_ownInitialModel).rawValue', + ) ..type = const Reference('Map'), ), ]) @@ -843,80 +858,50 @@ class FormGenerator { ..body = const Code(''' return !const DeepCollectionEquality().equals( currentForm.rawValue, - initial, + FormModel.sliceByPath(initialRawValue, path), ); '''), ), - currentFormMethod, - updateValueMethod, - upsertValueMethod, - resetMethod, Method( (b) => b - ..name = 'updateInitial' - ..lambda = false + ..name = 'initialRawValue' + ..type = MethodType.getter ..annotations.add(const CodeExpression(Code('override'))) - ..requiredParameters.addAll([ - Parameter( - (b) => b - ..name = 'value' - ..type = const Reference('Map?'), - ), + ..returns = const Reference('Map') + ..body = const Code(''' + return _formModel != null + ? _formModel!.initialRawValue + : _ownInitialRawValue; + '''), + ), + Method( + (b) => b + ..name = 'initialModel' + ..type = MethodType.getter + ..returns = Reference('${element.fullTypeName}?') + ..body = const Code('return _ownInitialModel;'), + ), + Method( + (b) => b + ..name = 'commitInitial' + ..returns = const Reference('void') + ..optionalParameters.add( Parameter( - (b) => b - ..name = 'path' - ..type = const Reference('String?'), + (p) => p + ..name = 'newModel' + ..type = Reference('${element.fullTypeName}?'), ), - ]) - ..returns = const Reference('void') - ..body = const Code(''' - if (_formModel != null) { - _formModel?.updateInitial(currentForm.rawValue, path); - return; - } - - if (value == null) return; - - if (path == null || path.isEmpty) { - initial.addAll(value); - return; - } - - final keys = path.split('.'); - Object? current = initial; - for (var i = 0; i < keys.length - 1; i++) { - final key = keys[i]; - - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current = current[index]; - continue; - } - } - - if (current is Map) { - if (!current.containsKey(key)) { - current[key] = {}; - } - current = current[key]; - continue; - } - - return; - } - - final key = keys.last; - if (current is List) { - final index = int.tryParse(key); - if (index != null && index >= 0 && index < current.length) { - current[index] = value; - } - } else if (current is Map) { - current[key] = value; - } + ) + ..body = Code(''' + _ownInitialModel = newModel ?? rawModel; + _ownInitialRawValue = + $className.formElements(_ownInitialModel).rawValue; '''), ), + currentFormMethod, + updateValueMethod, + upsertValueMethod, + resetMethod, Method( (b) => b ..name = 'pathBuilder' diff --git a/packages/reactive_forms_generator/lib/src/output/rf_annotation_arguments_visitor.dart b/packages/reactive_forms_generator/lib/src/output/rf_annotation_arguments_visitor.dart index de4c9317..dbb39cfe 100644 --- a/packages/reactive_forms_generator/lib/src/output/rf_annotation_arguments_visitor.dart +++ b/packages/reactive_forms_generator/lib/src/output/rf_annotation_arguments_visitor.dart @@ -49,7 +49,6 @@ class ClassRenameVisitor extends GeneralizingAstVisitor { .toList(), augmentKeyword: node.augmentKeyword, abstractKeyword: node.abstractKeyword, - macroKeyword: node.macroKeyword, sealedKeyword: node.sealedKeyword, baseKeyword: node.baseKeyword, interfaceKeyword: node.interfaceKeyword, @@ -57,6 +56,7 @@ class ClassRenameVisitor extends GeneralizingAstVisitor { mixinKeyword: node.mixinKeyword, classKeyword: node.classKeyword, name: StringToken(TokenType.STRING, '${node.name.lexeme}Output', 0), + namePart: ClassNamePartImplStub(), typeParameters: node.typeParameters, extendsClause: node.extendsClause, withClause: node.withClause != null @@ -78,6 +78,7 @@ class ClassRenameVisitor extends GeneralizingAstVisitor { : null, implementsClause: node.implementsClause, nativeClause: node.nativeClause, + body: ClassBodyImplStub(), leftBracket: node.leftBracket, members: node.members.map((e) { return switch (e) { diff --git a/packages/reactive_forms_generator/lib/src/output/x.dart b/packages/reactive_forms_generator/lib/src/output/x.dart index 11a54549..ee373932 100644 --- a/packages/reactive_forms_generator/lib/src/output/x.dart +++ b/packages/reactive_forms_generator/lib/src/output/x.dart @@ -275,12 +275,7 @@ class ElementDisplayStringBuilder2 extends ElementDisplayStringBuilder { _writeTypeParameters(element.typeParameters); _write(' = '); - var aliasedElement = element.aliasedElement; - if (aliasedElement != null) { - aliasedElement.appendTo(this); - } else { - _writeType(element.aliasedType); - } + _writeType(element.aliasedType); } void writeTypeParameter(TypeParameterElement element) { diff --git a/packages/reactive_forms_generator/lib/src/reactive_forms/reactive_form_builder.dart b/packages/reactive_forms_generator/lib/src/reactive_forms/reactive_form_builder.dart index a4b05eb2..f9302ceb 100644 --- a/packages/reactive_forms_generator/lib/src/reactive_forms/reactive_form_builder.dart +++ b/packages/reactive_forms_generator/lib/src/reactive_forms/reactive_form_builder.dart @@ -158,7 +158,7 @@ class ReactiveFormBuilder { ..annotations.add(const CodeExpression(Code('override'))) ..returns = const Reference('void') ..body = Code(''' - _formModel = ${reactiveForm.reactiveInheritedStreamer.formGenerator.classNameFull}(${reactiveForm.reactiveInheritedStreamer.formGenerator.className}.formElements${reactiveForm.reactiveInheritedStreamer.formGenerator.element.generics}(widget.model), null, null); + _formModel = ${reactiveForm.reactiveInheritedStreamer.formGenerator.classNameFull}(${reactiveForm.reactiveInheritedStreamer.formGenerator.className}.formElements${reactiveForm.reactiveInheritedStreamer.formGenerator.element.generics}(widget.model), null, null, initialModel: widget.model); if (_formModel.form.disabled) { _formModel.form.markAsDisabled(); @@ -212,9 +212,11 @@ class ReactiveFormBuilder { ) ..body = const Code(''' if (widget.model != oldWidget.model) { - _formModel.updateValue(widget.model); + _formModel + ..updateValue(widget.model) + ..commitInitial(widget.model); } - + super.didUpdateWidget(oldWidget); '''), ), diff --git a/packages/reactive_forms_generator/lib/src/types.dart b/packages/reactive_forms_generator/lib/src/types.dart index 3175cee1..0b8676fd 100644 --- a/packages/reactive_forms_generator/lib/src/types.dart +++ b/packages/reactive_forms_generator/lib/src/types.dart @@ -78,40 +78,6 @@ extension NodeListImplAnnotationImplExt on NodeListImpl { } } -extension on Annotatable { - Map annotationParams1(TypeChecker? typeChecker) { - final result = {}; - final annotation = typeChecker?.firstAnnotationOf(this); - try { - if (annotation != null) { - for (final meta in metadata.annotations) { - final obj = meta.computeConstantValue()!; - - final isExactlyType = typeChecker?.isExactlyType(obj.type!) ?? false; - if (isExactlyType) { - final argumentList = - (meta as ElementAnnotationImpl).annotationAst.arguments - as ArgumentListImpl; - for (var argument in argumentList.arguments) { - final argumentNamedExpression = argument as NamedExpressionImpl; - result.addEntries([ - MapEntry( - argumentNamedExpression.name.label.toSource(), - argumentNamedExpression.expression.toSource(), - ), - ]); - } - } - } - } - - return result; - } catch (e) { - return result; - } - } -} - // extension FormalParameterElementExt on FormalParameterElement { // Map annotationParams(TypeChecker? typeChecker) { // final result = {}; @@ -168,11 +134,34 @@ extension ElementRfExt on Element { Map annotationParams(TypeChecker? typeChecker) { final result = {}; - if (this is Annotatable) { - return (this as Annotatable).annotationParams1(typeChecker); - } + final annotation = typeChecker?.firstAnnotationOf(this); + try { + if (annotation != null) { + for (final meta in metadata.annotations) { + final obj = meta.computeConstantValue()!; + + final isExactlyType = typeChecker?.isExactlyType(obj.type!) ?? false; + if (isExactlyType) { + final argumentList = + (meta as ElementAnnotationImpl).annotationAst.arguments + as ArgumentListImpl; + for (var argument in argumentList.arguments) { + final argumentNamedExpression = argument as NamedExpressionImpl; + result.addEntries([ + MapEntry( + argumentNamedExpression.name.label.toSource(), + argumentNamedExpression.expression.toSource(), + ), + ]); + } + } + } + } - return result; + return result; + } catch (e) { + return result; + } // final annotation = typeChecker?.firstAnnotationOf(this); // try { // if (annotation != null) { diff --git a/packages/reactive_forms_generator/pubspec.yaml b/packages/reactive_forms_generator/pubspec.yaml index a17ddf77..49f5ce0a 100644 --- a/packages/reactive_forms_generator/pubspec.yaml +++ b/packages/reactive_forms_generator/pubspec.yaml @@ -2,17 +2,17 @@ name: reactive_forms_generator description: Generator for reactive_forms. Generates form classes based on model. repository: https://github.com/artflutter/reactive_forms_generator -version: 7.5.0-beta1 +version: 7.5.0-beta9 environment: - sdk: ">=3.8.0 <4.0.0" + sdk: ">=3.9.0 <4.0.0" resolution: workspace dependencies: build: ^4.0.1 source_gen: ^4.0.1 - _fe_analyzer_shared: ^89.0.0 - analyzer: ^8.2.0 + _fe_analyzer_shared: ^92.0.0 + analyzer: ^9.0.0 path: ^1.8.1 build_runner: ^2.7.0 code_builder: ^4.10.1 @@ -28,4 +28,4 @@ dev_dependencies: #dependency_overrides: # build_resolvers: ^2.1.0 # test_api: ^0.4.18 -# source_helper: ^1.3.3 \ No newline at end of file +# source_helper: ^1.3.3 diff --git a/pubspec.lock b/pubspec.lock index ded7be08..ef9eb8f7 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -5,18 +5,18 @@ packages: dependency: "direct overridden" description: name: _fe_analyzer_shared - sha256: dd3d2ad434b9510001d089e8de7556d50c834481b9abc2891a0184a8493a19dc + sha256: "5b7468c326d2f8a4f630056404ca0d291ade42918f4a3c6233618e724f39da8e" url: "https://pub.dev" source: hosted - version: "89.0.0" + version: "92.0.0" analyzer: dependency: "direct overridden" description: name: analyzer - sha256: c22b6e7726d1f9e5db58c7251606076a71ca0dbcf76116675edfadbec0c9e875 + sha256: "70e4b1ef8003c64793a9e268a551a82869a8a96f39deb73dea28084b0e8bf75e" url: "https://pub.dev" source: hosted - version: "8.2.0" + version: "9.0.0" ansi_styles: dependency: transitive description: @@ -109,10 +109,10 @@ packages: dependency: transitive description: name: characters - sha256: f71061c654a3380576a52b451dd5532377954cf9dbd272a78fc8479606670803 + sha256: faf38497bda5ead2a8c7615f4f7939df04333478bf32e4173fcb06d428b5716b url: "https://pub.dev" source: hosted - version: "1.4.0" + version: "1.4.1" charcode: dependency: transitive description: @@ -376,14 +376,6 @@ packages: url: "https://pub.dev" source: hosted version: "1.0.5" - js: - dependency: transitive - description: - name: js - sha256: "53385261521cc4a0c4658fd0ad07a7d14591cf8fc33abbceae306ddb974888dc" - url: "https://pub.dev" - source: hosted - version: "0.7.2" json_annotation: dependency: transitive description: @@ -444,18 +436,18 @@ packages: dependency: transitive description: name: matcher - sha256: dc58c723c3c24bf8d3e2d3ad3f2f9d7bd9cf43ec6feaa64181775e60190153f2 + sha256: dc0b7dc7651697ea4ff3e69ef44b0407ea32c487a39fff6a4004fa585e901861 url: "https://pub.dev" source: hosted - version: "0.12.17" + version: "0.12.19" material_color_utilities: dependency: transitive description: name: material_color_utilities - sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec + sha256: "9c337007e82b1889149c82ed242ed1cb24a66044e30979c44912381e9be4c48b" url: "https://pub.dev" source: hosted - version: "0.11.1" + version: "0.13.0" melos: dependency: "direct dev" description: @@ -729,26 +721,26 @@ packages: dependency: transitive description: name: test - sha256: "75906bf273541b676716d1ca7627a17e4c4070a3a16272b7a3dc7da3b9f3f6b7" + sha256: "280d6d890011ca966ad08df7e8a4ddfab0fb3aa49f96ed6de56e3521347a9ae7" url: "https://pub.dev" source: hosted - version: "1.26.3" + version: "1.30.0" test_api: - dependency: "direct overridden" + dependency: transitive description: name: test_api - sha256: fb31f383e2ee25fbbfe06b40fe21e1e458d14080e3c67e7ba0acfde4df4e0bbd + sha256: "8161c84903fd860b26bfdefb7963b3f0b68fee7adea0f59ef805ecca346f0c7a" url: "https://pub.dev" source: hosted - version: "0.7.4" + version: "0.7.10" test_core: - dependency: "direct overridden" + dependency: transitive description: name: test_core - sha256: "84d17c3486c8dfdbe5e12a50c8ae176d15e2a771b96909a9442b40173649ccaa" + sha256: "0381bd1585d1a924763c308100f2138205252fb90c9d4eeaf28489ee65ccde51" url: "https://pub.dev" source: hosted - version: "0.6.8" + version: "0.6.16" typed_data: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 968298c5..14ab9a27 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -7,10 +7,7 @@ workspace: - packages/reactive_forms_generator/example environment: - sdk: ">=3.8.0 <4.0.0" - -dependency_overrides: - test_api: 0.7.4 + sdk: ">=3.9.0 <4.0.0" dev_dependencies: build_runner: ^2.4.15