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 = 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( WebSocketLoadEventData.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), ); } } Future onDuration( EventCallback callback, ) async { if (type == WsEvent.duration) { await callback( WebSocketDurationEvent( Duration(seconds: data as int), ), ); } } Future onSeek( EventCallback callback, ) async { if (type == WsEvent.seek) { await callback( WebSocketSeekEvent( Duration(seconds: data as int), ), ); } } Future onShuffle( EventCallback callback, ) async { if (type == WsEvent.shuffle) { await callback(WebSocketShuffleEvent(data as bool)); } } Future onLoop( EventCallback callback, ) async { if (type == WsEvent.loop) { await callback( WebSocketLoopEvent( PlaylistMode.values.firstWhere((e) => e.name == data as String), ), ); } } Future onRemoveTrack( EventCallback callback, ) async { if (type == WsEvent.removeTrack) { await callback(WebSocketRemoveTrackEvent(data as String)); } } Future onAddTrack( EventCallback callback, ) async { if (type == WsEvent.addTrack) { await callback( WebSocketAddTrackEvent.fromJson(data as Map)); } } Future onReorder( EventCallback callback, ) async { if (type == WsEvent.reorder) { await callback( WebSocketReorderEvent.fromJson(data as Map)); } } Future onVolume( EventCallback callback, ) async { if (type == WsEvent.volume) { await callback(WebSocketVolumeEvent(data as double)); } } } class WebSocketLoopEvent extends WebSocketEvent { WebSocketLoopEvent(PlaylistMode data) : super(WsEvent.loop, data); WebSocketLoopEvent.fromJson(Map json) : super( WsEvent.loop, PlaylistMode.values.firstWhere( (e) => e.name == json["data"] as String, ), ); @override String toJson() { return jsonEncode({ "type": type.name, "data": data.name, }); } } 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 WebSocketDurationEvent extends WebSocketEvent { WebSocketDurationEvent(Duration data) : super(WsEvent.duration, data); WebSocketDurationEvent.fromJson(Map 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 { WebSocketSeekEvent(Duration data) : super(WsEvent.seek, data); WebSocketSeekEvent.fromJson(Map 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 { WebSocketShuffleEvent(bool data) : super(WsEvent.shuffle, data); } 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(AudioPlayerState data) : super(WsEvent.queue, data); factory WebSocketQueueEvent.fromJson(Map json) => WebSocketQueueEvent( AudioPlayerState.fromJson(json), ); } class WebSocketRemoveTrackEvent extends WebSocketEvent { WebSocketRemoveTrackEvent(String data) : super(WsEvent.removeTrack, data); } class WebSocketAddTrackEvent extends WebSocketEvent { WebSocketAddTrackEvent(Track data) : super(WsEvent.addTrack, data); WebSocketAddTrackEvent.fromJson(Map json) : super( WsEvent.addTrack, Track.fromJson(json["data"] as Map), ); } typedef ReorderData = ({int oldIndex, int newIndex}); class WebSocketReorderEvent extends WebSocketEvent { WebSocketReorderEvent(ReorderData data) : super(WsEvent.reorder, data); factory WebSocketReorderEvent.fromJson(Map 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 { WebSocketVolumeEvent(double data) : super(WsEvent.volume, data); }