Skip to content

Commit

Permalink
Make MLibraryPlaylist conform to PlayableMusicItem
Browse files Browse the repository at this point in the history
  • Loading branch information
rudrankriyam committed Aug 3, 2023
1 parent b04052f commit 3f32b9c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
44 changes: 13 additions & 31 deletions Sources/MusadoraKit/Library/LibrarySong.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<Song>()
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<Song>()
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!
///
Expand Down
21 changes: 18 additions & 3 deletions Sources/MusadoraKit/Library/MLibraryPlaylist.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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?
Expand All @@ -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
Expand All @@ -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
}
}
}
3 changes: 3 additions & 0 deletions Sources/MusadoraKit/Models/MusadoraKitError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ 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 {
public var description: String {
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."
}
}
}
Expand Down

0 comments on commit 3f32b9c

Please sign in to comment.