From 8c5fd661c64b4736a88a0dc2b416876c7711629c Mon Sep 17 00:00:00 2001 From: Rudrank Riyam Date: Sun, 24 Jul 2022 00:36:15 +0530 Subject: [PATCH] Add .all for all extended properties for both iOS 15 and iOS 16 --- .../MusadoraKit/Catalog/CatalogAlbum.swift | 12 +++++ .../MusadoraKit/Catalog/CatalogArtist.swift | 38 ++++++++++++++-- .../MusadoraKit/Catalog/CatalogCurator.swift | 8 ++++ .../Catalog/CatalogMusicVideo.swift | 6 +++ .../MusadoraKit/Catalog/CatalogPlaylist.swift | 12 +++++ .../Catalog/CatalogRadioShow.swift | 8 ++++ .../Catalog/CatalogRecordLabel.swift | 44 +++++++++++++++++-- Sources/MusadoraKit/Catalog/CatalogSong.swift | 9 +++- 8 files changed, 127 insertions(+), 10 deletions(-) diff --git a/Sources/MusadoraKit/Catalog/CatalogAlbum.swift b/Sources/MusadoraKit/Catalog/CatalogAlbum.swift index ed60d375d..5b4143a80 100644 --- a/Sources/MusadoraKit/Catalog/CatalogAlbum.swift +++ b/Sources/MusadoraKit/Catalog/CatalogAlbum.swift @@ -75,3 +75,15 @@ public extension MusadoraKit { return response.items } } + +extension Array where Element == PartialMusicAsyncProperty { + public static var all: Self { +#if compiler(>=5.7) + if #available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *) { + return [.artistURL, .genres, .artists, .audioVariants, .appearsOn, .otherVersions, .recordLabels, .relatedAlbums, .relatedVideos, .tracks] + } +#else + return [.artistURL, .genres, .artists, .appearsOn, .otherVersions, .recordLabels, .relatedAlbums, .relatedVideos, .tracks] +#endif + } +} diff --git a/Sources/MusadoraKit/Catalog/CatalogArtist.swift b/Sources/MusadoraKit/Catalog/CatalogArtist.swift index e1e15bc7a..b596aec8c 100644 --- a/Sources/MusadoraKit/Catalog/CatalogArtist.swift +++ b/Sources/MusadoraKit/Catalog/CatalogArtist.swift @@ -17,8 +17,7 @@ public extension MusadoraKit { /// - properties: Additional relationships to fetch with the artist. /// - Returns: `Artist` matching the given identifier. static func catalogArtist(id: MusicItemID, - with properties: [PartialMusicAsyncProperty] = []) async throws -> Artist - { + with properties: [PartialMusicAsyncProperty] = []) async throws -> Artist { var request = MusicCatalogResourceRequest(matching: \.id, equalTo: id) request.properties = properties let response = try await request.response() @@ -29,17 +28,48 @@ public extension MusadoraKit { return artist } + /// Fetch an artist from the Apple Music catalog by using its identifier with all properties. + /// - Parameters: + /// - id: The unique identifier for the artist. + /// - Returns: `Artist` matching the given identifier. + static func catalogArtist(id: MusicItemID) async throws -> Artist { + var request = MusicCatalogResourceRequest(matching: \.id, equalTo: id) + request.properties = .all + let response = try await request.response() + + guard let artist = response.items.first else { + throw MusadoraKitError.notFound(for: id.rawValue) + } + return artist + } + /// Fetch multiple artists from the Apple Music catalog by using their identifiers. /// - Parameters: /// - ids: The unique identifiers for the artists. /// - properties: Additional relationships to fetch with the artists. /// - Returns: `Artists` matching the given identifiers. static func catalogArtists(ids: [MusicItemID], - with properties: [PartialMusicAsyncProperty] = []) async throws -> Artists - { + with properties: [PartialMusicAsyncProperty] = []) async throws -> Artists { var request = MusicCatalogResourceRequest(matching: \.id, memberOf: ids) request.properties = properties let response = try await request.response() return response.items } + + /// Fetch multiple artists from the Apple Music catalog by using their identifiers with all properties. + /// - Parameters: + /// - ids: The unique identifiers for the artists. + /// - Returns: `Artists` matching the given identifiers. + static func catalogArtists(ids: [MusicItemID]) async throws -> Artists { + var request = MusicCatalogResourceRequest(matching: \.id, memberOf: ids) + request.properties = .all + let response = try await request.response() + return response.items + } +} + +extension Array where Element == PartialMusicAsyncProperty { + public static var all: Self { + [.genres, .station, .musicVideos, .albums, .playlists, .appearsOnAlbums, .fullAlbums, .featuredAlbums, .liveAlbums, .compilationAlbums, .featuredPlaylists, .latestRelease, .topSongs, .topMusicVideos, .similarArtists, .singles] + } } diff --git a/Sources/MusadoraKit/Catalog/CatalogCurator.swift b/Sources/MusadoraKit/Catalog/CatalogCurator.swift index dba38fb3e..36599da12 100644 --- a/Sources/MusadoraKit/Catalog/CatalogCurator.swift +++ b/Sources/MusadoraKit/Catalog/CatalogCurator.swift @@ -47,3 +47,11 @@ public extension MusadoraKit { return response.items } } + +@available(iOS 15.4, macOS 12.3, tvOS 15.4, *) +@available(watchOS, unavailable) +extension Array where Element == PartialMusicAsyncProperty { + public static var all: Self { + [.playlists] + } +} diff --git a/Sources/MusadoraKit/Catalog/CatalogMusicVideo.swift b/Sources/MusadoraKit/Catalog/CatalogMusicVideo.swift index 790f46b6c..e78171e48 100644 --- a/Sources/MusadoraKit/Catalog/CatalogMusicVideo.swift +++ b/Sources/MusadoraKit/Catalog/CatalogMusicVideo.swift @@ -75,3 +75,9 @@ public extension MusadoraKit { return response.items } } + +extension Array where Element == PartialMusicAsyncProperty { + public static var all: Self { + [.albums, .genres, .artists, .artistURL, .moreInGenre, .songs, .moreByArtist] + } +} diff --git a/Sources/MusadoraKit/Catalog/CatalogPlaylist.swift b/Sources/MusadoraKit/Catalog/CatalogPlaylist.swift index faebbeb1d..91391daa4 100644 --- a/Sources/MusadoraKit/Catalog/CatalogPlaylist.swift +++ b/Sources/MusadoraKit/Catalog/CatalogPlaylist.swift @@ -43,3 +43,15 @@ public extension MusadoraKit { return response.items } } + +extension Array where Element == PartialMusicAsyncProperty { + public static var all: Self { +#if compiler(>=5.7) + if #available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *) { + return [.tracks, .curator, .featuredArtists, .moreByCurator, .radioShow] + } +#else + return [.tracks, .featuredArtists, .moreByCurator] +#endif + } +} diff --git a/Sources/MusadoraKit/Catalog/CatalogRadioShow.swift b/Sources/MusadoraKit/Catalog/CatalogRadioShow.swift index b0bd1f0a2..aa1231512 100644 --- a/Sources/MusadoraKit/Catalog/CatalogRadioShow.swift +++ b/Sources/MusadoraKit/Catalog/CatalogRadioShow.swift @@ -47,3 +47,11 @@ public extension MusadoraKit { return response.items } } + +@available(iOS 15.4, macOS 12.3, tvOS 15.4, *) +@available(watchOS, unavailable) +extension Array where Element == PartialMusicAsyncProperty { + public static var all: Self { + [.playlists] + } +} diff --git a/Sources/MusadoraKit/Catalog/CatalogRecordLabel.swift b/Sources/MusadoraKit/Catalog/CatalogRecordLabel.swift index 4f60e9551..6f907ee6e 100644 --- a/Sources/MusadoraKit/Catalog/CatalogRecordLabel.swift +++ b/Sources/MusadoraKit/Catalog/CatalogRecordLabel.swift @@ -17,8 +17,7 @@ public extension MusadoraKit { /// - properties: Additional relationships to fetch with the record label. /// - Returns: `RecordLabel` matching the given identifier. static func catalogRecordLabel(id: MusicItemID, - with properties: [PartialMusicAsyncProperty] = []) async throws -> RecordLabel - { + with properties: [PartialMusicAsyncProperty] = []) async throws -> RecordLabel { var request = MusicCatalogResourceRequest(matching: \.id, equalTo: id) request.properties = properties let response = try await request.response() @@ -29,17 +28,54 @@ public extension MusadoraKit { return recordLabel } +#if compiler(>=5.7) + /// Fetch a record label from the Apple Music catalog by using its identifier with all properties. + /// - Parameters: + /// - id: The unique identifier for the record label. + /// - Returns: `RecordLabel` matching the given identifier. + static func catalogRecordLabel(id: MusicItemID) async throws -> RecordLabel { + var request = MusicCatalogResourceRequest(matching: \.id, equalTo: id) + request.properties = .all + let response = try await request.response() + + guard let recordLabel = response.items.first else { + throw MusadoraKitError.notFound(for: id.rawValue) + } + return recordLabel + } +#endif + /// Fetch multiple record labels from the Apple Music catalog by using their identifiers. /// - Parameters: /// - ids: The unique identifiers for the record labels. /// - properties: Additional relationships to fetch with the record labels. /// - Returns: `RecordLabels` matching the given identifiers. static func catalogRecordLabels(ids: [MusicItemID], - with properties: [PartialMusicAsyncProperty] = []) async throws -> RecordLabels - { + with properties: [PartialMusicAsyncProperty] = []) async throws -> RecordLabels { var request = MusicCatalogResourceRequest(matching: \.id, memberOf: ids) request.properties = properties let response = try await request.response() return response.items } + +#if compiler(>=5.7) + /// Fetch multiple record labels from the Apple Music catalog by using their identifiers with all properties. + /// - Parameters: + /// - ids: The unique identifiers for the record labels. + /// - Returns: `RecordLabels` matching the given identifiers. + static func catalogRecordLabels(ids: [MusicItemID]) async throws -> RecordLabels { + var request = MusicCatalogResourceRequest(matching: \.id, memberOf: ids) + request.properties = .all + let response = try await request.response() + return response.items + } +#endif +} + +#if compiler(>=5.7) +extension Array where Element == PartialMusicAsyncProperty { + public static var all: Self { + [.latestReleases, .topReleases] + } } +#endif diff --git a/Sources/MusadoraKit/Catalog/CatalogSong.swift b/Sources/MusadoraKit/Catalog/CatalogSong.swift index e02acbab2..d1a867271 100644 --- a/Sources/MusadoraKit/Catalog/CatalogSong.swift +++ b/Sources/MusadoraKit/Catalog/CatalogSong.swift @@ -90,9 +90,14 @@ public extension MusadoraKit { } } -@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *) extension Array where Element == PartialMusicAsyncProperty { public static var all: Self { - [.audioVariants, .albums, .artists, .composers, .genres, .musicVideos, .artistURL, .station] +#if compiler(>=5.7) + if #available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *) { + return [.audioVariants, .albums, .artists, .composers, .genres, .musicVideos, .artistURL, .station] + } +#else + return [.albums, .artists, .composers, .genres, .musicVideos, .artistURL, .station] +#endif } }