From 7d15b30e39b95b71dd8a72800ea7677d3feb4dc8 Mon Sep 17 00:00:00 2001 From: Petr Nymsa Date: Thu, 2 Jan 2025 22:45:46 +0100 Subject: [PATCH 1/7] Fix onDependencyChange logic --- glade_forms/lib/src/model/glade_model.dart | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/glade_forms/lib/src/model/glade_model.dart b/glade_forms/lib/src/model/glade_model.dart index 2777699..5c0f493 100644 --- a/glade_forms/lib/src/model/glade_model.dart +++ b/glade_forms/lib/src/model/glade_model.dart @@ -122,9 +122,10 @@ abstract class GladeModel extends ChangeNotifier { } void notifyDependencies() { - final updatedKeys = _lastUpdates.map((e) => e.inputKey); + final updatedKeys = _lastUpdates.map((e) => e.inputKey).toSet(); for (final input in inputs) { - final union = input.dependencies.map((e) => e.inputKey).toSet().union(updatedKeys.toSet()); + final updatedKeysExceptInputItself = updatedKeys.toSet().difference({input.inputKey}); + final union = input.dependencies.map((e) => e.inputKey).toSet().intersection(updatedKeysExceptInputItself); if (union.isNotEmpty) input.onDependencyChange?.call(union.toList()); } From fca35d58dc7ed0077bd7f3db1e6b098acf1daaf7 Mon Sep 17 00:00:00 2001 From: Petr Nymsa Date: Thu, 2 Jan 2025 22:46:01 +0100 Subject: [PATCH 2/7] Add better example how to solve cyclic dependencies --- .../onchange/two_way_checkbox_change.dart | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/storybook/lib/usecases/onchange/two_way_checkbox_change.dart b/storybook/lib/usecases/onchange/two_way_checkbox_change.dart index 28de5f5..408efaf 100644 --- a/storybook/lib/usecases/onchange/two_way_checkbox_change.dart +++ b/storybook/lib/usecases/onchange/two_way_checkbox_change.dart @@ -51,9 +51,11 @@ class AgeRestrictedModel extends GladeModel { return devMessage; }, - onChange: (info) { - vipInput.value = info.value >= 18; - }, + // * cyclic dependency between ageInput and vipInput + // Better to use onDependencyChange inside vipInput + // onChange: (info) { + // vipInput.value = info.value >= 18; + // }, useTextEditingController: true, ); vipInput = GladeInput.create( @@ -61,9 +63,15 @@ class AgeRestrictedModel extends GladeModel { value: false, inputKey: 'vip-input', dependencies: () => [ageInput], + onDependencyChange: (updateInputKeys) { + print('onDependencyChange: $updateInputKeys'); + if (updateInputKeys.contains('age-input')) { + vipInput.updateValue(ageInput.value >= 18, shouldTriggerOnChange: false); + } + }, onChange: (info) { if (info.value && ageInput.value < 18) { - ageInput.value = 18; + ageInput.updateValue(18, shouldTriggerOnChange: false); } }, ); From 79d71c052be704c0405d526178fe1e2f4b85761b Mon Sep 17 00:00:00 2001 From: Petr Nymsa Date: Thu, 2 Jan 2025 22:46:15 +0100 Subject: [PATCH 3/7] Override GladeInput toString method --- glade_forms/lib/src/core/glade_input.dart | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/glade_forms/lib/src/core/glade_input.dart b/glade_forms/lib/src/core/glade_input.dart index 7648d86..6a66363 100644 --- a/glade_forms/lib/src/core/glade_input.dart +++ b/glade_forms/lib/src/core/glade_input.dart @@ -628,6 +628,11 @@ class GladeInput { _textEditingController?.removeListener(_onTextControllerChange); } + @override + String toString() { + return '$inputKey ($value)'; + } + void _syncValueWithController(T value, {required bool shouldTriggerOnChange}) { final converter = stringToValueConverter ?? _defaultConverter; try { From 688d4785baabdde70af4e2cecab1890519d1c7b2 Mon Sep 17 00:00:00 2001 From: Petr Nymsa Date: Thu, 2 Jan 2025 22:46:28 +0100 Subject: [PATCH 4/7] Update changelog and version --- glade_forms/CHANGELOG.md | 6 ++++++ glade_forms/pubspec.yaml | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/glade_forms/CHANGELOG.md b/glade_forms/CHANGELOG.md index b6ba021..ba8d55e 100644 --- a/glade_forms/CHANGELOG.md +++ b/glade_forms/CHANGELOG.md @@ -1,5 +1,11 @@ +# 3.0.1 +- **[Fix]**: GladeFormProvider is missing key property [Fix 73](https://github.com/netglade/glade_forms/issues/73) +- **[Fix]**: enable value transform with text editing controller [Fix 72](https://github.com/netglade/glade_forms/issues/72) +- **[Fix]**: Input subscribed to its own changes in onDependencyChange [Fix 76](https://github.com/netglade/glade_forms/issues/76) + ## 3.0.0 + **Breaking release** - **[Add]**: Add `allowBlank` parameter to `isEmpty` string validator. diff --git a/glade_forms/pubspec.yaml b/glade_forms/pubspec.yaml index 8023bf0..4888a93 100644 --- a/glade_forms/pubspec.yaml +++ b/glade_forms/pubspec.yaml @@ -1,6 +1,6 @@ name: glade_forms description: A universal way to define form validators with support of translations. -version: 3.0.0 +version: 3.0.1 repository: https://github.com/netglade/glade_forms issue_tracker: https://github.com/netglade/glade_forms/issues screenshots: From 6d22c2f436c63fe340fb800f45ebbab614b19bfc Mon Sep 17 00:00:00 2001 From: Petr Nymsa Date: Wed, 8 Jan 2025 21:44:44 +0100 Subject: [PATCH 5/7] Add unit tests --- glade_forms/lib/src/model/glade_model.dart | 2 +- glade_forms/test/model/glade_model_test.dart | 88 ++++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 glade_forms/test/model/glade_model_test.dart diff --git a/glade_forms/lib/src/model/glade_model.dart b/glade_forms/lib/src/model/glade_model.dart index 5c0f493..6053e62 100644 --- a/glade_forms/lib/src/model/glade_model.dart +++ b/glade_forms/lib/src/model/glade_model.dart @@ -124,7 +124,7 @@ abstract class GladeModel extends ChangeNotifier { void notifyDependencies() { final updatedKeys = _lastUpdates.map((e) => e.inputKey).toSet(); for (final input in inputs) { - final updatedKeysExceptInputItself = updatedKeys.toSet().difference({input.inputKey}); + final updatedKeysExceptInputItself = updatedKeys.difference({input.inputKey}); final union = input.dependencies.map((e) => e.inputKey).toSet().intersection(updatedKeysExceptInputItself); if (union.isNotEmpty) input.onDependencyChange?.call(union.toList()); diff --git a/glade_forms/test/model/glade_model_test.dart b/glade_forms/test/model/glade_model_test.dart new file mode 100644 index 0000000..8989ee3 --- /dev/null +++ b/glade_forms/test/model/glade_model_test.dart @@ -0,0 +1,88 @@ +// ignore_for_file: avoid-duplicate-test-assertions, prefer-correct-callback-field-name, avoid-global-state + +import 'package:glade_forms/glade_forms.dart'; +import 'package:test/test.dart'; + +class _ModeWithDependencies extends GladeModel { + late GladeInput a; + late GladeInput b; + late GladeInput c; + + int aUpdated = 0; + int bUpdated = 0; + int cUpdated = 0; + + int onDepenencyCalledCount = 0; + + List updatedDependencyKeys = []; + + @override + List> get inputs => [a, b, c]; + + @override + void initialize() { + a = GladeInput.intInput(value: 0, inputKey: 'a'); + b = GladeInput.intInput(value: 1, inputKey: 'b'); + c = GladeInput.intInput( + value: 1, + inputKey: 'c', + dependencies: () => [a, b], + onDependencyChange: (keys) { + if (keys.contains('a')) aUpdated++; + if (keys.contains('b')) bUpdated++; + + onDepenencyCalledCount++; + + updatedDependencyKeys = keys; + }, + ); + + super.initialize(); + } +} + +void main() { + test('When updating [a] onDependencyChange is called for a', () { + final model = _ModeWithDependencies(); + + model.a.value = 5; + expect(model.a.value, equals(5)); + expect(model.onDepenencyCalledCount, equals(1)); + expect(model.lastUpdatedInputKeys, equals(['a'])); + expect(model.updatedDependencyKeys, equals(['a'])); + }); + + test('When updating [b] onDependencyChange is called for b', () { + final model = _ModeWithDependencies(); + + model.b.value = 5; + expect(model.b.value, equals(5)); + expect(model.onDepenencyCalledCount, equals(1)); + expect(model.lastUpdatedInputKeys, equals(['b'])); + expect(model.updatedDependencyKeys, equals(['b'])); + }); + + test('When updating [a, b] together onDependencyChange is called for [a,b]', () { + final model = _ModeWithDependencies(); + model.groupEdit(() { + model.b.value = 5; + model.a.value = 5; + }); + + expect(model.a.value, equals(5)); + expect(model.b.value, equals(5)); + expect(model.onDepenencyCalledCount, equals(1)); + expect(model.lastUpdatedInputKeys.toSet().difference({'a', 'b'}), equals({})); + expect(model.updatedDependencyKeys.toSet().difference({'a', 'b'}), equals({})); + }); + + test('When updating [c] onDependencyChange is not called', () { + final model = _ModeWithDependencies(); + model.c.value = 5; + + expect(model.c.value, equals(5)); + expect(model.lastUpdatedInputKeys.toSet().difference({'c'}), equals({})); + expect(model.onDepenencyCalledCount, isZero); + expect(model.updatedDependencyKeys.toSet().difference({}), equals({})); + }); +} From 0121df7c94658d70ff5b22a0629fc139132a918b Mon Sep 17 00:00:00 2001 From: Petr Nymsa Date: Fri, 10 Jan 2025 09:19:20 +0100 Subject: [PATCH 6/7] Code clenaup --- storybook/lib/usecases/onchange/two_way_checkbox_change.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/storybook/lib/usecases/onchange/two_way_checkbox_change.dart b/storybook/lib/usecases/onchange/two_way_checkbox_change.dart index 408efaf..a83a901 100644 --- a/storybook/lib/usecases/onchange/two_way_checkbox_change.dart +++ b/storybook/lib/usecases/onchange/two_way_checkbox_change.dart @@ -64,7 +64,6 @@ class AgeRestrictedModel extends GladeModel { inputKey: 'vip-input', dependencies: () => [ageInput], onDependencyChange: (updateInputKeys) { - print('onDependencyChange: $updateInputKeys'); if (updateInputKeys.contains('age-input')) { vipInput.updateValue(ageInput.value >= 18, shouldTriggerOnChange: false); } From 3704de73ac80f1dcc89718f7272fac0295b778fe Mon Sep 17 00:00:00 2001 From: Petr Nymsa Date: Fri, 10 Jan 2025 10:10:23 +0100 Subject: [PATCH 7/7] Fix tests --- glade_forms/test/model/group_edit_test.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/glade_forms/test/model/group_edit_test.dart b/glade_forms/test/model/group_edit_test.dart index fea37bf..f08c941 100644 --- a/glade_forms/test/model/group_edit_test.dart +++ b/glade_forms/test/model/group_edit_test.dart @@ -40,9 +40,11 @@ class _ModeWithDependencies extends GladeModel { c = GladeInput.intInput( value: 1, inputKey: 'c', + dependencies: () => [a, b], onDependencyChange: (keys) { if (keys.contains('a')) aUpdated++; if (keys.contains('b')) bUpdated++; + if (keys.contains('c')) cUpdated++; onDepenencyCalledCount++; },