import 'package:json_annotation/json_annotation.dart'; part 'spotify_track.g.dart'; @JsonSerializable() class SpotifyTrack { final String id; final String name; final List artists; final SpotifyAlbum album; @JsonKey(name: 'duration_ms') final int durationMs; @JsonKey(name: 'external_urls') final Map externalUrls; @JsonKey(name: 'preview_url') final String? previewUrl; SpotifyTrack({ required this.id, required this.name, required this.artists, required this.album, required this.durationMs, required this.externalUrls, this.previewUrl, }); factory SpotifyTrack.fromJson(Map json) => _$SpotifyTrackFromJson(json); Map toJson() => _$SpotifyTrackToJson(this); String get artistNames => artists.map((artist) => artist.name).join(', '); String get duration { final minutes = (durationMs / 60000).floor(); final seconds = ((durationMs % 60000) / 1000).floor(); return '$minutes:${seconds.toString().padLeft(2, '0')}'; } String get imageUrl => album.images.isNotEmpty ? album.images.first.url : ''; } @JsonSerializable() class SpotifyArtist { final String id; final String name; SpotifyArtist({ required this.id, required this.name, }); factory SpotifyArtist.fromJson(Map json) => _$SpotifyArtistFromJson(json); Map toJson() => _$SpotifyArtistToJson(this); } @JsonSerializable() class SpotifyAlbum { final String id; final String name; final List images; SpotifyAlbum({ required this.id, required this.name, required this.images, }); factory SpotifyAlbum.fromJson(Map json) => _$SpotifyAlbumFromJson(json); Map toJson() => _$SpotifyAlbumToJson(this); } @JsonSerializable() class SpotifyImage { final int height; final int width; final String url; SpotifyImage({ required this.height, required this.width, required this.url, }); factory SpotifyImage.fromJson(Map json) => _$SpotifyImageFromJson(json); Map toJson() => _$SpotifyImageToJson(this); } @JsonSerializable() class SpotifySearchResponse { final SpotifyTracks tracks; SpotifySearchResponse({required this.tracks}); factory SpotifySearchResponse.fromJson(Map json) => _$SpotifySearchResponseFromJson(json); Map toJson() => _$SpotifySearchResponseToJson(this); } @JsonSerializable() class SpotifyTracks { final List items; final int total; SpotifyTracks({ required this.items, required this.total, }); factory SpotifyTracks.fromJson(Map json) => _$SpotifyTracksFromJson(json); Map toJson() => _$SpotifyTracksToJson(this); }