Skip to content

Commit

Permalink
Refactor SpinifyImpl to update JSON codec support in SpinifySubscript…
Browse files Browse the repository at this point in the history
…ionMixin
  • Loading branch information
PlugFox committed Jun 13, 2024
1 parent d15c375 commit e3af287
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 80 deletions.
75 changes: 0 additions & 75 deletions lib/src/model/state.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:convert';

import 'package:meta/meta.dart';

/// {@template state}
Expand Down Expand Up @@ -68,54 +66,6 @@ sealed class SpinifyState extends _$SpinifyStateBase {
/// {@macro state}
factory SpinifyState.closed({DateTime? timestamp}) = SpinifyState$Closed;

/// Restore state from JSON
/// {@macro state}
factory SpinifyState.fromJson(Map<String, Object?> json) => switch ((
json['type']?.toString().trim().toLowerCase(),
json['timestamp'] ?? DateTime.now().microsecondsSinceEpoch,
json['url'],
)) {
('disconnected', int timestamp, _) => SpinifyState.disconnected(
timestamp: DateTime.fromMicrosecondsSinceEpoch(timestamp),
),
('connecting', int timestamp, String url) => SpinifyState.connecting(
url: url,
timestamp: DateTime.fromMicrosecondsSinceEpoch(timestamp),
),
('connected', int timestamp, String url) => SpinifyState.connected(
url: url,
timestamp: DateTime.fromMicrosecondsSinceEpoch(timestamp),
client: json['client']?.toString(),
version: json['version']?.toString(),
expires: switch (json['expires']) {
bool expires => expires,
_ => false,
},
ttl: switch (json['ttl']) {
int ttl => DateTime.fromMicrosecondsSinceEpoch(ttl),
_ => null,
},
pingInterval: switch (json['pingInterval']) {
int pingInterval => Duration(seconds: pingInterval),
_ => null,
},
sendPong: switch (json['sendPong']) {
bool sendPong => sendPong,
_ => null,
},
session: json['session']?.toString(),
node: json['node']?.toString(),
data: switch (json['data']) {
String data when data.isNotEmpty => base64Decode(data),
_ => null,
},
),
('closed', int timestamp, _) => SpinifyState.closed(
timestamp: DateTime.fromMicrosecondsSinceEpoch(timestamp),
),
_ => throw FormatException('Unknown state: $json'),
};

@override
String toString() => type;
}
Expand Down Expand Up @@ -163,11 +113,6 @@ final class SpinifyState$Disconnected extends SpinifyState {
}) =>
disconnected(this);

@override
Map<String, Object?> toJson() => <String, Object?>{
...super.toJson(),
};

@override
int get hashCode => timestamp.millisecondsSinceEpoch * 10 + 0;

Expand Down Expand Up @@ -307,20 +252,6 @@ final class SpinifyState$Connected extends SpinifyState {
}) =>
connected(this);

@override
Map<String, Object?> toJson() => <String, Object?>{
...super.toJson(),
if (client != null) 'client': client,
if (version != null) 'version': version,
'expires': expires,
if (ttl != null) 'ttl': ttl?.microsecondsSinceEpoch,
if (pingInterval != null) 'pingInterval': pingInterval?.inSeconds,
if (sendPong != null) 'sendPong': sendPong,
if (session != null) 'session': session,
if (node != null) 'node': node,
if (data != null) 'data': base64Encode(data!),
};

@override
int get hashCode => timestamp.millisecondsSinceEpoch * 10 + 2;

Expand Down Expand Up @@ -445,10 +376,4 @@ abstract base class _$SpinifyStateBase {
connected: connected ?? (_) => null,
closed: closed ?? (_) => null,
);

Map<String, Object?> toJson() => <String, Object?>{
'type': type,
'timestamp': timestamp.toUtc().toIso8601String(),
if (url != null) 'url': url,
};
}
5 changes: 0 additions & 5 deletions lib/src/model/subscription_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,4 @@ abstract base class _$SpinifySubscriptionStateBase {
subscribing: subscribing ?? (_) => null,
subscribed: subscribed ?? (_) => null,
);

Map<String, Object?> toJson() => <String, Object?>{
'type': type,
'timestamp': timestamp.toUtc().toIso8601String(),
};
}

0 comments on commit e3af287

Please sign in to comment.