From da7b826a798fe0aa97450774edb423198841725d Mon Sep 17 00:00:00 2001 From: Nikita Sin <74384266+lesleysin@users.noreply.github.com> Date: Mon, 30 Sep 2024 22:22:46 +0300 Subject: [PATCH] Improved data merging with component template (#14) * Added processing of ValueReference in widgets at the stage of creating component descriptions * Fixed types, impl id replacement method * Upd package --- CHANGELOG.md | 6 +- .../registry_api/component_description.dart | 55 +++++++++++++++++++ lib/src/registry_api/factory_record.dart | 4 +- lib/src/registry_api/factory_set.dart | 8 --- lib/src/registry_api/index.dart | 3 +- lib/src/registry_api/reference_target.dart | 11 ++++ lib/src/registry_api/value_reference.dart | 3 + pubspec.yaml | 2 +- 8 files changed, 79 insertions(+), 13 deletions(-) delete mode 100644 lib/src/registry_api/factory_set.dart create mode 100644 lib/src/registry_api/reference_target.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b0cd17..4eb250b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ +## 2.1.0 + +- Implemented preliminary preparation of component layouts + ## 2.0.1 -- Fixed a bug with generic factory types +- Fixed a bug with generic factory types ## 2.0.0 diff --git a/lib/src/registry_api/component_description.dart b/lib/src/registry_api/component_description.dart index e468b68..a24aae7 100644 --- a/lib/src/registry_api/component_description.dart +++ b/lib/src/registry_api/component_description.dart @@ -1,23 +1,78 @@ +import 'package:duit_kernel/duit_kernel.dart'; + /// Description of the component for registering it /// in the [DuitRegistry] under the key corresponding to the [tag] property final class DuitComponentDescription { final String tag; final Map data; + final Set refs; DuitComponentDescription({ required this.tag, required this.data, + required this.refs, }); + static void _replaceId(Map map) { + final type = map["type"]; + final timestamp = DateTime.now().microsecondsSinceEpoch; + final id = "${type}_$timestamp"; + map["id"] = id; + } + + static void _prepareRefs( + Map obj, + Set container, + ) { + if (obj['controlled'] == true) { + _replaceId(obj); + } + + final attributes = obj['attributes'] as Map; + final refs = attributes['refs']; + + if (refs != null) { + final list = List.from( + refs, + growable: false, + ); + + for (final ref in list) { + final rT = RefWithTarget( + target: attributes, + ref: ValueReference.fromJson(ref), + ); + container.add(rT); + } + } + + if (obj['children'] != null) { + final children = List.from( + obj['children'], + growable: false, + ); + for (final child in children) { + _prepareRefs(child, container); + } + } + + if (obj['child'] != null) { + _prepareRefs(obj['child'], container); + } + } + factory DuitComponentDescription.fromJson(Map json) { assert( json['tag'] != null, "Tag must be provided in component description"); assert(json['layoutRoot'] != null, "Layout must be provided in component description"); final root = json['layoutRoot']; + final rf = {}; + _prepareRefs(root, rf); return DuitComponentDescription( tag: json['tag'], data: root, + refs: rf, ); } } diff --git a/lib/src/registry_api/factory_record.dart b/lib/src/registry_api/factory_record.dart index 6a7513f..720def4 100644 --- a/lib/src/registry_api/factory_record.dart +++ b/lib/src/registry_api/factory_record.dart @@ -1,8 +1,8 @@ -import '../index.dart'; +import 'package:duit_kernel/duit_kernel.dart'; /// The [FactoryRecord] is a [Record] of factory functions that can be used to build custom components typedef FactoryRecord = ({ AttributesFactory attributesFactory, ModelFactory modelFactory, BuildFactory buildFactory, -}); \ No newline at end of file +}); diff --git a/lib/src/registry_api/factory_set.dart b/lib/src/registry_api/factory_set.dart deleted file mode 100644 index 6a7513f..0000000 --- a/lib/src/registry_api/factory_set.dart +++ /dev/null @@ -1,8 +0,0 @@ -import '../index.dart'; - -/// The [FactoryRecord] is a [Record] of factory functions that can be used to build custom components -typedef FactoryRecord = ({ - AttributesFactory attributesFactory, - ModelFactory modelFactory, - BuildFactory buildFactory, -}); \ No newline at end of file diff --git a/lib/src/registry_api/index.dart b/lib/src/registry_api/index.dart index 1ead7ee..45f3811 100644 --- a/lib/src/registry_api/index.dart +++ b/lib/src/registry_api/index.dart @@ -3,4 +3,5 @@ export 'value_reference.dart'; export 'component_description.dart'; export 'model_factory.dart'; export 'attributes_factory.dart'; -export 'build_factory.dart'; \ No newline at end of file +export 'build_factory.dart'; +export 'reference_target.dart'; \ No newline at end of file diff --git a/lib/src/registry_api/reference_target.dart b/lib/src/registry_api/reference_target.dart new file mode 100644 index 0000000..33f74c8 --- /dev/null +++ b/lib/src/registry_api/reference_target.dart @@ -0,0 +1,11 @@ +import 'package:duit_kernel/duit_kernel.dart'; + +final class RefWithTarget { + final Map target; + final ValueReference ref; + + RefWithTarget({ + required this.target, + required this.ref, + }); +} diff --git a/lib/src/registry_api/value_reference.dart b/lib/src/registry_api/value_reference.dart index 90a25cc..0ffbdf4 100644 --- a/lib/src/registry_api/value_reference.dart +++ b/lib/src/registry_api/value_reference.dart @@ -2,16 +2,19 @@ ///can be used to merge with a component description final class ValueReference { String objectKey, attributeKey; + Object? defaultValue; ValueReference({ required this.objectKey, required this.attributeKey, + this.defaultValue, }); factory ValueReference.fromJson(Map json) { return ValueReference( objectKey: json['objectKey'], attributeKey: json['attributeKey'], + defaultValue: json['defaultValue'], ); } } diff --git a/pubspec.yaml b/pubspec.yaml index eeba730..e8a8c92 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: duit_kernel description: "Core library for flutter_duit package. Contains reusable models for developing third-party packages." -version: 2.0.1 +version: 2.1.0 repository: https://github.com/lesleysin/duit_kernel environment: