Skip to content

Commit

Permalink
feat: added conditional action
Browse files Browse the repository at this point in the history
  • Loading branch information
pavanpodila committed Mar 23, 2024
1 parent 7cfb6a8 commit 546c5d3
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<CaseAction>? cases;

final String? defaultCase;
final Condition? condition;

ConditionalAction({this.cases, this.condition, this.defaultCase})
: super(schemaType: ConditionalAction.schemaName);

factory ConditionalAction.fromJson(Map<String, dynamic> 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<String, dynamic> json) =>
_$CaseActionFromJson(json);
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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!);
Expand Down
1 change: 1 addition & 0 deletions packages/system/vyuh_feature_system/lib/feature.dart
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ final feature = FeatureDescriptor(
actions: [
NavigationAction.typeDescriptor,
JavaScriptAction.typeDescriptor,
ConditionalAction.typeDescriptor,
],
),
ScriptExtensionDescriptor(
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down

0 comments on commit 546c5d3

Please sign in to comment.