import 'package:equatable/equatable.dart'; import 'album_model.dart'; import 'folder_model.dart'; import 'playlist_model.dart'; import 'track_model.dart'; class SearchResultsModel extends Equatable { final List tracks; final List albums; final List artists; final List folders; final List playlists; const SearchResultsModel({ this.tracks = const [], this.albums = const [], this.artists = const [], this.folders = const [], this.playlists = const [], }); SearchResultsModel copyWith({ List? tracks, List? albums, List? artists, List? folders, List? playlists, }) { return SearchResultsModel( tracks: tracks ?? this.tracks, albums: albums ?? this.albums, artists: artists ?? this.artists, folders: folders ?? this.folders, playlists: playlists ?? this.playlists, ); } factory SearchResultsModel.fromJson(Map json) { return SearchResultsModel( tracks: (json['tracks'] as List?) ?.map((track) => TrackModel.fromJson(track)) .toList() ?? [], albums: (json['albums'] as List?) ?.map((album) => AlbumModel.fromJson(album)) .toList() ?? [], artists: (json['artists'] as List?) ?.map((artist) => ArtistModel.fromJson(artist)) .toList() ?? [], folders: (json['folders'] as List?) ?.map((folder) => FolderModel.fromJson(folder)) .toList() ?? [], playlists: (json['playlists'] as List?) ?.map((playlist) => PlaylistModel.fromJson(playlist)) .toList() ?? [], ); } Map toJson() { return { 'tracks': tracks.map((track) => track.toJson()).toList(), 'albums': albums.map((album) => album.toJson()).toList(), 'artists': artists.map((artist) => artist.toJson()).toList(), 'folders': folders.map((folder) => folder.toJson()).toList(), 'playlists': playlists.map((playlist) => playlist.toJson()).toList(), }; } bool get isEmpty => tracks.isEmpty && albums.isEmpty && artists.isEmpty && folders.isEmpty && playlists.isEmpty; bool get isNotEmpty => !isEmpty; @override List get props => [ tracks, albums, artists, folders, playlists, ]; } class TopSearchResultsModel extends Equatable { final List topResults; final SearchResultsModel allResults; const TopSearchResultsModel({ required this.topResults, required this.allResults, }); factory TopSearchResultsModel.fromJson(Map json) { return TopSearchResultsModel( topResults: (json['top_results'] as List?) ?.map((item) => TopResultItemModel.fromJson(item)) .toList() ?? [], allResults: SearchResultsModel.fromJson(json['all_results'] ?? {}), ); } Map toJson() { return { 'top_results': topResults.map((item) => item.toJson()).toList(), 'all_results': allResults.toJson(), }; } @override List get props => [topResults, allResults]; } class TopResultItemModel extends Equatable { final String type; // 'track', 'album', 'artist', 'folder', 'playlist' final String title; final String subtitle; final String? image; final dynamic data; // The actual model object const TopResultItemModel({ required this.type, required this.title, required this.subtitle, this.image, this.data, }); factory TopResultItemModel.fromJson(Map json) { return TopResultItemModel( type: json['type'] ?? '', title: json['title'] ?? '', subtitle: json['subtitle'] ?? '', image: json['image'], data: json['data'], ); } Map toJson() { return { 'type': type, 'title': title, 'subtitle': subtitle, 'image': image, 'data': data, }; } @override List get props => [type, title, subtitle, image, data]; }