-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathevent.dart
More file actions
204 lines (167 loc) · 5.07 KB
/
Copy pathevent.dart
File metadata and controls
204 lines (167 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import 'package:duit_kernel/duit_kernel.dart';
/// The [ServerEvent] class represents an event that was sent by the server.
///
/// The concrete event types are defined in sub-classes of [ServerEvent].
///
/// The [ServerEvent] class provides a static [parse] method for parsing a JSON map
/// into a concrete event object.
base class ServerEvent {
static late Parser<ServerEvent> _eventParser;
static set eventParser(Parser<ServerEvent> value) {
_eventParser = value;
}
final String type;
const ServerEvent({
required this.type,
});
static ServerEvent parseEvent(Map<String, dynamic> json) {
return _eventParser.parse(json);
}
}
final class NullEvent extends ServerEvent {
NullEvent() : super(type: "null_event");
}
/// Represents an update event.
final class UpdateEvent extends ServerEvent {
/// The updates associated with the event.
Map<String, dynamic> updates;
/// Constructs an [UpdateEvent] object with the specified updates.
///
/// The [updates] parameter is a map of key-value pairs representing the updates.
UpdateEvent({
required this.updates,
}) : super(type: "update");
/// Creates an [UpdateEvent] object from a JSON object.
///
/// The [json] parameter is a JSON object representing the update event.
/// Returns an [UpdateEvent] object if the JSON object is valid, otherwise throws an exception.
factory UpdateEvent.fromJson(Map<String, dynamic> json) {
return UpdateEvent(
updates: json["updates"] ?? const <String, dynamic>{},
);
}
}
final class NavigationEvent extends ServerEvent {
final String path;
final Map<String, dynamic> extra;
NavigationEvent({
required this.path,
required this.extra,
}) : super(type: "navigation");
factory NavigationEvent.fromJson(Map<String, dynamic> json) {
final source = DuitDataSource(json);
return NavigationEvent(
path: source.getString(key: "path"),
extra: source["extra"] ?? const <String, dynamic>{},
);
}
}
final class OpenUrlEvent extends ServerEvent {
final String url;
OpenUrlEvent({
required this.url,
}) : super(type: "openUrl");
factory OpenUrlEvent.fromJson(Map<String, dynamic> json) {
return OpenUrlEvent(
url: DuitDataSource(json).getString(key: "url"),
);
}
}
final class CustomEvent extends ServerEvent {
final String key;
final Map<String, dynamic> extra;
CustomEvent({
required this.key,
required this.extra,
}) : super(type: "custom");
factory CustomEvent.fromJson(Map<String, dynamic> json) {
final source = DuitDataSource(json);
return CustomEvent(
key: source.getString(key: "key"),
extra: source["extra"] ?? const <String, dynamic>{},
);
}
}
final class GroupMember {
final ServerEvent event;
GroupMember({
required this.event,
});
}
final class SequencedGroupMember extends GroupMember {
final Duration delay;
SequencedGroupMember({
required super.event,
required this.delay,
});
}
final class CommonEventGroup extends ServerEvent {
final List<GroupMember> events;
CommonEventGroup({
required this.events,
}) : super(type: "grouped");
factory CommonEventGroup.fromJson(Map<String, dynamic> json) {
final list = List<Map<String, dynamic>>.from(json["events"] ?? []);
final events = list.map(
(model) {
return GroupMember(
event: ServerEvent.parseEvent(
model["event"],
),
);
},
);
return CommonEventGroup(events: events.toList());
}
}
final class SequencedEventGroup extends ServerEvent {
final List<SequencedGroupMember> events;
SequencedEventGroup({
required this.events,
}) : super(type: "sequenced");
factory SequencedEventGroup.fromJson(Map<String, dynamic> json) {
final list = List<Map<String, dynamic>>.from(json["events"] ?? []);
final events = list.map(
(model) {
final source = DuitDataSource(model);
final delay = source.duration(key: "delay");
return SequencedGroupMember(
event: ServerEvent.parseEvent(source["event"]),
delay: delay,
);
},
);
return SequencedEventGroup(events: events.toList());
}
}
final class TimerEvent extends ServerEvent {
final Duration timerDelay;
final ServerEvent payload;
TimerEvent({
required this.timerDelay,
required this.payload,
}) : super(type: "timer");
factory TimerEvent.fromJson(Map<String, dynamic> json) {
final source = DuitDataSource(json);
return TimerEvent(
timerDelay: source.duration(key: "timerDelay"),
payload: ServerEvent.parseEvent(source["event"]),
);
}
}
final class CommandEvent extends ServerEvent {
final RemoteCommand command;
const CommandEvent({
required this.command,
}) : super(type: "command");
factory CommandEvent.fromJson(Map<String, dynamic> json) {
final source = DuitDataSource(json);
return CommandEvent(
command: RemoteCommand(
controllerId: source.getString(key: "controllerId"),
type: source.getString(key: "type"),
commandData: source["commandData"] ?? <String, dynamic>{},
),
);
}
}