diff --git a/lib/src/api/models/action/set_variable_action.dart b/lib/src/api/models/action/set_variable_action.dart index 3e7c245..a939daf 100644 --- a/lib/src/api/models/action/set_variable_action.dart +++ b/lib/src/api/models/action/set_variable_action.dart @@ -12,6 +12,40 @@ import 'action.dart'; part 'set_variable_action.g.dart'; +/// Defines the operation to be perfomed on a list type variable. +enum ListOperation { + /// Replace entire list. + replace, + + /// Add [newValue] to the list. + add, + + /// Insert [newValue] at [index] in the list. + insert, + + /// Remove value at [index] from the list. + remove, + + /// Update value at [index] with [newValue] in the list. + update; + + /// Returns a string representation of this enum. + String get prettify { + switch (this) { + case ListOperation.replace: + return 'Replace'; + case ListOperation.add: + return 'Add'; + case ListOperation.insert: + return 'Insert'; + case ListOperation.remove: + return 'Remove'; + case ListOperation.update: + return 'Update'; + } + } +} + /// An action that sets value of a variable. @JsonSerializable() class SetVariableAction extends ActionModel @@ -26,11 +60,20 @@ class SetVariableAction extends ActionModel /// if the variable is a boolean. final bool toggled; + final ListOperation listOperation; + + /// Index of the value to be updated/removed/inserted. + /// Can be a discrete value or a variable refered by '${}' syntax. + /// Used for list type variable. + final String index; + /// Creates a new [SetValueAction]. SetVariableAction({ required this.variable, required this.newValue, this.toggled = false, + this.listOperation = ListOperation.replace, + this.index = '0', }) : super(type: ActionType.setVariable); @override @@ -41,11 +84,15 @@ class SetVariableAction extends ActionModel VariableData? variable, String? newValue, bool? toggled, + ListOperation? listOperation, + String? index, }) => SetVariableAction( variable: variable ?? this.variable, newValue: newValue ?? this.newValue, toggled: toggled ?? this.toggled, + listOperation: listOperation ?? this.listOperation, + index: index ?? this.index, ); /// Creates a new [SetVariableAction] instance from a JSON data. diff --git a/lib/src/api/models/action/set_variable_action.g.dart b/lib/src/api/models/action/set_variable_action.g.dart index 8be2a3a..6b4494f 100644 --- a/lib/src/api/models/action/set_variable_action.g.dart +++ b/lib/src/api/models/action/set_variable_action.g.dart @@ -11,6 +11,10 @@ SetVariableAction _$SetVariableActionFromJson(Map json) => SetVariableAction( Map.from(json['variable'] as Map)), newValue: json['newValue'] as String, toggled: json['toggled'] as bool? ?? false, + listOperation: + $enumDecodeNullable(_$ListOperationEnumMap, json['listOperation']) ?? + ListOperation.replace, + index: json['index'] as String? ?? '0', )..type = $enumDecode(_$ActionTypeEnumMap, json['type']); Map _$SetVariableActionToJson(SetVariableAction instance) => @@ -19,8 +23,18 @@ Map _$SetVariableActionToJson(SetVariableAction instance) => 'variable': instance.variable.toJson(), 'newValue': instance.newValue, 'toggled': instance.toggled, + 'listOperation': _$ListOperationEnumMap[instance.listOperation]!, + 'index': instance.index, }; +const _$ListOperationEnumMap = { + ListOperation.replace: 'replace', + ListOperation.add: 'add', + ListOperation.insert: 'insert', + ListOperation.remove: 'remove', + ListOperation.update: 'update', +}; + const _$ActionTypeEnumMap = { ActionType.navigation: 'navigation', ActionType.link: 'link',