Skip to content

Commit 362ec87

Browse files
committed
Wrap component inside NamedItem
1 parent d7078cf commit 362ec87

File tree

12 files changed

+270
-44
lines changed

12 files changed

+270
-44
lines changed

api/lib/src/models/data.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,14 @@ final class NoteData extends ArchiveData<NoteData> {
367367
Iterable<String> getComponents() =>
368368
getAssets('$kComponentsArchiveDirectory/', true);
369369

370+
@useResult
371+
Iterable<NamedItem<ButterflyComponent>> getNamedComponents() =>
372+
getComponents().map((e) {
373+
final component = getComponent(e);
374+
if (component == null) return null;
375+
return NamedItem<ButterflyComponent>(name: e, item: component);
376+
}).nonNulls;
377+
370378
@useResult
371379
ButterflyComponent? getComponent(String componentName) {
372380
final data = getAsset('$kComponentsArchiveDirectory/$componentName.json');

api/lib/src/models/pack.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,27 @@ final class PackAssetLocation {
8181
const PackAssetLocation(this.namespace, this.key);
8282
}
8383

84+
@Freezed(genericArgumentFactories: true)
85+
sealed class NamedItem<T extends PackAsset> with _$NamedItem<T> {
86+
const NamedItem._();
87+
88+
const factory NamedItem({
89+
required String name,
90+
required T item,
91+
}) = _NamedItem<T>;
92+
93+
factory NamedItem.fromJson(
94+
Map<String, dynamic> json,
95+
T Function(Object? json) fromJsonT,
96+
) =>
97+
_$NamedItemFromJson(json, fromJsonT);
98+
99+
@override
100+
Map<String, dynamic> toJson(Object? Function(T) toJsonT) =>
101+
throw UnimplementedError(
102+
'toJson is not implemented for NamedItem<T>. Use NamedItem.fromJson instead.');
103+
}
104+
84105
final class PackItem<T extends PackAsset> implements PackAssetLocation {
85106
final PackAssetLocation location;
86107
final NoteData pack;

api/lib/src/models/pack.freezed.dart

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,4 +893,164 @@ class _$DoubleParameterCopyWithImpl<$Res>
893893
}
894894
}
895895

896+
/// @nodoc
897+
mixin _$NamedItem<T extends PackAsset> {
898+
String get name;
899+
T get item;
900+
901+
/// Create a copy of NamedItem
902+
/// with the given fields replaced by the non-null parameter values.
903+
@JsonKey(includeFromJson: false, includeToJson: false)
904+
@pragma('vm:prefer-inline')
905+
$NamedItemCopyWith<T, NamedItem<T>> get copyWith =>
906+
_$NamedItemCopyWithImpl<T, NamedItem<T>>(
907+
this as NamedItem<T>, _$identity);
908+
909+
/// Serializes this NamedItem to a JSON map.
910+
Map<String, dynamic> toJson(Object? Function(T) toJsonT);
911+
912+
@override
913+
bool operator ==(Object other) {
914+
return identical(this, other) ||
915+
(other.runtimeType == runtimeType &&
916+
other is NamedItem<T> &&
917+
(identical(other.name, name) || other.name == name) &&
918+
const DeepCollectionEquality().equals(other.item, item));
919+
}
920+
921+
@JsonKey(includeFromJson: false, includeToJson: false)
922+
@override
923+
int get hashCode =>
924+
Object.hash(runtimeType, name, const DeepCollectionEquality().hash(item));
925+
926+
@override
927+
String toString() {
928+
return 'NamedItem<$T>(name: $name, item: $item)';
929+
}
930+
}
931+
932+
/// @nodoc
933+
abstract mixin class $NamedItemCopyWith<T extends PackAsset, $Res> {
934+
factory $NamedItemCopyWith(
935+
NamedItem<T> value, $Res Function(NamedItem<T>) _then) =
936+
_$NamedItemCopyWithImpl;
937+
@useResult
938+
$Res call({String name, T item});
939+
}
940+
941+
/// @nodoc
942+
class _$NamedItemCopyWithImpl<T extends PackAsset, $Res>
943+
implements $NamedItemCopyWith<T, $Res> {
944+
_$NamedItemCopyWithImpl(this._self, this._then);
945+
946+
final NamedItem<T> _self;
947+
final $Res Function(NamedItem<T>) _then;
948+
949+
/// Create a copy of NamedItem
950+
/// with the given fields replaced by the non-null parameter values.
951+
@pragma('vm:prefer-inline')
952+
@override
953+
$Res call({
954+
Object? name = null,
955+
Object? item = null,
956+
}) {
957+
return _then(_self.copyWith(
958+
name: null == name
959+
? _self.name
960+
: name // ignore: cast_nullable_to_non_nullable
961+
as String,
962+
item: null == item
963+
? _self.item
964+
: item // ignore: cast_nullable_to_non_nullable
965+
as T,
966+
));
967+
}
968+
}
969+
970+
/// @nodoc
971+
@JsonSerializable(genericArgumentFactories: true)
972+
class _NamedItem<T extends PackAsset> extends NamedItem<T> {
973+
const _NamedItem({required this.name, required this.item}) : super._();
974+
factory _NamedItem.fromJson(
975+
Map<String, dynamic> json, T Function(Object?) fromJsonT) =>
976+
_$NamedItemFromJson(json, fromJsonT);
977+
978+
@override
979+
final String name;
980+
@override
981+
final T item;
982+
983+
/// Create a copy of NamedItem
984+
/// with the given fields replaced by the non-null parameter values.
985+
@override
986+
@JsonKey(includeFromJson: false, includeToJson: false)
987+
@pragma('vm:prefer-inline')
988+
_$NamedItemCopyWith<T, _NamedItem<T>> get copyWith =>
989+
__$NamedItemCopyWithImpl<T, _NamedItem<T>>(this, _$identity);
990+
991+
@override
992+
Map<String, dynamic> toJson(Object? Function(T) toJsonT) {
993+
return _$NamedItemToJson<T>(this, toJsonT);
994+
}
995+
996+
@override
997+
bool operator ==(Object other) {
998+
return identical(this, other) ||
999+
(other.runtimeType == runtimeType &&
1000+
other is _NamedItem<T> &&
1001+
(identical(other.name, name) || other.name == name) &&
1002+
const DeepCollectionEquality().equals(other.item, item));
1003+
}
1004+
1005+
@JsonKey(includeFromJson: false, includeToJson: false)
1006+
@override
1007+
int get hashCode =>
1008+
Object.hash(runtimeType, name, const DeepCollectionEquality().hash(item));
1009+
1010+
@override
1011+
String toString() {
1012+
return 'NamedItem<$T>(name: $name, item: $item)';
1013+
}
1014+
}
1015+
1016+
/// @nodoc
1017+
abstract mixin class _$NamedItemCopyWith<T extends PackAsset, $Res>
1018+
implements $NamedItemCopyWith<T, $Res> {
1019+
factory _$NamedItemCopyWith(
1020+
_NamedItem<T> value, $Res Function(_NamedItem<T>) _then) =
1021+
__$NamedItemCopyWithImpl;
1022+
@override
1023+
@useResult
1024+
$Res call({String name, T item});
1025+
}
1026+
1027+
/// @nodoc
1028+
class __$NamedItemCopyWithImpl<T extends PackAsset, $Res>
1029+
implements _$NamedItemCopyWith<T, $Res> {
1030+
__$NamedItemCopyWithImpl(this._self, this._then);
1031+
1032+
final _NamedItem<T> _self;
1033+
final $Res Function(_NamedItem<T>) _then;
1034+
1035+
/// Create a copy of NamedItem
1036+
/// with the given fields replaced by the non-null parameter values.
1037+
@override
1038+
@pragma('vm:prefer-inline')
1039+
$Res call({
1040+
Object? name = null,
1041+
Object? item = null,
1042+
}) {
1043+
return _then(_NamedItem<T>(
1044+
name: null == name
1045+
? _self.name
1046+
: name // ignore: cast_nullable_to_non_nullable
1047+
as String,
1048+
item: null == item
1049+
? _self.item
1050+
: item // ignore: cast_nullable_to_non_nullable
1051+
as T,
1052+
));
1053+
}
1054+
}
1055+
8961056
// dart format on

api/lib/src/models/pack.g.dart

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/lib/src/models/tool.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ sealed class Tool with _$Tool {
161161
factory Tool.stamp({
162162
@Default('') String name,
163163
@Default('') String displayIcon,
164-
ButterflyComponent? component,
164+
NamedItem<ButterflyComponent>? component,
165165
}) = StampTool;
166166

167167
factory Tool.presentation({

api/lib/src/models/tool.freezed.dart

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,7 +1524,7 @@ class StampTool extends Tool {
15241524
@override
15251525
@JsonKey()
15261526
final String displayIcon;
1527-
final ButterflyComponent? component;
1527+
final NamedItem<ButterflyComponent>? component;
15281528

15291529
@JsonKey(name: 'type')
15301530
final String $type;
@@ -1556,9 +1556,12 @@ abstract mixin class $StampToolCopyWith<$Res> implements $ToolCopyWith<$Res> {
15561556
_$StampToolCopyWithImpl;
15571557
@override
15581558
@useResult
1559-
$Res call({String name, String displayIcon, ButterflyComponent? component});
1559+
$Res call(
1560+
{String name,
1561+
String displayIcon,
1562+
NamedItem<ButterflyComponent>? component});
15601563

1561-
$ButterflyComponentCopyWith<$Res>? get component;
1564+
$NamedItemCopyWith<ButterflyComponent, $Res>? get component;
15621565
}
15631566

15641567
/// @nodoc
@@ -1589,20 +1592,21 @@ class _$StampToolCopyWithImpl<$Res> implements $StampToolCopyWith<$Res> {
15891592
component: freezed == component
15901593
? _self.component
15911594
: component // ignore: cast_nullable_to_non_nullable
1592-
as ButterflyComponent?,
1595+
as NamedItem<ButterflyComponent>?,
15931596
));
15941597
}
15951598

15961599
/// Create a copy of Tool
15971600
/// with the given fields replaced by the non-null parameter values.
15981601
@override
15991602
@pragma('vm:prefer-inline')
1600-
$ButterflyComponentCopyWith<$Res>? get component {
1603+
$NamedItemCopyWith<ButterflyComponent, $Res>? get component {
16011604
if (_self.component == null) {
16021605
return null;
16031606
}
16041607

1605-
return $ButterflyComponentCopyWith<$Res>(_self.component!, (value) {
1608+
return $NamedItemCopyWith<ButterflyComponent, $Res>(_self.component!,
1609+
(value) {
16061610
return _then(_self.copyWith(component: value));
16071611
});
16081612
}

api/lib/src/models/tool.g.dart

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/pubspec.lock

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ packages:
1313
dependency: "direct dev"
1414
description:
1515
name: analyzer
16-
sha256: "13c1e6c6fd460522ea840abec3f677cc226f5fec7872c04ad7b425517ccf54f7"
16+
sha256: "904ae5bb474d32c38fb9482e2d925d5454cda04ddd0e55d2e6826bc72f6ba8c0"
1717
url: "https://pub.dev"
1818
source: hosted
19-
version: "7.4.4"
19+
version: "7.4.5"
2020
archive:
2121
dependency: "direct main"
2222
description:
@@ -575,26 +575,26 @@ packages:
575575
dependency: "direct dev"
576576
description:
577577
name: test
578-
sha256: "301b213cd241ca982e9ba50266bd3f5bd1ea33f1455554c5abb85d1be0e2d87e"
578+
sha256: f1665eeffe3b6b193548b5f515e8d1b54ccd9a6e0e7721a417e134e7ed7f06a1
579579
url: "https://pub.dev"
580580
source: hosted
581-
version: "1.25.15"
581+
version: "1.26.0"
582582
test_api:
583583
dependency: transitive
584584
description:
585585
name: test_api
586-
sha256: fb31f383e2ee25fbbfe06b40fe21e1e458d14080e3c67e7ba0acfde4df4e0bbd
586+
sha256: "6c7653816b1c938e121b69ff63a33c9dc68102b65a5fb0a5c0f9786256ed33e6"
587587
url: "https://pub.dev"
588588
source: hosted
589-
version: "0.7.4"
589+
version: "0.7.5"
590590
test_core:
591591
dependency: transitive
592592
description:
593593
name: test_core
594-
sha256: "84d17c3486c8dfdbe5e12a50c8ae176d15e2a771b96909a9442b40173649ccaa"
594+
sha256: "3caa7c3956b366643b2dedecff764cc32030317b2a15252aed845570df6bcc0f"
595595
url: "https://pub.dev"
596596
source: hosted
597-
version: "0.6.8"
597+
version: "0.6.9"
598598
timing:
599599
dependency: transitive
600600
description:

app/lib/handlers/stamp.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class StampHandler extends PastingHandler<StampTool> {
3939
super.onPointerDown(event, context);
4040
}
4141

42-
ButterflyComponent? getComponent() => data.component;
42+
ButterflyComponent? getComponent() => data.component?.item;
4343

4444
Future<void> _loadComponent(
4545
NoteData document, AssetService assetService, DocumentPage page,

app/lib/selections/tools/stamp.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ class StampToolSelection extends ToolSelection<StampTool> {
99
final state = bloc.state;
1010
if (state is! DocumentLoadSuccess) return [];
1111
final value = selected.first.component;
12-
void updateComponent(ButterflyComponent? component) => update(context,
12+
void updateComponent(NamedItem<ButterflyComponent>? component) => update(
13+
context,
1314
selected.map((e) => e.copyWith(component: component)).toList());
1415
return [
1516
...super.buildProperties(context),
1617
const SizedBox(height: 16),
1718
ListTile(
1819
title: Text(AppLocalizations.of(context).component),
19-
subtitle:
20-
value == null ? Text(AppLocalizations.of(context).notSet) : null,
20+
subtitle: value == null
21+
? Text(AppLocalizations.of(context).notSet)
22+
: Text(value.name),
2123
trailing: IconButton(
2224
icon: const PhosphorIcon(PhosphorIconsLight.trash),
2325
onPressed: () => updateComponent(null),

0 commit comments

Comments
 (0)