Skip to content

Commit 357b80f

Browse files
committed
Conditions #7
1 parent d1f23fb commit 357b80f

File tree

2 files changed

+130
-30
lines changed

2 files changed

+130
-30
lines changed

lib/src/api/models/condition.dart

Lines changed: 101 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:equatable/equatable.dart';
22
import 'package:json_annotation/json_annotation.dart';
33

44
import '../mixins.dart';
5+
import '../utils.dart';
56
import 'models.dart';
67

78
part 'condition.g.dart';
@@ -10,58 +11,58 @@ part 'condition.g.dart';
1011
enum ConditionOperation {
1112
/// equals to operator. Checks if the value of the variable is equal to the
1213
/// value provided.
13-
EqualsTo,
14+
equalsTo,
1415

1516
/// not equals to operator. Checks if the value of the variable is not equal
1617
/// to the value provided.
17-
NotEqualsTo,
18+
notEqualsTo,
1819

1920
/// greater than operator. Checks if the value of the variable is greater
2021
/// than the value provided.
21-
GreaterThan,
22+
greaterThan,
2223

2324
/// less than operator. Checks if the value of the variable is less than the
2425
/// value provided.
25-
LessThan;
26+
lessThan;
2627

2728
/// label for the operation
2829
String get label {
2930
switch (this) {
30-
case ConditionOperation.EqualsTo:
31+
case ConditionOperation.equalsTo:
3132
return 'Equal';
32-
case ConditionOperation.NotEqualsTo:
33+
case ConditionOperation.notEqualsTo:
3334
return 'Not Equals';
34-
case ConditionOperation.GreaterThan:
35+
case ConditionOperation.greaterThan:
3536
return 'Greater';
36-
case ConditionOperation.LessThan:
37+
case ConditionOperation.lessThan:
3738
return 'Less';
3839
}
3940
}
4041

4142
/// short description of the operation
4243
String get sentence {
4344
switch (this) {
44-
case ConditionOperation.EqualsTo:
45+
case ConditionOperation.equalsTo:
4546
return 'equal to';
46-
case ConditionOperation.NotEqualsTo:
47+
case ConditionOperation.notEqualsTo:
4748
return 'not equal to';
48-
case ConditionOperation.GreaterThan:
49+
case ConditionOperation.greaterThan:
4950
return 'greater than';
50-
case ConditionOperation.LessThan:
51+
case ConditionOperation.lessThan:
5152
return 'less than';
5253
}
5354
}
5455

5556
/// short description of the operation
5657
String get sign {
5758
switch (this) {
58-
case ConditionOperation.EqualsTo:
59+
case ConditionOperation.equalsTo:
5960
return '==';
60-
case ConditionOperation.NotEqualsTo:
61+
case ConditionOperation.notEqualsTo:
6162
return '!=';
62-
case ConditionOperation.GreaterThan:
63+
case ConditionOperation.greaterThan:
6364
return '>';
64-
case ConditionOperation.LessThan:
65+
case ConditionOperation.lessThan:
6566
return '<';
6667
}
6768
}
@@ -288,8 +289,11 @@ class ExpressionGroup extends BaseExpression {
288289

289290
/// Base class for conditions
290291
sealed class BaseCondition with EquatableMixin, SerializableMixin {
292+
/// id of the condition
293+
final String id;
294+
291295
/// Creates a base condition.
292-
const BaseCondition();
296+
const BaseCondition({required this.id});
293297

294298
/// Factory constructor for creating a new [BaseCondition] instance from
295299
/// JSON data.
@@ -318,13 +322,20 @@ class ElseCondition extends BaseCondition {
318322
final List<ActionModel> actions;
319323

320324
/// Creates an else condition
321-
ElseCondition(
325+
ElseCondition({
326+
required super.id,
322327
List<ActionModel>? actions,
323-
) : actions = actions ?? [];
328+
}) : actions = actions ?? [];
324329

325330
/// Duplicates the else condition with the provided actions list.
326-
ElseCondition copyWith({List<ActionModel>? actions}) {
327-
return ElseCondition(actions ?? this.actions);
331+
ElseCondition copyWith({
332+
String? id,
333+
List<ActionModel>? actions,
334+
}) {
335+
return ElseCondition(
336+
id: id ?? this.id,
337+
actions: actions ?? this.actions,
338+
);
328339
}
329340

330341
/// Factory constructor for creating a new [ElseCondition] instance from
@@ -356,17 +367,20 @@ class Condition extends BaseCondition {
356367

357368
/// Creates a simple condition
358369
const Condition({
370+
required super.id,
359371
required this.mode,
360372
required this.expression,
361373
required this.actions,
362374
});
363375

364376
/// CopyWith
365377
Condition copyWith({
378+
String? id,
366379
BaseExpression? expression,
367380
List<ActionModel>? actions,
368381
}) {
369382
return Condition(
383+
id: id ?? this.id,
370384
mode: mode,
371385
expression: expression ?? this.expression,
372386
actions: actions ?? this.actions,
@@ -391,9 +405,6 @@ class Condition extends BaseCondition {
391405
/// e.g. `if (a == b) { ... } else if (b == c) { ... } else { ... }`
392406
@JsonSerializable()
393407
class ConditionGroup extends BaseCondition {
394-
/// id of the condition group
395-
final String id;
396-
397408
/// if condition
398409
final Condition ifCondition;
399410

@@ -408,7 +419,7 @@ class ConditionGroup extends BaseCondition {
408419

409420
/// Creates a condition group
410421
ConditionGroup({
411-
required this.id,
422+
required super.id,
412423
this.name,
413424
required this.ifCondition,
414425
List<Condition>? elseIfConditions,
@@ -443,3 +454,68 @@ class ConditionGroup extends BaseCondition {
443454
@override
444455
Map toJson() => _$ConditionGroupToJson(this)..['type'] = 'ConditionGroup';
445456
}
457+
458+
/// Contains all the variables associated with a canvas inside a page.
459+
@JsonSerializable()
460+
class CanvasConditions with EquatableMixin {
461+
/// Unique ID of the canvas.
462+
final String id;
463+
464+
/// Variables associated with this canvas.
465+
final Map<String, BaseCondition> conditions;
466+
467+
/// Last updated time of this canvas.
468+
@JsonKey(toJson: dateToJson, fromJson: jsonToDate)
469+
final DateTime lastUpdated;
470+
471+
/// ID of the project this canvas belongs to.
472+
final String owner;
473+
474+
/// ID of the project this canvas belongs to.
475+
@JsonKey(name: 'project')
476+
final String projectId;
477+
478+
/// Creates a new [CanvasVariables].
479+
CanvasConditions({
480+
required this.id,
481+
required this.conditions,
482+
DateTime? lastUpdated,
483+
required this.projectId,
484+
required this.owner,
485+
}) : lastUpdated = lastUpdated ?? DateTime.now();
486+
487+
/// Duplicate a [CanvasVariables] with the given parameters.
488+
CanvasConditions copyWith({
489+
String? id,
490+
Map<String, BaseCondition>? conditions,
491+
String? projectId,
492+
String? owner,
493+
}) =>
494+
CanvasConditions(
495+
id: id ?? this.id,
496+
conditions: conditions ?? this.conditions,
497+
lastUpdated: DateTime.now(),
498+
projectId: projectId ?? this.projectId,
499+
owner: owner ?? this.owner,
500+
);
501+
502+
@override
503+
List<Object?> get props => [id, conditions];
504+
505+
/// Creates a new [CanvasVariables] from a JSON map.
506+
factory CanvasConditions.fromJson(Map<String, dynamic> json) =>
507+
_$CanvasConditionsFromJson(json);
508+
509+
/// Converts this [CanvasVariables] into a JSON map.
510+
Map<String, dynamic> toJson() => _$CanvasConditionsToJson(this)..remove('id');
511+
512+
/// Allows to access canvas variables by variable id.
513+
BaseCondition? operator [](String conditionId) {
514+
return conditions[conditionId];
515+
}
516+
517+
/// Allows to assign canvas variables by variable id.
518+
void operator []=(String conditionId, BaseCondition value) {
519+
conditions[conditionId] = value;
520+
}
521+
}

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

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

0 commit comments

Comments
 (0)