diff --git a/packages/system/vyuh_feature_system/lib/action/conditional_action.dart b/packages/system/vyuh_feature_system/lib/action/conditional_action.dart new file mode 100644 index 00000000..a1dc8ced --- /dev/null +++ b/packages/system/vyuh_feature_system/lib/action/conditional_action.dart @@ -0,0 +1,54 @@ +import 'package:collection/collection.dart'; +import 'package:flutter/material.dart' as flutter; +import 'package:json_annotation/json_annotation.dart'; +import 'package:vyuh_core/vyuh_core.dart'; +import 'package:vyuh_extension_content/vyuh_extension_content.dart'; + +part 'conditional_action.g.dart'; + +@JsonSerializable() +class ConditionalAction extends ActionConfiguration { + static const schemaName = 'vyuh.action.conditional'; + + static final typeDescriptor = TypeDescriptor( + schemaType: ConditionalAction.schemaName, + title: 'Conditional Action', + fromJson: ConditionalAction.fromJson, + ); + + @JsonKey(defaultValue: []) + final List? cases; + + final String? defaultCase; + final Condition? condition; + + ConditionalAction({this.cases, this.condition, this.defaultCase}) + : super(schemaType: ConditionalAction.schemaName); + + factory ConditionalAction.fromJson(Map json) => + _$ConditionalActionFromJson(json); + + @override + void execute(flutter.BuildContext context) async { + final value = (await condition?.execute()) ?? defaultCase; + + if (context.mounted) { + final caseAction = + cases?.firstWhereOrNull((element) => element.value == value); + + caseAction?.action?.execute(context); + } + } +} + +@JsonSerializable() +final class CaseAction { + final String? value; + + final Action? action; + + CaseAction({this.value, this.action}); + + factory CaseAction.fromJson(Map json) => + _$CaseActionFromJson(json); +} diff --git a/packages/system/vyuh_feature_system/lib/action/conditional_action.g.dart b/packages/system/vyuh_feature_system/lib/action/conditional_action.g.dart new file mode 100644 index 00000000..938e12f4 --- /dev/null +++ b/packages/system/vyuh_feature_system/lib/action/conditional_action.g.dart @@ -0,0 +1,26 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'conditional_action.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +ConditionalAction _$ConditionalActionFromJson(Map json) => + ConditionalAction( + cases: (json['cases'] as List?) + ?.map((e) => CaseAction.fromJson(e as Map)) + .toList() ?? + [], + condition: json['condition'] == null + ? null + : Condition.fromJson(json['condition'] as Map), + defaultCase: json['defaultCase'] as String?, + ); + +CaseAction _$CaseActionFromJson(Map json) => CaseAction( + value: json['value'] as String?, + action: json['action'] == null + ? null + : Action.fromJson(json['action'] as Map), + ); diff --git a/packages/system/vyuh_feature_system/lib/action/navigation.dart b/packages/system/vyuh_feature_system/lib/action/navigation.dart index 26b5beeb..09547757 100644 --- a/packages/system/vyuh_feature_system/lib/action/navigation.dart +++ b/packages/system/vyuh_feature_system/lib/action/navigation.dart @@ -44,6 +44,7 @@ final class NavigationAction extends ActionConfiguration { if (linkType == LinkType.route && route != null) { _performNavigation(context, routeId: route!.ref); + return; } final uri = Uri.parse(url!); diff --git a/packages/system/vyuh_feature_system/lib/feature.dart b/packages/system/vyuh_feature_system/lib/feature.dart index 2a628777..79bab333 100644 --- a/packages/system/vyuh_feature_system/lib/feature.dart +++ b/packages/system/vyuh_feature_system/lib/feature.dart @@ -178,6 +178,7 @@ final feature = FeatureDescriptor( actions: [ NavigationAction.typeDescriptor, JavaScriptAction.typeDescriptor, + ConditionalAction.typeDescriptor, ], ), ScriptExtensionDescriptor( diff --git a/packages/system/vyuh_feature_system/lib/vyuh_feature_system.dart b/packages/system/vyuh_feature_system/lib/vyuh_feature_system.dart index cb40e2e8..9ce38b0f 100644 --- a/packages/system/vyuh_feature_system/lib/vyuh_feature_system.dart +++ b/packages/system/vyuh_feature_system/lib/vyuh_feature_system.dart @@ -1,5 +1,6 @@ library vyuh_feature_system; +export 'action/conditional_action.dart'; export 'action/navigation.dart'; export 'condition/boolean.dart'; export 'content/index.dart';