Peter-Schorn / SpotifyAPI

A Swift library for the Spotify web API. Supports all endpoints.
https://peter-schorn.github.io/SpotifyAPI/documentation/spotifywebapi
MIT License
269 stars 33 forks source link

`album.artists?.first?.images` is always nil #44

Closed dyoustra closed 1 year ago

dyoustra commented 1 year ago

album.artists?.first?.images seems to always be a nil value, as does playlist.owner?.images. For every album/playlist I try, my view is unable to fetch the artist's/playlist owner's image. I saw that in the documentation it says that Artist.images is "Only available for the full artist object." Am I running into this issue here?

let collection: Any
@State var artistImage = Image(systemName: "music.mic")
@State var cancellables: [AnyCancellable] = []

var body: some View {
    if let album = collection as? Album {
        HStack {
            artistImage
                .resizable()
                .aspectRatio(contentMode: .fit)
                .onAppear {
                    album.artists?.first?.images?.first?.load().sink(receiveCompletion: { _ in }, receiveValue: { image in
                        artistImage = image
                    }).store(in: &cancellables)
                }
            ForEach(album.artists ?? [Artist(name: "No Artist")], id: \.id) { artist in
                Text(artist.name)
                    .font(.title2)
                    .fontWeight(.semibold)
            }
        }

    } else if let playlist = collection as? Playlist<PlaylistItemsReference> {
        HStack {
            artistImage
                .resizable()
                .aspectRatio(contentMode: .fit)
                .onAppear {
                    playlist.owner?.images?.first?.load().sink(receiveCompletion: { _ in }, receiveValue: { image in
                        artistImage = image
                    }).store(in: &cancellables)
                }
            Text(playlist.owner?.displayName ?? "Unknown Owner")
                .font(.title2)
                .fontWeight(.semibold)
        }
    }
}
Peter-Schorn commented 1 year ago

Please post the code that initializes collection. Try making the requests outside a SwiftUI view. The playlist owner is a Spotify user who usually does not have an image.

Peter-Schorn commented 1 year ago

Album.artists returns the simplified artist objects, so album.artists.images returns nil.