Skip to content

Commit

Permalink
Improved data merging with component template (#14)
Browse files Browse the repository at this point in the history
* Added processing of ValueReference in widgets at the stage of creating component descriptions

* Fixed types, impl id replacement method

* Upd package
  • Loading branch information
lesleysin authored Sep 30, 2024
1 parent f123991 commit da7b826
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 13 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
55 changes: 55 additions & 0 deletions lib/src/registry_api/component_description.dart
Original file line number Diff line number Diff line change
@@ -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<String, dynamic> data;
final Set<RefWithTarget> refs;

DuitComponentDescription({
required this.tag,
required this.data,
required this.refs,
});

static void _replaceId(Map<String, dynamic> map) {
final type = map["type"];
final timestamp = DateTime.now().microsecondsSinceEpoch;
final id = "${type}_$timestamp";
map["id"] = id;
}

static void _prepareRefs(
Map<String, dynamic> obj,
Set<RefWithTarget> container,
) {
if (obj['controlled'] == true) {
_replaceId(obj);
}

final attributes = obj['attributes'] as Map<String, dynamic>;
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<String, dynamic> 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 = <RefWithTarget>{};
_prepareRefs(root, rf);
return DuitComponentDescription(
tag: json['tag'],
data: root,
refs: rf,
);
}
}
4 changes: 2 additions & 2 deletions lib/src/registry_api/factory_record.dart
Original file line number Diff line number Diff line change
@@ -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,
});
});
8 changes: 0 additions & 8 deletions lib/src/registry_api/factory_set.dart

This file was deleted.

3 changes: 2 additions & 1 deletion lib/src/registry_api/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export 'value_reference.dart';
export 'component_description.dart';
export 'model_factory.dart';
export 'attributes_factory.dart';
export 'build_factory.dart';
export 'build_factory.dart';
export 'reference_target.dart';
11 changes: 11 additions & 0 deletions lib/src/registry_api/reference_target.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:duit_kernel/duit_kernel.dart';

final class RefWithTarget {
final Map<String, dynamic> target;
final ValueReference ref;

RefWithTarget({
required this.target,
required this.ref,
});
}
3 changes: 3 additions & 0 deletions lib/src/registry_api/value_reference.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, dynamic> json) {
return ValueReference(
objectKey: json['objectKey'],
attributeKey: json['attributeKey'],
defaultValue: json['defaultValue'],
);
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit da7b826

Please sign in to comment.