mirror of
https://github.com/danbee/persephone
synced 2025-03-04 08:39:11 +00:00
Ensure artwork is fetched when the pref is changed
This commit is contained in:
parent
01371db6cb
commit
f014c4f929
@ -40,6 +40,7 @@ class AlbumViewController: NSViewController,
|
|||||||
albumCollectionView.dataSource = dataSource
|
albumCollectionView.dataSource = dataSource
|
||||||
|
|
||||||
preferences.addObserver(self, forKeyPath: "mpdLibraryDir")
|
preferences.addObserver(self, forKeyPath: "mpdLibraryDir")
|
||||||
|
preferences.addObserver(self, forKeyPath: "fetchMissingArtworkFromInternet")
|
||||||
}
|
}
|
||||||
|
|
||||||
override func viewWillLayout() {
|
override func viewWillLayout() {
|
||||||
@ -66,6 +67,9 @@ class AlbumViewController: NSViewController,
|
|||||||
switch keyPath {
|
switch keyPath {
|
||||||
case "mpdLibraryDir":
|
case "mpdLibraryDir":
|
||||||
albumCollectionView.reloadData()
|
albumCollectionView.reloadData()
|
||||||
|
case "fetchMissingArtworkFromInternet":
|
||||||
|
dataSource.resetCoverArt()
|
||||||
|
albumCollectionView.reloadData()
|
||||||
default:
|
default:
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -75,7 +79,7 @@ class AlbumViewController: NSViewController,
|
|||||||
guard let albums = notification.userInfo?[Notification.albumsKey] as? [MPDClient.MPDAlbum]
|
guard let albums = notification.userInfo?[Notification.albumsKey] as? [MPDClient.MPDAlbum]
|
||||||
else { return }
|
else { return }
|
||||||
|
|
||||||
dataSource.albums = albums.map { Album(mpdAlbum: $0, coverArt: nil) }
|
dataSource.albums = albums.map { Album(mpdAlbum: $0) }
|
||||||
albumCollectionView.reloadData()
|
albumCollectionView.reloadData()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -72,11 +72,11 @@ class QueueViewController: NSViewController,
|
|||||||
|
|
||||||
albumArtService.fetchBigAlbumArt()
|
albumArtService.fetchBigAlbumArt()
|
||||||
.done() {
|
.done() {
|
||||||
guard let image = $0 else { return }
|
if let image = $0 {
|
||||||
|
self.queueAlbumArtImage.image = image
|
||||||
self.queueAlbumArtImage.image = image.toFitBox(
|
} else {
|
||||||
size: NSSize(width: 500, height: 500)
|
self.queueAlbumArtImage.image = NSImage.defaultCoverArt
|
||||||
)
|
}
|
||||||
}
|
}
|
||||||
.cauterize()
|
.cauterize()
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -16,6 +16,14 @@ class AlbumDataSource: NSObject, NSCollectionViewDataSource {
|
|||||||
return albums.count
|
return albums.count
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func resetCoverArt() {
|
||||||
|
albums = albums.map {
|
||||||
|
var album = $0
|
||||||
|
album.coverArtFetched = false
|
||||||
|
return album
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
|
func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
|
||||||
let item = collectionView.makeItem(withIdentifier: .albumViewItem, for: indexPath)
|
let item = collectionView.makeItem(withIdentifier: .albumViewItem, for: indexPath)
|
||||||
guard let albumViewItem = item as? AlbumViewItem else { return item }
|
guard let albumViewItem = item as? AlbumViewItem else { return item }
|
||||||
@ -23,7 +31,9 @@ class AlbumDataSource: NSObject, NSCollectionViewDataSource {
|
|||||||
albumViewItem.view.wantsLayer = true
|
albumViewItem.view.wantsLayer = true
|
||||||
albumViewItem.setAlbum(albums[indexPath.item])
|
albumViewItem.setAlbum(albums[indexPath.item])
|
||||||
|
|
||||||
if albums[indexPath.item].coverArt == nil {
|
if albums[indexPath.item].coverArt == nil &&
|
||||||
|
!albums[indexPath.item].coverArtFetched {
|
||||||
|
|
||||||
AppDelegate.mpdClient.getAlbumFirstSong(for: albums[indexPath.item].mpdAlbum) {
|
AppDelegate.mpdClient.getAlbumFirstSong(for: albums[indexPath.item].mpdAlbum) {
|
||||||
guard let song = $0 else { return }
|
guard let song = $0 else { return }
|
||||||
|
|
||||||
@ -31,6 +41,7 @@ class AlbumDataSource: NSObject, NSCollectionViewDataSource {
|
|||||||
.fetchAlbumArt()
|
.fetchAlbumArt()
|
||||||
.done { image in
|
.done { image in
|
||||||
self.albums[indexPath.item].coverArt = image
|
self.albums[indexPath.item].coverArt = image
|
||||||
|
self.albums[indexPath.item].coverArtFetched = true
|
||||||
|
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
collectionView.reloadItems(at: [indexPath])
|
collectionView.reloadItems(at: [indexPath])
|
||||||
|
|||||||
@ -1,24 +0,0 @@
|
|||||||
//
|
|
||||||
// String.swift
|
|
||||||
// Persephone
|
|
||||||
//
|
|
||||||
// Created by Daniel Barber on 2019/3/01.
|
|
||||||
// Copyright © 2019 Dan Barber. All rights reserved.
|
|
||||||
//
|
|
||||||
|
|
||||||
import Foundation
|
|
||||||
|
|
||||||
extension String {
|
|
||||||
func sha1() -> String {
|
|
||||||
let data = self.data(using: String.Encoding.utf8)!
|
|
||||||
var digest = [UInt8](repeating: 0, count:Int(CC_SHA1_DIGEST_LENGTH))
|
|
||||||
|
|
||||||
data.withUnsafeBytes {
|
|
||||||
_ = CC_SHA1($0, CC_LONG(data.count), &digest)
|
|
||||||
}
|
|
||||||
|
|
||||||
let hexBytes = digest.map { String(format: "%02hhx", $0) }
|
|
||||||
|
|
||||||
return hexBytes.joined()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -20,6 +20,6 @@ struct Song {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var album: Album {
|
var album: Album {
|
||||||
return Album(mpdAlbum: mpdSong.album, coverArt: nil)
|
return Album(mpdAlbum: mpdSong.album)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -42,6 +42,8 @@ class AlbumArtService {
|
|||||||
} else {
|
} else {
|
||||||
return image
|
return image
|
||||||
}
|
}
|
||||||
|
}.recover { (_) -> Guarantee<NSImage?> in
|
||||||
|
return .value(nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,13 +56,7 @@ class AlbumArtService {
|
|||||||
artwork.map(Promise.value) ?? self.getRemoteArtwork()
|
artwork.map(Promise.value) ?? self.getRemoteArtwork()
|
||||||
}.compactMap(on: artworkQueue) {
|
}.compactMap(on: artworkQueue) {
|
||||||
return self.sizeAndCacheImage($0).map(Optional.some)
|
return self.sizeAndCacheImage($0).map(Optional.some)
|
||||||
}.recover { error in
|
}.recover { _ in
|
||||||
switch error {
|
|
||||||
case RemoteArtworkError.noArtworkAvailable:
|
|
||||||
self.cacheArtwork(data: Data())
|
|
||||||
default:
|
|
||||||
break
|
|
||||||
}
|
|
||||||
return .value(nil)
|
return .value(nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user