Skip to content

Commit

Permalink
Add iOS 16+ library music item count and recently played/added librar…
Browse files Browse the repository at this point in the history
…y items
  • Loading branch information
rudrankriyam committed Aug 29, 2022
1 parent 0b981a4 commit 1e0cd28
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 25 deletions.
46 changes: 46 additions & 0 deletions Sources/MusadoraKit/Library/LibraryAlbum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ public extension MusadoraKit {
return response.items
}

#if compiler(>=5.7)
@available(iOS 16.0, tvOS 16.0, watchOS 9.0, *)
@available(macOS, unavailable)
@available(macCatalyst, unavailable)
static var libraryAlbumsCount: Int {
get async throws {
let request = MusicLibraryRequest<Album>()
let response = try await request.response()
return response.items.count
}
}
#else
@available(macOS, unavailable)
@available(macCatalyst, unavailable)
@available(tvOS, unavailable)
Expand All @@ -85,6 +97,7 @@ public extension MusadoraKit {
}
}
}
#endif

/// Taken from https://github.com/marcelmendesfilho/MusadoraKit/blob/feature/improvements/Sources/MusadoraKit/Library/LibraryAlbum.swift
/// Thanks @marcelmendesfilho!
Expand All @@ -108,3 +121,36 @@ public extension MusadoraKit {
return response
}
}

#if compiler(>=5.7)
@available(iOS 16.0, tvOS 16.0, watchOS 9.0, *)
@available(macOS, unavailable)
@available(macCatalyst, unavailable)
public extension MusadoraKit {
/// Fetch recently added albums from the user's library sorted by the date added.
/// - Parameters:
/// - limit: The number of albums returned.
/// - Returns: `Albums` for the given limit.
static func recentlyAddedAlbums(limit: Int = 10, offset: Int = 0) async throws -> Albums {
var request = MusicLibraryRequest<Album>()
request.limit = limit
request.offset = offset
request.sort(by: \.libraryAddedDate, ascending: false)
let response = try await request.response()
return response.items
}

/// Fetch recently played albums from the user's library sorted by the date added.
/// - Parameters:
/// - limit: The number of albums returned.
/// - Returns: `Albums` for the given limit.
static func recentlyLibraryPlayedAlbums(limit: Int = 0, offset: Int = 0) async throws -> Albums {
var request = MusicLibraryRequest<Album>()
request.limit = limit
request.offset = offset
request.sort(by: \.lastPlayedDate, ascending: false)
let response = try await request.response()
return response.items
}
}
#endif
23 changes: 15 additions & 8 deletions Sources/MusadoraKit/Library/LibraryArtists.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public extension MusadoraKit {
}
return artist
}
#else
#else
static func libraryArtist(id: MusicItemID) async throws -> Artist {
let request = MusicLibraryResourceRequest<Artist>(matching: \.id, equalTo: id)
let response = try await request.response()
Expand All @@ -43,7 +43,7 @@ public extension MusadoraKit {
}
return artist
}
#endif
#endif

/// Fetch all artists from the user's library in alphabetical order.
/// - Parameters:
Expand All @@ -66,23 +66,30 @@ public extension MusadoraKit {
return response.items
}

#if compiler(>=5.7)
@available(iOS 16.0, tvOS 16.0, watchOS 9.0, *)
@available(macOS, unavailable)
@available(macCatalyst, unavailable)
static var libraryArtistsCount: Int {
get async throws {
let request = MusicLibraryRequest<Artist>()
let response = try await request.response()
return response.items.count
}
}
#else
@available(macOS, unavailable)
@available(macCatalyst, unavailable)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
static var libraryArtistsCount: Int {
get async throws {
// if #available(iOS 16, tvOS 16.0, watchOS 9.0, *) {
// let request = MusicLibraryRequest<Artist>()
// let response = try await request.response()
// return response.items.count
// } else {
if let items = MPMediaQuery.artists().items {
return items.count
} else {
throw MediaPlayError.notFound(for: "artists")
}
// }
}
}
#endif
}
52 changes: 46 additions & 6 deletions Sources/MusadoraKit/Library/LibraryPlaylist.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,32 @@ public extension MusadoraKit {
return response.items
}

#if compiler(>=5.7)
@available(iOS 16.0, tvOS 16.0, watchOS 9.0, *)
@available(macOS, unavailable)
@available(macCatalyst, unavailable)
static var libraryPlaylistsCount: Int {
get async throws {
let request = MusicLibraryRequest<Playlist>()
let response = try await request.response()
return response.items.count
}
}
#else
@available(macOS, unavailable)
@available(macCatalyst, unavailable)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
static var libraryPlaylistsCount: Int {
get async throws {
// if #available(iOS 16, tvOS 16.0, watchOS 9.0, *) {
// let request = MusicLibraryRequest<Playlist>()
// let response = try await request.response()
// return response.items.count
// } else {
if let items = MPMediaQuery.playlists().items {
return items.count
} else {
throw MediaPlayError.notFound(for: "playlists")
}
// }
}
}
#endif

