part of 'connect.dart'; enum WsEvent { error, 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 = FutureOr Function(T event); class WebSocketEvent { final WsEvent type; final T data; WebSocketEvent(this.type, this.data); factory WebSocketEvent.fromJson( Map json, T Function(dynamic) fromJson, ) { return WebSocketEvent( WsEvent.fromString(json["type"]), fromJson(json["data"]), ); } String toJson() { return jsonEncode({ "type": type.name, "data": data, }); } Future onPosition( EventCallback callback, ) async { if (type == WsEvent.position) { await callback(WebSocketPositionEvent.fromJson({"data": data})); } } Future onPlaying( EventCallback callback, ) async { if (type == WsEvent.playing) { await callback(WebSocketPlayingEvent(data as bool)); } } Future onResume( EventCallback callback, ) async { if (type == WsEvent.resume) { await callback(WebSocketResumeEvent()); } } Future onPause( EventCallback callback, ) async { if (type == WsEvent.pause) { await callback(WebSocketPauseEvent()); } } Future onStop( EventCallback callback, ) async { if (type == WsEvent.stop) { await callback(WebSocketStopEvent()); } } Future onLoad( EventCallback callback, ) async { if (type == WsEvent.load) { await callback(WebSocketLoadEvent.fromJson(data as Map)); } } Future onNext( EventCallback callback, ) async { if (type == WsEvent.next) { await callback(WebSocketNextEvent()); } } Future onPrevious( EventCallback callback, ) async { if (type == WsEvent.previous) { await callback(WebSocketPreviousEvent()); } } Future onJump( EventCallback callback, ) async { if (type == WsEvent.jump) { await callback(WebSocketJumpEvent(data as int)); } } Future onError( EventCallback callback, ) async { if (type == WsEvent.error) { await callback(WebSocketErrorEvent(data as String)); } } Future onQueue( EventCallback callback, ) async { if (type == WsEvent.queue) { await callback( WebSocketQueueEvent.fromJson(data as Map), ); } } } class WebSocketPositionEvent extends WebSocketEvent { WebSocketPositionEvent(Duration data) : super(WsEvent.position, data); WebSocketPositionEvent.fromJson(Map json) : super(WsEvent.position, Duration(seconds: json["data"] as int)); @override String toJson() { return jsonEncode({ "type": type.name, "data": data.inSeconds, }); } } class WebSocketPlayingEvent extends WebSocketEvent { WebSocketPlayingEvent(bool data) : super(WsEvent.playing, data); } class WebSocketResumeEvent extends WebSocketEvent { WebSocketResumeEvent() : super(WsEvent.resume, null); } class WebSocketPauseEvent extends WebSocketEvent { WebSocketPauseEvent() : super(WsEvent.pause, null); } class WebSocketStopEvent extends WebSocketEvent { WebSocketStopEvent() : super(WsEvent.stop, null); } class WebSocketNextEvent extends WebSocketEvent { WebSocketNextEvent() : super(WsEvent.next, null); } class WebSocketPreviousEvent extends WebSocketEvent { WebSocketPreviousEvent() : super(WsEvent.previous, null); } class WebSocketJumpEvent extends WebSocketEvent { WebSocketJumpEvent(int data) : super(WsEvent.jump, data); } class WebSocketErrorEvent extends WebSocketEvent { WebSocketErrorEvent(String data) : super(WsEvent.error, data); } class WebSocketQueueEvent extends WebSocketEvent { WebSocketQueueEvent(ProxyPlaylist data) : super(WsEvent.queue, data); factory WebSocketQueueEvent.fromJson(Map json) => WebSocketQueueEvent( ProxyPlaylist.fromJsonRaw(json), ); }