Skip to content

Commit

Permalink
fix: artist page error KRTirtho#1018
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Jan 22, 2024
1 parent 27057ea commit 8cd650b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
56 changes: 56 additions & 0 deletions lib/services/custom_spotify_endpoints/spotify_endpoints.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
7 changes: 5 additions & 2 deletions lib/services/queries/artist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
);
}
Expand Down Expand Up @@ -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,
);
Expand Down

0 comments on commit 8cd650b

Please sign in to comment.