diff --git a/Sources/MusadoraKit/Library/LibrarySong.swift b/Sources/MusadoraKit/Library/LibrarySong.swift index 1f480bb3..570a6995 100644 --- a/Sources/MusadoraKit/Library/LibrarySong.swift +++ b/Sources/MusadoraKit/Library/LibrarySong.swift @@ -145,7 +145,6 @@ public extension MLibrary { return try await song.with(properties, preferredSource: .library) } -#if compiler(>=5.7) /// Access the total number of songs in the user's library. /// /// Use this property to retrieve the total number of songs in the user's library. @@ -158,43 +157,26 @@ public extension MLibrary { /// /// - Returns: An `Int` representing the total number of songs in the user's library. /// - Throws: An error if the retrieval fails, such as access restrictions or unavailable platform. - @available(iOS 16.0, tvOS 16.0, watchOS 9.0, macOS 14.0, macCatalyst 17.0, *) static var songsCount: Int { get async throws { - let request = MusicLibraryRequest() - let response = try await request.response() - return response.items.count - } - } -#else - /// Access the total number of songs in the user's library. - /// - /// Use this property to retrieve the total number of songs in the user's library. - /// The property returns an integer value representing the count of songs. - /// - /// Example usage: - /// - /// let count = try await MLibrary.songsCount - /// print("Total number of songs in the library: \(count)") - /// - /// - Returns: An `Int` representing the total number of songs in the user's library. - /// - Throws: An error if the retrieval fails, such as access restrictions or unavailable platform. - @available(macOS, unavailable) - @available(macCatalyst, unavailable) - @available(tvOS, unavailable) - @available(watchOS, unavailable) - static var songsCount: Int { - get async throws { - if let items = MPMediaQuery.songs().items { - return items.count + if #available(iOS 16.0, tvOS 16.0, watchOS 9.0, macOS 14.0, macCatalyst 17.0, *) { + let request = MusicLibraryRequest() + let response = try await request.response() + return response.items.count } else { - throw MediaPlayError.notFound(for: "songs") +#if os(iOS) + if let items = MPMediaQuery.songs().items { + return items.count + } else { + throw MediaPlayError.notFound(for: "songs") + } +#else + throw MediaPlayError.platformNotSupported +#endif } } } -#endif - /// Taken from https://github.com/marcelmendesfilho/MusadoraKit/blob/feature/improvements/Sources/MusadoraKit/Library/LibrarySong.swift /// Thanks @marcelmendesfilho! /// diff --git a/Sources/MusadoraKit/Library/MLibraryPlaylist.swift b/Sources/MusadoraKit/Library/MLibraryPlaylist.swift index e48933e5..cef8ca38 100644 --- a/Sources/MusadoraKit/Library/MLibraryPlaylist.swift +++ b/Sources/MusadoraKit/Library/MLibraryPlaylist.swift @@ -14,7 +14,7 @@ public struct MLibraryPlaylist: Codable, MusicItem { public let id: MusicItemID /// The attributes of the playlist. - public let attributes: Attributes + public var attributes: Attributes /// A structure containing the attributes of a playlist. public struct Attributes: Codable, Sendable { @@ -32,7 +32,7 @@ public struct MLibraryPlaylist: Codable, MusicItem { public let hasCatalog: Bool /// The play parameters for the playlist. - public let playParams: PlayParameters + public var playParams: MPlayParameters /// The description of the playlist, if available. public let description: Description? @@ -49,7 +49,7 @@ public struct MLibraryPlaylist: Codable, MusicItem { } /// A structure representing the play parameters of a playlist. - public struct PlayParameters: Codable, Sendable { + public struct MPlayParameters: Codable, Sendable { /// The identifier of the playlist. public let id: MusicItemID @@ -71,3 +71,18 @@ public struct MLibraryPlaylist: Codable, MusicItem { attributes.playParams.globalID?.rawValue } } + +@available(macOS 14.0, *) +@available(watchOS, unavailable) +extension MLibraryPlaylist: PlayableMusicItem { + public var playParameters: PlayParameters? { + get { + let parameters = try? JSONEncoder().encode(attributes.playParams) + + guard let parameters else { return nil } + + let playParams = try? JSONDecoder().decode(PlayParameters.self, from: parameters) + return playParams + } + } +} diff --git a/Sources/MusadoraKit/Models/MusadoraKitError.swift b/Sources/MusadoraKit/Models/MusadoraKitError.swift index 3e583c22..09b6e7e1 100644 --- a/Sources/MusadoraKit/Models/MusadoraKitError.swift +++ b/Sources/MusadoraKit/Models/MusadoraKitError.swift @@ -27,6 +27,7 @@ extension RatingsError: CustomStringConvertible { /// An enum representing the possible errors that can occur when playing a piece of media. public enum MediaPlayError: Error, Equatable { case notFound(for: String) + case platformNotSupported } extension MediaPlayError: CustomStringConvertible { @@ -34,6 +35,8 @@ extension MediaPlayError: CustomStringConvertible { switch self { case let .notFound(item): return "Not able to count the music items for \(item)." + case .platformNotSupported: + return "This is only available on iOS." } } }