1
1
mirror of https://github.com/danbee/persephone synced 2025-03-04 08:39:11 +00:00

Compare commits

..

3 Commits

Author SHA1 Message Date
f82d6b027a
Refactor the album art code to use promises 2019-03-03 17:44:56 -05:00
09b6e424b0
This is how the collection view should be updated
The way I was doing it before was resulting in reused views getting
updated by the code, which caused albums to appear in the wrong place.
2019-03-03 17:26:13 -05:00
190b2ed432
New blank album icon 2019-03-03 17:25:46 -05:00
12 changed files with 29 additions and 46 deletions

View File

@ -32,4 +32,4 @@
"template-rendering-intent" : "original",
"preserves-vector-representation" : true
}
}
}

View File

@ -27,7 +27,7 @@ class AlbumDataSource: NSObject, NSCollectionViewDataSource {
self.albums[indexPath.item].coverArt = image
DispatchQueue.main.async {
albumViewItem.setAlbum(self.albums[indexPath.item])
collectionView.reloadItems(at: [indexPath])
}
}
}

View File

@ -14,10 +14,11 @@ import PMKFoundation
class AlbumArtService: NSObject {
static var shared = AlbumArtService()
var session = URLSession(configuration: .default)
let cacheQueue = DispatchQueue(label: "albumArtCacheQueue", attributes: .concurrent)
let albumArtQueue = DispatchQueue(label: "albumArtCacheQueue", attributes: .concurrent)
func fetchAlbumArt(for album: AlbumItem, callback: @escaping (_ image: NSImage) -> Void) {
cacheQueue.async {
albumArtQueue.async {
print(album)
if !self.getCachedArtwork(for: album, callback: callback) {
self.getRemoteArtwork(for: album, callback: callback)
}
@ -35,7 +36,7 @@ class AlbumArtService: NSObject {
if FileManager.default.fileExists(atPath: cacheFilePath) {
guard let data = FileManager.default.contents(atPath: cacheFilePath),
let image = NSImage(data: data)
else { return false }
else { return true }
callback(image)
@ -65,50 +66,32 @@ class AlbumArtService: NSObject {
}
func getArtworkFromMusicBrainz(for album: AlbumItem, callback: @escaping (_ image: NSImage) -> Void) {
if var urlComponents = URLComponents(string: "https://musicbrainz.org/ws/2/release/") {
urlComponents.query = "query=artist:\(album.artist) AND release:\(album.title) AND country:US&limit=1&fmt=json"
guard var urlComponents = URLComponents(string: "https://musicbrainz.org/ws/2/release/")
else { return }
guard let searchURL = urlComponents.url
else { return }
urlComponents.query = "query=artist:\(album.artist) AND release:\(album.title) AND country:US&limit=1&fmt=json"
let releaseTask = session.dataTask(with: searchURL) { data, response, error in
if let _ = error {
return
}
guard let searchURL = urlComponents.url
else { return }
guard let httpResponse = response as? HTTPURLResponse,
(200...299).contains(httpResponse.statusCode) else {
return
}
if let mimeType = httpResponse.mimeType, mimeType == "application/json",
let data = data,
let json = try? JSON(data: data) {
let releaseId = json["releases"][0]["id"]
let coverURL = URLComponents(string: "https://coverartarchive.org/release/\(releaseId)/front-500")
let coverArtTask = self.session.dataTask(with: coverURL!.url!) { data, response, error in
guard let httpResponse = response as? HTTPURLResponse,
(200...299).contains(httpResponse.statusCode) else {
return
}
if let mimeType = httpResponse.mimeType, mimeType == "image/jpeg",
let data = data,
let coverImage = NSImage(data: data) {
self.cacheArtwork(for: album, data: data)
callback(coverImage)
}
}
coverArtTask.resume()
}
}
releaseTask.resume()
firstly {
URLSession.shared.dataTask(.promise, with: searchURL).validate()
}.compactMap {
JSON($0.data)
}.compactMap {
$0["releases"][0]["id"].string
}.compactMap {
URLComponents(string: "https://coverartarchive.org/release/\($0)/front-500")
}.then { (urlComponents: URLComponents?) -> Promise<(data: Data, response: URLResponse)> in
let url = urlComponents!.url
return URLSession.shared.dataTask(.promise, with: url!).validate()
}.compactMap {
self.cacheArtwork(for: album, data: $0.data)
return NSImage(data: $0.data)
}.done {
callback($0)
}.catch { (error: Error) in
self.cacheArtwork(for: album, data: Data())
}
}
}

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 982 B

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.3 KiB

After

Width:  |  Height:  |  Size: 7.1 KiB

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 976 B

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 KiB

After

Width:  |  Height:  |  Size: 7.4 KiB

Binary file not shown.