forked from KRTirtho/spotube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws_event.dart
374 lines (317 loc) · 8.73 KB
/
ws_event.dart
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
part of 'connect.dart';
enum WsEvent {
error,
volume,
removeTrack,
addTrack,
reorder,
shuffle,
loop,
seek,
duration,
queue,
position,
playing,
resume,
pause,
load,
next,
previous,
jump,
stop;
static WsEvent fromString(String value) {
return WsEvent.values.firstWhere((e) => e.name == value);
}
}
typedef EventCallback<T> = FutureOr<void> Function(T event);
class WebSocketEvent<T> {
final WsEvent type;
final T data;
WebSocketEvent(this.type, this.data);
factory WebSocketEvent.fromJson(
Map<String, dynamic> json,
T Function(dynamic) fromJson,
) {
return WebSocketEvent(
WsEvent.fromString(json["type"]),
fromJson(json["data"]),
);
}
String toJson() {
return jsonEncode({
"type": type.name,
"data": data,
});
}
Future<void> onPosition(
EventCallback<WebSocketPositionEvent> callback,
) async {
if (type == WsEvent.position) {
await callback(WebSocketPositionEvent.fromJson({"data": data}));
}
}
Future<void> onPlaying(
EventCallback<WebSocketPlayingEvent> callback,
) async {
if (type == WsEvent.playing) {
await callback(WebSocketPlayingEvent(data as bool));
}
}
Future<void> onResume(
EventCallback<WebSocketResumeEvent> callback,
) async {
if (type == WsEvent.resume) {
await callback(WebSocketResumeEvent());
}
}
Future<void> onPause(
EventCallback<WebSocketPauseEvent> callback,
) async {
if (type == WsEvent.pause) {
await callback(WebSocketPauseEvent());
}
}
Future<void> onStop(
EventCallback<WebSocketStopEvent> callback,
) async {
if (type == WsEvent.stop) {
await callback(WebSocketStopEvent());
}
}
Future<void> onLoad(
EventCallback<WebSocketLoadEvent> callback,
) async {
if (type == WsEvent.load) {
await callback(
WebSocketLoadEvent(
WebSocketLoadEventData.fromJson(data as Map<String, dynamic>),
),
);
}
}
Future<void> onNext(
EventCallback<WebSocketNextEvent> callback,
) async {
if (type == WsEvent.next) {
await callback(WebSocketNextEvent());
}
}
Future<void> onPrevious(
EventCallback<WebSocketPreviousEvent> callback,
) async {
if (type == WsEvent.previous) {
await callback(WebSocketPreviousEvent());
}
}
Future<void> onJump(
EventCallback<WebSocketJumpEvent> callback,
) async {
if (type == WsEvent.jump) {
await callback(WebSocketJumpEvent(data as int));
}
}
Future<void> onError(
EventCallback<WebSocketErrorEvent> callback,
) async {
if (type == WsEvent.error) {
await callback(WebSocketErrorEvent(data as String));
}
}
Future<void> onQueue(
EventCallback<WebSocketQueueEvent> callback,
) async {
if (type == WsEvent.queue) {
await callback(
WebSocketQueueEvent.fromJson(data as Map<String, dynamic>),
);
}
}
Future<void> onDuration(
EventCallback<WebSocketDurationEvent> callback,
) async {
if (type == WsEvent.duration) {
await callback(
WebSocketDurationEvent(
Duration(seconds: data as int),
),
);
}
}
Future<void> onSeek(
EventCallback<WebSocketSeekEvent> callback,
) async {
if (type == WsEvent.seek) {
await callback(
WebSocketSeekEvent(
Duration(seconds: data as int),
),
);
}
}
Future<void> onShuffle(
EventCallback<WebSocketShuffleEvent> callback,
) async {
if (type == WsEvent.shuffle) {
await callback(WebSocketShuffleEvent(data as bool));
}
}
Future<void> onLoop(
EventCallback<WebSocketLoopEvent> callback,
) async {
if (type == WsEvent.loop) {
await callback(
WebSocketLoopEvent(
PlaybackLoopMode.fromString(data as String),
),
);
}
}
Future<void> onRemoveTrack(
EventCallback<WebSocketRemoveTrackEvent> callback,
) async {
if (type == WsEvent.removeTrack) {
await callback(WebSocketRemoveTrackEvent(data as String));
}
}
Future<void> onAddTrack(
EventCallback<WebSocketAddTrackEvent> callback,
) async {
if (type == WsEvent.addTrack) {
await callback(
WebSocketAddTrackEvent.fromJson(data as Map<String, dynamic>));
}
}
Future<void> onReorder(
EventCallback<WebSocketReorderEvent> callback,
) async {
if (type == WsEvent.reorder) {
await callback(
WebSocketReorderEvent.fromJson(data as Map<String, dynamic>));
}
}
Future<void> onVolume(
EventCallback<WebSocketVolumeEvent> callback,
) async {
if (type == WsEvent.volume) {
await callback(WebSocketVolumeEvent(data as double));
}
}
}
class WebSocketLoopEvent extends WebSocketEvent<PlaybackLoopMode> {
WebSocketLoopEvent(PlaybackLoopMode data) : super(WsEvent.loop, data);
WebSocketLoopEvent.fromJson(Map<String, dynamic> json)
: super(
WsEvent.loop, PlaybackLoopMode.fromString(json["data"] as String));
@override
String toJson() {
return jsonEncode({
"type": type.name,
"data": data.name,
});
}
}
class WebSocketPositionEvent extends WebSocketEvent<Duration> {
WebSocketPositionEvent(Duration data) : super(WsEvent.position, data);
WebSocketPositionEvent.fromJson(Map<String, dynamic> json)
: super(WsEvent.position, Duration(seconds: json["data"] as int));
@override
String toJson() {
return jsonEncode({
"type": type.name,
"data": data.inSeconds,
});
}
}
class WebSocketDurationEvent extends WebSocketEvent<Duration> {
WebSocketDurationEvent(Duration data) : super(WsEvent.duration, data);
WebSocketDurationEvent.fromJson(Map<String, dynamic> json)
: super(WsEvent.duration, Duration(seconds: json["data"] as int));
@override
String toJson() {
return jsonEncode({
"type": type.name,
"data": data.inSeconds,
});
}
}
class WebSocketSeekEvent extends WebSocketEvent<Duration> {
WebSocketSeekEvent(Duration data) : super(WsEvent.seek, data);
WebSocketSeekEvent.fromJson(Map<String, dynamic> json)
: super(WsEvent.seek, Duration(seconds: json["data"] as int));
@override
String toJson() {
return jsonEncode({
"type": type.name,
"data": data.inSeconds,
});
}
}
class WebSocketShuffleEvent extends WebSocketEvent<bool> {
WebSocketShuffleEvent(bool data) : super(WsEvent.shuffle, data);
}
class WebSocketPlayingEvent extends WebSocketEvent<bool> {
WebSocketPlayingEvent(bool data) : super(WsEvent.playing, data);
}
class WebSocketResumeEvent extends WebSocketEvent<void> {
WebSocketResumeEvent() : super(WsEvent.resume, null);
}
class WebSocketPauseEvent extends WebSocketEvent<void> {
WebSocketPauseEvent() : super(WsEvent.pause, null);
}
class WebSocketStopEvent extends WebSocketEvent<void> {
WebSocketStopEvent() : super(WsEvent.stop, null);
}
class WebSocketNextEvent extends WebSocketEvent<void> {
WebSocketNextEvent() : super(WsEvent.next, null);
}
class WebSocketPreviousEvent extends WebSocketEvent<void> {
WebSocketPreviousEvent() : super(WsEvent.previous, null);
}
class WebSocketJumpEvent extends WebSocketEvent<int> {
WebSocketJumpEvent(int data) : super(WsEvent.jump, data);
}
class WebSocketErrorEvent extends WebSocketEvent<String> {
WebSocketErrorEvent(String data) : super(WsEvent.error, data);
}
class WebSocketQueueEvent extends WebSocketEvent<ProxyPlaylist> {
WebSocketQueueEvent(ProxyPlaylist data) : super(WsEvent.queue, data);
factory WebSocketQueueEvent.fromJson(Map<String, dynamic> json) =>
WebSocketQueueEvent(
ProxyPlaylist.fromJsonRaw(json),
);
}
class WebSocketRemoveTrackEvent extends WebSocketEvent<String> {
WebSocketRemoveTrackEvent(String data) : super(WsEvent.removeTrack, data);
}
class WebSocketAddTrackEvent extends WebSocketEvent<Track> {
WebSocketAddTrackEvent(Track data) : super(WsEvent.addTrack, data);
WebSocketAddTrackEvent.fromJson(Map<String, dynamic> json)
: super(
WsEvent.addTrack,
Track.fromJson(json["data"] as Map<String, dynamic>),
);
}
typedef ReorderData = ({int oldIndex, int newIndex});
class WebSocketReorderEvent extends WebSocketEvent<ReorderData> {
WebSocketReorderEvent(ReorderData data) : super(WsEvent.reorder, data);
factory WebSocketReorderEvent.fromJson(Map<String, dynamic> json) =>
WebSocketReorderEvent(
(
oldIndex: json["oldIndex"] as int,
newIndex: json["newIndex"] as int,
),
);
@override
String toJson() {
return jsonEncode({
"type": type.name,
"data": {
"oldIndex": data.oldIndex,
"newIndex": data.newIndex,
},
});
}
}
class WebSocketVolumeEvent extends WebSocketEvent<double> {
WebSocketVolumeEvent(double data) : super(WsEvent.volume, data);
}