/// Add a playlist to the user's library by using its identifier.
/// - Parameters:
Expand All @@ -84,3 +91,36 @@ public extension MusadoraKit {
return response
}
}

#if compiler(>=5.7)
@available(iOS 16.0, tvOS 16.0, watchOS 9.0, *)
@available(macOS, unavailable)
@available(macCatalyst, unavailable)
public extension MusadoraKit {
/// Fetch recently added playlists from the user's library sorted by the date added.
/// - Parameters:
/// - limit: The number of playlists returned.
/// - Returns: `Playlists` for the given limit.
static func recentlyAddedPlaylists(limit: Int = 10, offset: Int = 0) async throws -> Playlists {
var request = MusicLibraryRequest<Playlist>()
request.limit = limit
request.offset = offset
request.sort(by: \.libraryAddedDate, ascending: false)
let response = try await request.response()
return response.items
}

/// Fetch recently played playlists from the user's library sorted by the date added.
/// - Parameters:
/// - limit: The number of playlists returned.
/// - Returns: `Playlists` for the given limit.
static func recentlyLibraryPlayedPlaylists(limit: Int = 0, offset: Int = 0) async throws -> Playlists {
var request = MusicLibraryRequest<Playlist>()
request.limit = limit
request.offset = offset
request.sort(by: \.lastPlayedDate, ascending: false)
let response = try await request.response()
return response.items
}
}
#endif
55 changes: 44 additions & 11 deletions Sources/MusadoraKit/Library/LibrarySong.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,32 @@ public extension MusadoraKit {
}
#endif

#if compiler(>=5.7)
@available(iOS 16.0, tvOS 16.0, watchOS 9.0, *)
@available(macOS, unavailable)
@available(macCatalyst, unavailable)
static var librarySongsCount: Int {
get async throws {
let request = MusicLibraryRequest<Song>()
let response = try await request.response()
return response.items.count
}
}
#else
@available(macOS, unavailable)
@available(macCatalyst, unavailable)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
static var librarySongsCount: Int {
get async throws {
// if #available(iOS 16, tvOS 16.0, watchOS 9.0, *) {
// let request = MusicLibraryRequest<Song>()
// let response = try await request.response()
// return response.items.count
// } else {
if let items = MPMediaQuery.songs().items {
return items.count
} else {
throw MediaPlayError.notFound(for: "songs")
}
// }
}
}
#endif

/// Taken from https://github.com/marcelmendesfilho/MusadoraKit/blob/feature/improvements/Sources/MusadoraKit/Library/LibrarySong.swift
/// Thanks @marcelmendesfilho!
Expand Down Expand Up @@ -148,18 +155,44 @@ public extension MusadoraKit {
}

#if compiler(>=5.7)
@available(iOS 16.0, tvOS 16.0, watchOS 9.0, *)
@available(macOS, unavailable)
@available(macCatalyst, unavailable)
public extension MusadoraKit {
/// Fetch recently added songs from the user's library sorted by the date added.
/// - Parameters:
/// - limit: The number of songs returned.
/// - Returns: `Songs` for the given limit.
@available(iOS 16.0, tvOS 16.0, watchOS 9.0, *)
@available(macOS, unavailable)
@available(macCatalyst, unavailable)
static func recentlyAddedSongs(limit: Int = 10) async throws -> Songs {
static func recentlyAddedSongs(limit: Int = 10, offset: Int = 0) async throws -> Songs {
var request = MusicLibraryRequest<Song>()
request.limit = limit
request.offset = offset
request.sort(by: \.libraryAddedDate, ascending: false)
let response = try await request.response()
return response.items
}

/// Fetch recently played songs from the user's library sorted by the date added.
/// - Parameters:
/// - limit: The number of songs returned.
/// - Returns: `Songs` for the given limit.
static func recentlyLibraryPlayedSongs(limit: Int = 0, offset: Int = 0) async throws -> Songs {
var request = MusicLibraryRequest<Song>()
request.limit = limit
request.sort(by: \.libraryAddedDate, ascending: true)
request.offset = offset
request.sort(by: \.lastPlayedDate, ascending: false)
let response = try await request.response()
return response.items
}

/// Fetch recently played songs sorted by the date added.
/// - Parameters:
/// - limit: The number of songs returned.
/// - Returns: `Songs` for the given limit.
static func recentlyPlayedSongs(limit: Int = 0, offset: Int = 0) async throws -> Songs {
var request = MusicRecentlyPlayedRequest<Song>()
request.limit = limit
request.offset = offset
let response = try await request.response()
return response.items
}
Expand Down

0 comments on commit 1e0cd28

Please sign in to comment.