From 4322a25b8b80bc319f5a65a70ae3ccaffa451942 Mon Sep 17 00:00:00 2001 From: Daniel Barber Date: Fri, 22 Nov 2019 13:04:31 -0500 Subject: [PATCH] Get currently playing art using Kingfisher --- Persephone.xcodeproj/project.pbxproj | 24 ------- .../Browser/Album Browser/AlbumViewItem.swift | 23 +----- .../Album Detail/AlbumDetailView.swift | 20 +++--- .../Queue/CurrentCoverArtView.swift | 26 +++++-- .../Shared/UserNotificationsController.swift | 18 ++--- .../Components/Window/WindowController.swift | 1 - Persephone/Models/Album.swift | 20 ++++++ Persephone/Services/CoverArtService.swift | 70 ------------------- .../Extensions/CoverArtService+Caching.swift | 51 -------------- .../CoverArtService+Filesystem.swift | 68 ------------------ .../Extensions/CoverArtService+Remote.swift | 62 ---------------- Persephone/State/PlayerState.swift | 1 - Persephone/State/Reducers/PlayerReducer.swift | 22 +----- 13 files changed, 64 insertions(+), 342 deletions(-) delete mode 100644 Persephone/Services/CoverArtService.swift delete mode 100644 Persephone/Services/Extensions/CoverArtService+Caching.swift delete mode 100644 Persephone/Services/Extensions/CoverArtService+Filesystem.swift delete mode 100644 Persephone/Services/Extensions/CoverArtService+Remote.swift diff --git a/Persephone.xcodeproj/project.pbxproj b/Persephone.xcodeproj/project.pbxproj index 7f066c9..f1c2cd2 100644 --- a/Persephone.xcodeproj/project.pbxproj +++ b/Persephone.xcodeproj/project.pbxproj @@ -29,9 +29,6 @@ E41E5307223C019100173814 /* MPDClient+Status.swift in Sources */ = {isa = PBXBuildFile; fileRef = E41E5306223C019100173814 /* MPDClient+Status.swift */; }; E41E5309223C020400173814 /* MPDClient+Command.swift in Sources */ = {isa = PBXBuildFile; fileRef = E41E5308223C020400173814 /* MPDClient+Command.swift */; }; E41E530B223C033700173814 /* MPDClient+Album.swift in Sources */ = {isa = PBXBuildFile; fileRef = E41E530A223C033700173814 /* MPDClient+Album.swift */; }; - E41E530E223EF4CF00173814 /* CoverArtService+Caching.swift in Sources */ = {isa = PBXBuildFile; fileRef = E41E530D223EF4CF00173814 /* CoverArtService+Caching.swift */; }; - E41E5310223EF6CE00173814 /* CoverArtService+Remote.swift in Sources */ = {isa = PBXBuildFile; fileRef = E41E530F223EF6CE00173814 /* CoverArtService+Remote.swift */; }; - E41E5312223EF74A00173814 /* CoverArtService+Filesystem.swift in Sources */ = {isa = PBXBuildFile; fileRef = E41E5311223EF74A00173814 /* CoverArtService+Filesystem.swift */; }; E41EA46C221636AF0068EF46 /* GeneralPrefsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E41EA46B221636AF0068EF46 /* GeneralPrefsViewController.swift */; }; E4235640228623D2001216D6 /* QueueSongTitleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E423563F228623D2001216D6 /* QueueSongTitleView.swift */; }; E42410B62241B956005ED6DF /* MPDClient+Database.swift in Sources */ = {isa = PBXBuildFile; fileRef = E42410B52241B956005ED6DF /* MPDClient+Database.swift */; }; @@ -85,7 +82,6 @@ E4A642DA22090CBE00067D21 /* MPDStatus.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4A642D922090CBE00067D21 /* MPDStatus.swift */; }; E4A83BEF2221F8CF0098FED6 /* CoverArtPrefsController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4A83BEE2221F8CF0098FED6 /* CoverArtPrefsController.swift */; }; E4A83BF12221FAA00098FED6 /* PreferencesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4A83BF02221FAA00098FED6 /* PreferencesViewController.swift */; }; - E4A83BF4222207D50098FED6 /* CoverArtService.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4A83BF3222207D50098FED6 /* CoverArtService.swift */; }; E4B11B53226928F20075461B /* AppState.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4B11B52226928F20075461B /* AppState.swift */; }; E4B11B61226A4C000075461B /* PlayerReducer.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4B11B60226A4BFF0075461B /* PlayerReducer.swift */; }; E4B11B63226A4C510075461B /* AppReducer.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4B11B62226A4C510075461B /* AppReducer.swift */; }; @@ -234,9 +230,6 @@ E41E5306223C019100173814 /* MPDClient+Status.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MPDClient+Status.swift"; sourceTree = ""; }; E41E5308223C020400173814 /* MPDClient+Command.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MPDClient+Command.swift"; sourceTree = ""; }; E41E530A223C033700173814 /* MPDClient+Album.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MPDClient+Album.swift"; sourceTree = ""; }; - E41E530D223EF4CF00173814 /* CoverArtService+Caching.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CoverArtService+Caching.swift"; sourceTree = ""; }; - E41E530F223EF6CE00173814 /* CoverArtService+Remote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CoverArtService+Remote.swift"; sourceTree = ""; }; - E41E5311223EF74A00173814 /* CoverArtService+Filesystem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CoverArtService+Filesystem.swift"; sourceTree = ""; }; E41EA46B221636AF0068EF46 /* GeneralPrefsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GeneralPrefsViewController.swift; sourceTree = ""; }; E423563F228623D2001216D6 /* QueueSongTitleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QueueSongTitleView.swift; sourceTree = ""; }; E42410B52241B956005ED6DF /* MPDClient+Database.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MPDClient+Database.swift"; sourceTree = ""; }; @@ -284,7 +277,6 @@ E4A642D922090CBE00067D21 /* MPDStatus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MPDStatus.swift; sourceTree = ""; }; E4A83BEE2221F8CF0098FED6 /* CoverArtPrefsController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoverArtPrefsController.swift; sourceTree = ""; }; E4A83BF02221FAA00098FED6 /* PreferencesViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesViewController.swift; sourceTree = ""; }; - E4A83BF3222207D50098FED6 /* CoverArtService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoverArtService.swift; sourceTree = ""; }; E4B11B52226928F20075461B /* AppState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppState.swift; sourceTree = ""; }; E4B11B60226A4BFF0075461B /* PlayerReducer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerReducer.swift; sourceTree = ""; }; E4B11B62226A4C510075461B /* AppReducer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppReducer.swift; sourceTree = ""; }; @@ -514,16 +506,6 @@ path = mpd; sourceTree = ""; }; - E41E530C223EF4BA00173814 /* Extensions */ = { - isa = PBXGroup; - children = ( - E41E530D223EF4CF00173814 /* CoverArtService+Caching.swift */, - E41E5311223EF74A00173814 /* CoverArtService+Filesystem.swift */, - E41E530F223EF6CE00173814 /* CoverArtService+Remote.swift */, - ); - path = Extensions; - sourceTree = ""; - }; E442CCC42347D5B900004E0C /* Components */ = { isa = PBXGroup; children = ( @@ -642,8 +624,6 @@ E4A83BF2222207BE0098FED6 /* Services */ = { isa = PBXGroup; children = ( - E41E530C223EF4BA00173814 /* Extensions */, - E4A83BF3222207D50098FED6 /* CoverArtService.swift */, E439109722640213002982E9 /* SongNotifierService.swift */, ); path = Services; @@ -949,7 +929,6 @@ E4C8B53C22342005009A20F3 /* PreferencesWindowController.swift in Sources */, E4B11B63226A4C510075461B /* AppReducer.swift in Sources */, E41E5307223C019100173814 /* MPDClient+Status.swift in Sources */, - E41E5310223EF6CE00173814 /* CoverArtService+Remote.swift in Sources */, E442CCCD2347E73C00004E0C /* Artist.swift in Sources */, E41E530B223C033700173814 /* MPDClient+Album.swift in Sources */, E42410B62241B956005ED6DF /* MPDClient+Database.swift in Sources */, @@ -1005,18 +984,15 @@ E4120D6C22AD8139004CB1F8 /* QueueView.swift in Sources */, E41EA46C221636AF0068EF46 /* GeneralPrefsViewController.swift in Sources */, E4E8CC902204EC7F0024217A /* Delegate.swift in Sources */, - E4A83BF4222207D50098FED6 /* CoverArtService.swift in Sources */, E47E2FD5222071FD00F747E6 /* AlbumViewItem.swift in Sources */, E408D3BE220E03EE0006D9BE /* RawRepresentable.swift in Sources */, E4B11B66226A4F830075461B /* PlayerState.swift in Sources */, E4B11BBE2275EDAA0075461B /* PlayerActions.swift in Sources */, E4F26F7723411AE300D45FF9 /* ArtistListActions.swift in Sources */, E4FF71942276043A00D4C412 /* MPDServer.swift in Sources */, - E41E530E223EF4CF00173814 /* CoverArtService+Caching.swift in Sources */, E4E8CC922204F4B80024217A /* QueueViewController.swift in Sources */, E440519C227BAF2E0090CD6F /* UIActions.swift in Sources */, E45878382296173C00586A1C /* AlbumDetailSongRowView.swift in Sources */, - E41E5312223EF74A00173814 /* CoverArtService+Filesystem.swift in Sources */, E41E5301223BF99300173814 /* MPDClient+Queue.swift in Sources */, E4EB237B220F7CF1008C70C0 /* MPDAlbum.swift in Sources */, E41E5303223BF9C300173814 /* MPDClient+Idle.swift in Sources */, diff --git a/Persephone/Components/Browser/Album Browser/AlbumViewItem.swift b/Persephone/Components/Browser/Album Browser/AlbumViewItem.swift index c00bf91..e8cfa31 100644 --- a/Persephone/Components/Browser/Album Browser/AlbumViewItem.swift +++ b/Persephone/Components/Browser/Album Browser/AlbumViewItem.swift @@ -13,18 +13,6 @@ class AlbumViewItem: NSCollectionViewItem { var observer: NSKeyValueObservation? var album: Album? - var coverArtFilenames: [String] { - return [ - "folder.jpg", - "cover.jpg", - "\(album?.artist ?? "") - \(album?.title ?? "").jpg" - ] - } - - var musicDir: String { - return App.store.state.preferencesState.expandedMpdLibraryDir - } - override var isSelected: Bool { didSet { setAppearance(selected: isSelected) @@ -67,7 +55,7 @@ class AlbumViewItem: NSCollectionViewItem { } func setAlbumCover(_ album: Album) { - guard let imagePath = fileSystemArtworkFilePath() else { return } + guard let imagePath = album.coverArtFilePath else { return } let imageURL = URL(fileURLWithPath: imagePath) let provider = LocalFileImageDataProvider(fileURL: imageURL) @@ -81,15 +69,6 @@ class AlbumViewItem: NSCollectionViewItem { ) } - func fileSystemArtworkFilePath() -> String? { - return self.coverArtFilenames - .lazy - .map { "\(self.musicDir)/\(self.album?.mpdAlbum.path ?? "")/\($0)" } - .first { - FileManager.default.fileExists(atPath: $0) - } - } - func setAppearance(selected isSelected: Bool) { guard let viewLayer = albumCoverView.layer, let boxLayer = albumCoverBox.layer diff --git a/Persephone/Components/Browser/Album Detail/AlbumDetailView.swift b/Persephone/Components/Browser/Album Detail/AlbumDetailView.swift index 41b2977..b1b2ca2 100644 --- a/Persephone/Components/Browser/Album Detail/AlbumDetailView.swift +++ b/Persephone/Components/Browser/Album Detail/AlbumDetailView.swift @@ -7,6 +7,7 @@ // import AppKit +import Kingfisher class AlbumDetailView: NSViewController { var observer: NSKeyValueObservation? @@ -130,15 +131,18 @@ class AlbumDetailView: NSViewController { } func getBigCoverArt(song: Song, album: Album) { - let coverArtService = CoverArtService(path: song.mpdSong.path, album: album) + guard let imagePath = album.coverArtFilePath else { return } - coverArtService.fetchBigCoverArt() - .done(on: DispatchQueue.main) { [weak self] image in - if let image = image { - self?.albumCoverView.image = image - } - } - .cauterize() + let imageURL = URL(fileURLWithPath: imagePath) + let provider = LocalFileImageDataProvider(fileURL: imageURL) + albumCoverView.kf.setImage( + with: .provider(provider), + placeholder: NSImage.defaultCoverArt, + options: [ + .processor(DownsamplingImageProcessor(size: NSSize(width: 500, height: 500))), + .scaleFactor(2), + ] + ) } func setAppearance() { diff --git a/Persephone/Components/Queue/CurrentCoverArtView.swift b/Persephone/Components/Queue/CurrentCoverArtView.swift index b06bfa3..2509830 100644 --- a/Persephone/Components/Queue/CurrentCoverArtView.swift +++ b/Persephone/Components/Queue/CurrentCoverArtView.swift @@ -8,23 +8,39 @@ import AppKit import ReSwift +import Kingfisher class CurrentCoverArtView: NSImageView { required init?(coder: NSCoder) { super.init(coder: coder) App.store.subscribe(self) { - $0.select { $0.playerState.currentArtwork } + $0.select { $0.playerState.currentSong } } } + + func setAlbumImage(_ album: Album) { + guard let imagePath = album.coverArtFilePath else { return } + + let imageURL = URL(fileURLWithPath: imagePath) + let provider = LocalFileImageDataProvider(fileURL: imageURL) + self.kf.setImage( + with: .provider(provider), + placeholder: NSImage.defaultCoverArt, + options: [ + .processor(DownsamplingImageProcessor(size: NSSize(width: 500, height: 500))), + .scaleFactor(2), + ] + ) + } } extension CurrentCoverArtView: StoreSubscriber { - typealias StoreSubscriberStateType = NSImage? + typealias StoreSubscriberStateType = Song? - func newState(state: NSImage?) { - if let coverArt = state { - image = coverArt + func newState(state: Song?) { + if let song = state { + setAlbumImage(song.album) } else { image = .defaultCoverArt } diff --git a/Persephone/Components/Shared/UserNotificationsController.swift b/Persephone/Components/Shared/UserNotificationsController.swift index 6825e41..9c74f72 100644 --- a/Persephone/Components/Shared/UserNotificationsController.swift +++ b/Persephone/Components/Shared/UserNotificationsController.swift @@ -21,15 +21,15 @@ class UserNotificationsController { let status = App.mpdClient.status, status.state == .playing else { return } - - let coverArtService = CoverArtService(path: currentSong.mpdSong.path, album: currentSong.album) - - coverArtService.fetchBigCoverArt() - .done() { - SongNotifierService(song: currentSong, image: $0) - .deliver() - } - .cauterize() +// +// let coverArtService = CoverArtService(path: currentSong.mpdSong.path, album: currentSong.album) +// +// coverArtService.fetchBigCoverArt() +// .done() { +// SongNotifierService(song: currentSong, image: $0) +// .deliver() +// } +// .cauterize() } } diff --git a/Persephone/Components/Window/WindowController.swift b/Persephone/Components/Window/WindowController.swift index c183b01..7847e32 100644 --- a/Persephone/Components/Window/WindowController.swift +++ b/Persephone/Components/Window/WindowController.swift @@ -162,7 +162,6 @@ class WindowController: NSWindowController { @IBAction func handleSearchQuery(_ sender: NSSearchField) { //App.store.dispatch(SetSearchQuery(searchQuery: sender.stringValue)) - CoverArtService.coverArtQueue App.mpdClient.fetchAlbums(filter: sender.stringValue) } } diff --git a/Persephone/Models/Album.swift b/Persephone/Models/Album.swift index f3d3380..c0a0026 100644 --- a/Persephone/Models/Album.swift +++ b/Persephone/Models/Album.swift @@ -32,6 +32,26 @@ struct Album { var hash: String { return "\(title) - \(artist)".sha1() } + + var coverArtFilenames: [String] { + return [ + "folder.jpg", + "cover.jpg", + "\(artist) - \(title ).jpg" + ] + } + + var coverArtFilePath: String? { + let musicDir = App.store.state.preferencesState.expandedMpdLibraryDir + guard let albumPath = mpdAlbum.path else { return nil } + + return coverArtFilenames + .lazy + .map { "\(musicDir)/\(albumPath)/\($0)" } + .first { + FileManager.default.fileExists(atPath: $0) + } + } } extension Album: Equatable { diff --git a/Persephone/Services/CoverArtService.swift b/Persephone/Services/CoverArtService.swift deleted file mode 100644 index 3ec1a30..0000000 --- a/Persephone/Services/CoverArtService.swift +++ /dev/null @@ -1,70 +0,0 @@ -// -// CoverArtService.swift -// Persephone -// -// Created by Daniel Barber on 2019/2/23. -// Copyright © 2019 Dan Barber. All rights reserved. -// - -import AppKit -import PromiseKit - -class CoverArtService { - let path: String - let album: Album - - let cachedArtworkSize = 180 - let cachedArtworkQuality: CGFloat = 0.5 - - let bigArtworkSize = 600 - - var session = URLSession(configuration: .default) - static let coverArtQueue = DispatchQueue(label: "coverArtQueue", qos: .utility) - - init(path: String, album: Album) { - self.path = path - self.album = album - } - - func fetchBigCoverArt() -> Promise { - return firstly { - self.getArtworkFromFilesystem() - }.then { (image: NSImage?) -> Promise in - image.map(Promise.value) ?? self.getRemoteArtwork() - }.recover { (_) -> Guarantee in - return .value(nil) - } - } - - func fetchCoverArt() -> Guarantee { - return firstly { - self.getCachedArtwork() - }.then { (artwork: NSImage?) -> Promise in - artwork.map(Promise.value) ?? self.getArtworkFromFilesystem() - }.then { (artwork: NSImage?) -> Promise in - artwork.map(Promise.value) ?? self.getRemoteArtwork() - }.compactMap(on: CoverArtService.coverArtQueue) { - return self.sizeAndCacheImage($0).map(Optional.some) - }.recover { _ in - return .value(nil) - } - } - - func sizeAndCacheImage(_ image: NSImage?) -> NSImage? { - switch image { - case nil: - self.cacheArtwork(data: Data()) - return image - case let image: - if self.isArtworkCached() { - return image - } else { - let sizedImage = image?.toFitBox( - size: NSSize(width: self.cachedArtworkSize, height: self.cachedArtworkSize) - ) - self.cacheArtwork(data: sizedImage?.jpegData(compressionQuality: self.cachedArtworkQuality)) - return sizedImage - } - } - } -} diff --git a/Persephone/Services/Extensions/CoverArtService+Caching.swift b/Persephone/Services/Extensions/CoverArtService+Caching.swift deleted file mode 100644 index 78d3bb1..0000000 --- a/Persephone/Services/Extensions/CoverArtService+Caching.swift +++ /dev/null @@ -1,51 +0,0 @@ -// -// CoverArtService+Caching.swift -// Persephone -// -// Created by Daniel Barber on 2019/3/17. -// Copyright © 2019 Dan Barber. All rights reserved. -// - -import AppKit -import PromiseKit - -extension CoverArtService { - static let cacheDir = try! FileManager.default.url(for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: true).appendingPathComponent(Bundle.main.bundleIdentifier!) - - func getCachedArtwork() -> Promise { - return Promise { seal in - CoverArtService.coverArtQueue.async { - if self.isArtworkCached() { - let cacheFilePath = CoverArtService.cacheDir.appendingPathComponent(self.album.hash).path - let data = FileManager.default.contents(atPath: cacheFilePath) - let image = NSImage(data: data ?? Data()) ?? NSImage.defaultCoverArt - - seal.fulfill(image) - } else { - seal.fulfill(nil) - } - } - } - } - - func cacheArtwork(data: Data?) { - CoverArtService.coverArtQueue.async { - guard let bundleIdentifier = Bundle.main.bundleIdentifier, - let cacheDir = try? FileManager.default.url(for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: true) - .appendingPathComponent(bundleIdentifier) - else { return } - - let cacheFilePath = cacheDir.appendingPathComponent(self.album.hash).path - - if !self.isArtworkCached() { - FileManager.default.createFile(atPath: cacheFilePath, contents: data, attributes: nil) - } - } - } - - func isArtworkCached() -> Bool { - let cacheFilePath = CoverArtService.cacheDir.appendingPathComponent(album.hash).path - - return FileManager.default.fileExists(atPath: cacheFilePath) - } -} diff --git a/Persephone/Services/Extensions/CoverArtService+Filesystem.swift b/Persephone/Services/Extensions/CoverArtService+Filesystem.swift deleted file mode 100644 index 6c42d5e..0000000 --- a/Persephone/Services/Extensions/CoverArtService+Filesystem.swift +++ /dev/null @@ -1,68 +0,0 @@ -// -// CoverArtService+Filesystem.swift -// Persephone -// -// Created by Daniel Barber on 2019/3/17. -// Copyright © 2019 Dan Barber. All rights reserved. -// - -import AppKit -import PromiseKit - -extension CoverArtService { - var coverArtFilenames: [String] { - return [ - "folder.jpg", - "cover.jpg", - "\(album.artist) - \(album.title).jpg" - ] - } - - var musicDir: String { - return App.store.state.preferencesState.expandedMpdLibraryDir - } - - func getArtworkFromFilesystem() -> Promise { - return Promise { seal in - CoverArtService.coverArtQueue.async { - guard let artworkPath = self.fileSystemArtworkFilePath() - else { seal.fulfill(nil); return } - - let image = self.tryImage(artworkPath) - - seal.fulfill(image) - } - } - } - - func saveArtworkToFilesystem(data: Data?) { - let artworkFileName = coverArtFilenames.first! - - if self.fileSystemArtworkFilePath() == nil { - FileManager.default.createFile( - atPath: "\(self.musicDir)/\(self.path)/\(artworkFileName)", - contents: data, - attributes: nil - ) - } - } - - func fileSystemArtworkFilePath() -> String? { - let musicDir = App.store.state.preferencesState.expandedMpdLibraryDir - - return self.coverArtFilenames - .lazy - .map { "\(musicDir)/\(self.path)/\($0)" } - .first { - FileManager.default.fileExists(atPath: $0) - } - } - - func tryImage(_ filePath: String) -> NSImage? { - guard let data = FileManager.default.contents(atPath: filePath), - let image = NSImage(data: data) - else { return nil } - - return image - } -} diff --git a/Persephone/Services/Extensions/CoverArtService+Remote.swift b/Persephone/Services/Extensions/CoverArtService+Remote.swift deleted file mode 100644 index 1d187d5..0000000 --- a/Persephone/Services/Extensions/CoverArtService+Remote.swift +++ /dev/null @@ -1,62 +0,0 @@ -// -// CoverArtService+Remote.swift -// Persephone -// -// Created by Daniel Barber on 2019/3/17. -// Copyright © 2019 Dan Barber. All rights reserved. -// - -import AppKit -import SwiftyJSON -import PromiseKit -import PMKFoundation - -extension CoverArtService { - enum RemoteArtworkError: Error { - case noArtworkAvailable - case notConfigured - } - - func getRemoteArtwork() -> Promise { - return Promise { seal in - if App.store.state.preferencesState .fetchMissingArtworkFromInternet { - CoverArtService.coverArtQueue.async { - let coverArtWorkItem = DispatchWorkItem { - self.getArtworkFromMusicBrainz().map(Optional.some).pipe(to: seal.resolve) - } - - CoverArtQueue.shared.addToQueue(workItem: coverArtWorkItem) - } - } else { - throw RemoteArtworkError.notConfigured - } - } - } - - func getArtworkFromMusicBrainz() -> Promise { - var search = URLComponents(string: "https://musicbrainz.org/ws/2/release/")! - search.query = "query=artist:\(album.artist) AND release:\(album.title) AND country:US&limit=1&fmt=json" - - return firstly { - URLSession.shared.dataTask(.promise, with: search.url!).validate() - }.compactMap { - JSON($0.data) - }.compactMap { - $0["releases"][0]["id"].string - }.compactMap { - URLComponents(string: "https://coverartarchive.org/release/\($0)/front-500")?.url - }.then { (url: URL?) -> Promise<(data: Data, response: URLResponse)> in - return URLSession.shared.dataTask(.promise, with: url!).validate() - }.compactMap { - NSImage(data: $0.data)?.toFitBox( - size: NSSize(width: self.cachedArtworkSize, height: self.cachedArtworkSize) - ) - }.recover { error -> Promise in - if case PMKHTTPError.badStatusCode(404, _, _) = error { - throw RemoteArtworkError.noArtworkAvailable - } else { - throw error - } - } - } -} diff --git a/Persephone/State/PlayerState.swift b/Persephone/State/PlayerState.swift index 6c58766..176bea2 100644 --- a/Persephone/State/PlayerState.swift +++ b/Persephone/State/PlayerState.swift @@ -12,7 +12,6 @@ import ReSwift struct PlayerState: StateType { var status: MPDClient.MPDStatus? var currentSong: Song? - var currentArtwork: NSImage? var state: MPDClient.MPDStatus.State? var shuffleState: Bool = false diff --git a/Persephone/State/Reducers/PlayerReducer.swift b/Persephone/State/Reducers/PlayerReducer.swift index 62bab20..f89ebf0 100644 --- a/Persephone/State/Reducers/PlayerReducer.swift +++ b/Persephone/State/Reducers/PlayerReducer.swift @@ -39,28 +39,8 @@ func playerReducer(action: Action, state: PlayerState?) -> PlayerState { case let action as UpdateCurrentSongAction: state.currentSong = action.currentSong - if let currentSong = state.currentSong { - let coverArtService = CoverArtService(path: currentSong.mpdSong.path, album: currentSong.album) - - coverArtService.fetchBigCoverArt() - .done() { image in - DispatchQueue.main.async { - if let image = image { - App.store.dispatch(UpdateCurrentCoverArtAction(coverArt: image)) - } else { - App.store.dispatch(UpdateCurrentCoverArtAction(coverArt: .defaultCoverArt)) - } - } - } - .cauterize() - } else { - DispatchQueue.main.async { - App.store.dispatch(UpdateCurrentCoverArtAction(coverArt: .defaultCoverArt)) - } - } - case let action as UpdateCurrentCoverArtAction: - state.currentArtwork = action.coverArt + break case let action as UpdateElapsedTimeAction: state.elapsedTimeMs = action.elapsedTimeMs