Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AlbumArt] Allow a custom provider #74

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Mac/Components/Browser/Album Browser/AlbumViewItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class AlbumViewItem: NSCollectionViewItem {
guard let song = album.mpdAlbum.firstSong
else { return }

let provider = MPDAlbumArtImageDataProvider(
let provider = AlbumArtImageDataProvider(
songUri: song.uriString,
cacheKey: album.hash
)
Expand Down
2 changes: 1 addition & 1 deletion Mac/Components/Browser/Album Detail/AlbumDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class AlbumDetailView: NSViewController {
}

func getBigCoverArt(song: Song, album: Album) {
let provider = MPDAlbumArtImageDataProvider(
let provider = AlbumArtImageDataProvider(
songUri: song.mpdSong.uriString,
cacheKey: album.hash
)
Expand Down
24 changes: 24 additions & 0 deletions Mac/Components/Preferences/CoverArtPrefsController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ class CoverArtPrefsController: NSViewController {
} else {
fetchMissingArtworkFromInternet.state = .off
}

if App.store.state.preferencesState.fetchArtworkFromCustomURL {
customArtworkURLButton.state = .on
} else {
customArtworkURLButton.state = .off
}

if let urlString = App.store.state.preferencesState.customArtworkURL?.absoluteString {
customArtworkURLTextField.stringValue = urlString
}

preferredContentSize = NSMakeSize(view.frame.size.width, view.frame.size.height)
}
Expand All @@ -37,11 +47,25 @@ class CoverArtPrefsController: NSViewController {
)
)
}

@IBAction func updateCustomArtworkURLToggle(_ sender: NSButton) {
App.store.dispatch(
UpdateCustomArtworkURLToggle(useCustomArtworkURL: sender.state == .on)
)
}

@IBAction func updateCustomArtworkURL(_ sender: NSTextField) {
App.store.dispatch(
UpdateCustomArtworkURL(customArtworkURL: sender.stringValue)
)
}

@IBAction func clearAlbumArtCache(_ sender: NSButton) {
KingfisherManager.shared.cache.clearDiskCache()
KingfisherManager.shared.cache.clearMemoryCache()
}

@IBOutlet var fetchMissingArtworkFromInternet: NSButton!
@IBOutlet var customArtworkURLTextField: NSTextField!
@IBOutlet var customArtworkURLButton: NSButton!
}
2 changes: 1 addition & 1 deletion Mac/Components/Queue/CurrentCoverArtView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CurrentCoverArtView: NSImageView {
}

