mirror of
https://github.com/danbee/persephone
synced 2025-03-04 08:39:11 +00:00
Compare commits
No commits in common. "0e71714260de4f2f8682cbac3e3d94f9199f9d84" and "17ed1cdecc36299d34def5234bfa11ea35313514" have entirely different histories.
0e71714260
...
17ed1cdecc
@ -11,8 +11,6 @@ import Cocoa
|
||||
class AlbumViewController: NSViewController,
|
||||
NSCollectionViewDelegate,
|
||||
NSCollectionViewDelegateFlowLayout {
|
||||
var preferences = Preferences()
|
||||
|
||||
let paddingWidth: CGFloat = 40
|
||||
let gutterWidth: CGFloat = 20
|
||||
|
||||
@ -38,8 +36,6 @@ class AlbumViewController: NSViewController,
|
||||
)
|
||||
|
||||
albumCollectionView.dataSource = dataSource
|
||||
|
||||
preferences.addObserver(self, forKeyPath: "mpdLibraryDir")
|
||||
}
|
||||
|
||||
override func viewWillLayout() {
|
||||
@ -57,20 +53,6 @@ class AlbumViewController: NSViewController,
|
||||
layout.setScrollPosition()
|
||||
}
|
||||
|
||||
override func observeValue(
|
||||
forKeyPath keyPath: String?,
|
||||
of object: Any?,
|
||||
change: [NSKeyValueChangeKey : Any]?,
|
||||
context: UnsafeMutableRawPointer?
|
||||
) {
|
||||
switch keyPath {
|
||||
case "mpdLibraryDir":
|
||||
albumCollectionView.reloadData()
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
@objc func updateAlbums(_ notification: Notification) {
|
||||
guard let albums = notification.userInfo?[Notification.albumsKey] as? [MPDClient.Album]
|
||||
else { return }
|
||||
|
||||
@ -36,7 +36,7 @@ class QueueViewController: NSViewController,
|
||||
let newQueuePos = queueView.selectedRow - 1
|
||||
|
||||
if newQueuePos >= 0 {
|
||||
AppDelegate.mpdClient.playTrack(at: newQueuePos)
|
||||
AppDelegate.mpdClient.playTrack(queuePos: newQueuePos)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -13,24 +13,4 @@ extension NSImage {
|
||||
static let pauseIcon = NSImage(named: "pauseButton")
|
||||
|
||||
static let defaultCoverArt = NSImage(named: "blankAlbum")
|
||||
|
||||
func toFitBox(size: NSSize) -> NSImage {
|
||||
let newImage = NSImage(size: size)
|
||||
newImage.lockFocus()
|
||||
self.draw(in: newImage.alignmentRect)
|
||||
newImage.unlockFocus()
|
||||
return newImage
|
||||
}
|
||||
|
||||
func jpegData(compressionQuality: CGFloat) -> Data? {
|
||||
guard let image = cgImage(forProposedRect: nil, context: nil, hints: nil)
|
||||
else { return nil }
|
||||
|
||||
let bitmapImageRep = NSBitmapImageRep(cgImage: image)
|
||||
|
||||
return bitmapImageRep.representation(
|
||||
using: .jpeg,
|
||||
properties: [.compressionFactor: compressionQuality]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@ -18,12 +18,8 @@ extension MPDClient {
|
||||
queueCommand(command: .playAlbum, userData: ["album": album])
|
||||
}
|
||||
|
||||
func getAlbumURI(for album: Album, callback: @escaping (String?) -> Void) {
|
||||
queueCommand(
|
||||
command: .getAlbumURI,
|
||||
priority: .low,
|
||||
userData: ["album": album, "callback": callback]
|
||||
)
|
||||
func getAlbumURI(for album: Album) {
|
||||
queueCommand(command: .getAlbumURI, userData: ["album": album])
|
||||
}
|
||||
|
||||
func sendPlayAlbum(_ album: Album) {
|
||||
@ -69,31 +65,30 @@ extension MPDClient {
|
||||
self.delegate?.didLoadAlbums(mpdClient: self, albums: albums)
|
||||
}
|
||||
|
||||
func albumURI(for album: Album, callback: (String?) -> Void) {
|
||||
func albumURI(for album: Album) -> String? {
|
||||
var songURI: String?
|
||||
|
||||
guard isConnected else { return }
|
||||
guard isConnected else { return nil }
|
||||
|
||||
print("Getting URI")
|
||||
mpd_search_db_songs(self.connection, true)
|
||||
mpd_search_add_tag_constraint(self.connection, MPD_OPERATOR_DEFAULT, MPD_TAG_ALBUM, album.title)
|
||||
mpd_search_add_tag_constraint(self.connection, MPD_OPERATOR_DEFAULT, MPD_TAG_ALBUM_ARTIST, album.artist)
|
||||
mpd_search_add_tag_constraint(self.connection, MPD_OPERATOR_DEFAULT, MPD_TAG_TRACK, "1")
|
||||
|
||||
mpd_search_commit(self.connection)
|
||||
print("Performed search")
|
||||
|
||||
while let mpdSong = mpd_recv_song(self.connection) {
|
||||
let song = Song(mpdSong)
|
||||
print(song)
|
||||
|
||||
if songURI == nil {
|
||||
songURI = song.uriString
|
||||
}
|
||||
}
|
||||
print("Got URI")
|
||||
|
||||
callback(
|
||||
songURI?
|
||||
.split(separator: "/")
|
||||
.dropLast()
|
||||
.joined(separator: "/")
|
||||
)
|
||||
return songURI
|
||||
}
|
||||
}
|
||||
|
||||
@ -24,22 +24,12 @@ extension MPDClient {
|
||||
sendStop()
|
||||
case .playPause:
|
||||
sendPlay()
|
||||
case .seekCurrentSong:
|
||||
guard let timeInSeconds = userData["timeInSeconds"] as? Float
|
||||
else { return }
|
||||
sendSeekCurrentSong(timeInSeconds: timeInSeconds)
|
||||
|
||||
// Status commands
|
||||
case .fetchStatus:
|
||||
sendRunStatus()
|
||||
|
||||
// Queue commands
|
||||
case .fetchQueue:
|
||||
sendFetchQueue()
|
||||
case .playTrack:
|
||||
guard let queuePos = userData["queuePos"] as? Int
|
||||
else { return }
|
||||
sendPlayTrack(at: queuePos)
|
||||
|
||||
// Album commands
|
||||
case .fetchAllAlbums:
|
||||
@ -48,10 +38,8 @@ extension MPDClient {
|
||||
guard let album = userData["album"] as? Album else { return }
|
||||
sendPlayAlbum(album)
|
||||
case .getAlbumURI:
|
||||
guard let album = userData["album"] as? Album,
|
||||
let callback = userData["callback"] as? (String?) -> Void
|
||||
else { return }
|
||||
albumURI(for: album, callback: callback)
|
||||
guard let album = userData["album"] as? Album else { return }
|
||||
_ = getAlbumURI(for: album)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -18,13 +18,17 @@ extension MPDClient {
|
||||
}
|
||||
|
||||
func idle() {
|
||||
if !self.isIdle && self.commandsQueued == 0 {
|
||||
mpd_send_idle(self.connection)
|
||||
self.isIdle = true
|
||||
let idleOperation = BlockOperation {
|
||||
if !self.isIdle && self.commandsQueued == 0 {
|
||||
mpd_send_idle(self.connection)
|
||||
self.isIdle = true
|
||||
|
||||
let result = mpd_recv_idle(self.connection, true)
|
||||
self.handleIdleResult(result)
|
||||
let result = mpd_recv_idle(self.connection, true)
|
||||
self.handleIdleResult(result)
|
||||
}
|
||||
}
|
||||
idleOperation.queuePriority = .veryLow
|
||||
commandQueue.addOperation(idleOperation)
|
||||
}
|
||||
|
||||
func handleIdleResult(_ result: mpd_idle) {
|
||||
|
||||
@ -14,12 +14,16 @@ extension MPDClient {
|
||||
sendCommand(command: .fetchQueue)
|
||||
}
|
||||
|
||||
func playTrack(at queuePos: Int) {
|
||||
queueCommand(command: .playTrack, userData: ["queuePos": queuePos])
|
||||
}
|
||||
func playTrack(queuePos: Int) {
|
||||
guard isConnected else { return }
|
||||
|
||||
func sendPlayTrack(at queuePos: Int) {
|
||||
mpd_run_play_pos(self.connection, UInt32(queuePos))
|
||||
noIdle()
|
||||
let commandOperation = BlockOperation { [unowned self] in
|
||||
mpd_run_play_pos(self.connection, UInt32(queuePos))
|
||||
}
|
||||
commandOperation.queuePriority = .veryHigh
|
||||
commandQueue.addOperation(commandOperation)
|
||||
idle()
|
||||
}
|
||||
|
||||
func sendFetchQueue() {
|
||||
|
||||
@ -27,10 +27,11 @@ extension MPDClient {
|
||||
}
|
||||
|
||||
func seekCurrentSong(timeInSeconds: Float) {
|
||||
queueCommand(
|
||||
command: .seekCurrentSong,
|
||||
userData: ["timeInSeconds": timeInSeconds]
|
||||
)
|
||||
noIdle()
|
||||
commandQueue.addOperation { [unowned self] in
|
||||
mpd_run_seek_current(self.connection, timeInSeconds, false)
|
||||
}
|
||||
idle()
|
||||
}
|
||||
|
||||
func sendNextTrack() {
|
||||
@ -61,7 +62,4 @@ extension MPDClient {
|
||||
}
|
||||
}
|
||||
|
||||
func sendSeekCurrentSong(timeInSeconds: Float) {
|
||||
mpd_run_seek_current(self.connection, timeInSeconds, false)
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,8 +22,8 @@ class MPDClient {
|
||||
var commandsQueued: UInt = 0
|
||||
|
||||
enum Command {
|
||||
case prevTrack, nextTrack, playPause, stop, seekCurrentSong,
|
||||
fetchStatus, fetchQueue, playTrack, fetchAllAlbums,
|
||||
case prevTrack, nextTrack, playPause, stop,
|
||||
fetchStatus, fetchQueue, fetchAllAlbums,
|
||||
playAlbum, getAlbumURI
|
||||
}
|
||||
|
||||
@ -40,15 +40,13 @@ class MPDClient {
|
||||
guard isConnected else { return }
|
||||
|
||||
noIdle()
|
||||
|
||||
let commandOperation = BlockOperation() { [unowned self] in
|
||||
self.commandsQueued -= 1
|
||||
self.sendCommand(command: command, userData: userData)
|
||||
|
||||
self.idle()
|
||||
}
|
||||
commandOperation.queuePriority = priority
|
||||
commandsQueued += 1
|
||||
commandQueue.addOperation(commandOperation)
|
||||
idle()
|
||||
}
|
||||
}
|
||||
|
||||
@ -58,10 +58,6 @@ struct Preferences {
|
||||
return mpdLibraryDir ?? mpdLibraryDirDefault
|
||||
}
|
||||
|
||||
var expandedMpdLibraryDir: String {
|
||||
return NSString(string: mpdLibraryDirOrDefault).expandingTildeInPath
|
||||
}
|
||||
|
||||
func addObserver(_ observer: NSObject, forKeyPath keyPath: String) {
|
||||
preferences.addObserver(observer, forKeyPath: keyPath, options: .new, context: nil)
|
||||
}
|
||||
|
||||
@ -13,16 +13,18 @@ import PMKFoundation
|
||||
|
||||
class AlbumArtService: NSObject {
|
||||
static var shared = AlbumArtService()
|
||||
var preferences = Preferences()
|
||||
|
||||
var session = URLSession(configuration: .default)
|
||||
let cacheQueue = DispatchQueue(label: "albumArtCacheQueue", attributes: .concurrent)
|
||||
let filesystemQueue = DispatchQueue(label: "albumArtFilesystemQueue", attributes: .concurrent)
|
||||
|
||||
func fetchAlbumArt(for album: AlbumItem, callback: @escaping (_ image: NSImage) -> Void) {
|
||||
cacheQueue.async { [unowned self] in
|
||||
//print("Trying cache")
|
||||
if !self.getCachedArtwork(for: album, callback: callback) {
|
||||
self.getArtworkFromFilesystem(for: album, callback: callback)
|
||||
// self.filesystemQueue.async {
|
||||
// _ = self.getArtworkFromFilesystem(for: album, callback: callback)
|
||||
// }
|
||||
// if !self.getArtworkFromFilesystem(for: album, callback: callback) {
|
||||
// // self.getRemoteArtwork(for: album, callback: callback)
|
||||
// }
|
||||
@ -51,48 +53,14 @@ class AlbumArtService: NSObject {
|
||||
}
|
||||
}
|
||||
|
||||
func getArtworkFromFilesystem(
|
||||
for album: AlbumItem,
|
||||
callback: @escaping (_ image: NSImage) -> Void
|
||||
) {
|
||||
let coverArtFilenames = [
|
||||
"folder.jpg",
|
||||
"cover.jpg",
|
||||
"\(album.artist) - \(album.title).jpg"
|
||||
]
|
||||
|
||||
AppDelegate.mpdClient.getAlbumURI(
|
||||
for: album.album,
|
||||
callback: { (_ albumURI: String?) in
|
||||
guard let albumURI = albumURI
|
||||
else { return }
|
||||
|
||||
let musicDir = self.preferences.expandedMpdLibraryDir
|
||||
let fullAlbumURI = "\(musicDir)/\(albumURI)"
|
||||
|
||||
for coverArtFilename in coverArtFilenames {
|
||||
let coverArtURI = "\(fullAlbumURI)/\(coverArtFilename)"
|
||||
|
||||
if FileManager.default.fileExists(atPath: coverArtURI),
|
||||
let data = FileManager.default.contents(atPath: coverArtURI),
|
||||
let image = NSImage(data: data) {
|
||||
|
||||
let imageThumb = image.toFitBox(
|
||||
size: NSSize(width: 180, height: 180)
|
||||
)
|
||||
self.cacheArtwork(
|
||||
for: album,
|
||||
data: imageThumb.jpegData(compressionQuality: 0.5)
|
||||
)
|
||||
callback(imageThumb)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
func getArtworkFromFilesystem(for album: AlbumItem, callback: @escaping (_ image: NSImage) -> Void) -> Bool {
|
||||
print("No cache trying filesystem")
|
||||
let uri = AppDelegate.mpdClient.getAlbumURI(for: album.album)
|
||||
print(uri)
|
||||
return false
|
||||
}
|
||||
|
||||
func cacheArtwork(for album: AlbumItem, data: Data?) {
|
||||
func cacheArtwork(for album: AlbumItem, data: Data) {
|
||||
guard let bundleIdentifier = Bundle.main.bundleIdentifier,
|
||||
let cacheDir = try? FileManager.default.url(for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
|
||||
.appendingPathComponent(bundleIdentifier)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user