Skip to content

Commit d052a57

Browse files
committed
SDK Local Storage #3
- Implement set storage action. - Fix storage listeners and notifiers.
1 parent 633591b commit d052a57

16 files changed

+270
-62
lines changed

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ enum ActionType {
2929
callFunction,
3030

3131
/// Call an API.
32-
callApi;
32+
callApi,
33+
34+
/// Update data in local storage.
35+
setStorage;
3336

3437
/// Displayable string representation of the [ActionType].
3538
String get prettify {
@@ -50,6 +53,8 @@ enum ActionType {
5053
return 'Call Function';
5154
case ActionType.callApi:
5255
return 'Call API';
56+
case ActionType.setStorage:
57+
return 'Set Storage';
5358
}
5459
}
5560
}
@@ -105,6 +110,8 @@ abstract class ActionModel with SerializableMixin {
105110
return CallFunctionAction.fromJson(json);
106111
case ActionType.callApi:
107112
return ApiCallAction.fromJson(json);
113+
case ActionType.setStorage:
114+
return SetStorageAction.fromJson(json);
108115
}
109116
}
110117

@@ -123,3 +130,38 @@ abstract class ActionModel with SerializableMixin {
123130
/// @returns The result of calling the visit method on the visitor.
124131
R? accept<R>(ActionVisitor<R> visitor);
125132
}
133+
134+
/// A base action class that represents an action that can be performed to
135+
/// modify some data in some way depending on the data type.
136+
abstract class DataOperationActionModel extends ActionModel {
137+
/// New value to be set.
138+
final String newValue;
139+
140+
/// Whether to toggle the value. This is used for boolean type variable.
141+
final bool toggled;
142+
143+
/// Operation to be performed on the list type variable.
144+
final ListOperation listOperation;
145+
146+
/// Index of the value to be updated/removed/inserted.
147+
/// Can be a discrete value or a variable refered by '${}' syntax.
148+
/// Used for list type variable.
149+
final String index;
150+
151+
/// Operation to be performed on the map type variable.
152+
final MapOperation mapOperation;
153+
154+
/// Key of the value to be updated/removed/inserted in the map.
155+
final String mapKey;
156+
157+
/// Creates a [DataOperationActionModel] with the given data.
158+
DataOperationActionModel({
159+
required super.type,
160+
this.newValue = '',
161+
this.toggled = false,
162+
this.listOperation = ListOperation.replace,
163+
this.index = '0',
164+
this.mapOperation = MapOperation.replace,
165+
this.mapKey = 'key',
166+
});
167+
}

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

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

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

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

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

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

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

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright (c) 2022, Codelessly.
2+
// All rights reserved. Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE.md file.
4+
5+
import 'package:equatable/equatable.dart';
6+
import 'package:json_annotation/json_annotation.dart';
7+
8+
import '../../mixins.dart';
9+
import '../models.dart';
10+
11+
part 'set_storage_action.g.dart';
12+
13+
/// Represents an operation to perform on a data in storage.
14+
enum StorageOperation {
15+
/// Remove the data from storage.
16+
remove,
17+
18+
/// Add or update the data in storage.
19+
addOrUpdate;
20+
21+
/// Displayable string representation of the [StorageOperation].
22+
String get label => switch (this) {
23+
StorageOperation.addOrUpdate => 'Add/Update',
24+
StorageOperation.remove => 'Remove',
25+
};
26+
}
27+
28+
/// An action that sets value of a variable.
29+
@JsonSerializable()
30+
class SetStorageAction extends DataOperationActionModel
31+
with EquatableMixin, SerializableMixin {
32+
/// Key of the data in storage.
33+
final String key;
34+
35+
/// Operation to perform on the data.
36+
final StorageOperation operation;
37+
38+
/// Type of the variable.
39+
/// This is used to convert the value to the correct type.
40+
final VariableType variableType;
41+
42+
/// Creates a new [SetValueAction].
43+
SetStorageAction({
44+
this.key = 'key',
45+
this.operation = StorageOperation.addOrUpdate,
46+
this.variableType = VariableType.text,
47+
super.newValue = '',
48+
super.toggled = false,
49+
super.listOperation = ListOperation.replace,
50+
super.index = '0',
51+
super.mapOperation = MapOperation.replace,
52+
super.mapKey = 'key',
53+
}) : super(type: ActionType.setStorage);
54+
55+
/// Duplicates this [SetStorageAction] with given data overrides.
56+
SetStorageAction copyWith({
57+
String? key,
58+
StorageOperation? operation,
59+
VariableType? variableType,
60+
String? value,
61+
bool? toggled,
62+
ListOperation? listOperation,
63+
String? index,
64+
MapOperation? mapOperation,
65+
String? mapKey,
66+
}) {
67+
final String? sanitizedValue = value == null
68+
? null
69+
: sanitizeValueForVariableType(
70+
value, variableType ?? this.variableType);
71+
return SetStorageAction(
72+
key: key ?? this.key,
73+
operation: operation ?? this.operation,
74+
variableType: variableType ?? this.variableType,
75+
newValue: sanitizedValue ?? newValue,
76+
toggled: toggled ?? this.toggled,
77+
listOperation: listOperation ?? this.listOperation,
78+
index: index ?? this.index,
79+
mapOperation: mapOperation ?? this.mapOperation,
80+
mapKey: mapKey ?? this.mapKey,
81+
);
82+
}
83+
84+
@override
85+
List<Object?> get props => [
86+
key,
87+
operation,
88+
variableType,
89+
newValue,
90+
toggled,
91+
listOperation,
92+
index,
93+
mapOperation,
94+
mapKey,
95+
];
96+
97+
/// Creates a new [SetStorageAction] instance from a JSON data.
98+
factory SetStorageAction.fromJson(Map json) =>
99+
_$SetStorageActionFromJson(json);
100+
101+
@override
102+
Map toJson() => _$SetStorageActionToJson(this);
103+
104+
@override
105+
R? accept<R>(ActionVisitor<R> visitor) => visitor.visitSetStorageAction(this);
106+
}

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

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

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

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

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

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -82,43 +82,20 @@ enum MapOperation {
8282

8383
/// An action that sets value of a variable.
8484
@JsonSerializable()
85-
class SetVariableAction extends ActionModel
85+
class SetVariableAction extends DataOperationActionModel
8686
with EquatableMixin, SerializableMixin {
8787
/// Variable whose value is to be set.
8888
final VariableData variable;
8989

90-
/// Value that the variable needs to be updated with.
91-
final String newValue;
92-
93-
/// Whether to toggle the value of the variable instead of setting it
94-
/// if the variable is a boolean.
95-
final bool toggled;
96-
97-
/// Operation to be performed on the list type variable.
98-
final ListOperation listOperation;
99-
100-
/// Index of the value to be updated/removed/inserted.
101-
/// Can be a discrete value or a variable refered by '${}' syntax.
102-
/// Used for list type variable.
103-
final String index;
104-
105-
/// Operation to be performed on the map type variable.
106-
final MapOperation mapOperation;
107-
108-
/// Key of the map to be updated/removed.
109-
/// Can be a discrete value or a variable refered by '${}' syntax.
110-
/// Used for map type variable.
111-
final String key;
112-
11390
/// Creates a new [SetValueAction].
11491
SetVariableAction({
11592
required this.variable,
116-
required this.newValue,
117-
this.toggled = false,
118-
this.listOperation = ListOperation.replace,
119-
this.index = '0',
120-
this.mapOperation = MapOperation.replace,
121-
this.key = 'key',
93+
required super.newValue,
94+
super.toggled = false,
95+
super.listOperation = ListOperation.replace,
96+
super.index = '0',
97+
super.mapOperation = MapOperation.replace,
98+
super.mapKey = 'key',
12299
}) : super(type: ActionType.setVariable);
123100

124101
@override
@@ -132,7 +109,7 @@ class SetVariableAction extends ActionModel
132109
ListOperation? listOperation,
133110
String? index,
134111
MapOperation? mapOperation,
135-
String? key,
112+
String? mapKey,
136113
}) =>
137114
SetVariableAction(
138115
variable: variable ?? this.variable,
@@ -141,7 +118,7 @@ class SetVariableAction extends ActionModel
141118
listOperation: listOperation ?? this.listOperation,
142119
index: index ?? this.index,
143120
mapOperation: mapOperation ?? this.mapOperation,
144-
key: key ?? this.key,
121+
mapKey: mapKey ?? this.mapKey,
145122
);
146123

147124
/// Creates a new [SetVariableAction] instance from a JSON data.

0 commit comments

Comments
 (0)