Skip to content

Commit a16112d

Browse files
committed
Conditions #14
- Implement conditions for embedded preview. - Implement conditions evaluation for visible property. - Refactor ActionModel to use enum for deserializing. - Implement variable listening for conditions.
1 parent c92d4bb commit a16112d

File tree

2 files changed

+59
-13
lines changed

2 files changed

+59
-13
lines changed

lib/src/api/models/action/set_value_action.dart

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import 'package:equatable/equatable.dart';
66
import 'package:json_annotation/json_annotation.dart';
77

88
import '../../mixins.dart';
9-
import 'action.dart';
9+
import '../models.dart';
1010

1111
part 'set_value_action.g.dart';
1212

@@ -77,6 +77,21 @@ enum SetValueMode {
7777
}
7878
}
7979

80+
/// Describes the type of the value.
81+
enum ValueType {
82+
/// Represents a text value.
83+
string,
84+
85+
/// Represents a number without a decimal point.
86+
int,
87+
88+
/// Represents a number with a decimal point.
89+
double,
90+
91+
/// Represents a boolean value.
92+
bool,
93+
}
94+
8095
/// Represents a value to set in a node.
8196
abstract class ValueModel<T> with SerializableMixin {
8297
/// The name of the property to set the value of.
@@ -88,11 +103,16 @@ abstract class ValueModel<T> with SerializableMixin {
88103
/// The value to set.
89104
final T value;
90105

106+
/// The type of the value.
107+
@JsonKey(includeToJson: true)
108+
final ValueType type;
109+
91110
/// Creates a new [ValueModel].
92111
const ValueModel({
93112
required this.name,
94113
this.mode = SetValueMode.discrete,
95114
required this.value,
115+
required this.type,
96116
});
97117

98118
/// Duplicate this [ValueModel] with given data overrides.
@@ -103,12 +123,27 @@ abstract class ValueModel<T> with SerializableMixin {
103123

104124
/// Creates a new [ValueModel] instance from a JSON data.
105125
static ValueModel fromJson(Map json) {
106-
final value = json['value'];
107-
if (value is bool?) return BoolValue.fromJson(json);
108-
if (value is int) return IntValue.fromJson(json);
109-
if (value is double) return DoubleValue.fromJson(json);
110-
if (value is String) return StringValue.fromJson(json);
111-
return IntValue.fromJson(json);
126+
if (json['type'] != null) {
127+
final type = ValueType.values.byName(json['type']);
128+
switch (type) {
129+
case ValueType.bool:
130+
return BoolValue.fromJson(json);
131+
case ValueType.int:
132+
return IntValue.fromJson(json);
133+
case ValueType.double:
134+
return DoubleValue.fromJson(json);
135+
case ValueType.string:
136+
return StringValue.fromJson(json);
137+
}
138+
} else {
139+
// backward compatibility
140+
final value = json['value'];
141+
if (value is bool?) return BoolValue.fromJson(json);
142+
if (value is int) return IntValue.fromJson(json);
143+
if (value is double) return DoubleValue.fromJson(json);
144+
if (value is String) return StringValue.fromJson(json);
145+
return IntValue.fromJson(json);
146+
}
112147
}
113148
}
114149

@@ -128,7 +163,7 @@ class BoolValue extends ValueModel<bool?> with SerializableMixin {
128163
super.mode,
129164
super.value = false,
130165
this.nullable = false,
131-
});
166+
}) : super(type: ValueType.bool);
132167

133168
@override
134169
BoolValue copyWith({
@@ -159,7 +194,7 @@ class IntValue extends ValueModel<int> with SerializableMixin {
159194
required super.name,
160195
super.mode,
161196
super.value = 0,
162-
}) {
197+
}) : super(type: ValueType.int) {
163198
assert(mode != SetValueMode.toggle, '${mode.prettify} mode not supported.');
164199
}
165200

@@ -169,7 +204,7 @@ class IntValue extends ValueModel<int> with SerializableMixin {
169204
required super.name,
170205
super.value = 0,
171206
super.mode,
172-
});
207+
}) : super(type: ValueType.int);
173208

174209
@override
175210
IntValue copyWith({
@@ -197,7 +232,7 @@ class DoubleValue extends ValueModel<double> with SerializableMixin {
197232
required super.name,
198233
super.mode,
199234
super.value = 0,
200-
}) {
235+
}) : super(type: ValueType.double) {
201236
assert(mode != SetValueMode.toggle, '${mode.prettify} mode not supported.');
202237
}
203238

@@ -207,7 +242,7 @@ class DoubleValue extends ValueModel<double> with SerializableMixin {
207242
required super.name,
208243
super.value = 0,
209244
super.mode,
210-
});
245+
}) : super(type: ValueType.double);
211246

212247
@override
213248
DoubleValue copyWith({
@@ -235,7 +270,7 @@ class StringValue extends ValueModel<String> with SerializableMixin {
235270
required super.name,
236271
super.mode,
237272
super.value = '',
238-
}) {
273+
}) : super(type: ValueType.string) {
239274
assert(mode != SetValueMode.toggle, '${mode.prettify} mode not supported.');
240275
}
241276

lib/src/api/models/action/set_value_action.g.dart

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

0 commit comments

Comments
 (0)