I'm attempting to write a function similar to the example shared over in https://developer.apple.com/forums/thread/700683 for playing a MusicItem
using the SystemMusicPlayer
. Currently, this function starts like this:
func play<MusicItemType>(item: MusicItemType) async throws
where
MusicItemType: FilterableMusicItem,
MusicItemType: Decodable,
MusicItemType: PlayableMusicItem
{
var queue : MusicKit.MusicPlayer.Queue?
switch MusicItemType.self {
case is Playlist.Type:
let request = MusicCatalogResourceRequest<Playlist>(matching: \.id, equalTo: MusicItemID("\(item.id)"))
let response = try await request.response()
if let responseItem = response.items.first {
queue = MusicKit.MusicPlayer.Queue(for: [responseItem])
}
case is Album.Type:
let request = MusicCatalogResourceRequest<Album>(matching: \.id, equalTo: MusicItemID("\(item.id)"))
let response = try await request.response()
if let responseItem = response.items.first {
queue = MusicKit.MusicPlayer.Queue(for: [responseItem])
}
...
I'd love to clean up this case statement and instead construct a MusicCatalogResourceRequest
using the generic MusicItemType
type parameter, but I haven't yet been able to figure how to construct a KeyPath
that matches the KeyPath<MusicItemType.FilterType, Value>
type expected. Does anyone have any examples of interacting with MusicCatalogResourceRequest
in a generic fashion that they'd be willing to share? Thanks!