Skip to content

Commit

Permalink
Add library music items count
Browse files Browse the repository at this point in the history
  • Loading branch information
rudrankriyam committed Jul 14, 2022
1 parent bfec4e5 commit a3b6510
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Sources/MusadoraKit/Library/LibraryAlbum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import MusicKit
import MediaPlayer

public extension MusadoraKit {
/// Fetch an album from the user's library by using its identifier.
Expand Down Expand Up @@ -67,4 +68,20 @@ public extension MusadoraKit {
return response.items
}
}

static var libraryAlbumsCount: Int {
get async throws {
if #available(iOS 16, tvOS 16.0, watchOS 9.0, *) {
let request = MusicLibraryRequest<Album>()
let response = try await request.response()
return response.items.count
} else {
if let items = MPMediaQuery.albums().items {
return items.count
} else {
throw MediaPlayError.notFound(for: "albums")
}
}
}
}
}
17 changes: 17 additions & 0 deletions Sources/MusadoraKit/Library/LibraryArtists.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import MusicKit
import MediaPlayer

public extension MusadoraKit {
/// Fetch an artist from the user's library by using its identifier.
Expand Down Expand Up @@ -42,4 +43,20 @@ public extension MusadoraKit {
let response = try await request.response()
return response.items
}

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")
}
}
}
}
}
17 changes: 17 additions & 0 deletions Sources/MusadoraKit/Library/LibraryPlaylist.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import MusicKit
import MediaPlayer

public extension MusadoraKit {
/// Fetch a playlist from the user's library by using its identifier.
Expand Down Expand Up @@ -42,4 +43,20 @@ public extension MusadoraKit {
let response = try await request.response()
return response.items
}

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")
}
}
}
}
}
17 changes: 17 additions & 0 deletions Sources/MusadoraKit/Library/LibrarySong.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import MusicKit
import MediaPlayer

public extension MusadoraKit {
/// Fetch a song from the user's library by using its identifier.
Expand Down Expand Up @@ -42,4 +43,20 @@ public extension MusadoraKit {
let response = try await request.response()
return response.items
}

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")
}
}
}
}
}
13 changes: 13 additions & 0 deletions Sources/MusadoraKit/Models/MusadoraKitError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ extension RatingsError: CustomStringConvertible {
}
}

public enum MediaPlayError: Error, Equatable {
case notFound(for: String)
}

extension MediaPlayError: CustomStringConvertible {
public var description: String {
switch self {
case let .notFound(item):
return "Not able to count the music items for \(item)."
}
}
}

public enum MusadoraKitError: Error, Equatable {
case notFound(for: String)
case typeMissing
Expand Down

0 comments on commit a3b6510

Please sign in to comment.