func setSongImage(_ song: Song) {
let provider = MPDAlbumArtImageDataProvider(
let provider = AlbumArtImageDataProvider(
songUri: song.mpdSong.uriString,
cacheKey: song.album.hash
)
Expand Down
2 changes: 1 addition & 1 deletion Mac/Components/Queue/QueueSongCoverView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class QueueSongCoverView: NSTableCellView {

isPlaying = queueItem.isPlaying

let provider = MPDAlbumArtImageDataProvider(
let provider = AlbumArtImageDataProvider(
songUri: song.mpdSong.uriString,
cacheKey: song.album.hash
)
Expand Down
2 changes: 1 addition & 1 deletion Mac/Components/Shared/DraggedSongView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class DraggedSongView: NSViewController {
func setCoverArt() {
let mpdAlbum = MPDClient.MPDAlbum(title: songAlbum, artist: songAlbumArtist)

let provider = MPDAlbumArtImageDataProvider(
let provider = AlbumArtImageDataProvider(
songUri: songUri,
cacheKey: Album(mpdAlbum: mpdAlbum).hash
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// CustomURLAlbumArtImageDataProvider.swift
// Persephone
//
// Created by Diego Torres on 07.11.20.
// Copyright © 2020 Dan Barber. All rights reserved.
//

import Foundation
import Kingfisher

public struct CustomURLAlbumArtImageDataProvider: ImageDataProvider {
let songUri: String
let baseURL: URL

init(baseURL: URL, songUri: String, cacheKey: String) {
self.songUri = songUri
self.cacheKey = cacheKey
self.baseURL = baseURL
}

public var cacheKey: String

public func data(handler: @escaping (Result<Data, Error>) -> Void) {
let task = URLSession.shared.dataTask(with: baseURL.appendingPathComponent(songUri)) { (data, response, error) in
let result = Result<Data, Error> {
if let error = error { throw error }
guard let data = data else { throw URLError(.badServerResponse) }
return data
}
handler(result)
}
task.resume()
}

public var contentURL: String? {
return songUri
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ public struct MPDAlbumArtImageDataProvider: ImageDataProvider {
return songUri
}
}

public func AlbumArtImageDataProvider(songUri: String, cacheKey: String) -> ImageDataProvider {
if App.store.state.preferencesState.fetchArtworkFromCustomURL, let url = App.store.state.preferencesState.customArtworkURL {
return CustomURLAlbumArtImageDataProvider(baseURL: url, songUri: songUri, cacheKey: cacheKey)
} else {
return MPDAlbumArtImageDataProvider(songUri: songUri, cacheKey: cacheKey)
}
}
2 changes: 1 addition & 1 deletion Mac/Components/Shared/UserNotificationsController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class UserNotificationsController {
status.state == .playing
else { return }

let provider = MPDAlbumArtImageDataProvider(
let provider = AlbumArtImageDataProvider(
songUri: song.mpdSong.uriString,
cacheKey: song.album.hash
)
Expand Down
38 changes: 32 additions & 6 deletions Mac/Components/Window/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="15705" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="16097.3" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="15705"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="16097.3"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand Down Expand Up @@ -536,13 +536,15 @@
<objects>
<viewController title="Cover Art" id="3C9-vU-zjZ" userLabel="Cover Art" customClass="CoverArtPrefsController" customModule="Persephone" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" id="PyK-v2-kus">
<rect key="frame" x="0.0" y="0.0" width="427" height="95"/>
<rect key="frame" x="0.0" y="0.0" width="427" height="158"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<gridView horizontalHuggingPriority="750" verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" verticalCompressionResistancePriority="250" xPlacement="leading" yPlacement="center" rowAlignment="none" rowSpacing="14" translatesAutoresizingMaskIntoConstraints="NO" id="M8L-ne-ZhL">
<rect key="frame" x="0.0" y="0.0" width="427" height="95"/>
<rect key="frame" x="0.0" y="0.0" width="427" height="158"/>
<rows>
<gridRow topPadding="20" id="jo0-w7-CZu"/>
<gridRow id="AZA-jX-c6l"/>
<gridRow id="16Y-Re-bkN"/>
<gridRow bottomPadding="26" id="g4x-Bc-qT7"/>
</rows>
<columns>
Expand All @@ -551,7 +553,7 @@
<gridCells>
<gridCell row="jo0-w7-CZu" column="y6E-R8-zfP" id="NAC-x2-Qyh">
<button key="contentView" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="pRL-MG-1Be">
<rect key="frame" x="78" y="59" width="271" height="18"/>
<rect key="frame" x="78" y="122" width="271" height="18"/>
<buttonCell key="cell" type="check" title="Fetch missing artwork from MusicBrainz" bezelStyle="regularSquare" imagePosition="left" enabled="NO" inset="2" id="LpD-Ew-HMd">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
Expand All @@ -561,6 +563,28 @@
</connections>
</button>
</gridCell>
<gridCell row="AZA-jX-c6l" column="y6E-R8-zfP" id="nIu-Aa-5ov">
<button key="contentView" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="iPP-w1-R8G">
<rect key="frame" x="78" y="94" width="271" height="18"/>
<buttonCell key="cell" type="check" title="Fetch from URL:" bezelStyle="regularSquare" imagePosition="left" inset="2" id="jbS-Rs-0ED">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
</button>
</gridCell>
<gridCell row="16Y-Re-bkN" column="y6E-R8-zfP" id="Kno-Fk-238">
<textField key="contentView" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="T3H-Cf-Rh3">
<rect key="frame" x="80" y="61" width="267" height="21"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" borderStyle="bezel" placeholderString="https://a.base.url" drawsBackground="YES" id="RiO-X6-KZA">
<font key="font" usesAppearanceFont="YES"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<connections>
<action selector="updateCustomArtworkURL:" target="3C9-vU-zjZ" id="I5e-oc-cN6"/>
</connections>
</textField>
</gridCell>
<gridCell row="g4x-Bc-qT7" column="y6E-R8-zfP" id="LTx-vQ-fTR">
<button key="contentView" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="mXh-kY-tMC">
<rect key="frame" x="74" y="19" width="279" height="32"/>
Expand All @@ -584,12 +608,14 @@
</constraints>
</view>
<connections>
<outlet property="customArtworkURLButton" destination="iPP-w1-R8G" id="w5V-Pn-iNI"/>
<outlet property="customArtworkURLTextField" destination="T3H-Cf-Rh3" id="Erb-cy-PSf"/>
<outlet property="fetchMissingArtworkFromInternet" destination="pRL-MG-1Be" id="Xcp-sb-iZm"/>
</connections>
</viewController>
<customObject id="KzD-E3-lpA" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="1411" y="-420"/>
<point key="canvasLocation" x="1410.5" y="-421"/>
</scene>
<!--General-->
<scene sceneID="xTC-Y5-Agk">
Expand Down
5 changes: 5 additions & 0 deletions Mac/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
<string>public.app-category.music</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsLocalNetworking</key>
<true/>
</dict>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2020 Dan Barber. All rights reserved.</string>
<key>NSMainStoryboardFile</key>
Expand Down
4 changes: 2 additions & 2 deletions Mac/Services/CoverArtService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import Kingfisher

struct CoverArtService {
let song: Song
let provider: MPDAlbumArtImageDataProvider
let provider: ImageDataProvider

init(song: Song) {
self.song = song

provider = MPDAlbumArtImageDataProvider(
provider = AlbumArtImageDataProvider(
songUri: song.mpdSong.uriString,
cacheKey: song.album.hash
)
Expand Down
4 changes: 4 additions & 0 deletions Persephone.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
2F2EBD8425570D49002CAB57 /* CustomURLAlbumArtImageDataProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F2EBD8325570D49002CAB57 /* CustomURLAlbumArtImageDataProvider.swift */; };
38BAC36B249CB1A7004BAEA4 /* AlbumDetailSongTitleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 38BAC36A249CB1A6004BAEA4 /* AlbumDetailSongTitleView.swift */; };
E407861C2110CE6E006887B1 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E407861B2110CE6E006887B1 /* AppDelegate.swift */; };
E40786202110CE70006887B1 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E407861F2110CE70006887B1 /* Assets.xcassets */; };
Expand Down Expand Up @@ -369,6 +370,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
2F2EBD8325570D49002CAB57 /* CustomURLAlbumArtImageDataProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomURLAlbumArtImageDataProvider.swift; sourceTree = "<group>"; };
38BAC36A249CB1A6004BAEA4 /* AlbumDetailSongTitleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlbumDetailSongTitleView.swift; sourceTree = "<group>"; };
E40786182110CE6E006887B1 /* Persephone.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Persephone.app; sourceTree = BUILT_PRODUCTS_DIR; };
E407861B2110CE6E006887B1 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1247,6 +1249,7 @@
isa = PBXGroup;
children = (
E4BB7F8E23E5E7BC00906E2F /* MPDAlbumArtImageDataProvider.swift */,
2F2EBD8325570D49002CAB57 /* CustomURLAlbumArtImageDataProvider.swift */,
);
path = ImageDataProviders;
sourceTree = "<group>";
Expand Down Expand Up @@ -1647,6 +1650,7 @@
E43AC1F822C7065A001E483C /* AlbumCoverButton.swift in Sources */,
E45962C62241A78500FC1A1E /* MPDCommand.swift in Sources */,
E408D3B9220DE98F0006D9BE /* NSUserInterfaceItemIdentifier.swift in Sources */,
2F2EBD8425570D49002CAB57 /* CustomURLAlbumArtImageDataProvider.swift in Sources */,
E48059C62426D73600362CF3 /* cplaylist.c in Sources */,
E4EB2379220F10B8008C70C0 /* MPDPair.swift in Sources */,
E489E3A422B9D31800CA8CBD /* DraggedSongView.swift in Sources */,
Expand Down
8 changes: 8 additions & 0 deletions Shared/State/Actions/PreferencesActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,12 @@ struct UpdateFetchMissingArtworkFromInternet: Action {
var fetchMissingArtworkFromInternet: Bool
}

struct UpdateCustomArtworkURLToggle: Action {
var useCustomArtworkURL: Bool
}

struct UpdateCustomArtworkURL: Action {
var customArtworkURL: String
}

struct SavePreferences: Action {}
6 changes: 6 additions & 0 deletions Shared/State/PreferencesState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ struct PreferencesState: StateType, Equatable {
var mpdServer: MPDServer

var fetchMissingArtworkFromInternet: Bool
var fetchArtworkFromCustomURL: Bool
var customArtworkURL: URL?

init() {
self.mpdServer = MPDServer(
Expand All @@ -24,6 +26,8 @@ struct PreferencesState: StateType, Equatable {
self.fetchMissingArtworkFromInternet = preferences.bool(
forKey: "fetchMissingArtworkFromInternet"
)
self.fetchArtworkFromCustomURL = preferences.bool(forKey: "fetchArtworkFromCustomURL")
self.customArtworkURL = preferences.url(forKey: "customArtworkURL")
}

func save() {
Expand All @@ -34,5 +38,7 @@ struct PreferencesState: StateType, Equatable {
preferences.removeObject(forKey: "mpdPort")
}
preferences.set(fetchMissingArtworkFromInternet, forKey: "fetchMissingArtworkFromInternet")
preferences.set(fetchArtworkFromCustomURL, forKey: "fetchArtworkFromCustomURL")
preferences.set(customArtworkURL, forKey: "customArtworkURL")
}
}
6 changes: 6 additions & 0 deletions Shared/State/Reducers/PreferencesReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ func preferencesReducer(action: Action, state: PreferencesState?) -> Preferences

case let action as UpdateServerPort:
state.mpdServer.port = action.port

case let action as UpdateCustomArtworkURLToggle:
state.fetchArtworkFromCustomURL = action.useCustomArtworkURL

case let action as UpdateCustomArtworkURL:
state.customArtworkURL = URL(string: action.customArtworkURL)

case is SavePreferences:
state.save()
Expand Down