fix: artist page error #1018

This commit is contained in:
Kingkor Roy Tirtho 2024-01-22 17:51:12 +06:00
parent 27057ea0c8
commit 8cd650b07e
2 changed files with 61 additions and 2 deletions

View File

@ -162,4 +162,60 @@ class CustomSpotifyEndpoints {
result["tracks"].map((track) => Track.fromJson(track)).toList(),
);
}
Future<Artist> artist({required String id}) async {
final pathQuery = "$_baseUrl/artists/$id";
final res = await _client.get(
Uri.parse(pathQuery),
headers: {
"content-type": "application/json",
if (accessToken.isNotEmpty) "authorization": "Bearer $accessToken",
"accept": "application/json",
},
);
final data = jsonDecode(res.body);
return Artist.fromJson(_purifyArtistResponse(data));
}
Future<List<Artist>> relatedArtists({required String id}) async {
final pathQuery = "$_baseUrl/artists/$id/related-artists";
final res = await _client.get(
Uri.parse(pathQuery),
headers: {
"content-type": "application/json",
if (accessToken.isNotEmpty) "authorization": "Bearer $accessToken",
"accept": "application/json",
},
);
final data = jsonDecode(res.body);
return List.castFrom<dynamic, Artist>(
data["artists"]
.map((artist) => Artist.fromJson(_purifyArtistResponse(artist)))
.toList(),
);
}
Map<String, dynamic> _purifyArtistResponse(Map<String, dynamic> data) {
if (data["popularity"] != null) {
data["popularity"] = data["popularity"].toInt();
}
if (data["followers"]?["total"] != null) {
data["followers"]["total"] = data["followers"]["total"].toInt();
}
if (data["images"] != null) {
data["images"] = data["images"].map((e) {
e["height"] = e["height"].toInt();
e["width"] = e["width"].toInt();
return e;
}).toList();
}
return data;
}
}

View File

@ -4,6 +4,7 @@ import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:spotify/spotify.dart';
import 'package:spotube/hooks/spotify/use_spotify_infinite_query.dart';
import 'package:spotube/hooks/spotify/use_spotify_query.dart';
import 'package:spotube/provider/custom_spotify_endpoint_provider.dart';
import 'package:spotube/provider/user_preferences/user_preferences_provider.dart';
import 'package:spotube/services/wikipedia/wikipedia.dart';
import 'package:wikipedia_api/wikipedia_api.dart';
@ -15,9 +16,10 @@ class ArtistQueries {
WidgetRef ref,
String artist,
) {
final customSpotify = ref.watch(customSpotifyEndpointProvider);
return useSpotifyQuery<Artist, dynamic>(
"artist-profile/$artist",
(spotify) => spotify.artists.get(artist),
(spotify) => customSpotify.artist(id: artist),
ref: ref,
);
}
@ -125,10 +127,11 @@ class ArtistQueries {
WidgetRef ref,
String artist,
) {
final customSpotify = ref.watch(customSpotifyEndpointProvider);
return useSpotifyQuery<Iterable<Artist>, dynamic>(
"artist-related-artist-query/$artist",
(spotify) {
return spotify.artists.relatedArtists(artist);
return customSpotify.relatedArtists(id: artist);
},
ref: ref,
);