Remember the preferred artwork type in the ArtworkSelectorController

This commit is contained in:
Damiano Galassi committed 2017-08-06 20:24:20 +02:00
1 parent daa41aad63
commit b10c99ba58
10 files changed
+104 -61

No files matched your search

+57 -23
View File
@@ -17,12 +17,12 @@ private class ArtworkImageObject : NSObject {
private var data: Data?
private var version: Int
private var cancelled: Bool
fileprivate let artwork: RemoteImage
fileprivate let source: RemoteImage
private let queue: DispatchQueue
private weak var delegate: ArtworkImageObjectDelegate?
init(artwork: RemoteImage, delegate: ArtworkImageObjectDelegate) {
self.artwork = artwork
self.source = artwork
self.delegate = delegate
self.version = 0
self.cancelled = false
@@ -48,7 +48,7 @@ private class ArtworkImageObject : NSObject {
if startDownload {
DispatchQueue.global(priority: .default).async {
let localData = URLSession.data(from: self.artwork.thumbURL)
let localData = URLSession.data(from: self.source.thumbURL)
var localCancelled = false
self.queue.sync {
@@ -81,7 +81,7 @@ private class ArtworkImageObject : NSObject {
}
@objc override func imageUID() -> String {
return artwork.thumbURL.absoluteString
return source.thumbURL.absoluteString
}
@objc override func imageVersion()-> Int {
@@ -93,12 +93,11 @@ private class ArtworkImageObject : NSObject {
}
@objc override func imageTitle() -> String {
return artwork.providerName.components(separatedBy: "|").first ?? ""
return source.service
}
@objc override func imageSubtitle() -> String {
let components = artwork.providerName.components(separatedBy: "|")
return components.count > 1 ? components[1] : ""
return source.type
}
}
@@ -117,15 +116,17 @@ public class ArtworkSelectorController: NSWindowController, ArtworkImageObjectDe
private var artworksUnloaded: [RemoteImage]
private var artworks: [ArtworkImageObject]
private let initialSize: CGSize?
private let type: MetadataSearch.Kind
private weak var delegate: ArtworkSelectorControllerDelegate?
// MARK: - Init
init(artworks: [RemoteImage], size: CGSize? = nil, delegate: ArtworkSelectorControllerDelegate) {
init(artworks: [RemoteImage], size: CGSize? = nil, type: MetadataSearch.Kind, delegate: ArtworkSelectorControllerDelegate) {
self.delegate = delegate
self.initialSize = size
self.artworksUnloaded = artworks
self.artworks = Array()
self.type = type
super.init(window: nil)
}
@@ -151,16 +152,18 @@ public class ArtworkSelectorController: NSWindowController, ArtworkImageObjectDe
super.windowDidLoad()
if let size = initialSize { window?.setContentSize(size) }
loadMoreArtwork(self)
imageBrowser.setSelectionIndexes(IndexSet(integer: 0), byExtendingSelection: false)
if let defaultType = UserDefaults.standard.string(forKey: "SBArtworkSelectorDefault|\(type.rawValue)") {
selectArtwork(type: defaultType)
}
if imageBrowser.selectionIndexes().count == 0 {
imageBrowser.setSelectionIndexes(IndexSet(integer: 0), byExtendingSelection: false)
}
}
@IBAction func loadMoreArtwork(_ sender: Any) {
let batchCount = 8
let endIndex = artworksUnloaded.count < batchCount ? artworksUnloaded.count : batchCount
artworks.append(contentsOf: artworksUnloaded[0 ..< endIndex].map { ArtworkImageObject(artwork: $0, delegate: self) })
artworksUnloaded.removeFirst(endIndex)
loadMoreArtworkButton.isEnabled = artworksUnloaded.count > 0
imageBrowser.reloadData()
loadMoreArtworks(count: 8)
}
// MARK: - User Interface
@@ -169,19 +172,47 @@ public class ArtworkSelectorController: NSWindowController, ArtworkImageObjectDe
imageBrowser.needsDisplay = true
}
func reloadData() {
fileprivate func reloadData() {
imageBrowser.reloadData()
}
private func selectArtwork(at index: Int) {
imageBrowser.setSelectionIndexes(IndexSet(integer: index), byExtendingSelection: false)
imageBrowser.scrollIndexToVisible(index)
}
private func loadMoreArtworks(count: Int) {
let endIndex = artworksUnloaded.count < count ? artworksUnloaded.count : count
artworks.append(contentsOf: artworksUnloaded[0 ..< endIndex].map { ArtworkImageObject(artwork: $0, delegate: self) })
artworksUnloaded.removeFirst(endIndex)
loadMoreArtworkButton.isEnabled = artworksUnloaded.count > 0
imageBrowser.reloadData()
}
private func selectArtwork(type: String) {
if let artwork = (artworks.filter { $0.imageSubtitle() == type } as [ArtworkImageObject]).first,
let index = artworks.index(of: artwork) {
selectArtwork(at: index)
}
else if artworksUnloaded.count > 0 {
for (index, artwork) in artworksUnloaded.enumerated() {
if artwork.type == type {
let offset = artworks.count
loadMoreArtworks(count: index + 1)
selectArtwork(at: index + offset)
break;
}
}
}
}
private func selectedArtworks() -> [ArtworkImageObject] {
return imageBrowser.selectionIndexes().map { artworks[$0] }
}
// MARK: - Finishing Up
@IBAction func addArtwork(_ sender: Any) {
if let indexes = imageBrowser.selectionIndexes() {
let selectedArtworks = indexes.map { artworks[$0].artwork }
delegate?.didSelect(artworks: selectedArtworks)
}
else {
delegate?.didSelect(artworks: [])
}
delegate?.didSelect(artworks:selectedArtworks().map { $0.source })
}
@IBAction func addNoArtwork(_ sender: Any) {
@@ -204,6 +235,9 @@ public class ArtworkSelectorController: NSWindowController, ArtworkImageObjectDe
override public func imageBrowserSelectionDidChange(_ aBrowser: IKImageBrowserView!) {
addArtworkButton.isEnabled = aBrowser.selectionIndexes().count > 0
if let artwork = selectedArtworks().first {
UserDefaults.standard.set(artwork.source.type, forKey: "SBArtworkSelectorDefault|\(type.rawValue)")
}
}
}
@@ -65,7 +65,7 @@ extension Array where Element == RemoteImage {
extension SBRemoteImage {
func toStruct() -> RemoteImage {
return RemoteImage(url: self.url, thumbURL: self.thumbURL, providerName: self.providerName)
return RemoteImage(url: self.url, thumbURL: self.thumbURL, service: self.service, type: self.type)
}
}
@@ -78,10 +78,11 @@ extension Array where Element == SBRemoteImage {
public struct RemoteImage {
let url: URL
let thumbURL: URL
let providerName: String
let service: String
let type: String
public func toClass() -> SBRemoteImage {
return SBRemoteImage(url: url, thumbURL: thumbURL, providerName: providerName)
return SBRemoteImage(url: url, thumbURL: thumbURL, service: service, type: type)
}
}
@@ -91,12 +91,12 @@ public enum MetadataSearch {
}
}
public enum MetadataSearchType: String {
public enum Kind: String {
case movie = "Movie"
case tvShow = "TV"
}
public var type: MetadataSearchType {
public var type: Kind {
get {
switch self {
case .movieSeach:
@@ -144,12 +144,12 @@ extension MetadataSearch {
}
}
public static func defaultLanguage(service: MetadataService, type: MetadataSearchType) -> String {
public static func defaultLanguage(service: MetadataService, type: Kind) -> String {
let language = UserDefaults.standard.string(forKey: "SBMetadataPreference|\(type.rawValue)|\(service.name)|Language") ?? service.defaultLanguage
return service.languageType.displayName(language: language)
}
public static func setDefaultLanguage(_ language: String, service: MetadataService, type: MetadataSearchType) {
public static func setDefaultLanguage(_ language: String, service: MetadataService, type: Kind) {
let extendedLanguage = service.languageType.extendedTag(displayName: language)
UserDefaults.standard.set(extendedLanguage, forKey: "SBMetadataPreference|\(type.rawValue)|\(service.name)|Language")
}
+3 -2
View File
@@ -49,11 +49,12 @@ extern NSString *const SBMetadataResultNetwork;
@interface SBRemoteImage : NSObject
+ (instancetype)remoteImageWithURL:(NSURL *)fullSizeURL thumbURL:(NSURL *)thumbURL providerName:(NSString *)providerName;
+ (instancetype)remoteImageWithURL:(NSURL *)fullSizeURL thumbURL:(NSURL *)thumbURL service:(NSString *)service type:(NSString *)kind;
@property (nonatomic, readonly) NSURL *URL;
@property (nonatomic, readonly) NSURL *thumbURL;
@property (nonatomic, readonly) NSString *providerName;
@property (nonatomic, readonly) NSString *service;
@property (nonatomic, readonly) NSString *type;
@end
+5 -4
View File
@@ -45,21 +45,22 @@ NSString *const SBMetadataResultNetwork = @"{Network}";
@implementation SBRemoteImage
- (instancetype)initWithURL:(NSURL *)fullSizeURL thumbURL:(NSURL *)thumbURL providerName:(NSString *)providerName
- (instancetype)initWithURL:(NSURL *)fullSizeURL thumbURL:(NSURL *)thumbURL service:(NSString *)service type:(NSString *)type
{
self = [super init];
if (self)
{
_URL = fullSizeURL;
_thumbURL = thumbURL;
_providerName = providerName;
_service = service;
_type = type;
}
return self;
}
+ (instancetype)remoteImageWithURL:(NSURL *)fullSizeURL thumbURL:(NSURL *)thumbURL providerName:(NSString *)providerName
+ (instancetype)remoteImageWithURL:(NSURL *)fullSizeURL thumbURL:(NSURL *)thumbURL service:(NSString *)service type:(NSString *)type
{
return [[self alloc] initWithURL:fullSizeURL thumbURL:thumbURL providerName:providerName];
return [[self alloc] initWithURL:fullSizeURL thumbURL:thumbURL service:service type:type];
}
@end
+10 -10
View File
@@ -86,10 +86,10 @@ public struct TheMovieDB: MetadataService {
// MARK: - Movie metadata loading
private func loadArtwork(filePath: String, baseURL: String, thumbSize: String, providerName: String) -> RemoteImage? {
private func loadArtwork(filePath: String, baseURL: String, thumbSize: String, kind: String) -> RemoteImage? {
guard let url = URL(string: baseURL + "original" + filePath),
let thumbURL = URL(string: baseURL + thumbSize + filePath) else { return nil }
return RemoteImage(url: url, thumbURL: thumbURL, providerName: providerName)
return RemoteImage(url: url, thumbURL: thumbURL, service: self.name, type: kind)
}
private func loadMovieArtworks(result: TMDBMovie) -> [RemoteImage] {
@@ -108,16 +108,16 @@ public struct TheMovieDB: MetadataService {
let backdropThumbnailSize = config.backdrop_sizes.first {
if let images = result.images?.posters {
artworks.append(contentsOf: images.flatMap { loadArtwork(filePath: $0.file_path, baseURL: imageBaseURL, thumbSize: posterThumbnailSize, providerName: "TheMovieDB|poster") } )
artworks.append(contentsOf: images.flatMap { loadArtwork(filePath: $0.file_path, baseURL: imageBaseURL, thumbSize: posterThumbnailSize, kind: "poster") } )
}
if result.images?.posters?.count == 0, let posterPath = result.poster_path,
let artwork = loadArtwork(filePath: posterPath, baseURL: imageBaseURL, thumbSize: posterThumbnailSize, providerName: "TheMovieDB|poster") {
let artwork = loadArtwork(filePath: posterPath, baseURL: imageBaseURL, thumbSize: posterThumbnailSize, kind: "poster") {
artworks.append(artwork)
}
if let backdropPath = result.backdrop_path,
let artwork = loadArtwork(filePath: backdropPath, baseURL: imageBaseURL, thumbSize: backdropThumbnailSize, providerName: "TheMovieDB|poster") {
let artwork = loadArtwork(filePath: backdropPath, baseURL: imageBaseURL, thumbSize: backdropThumbnailSize, kind: "poster") {
artworks.append(artwork)
}
}
@@ -209,7 +209,7 @@ public struct TheMovieDB: MetadataService {
for image in images {
if let url = URL(string: imageBaseURL + "original" + image.file_path),
let thumbURL = URL(string: imageBaseURL + posterThumbnailSize + image.file_path) {
let remoteImage = RemoteImage(url: url, thumbURL: thumbURL, providerName: "TheMovieDB|poster")
let remoteImage = RemoteImage(url: url, thumbURL: thumbURL, service: self.name, type: "poster")
artworks.append(remoteImage)
}
}
@@ -219,14 +219,14 @@ public struct TheMovieDB: MetadataService {
let posterPath = result.poster_path,
let url = URL(string: imageBaseURL + "original" + posterPath),
let thumbURL = URL(string: imageBaseURL + posterThumbnailSize + posterPath) {
let remoteImage = RemoteImage(url: url, thumbURL: thumbURL, providerName: "TheMovieDB|poster")
let remoteImage = RemoteImage(url: url, thumbURL: thumbURL, service: self.name, type: "poster")
artworks.append(remoteImage)
}
if let backdropPath = result.backdrop_path,
let url = URL(string: imageBaseURL + "original" + backdropPath),
let thumbURL = URL(string: imageBaseURL + backdropThumbnailSize + backdropPath) {
let remoteImage = RemoteImage(url: url, thumbURL: thumbURL, providerName: "TheMovieDB|poster")
let remoteImage = RemoteImage(url: url, thumbURL: thumbURL, service: self.name, type: "poster")
artworks.append(remoteImage)
}
}
@@ -332,8 +332,8 @@ public struct TheMovieDB: MetadataService {
let imageBaseURL = config.secure_base_url,
let posterThumbnailSize = config.poster_sizes.first {
artworks.append(contentsOf: seasonImages.flatMap { loadArtwork(filePath: $0.file_path, baseURL: imageBaseURL, thumbSize: posterThumbnailSize, providerName: "TheMovieDB|season") } )
artworks.append(contentsOf: episodeImages.flatMap { loadArtwork(filePath: $0.file_path, baseURL: imageBaseURL, thumbSize: posterThumbnailSize, providerName: "TheMovieDB|episode") } )
artworks.append(contentsOf: seasonImages.flatMap { loadArtwork(filePath: $0.file_path, baseURL: imageBaseURL, thumbSize: posterThumbnailSize, kind: "season") } )
artworks.append(contentsOf: episodeImages.flatMap { loadArtwork(filePath: $0.file_path, baseURL: imageBaseURL, thumbSize: posterThumbnailSize, kind: "episode") } )
}
}
+2 -2
View File
@@ -294,7 +294,7 @@ public struct TheTVDB : MetadataService, MetadataNameService {
}
if selected {
artworks.append(RemoteImage(url: fileURL, thumbURL: thumbURL, providerName: "TheTVDB|" + type.rawValue))
artworks.append(RemoteImage(url: fileURL, thumbURL: thumbURL, service: self.name, type: type.rawValue))
}
}
return artworks
@@ -320,7 +320,7 @@ public struct TheTVDB : MetadataService, MetadataNameService {
}
if let filename = info.filename, let url = URL(string: TheTVDB.bannerPath + filename) {
artworks.append(RemoteImage(url: url, thumbURL: url, providerName: "TheTVDB|episode"))
artworks.append(RemoteImage(url: url, thumbURL: url, service: self.name, type: "episode"))
}
}
+1 -1
View File
@@ -186,7 +186,7 @@ public struct iTunesStore: MetadataService {
if let artworkURL = URL(string: text),
let artworkFullSizeURL = URL(string: text.replacingOccurrences(of: "100x100bb", with: replacement)) {
return RemoteImage(url:artworkFullSizeURL, thumbURL:artworkURL, providerName:"iTunes")
return RemoteImage(url:artworkFullSizeURL, thumbURL: artworkURL, service: self.name, type: "")
}
return nil
+6 -5
View File
@@ -143,7 +143,7 @@ import Cocoa
popUpButton.removeAllItems()
popUpButton.addItems(withTitles: service.languages.map { service.languageType.displayName(language: $0) })
let type: MetadataSearch.MetadataSearchType = popUpButton == movieLanguage ? .movie : .tvShow
let type: MetadataSearch.Kind = popUpButton == movieLanguage ? .movie : .tvShow
popUpButton.selectItem(withTitle: MetadataSearch.defaultLanguage(service: service, type: type))
@@ -295,7 +295,7 @@ import Cocoa
DispatchQueue.main.async {
self.state = .closing(search: search, result: result)
if let artworks = result.remoteArtworks, artworks.count > 0 {
self.selectArtwork(artworks: artworks.toStruct())
self.selectArtwork(artworks: artworks.toStruct(), type: search.type)
}
else {
self.addMetadata()
@@ -303,8 +303,9 @@ import Cocoa
}
}
private func selectArtwork(artworks: [RemoteImage]) {
let artworkSelectorController = ArtworkSelectorController(artworks: artworks, size: window?.frame.size, delegate: self)
private func selectArtwork(artworks: [RemoteImage], type: MetadataSearch.Kind) {
let artworkSelectorController = ArtworkSelectorController(artworks: artworks, size: window?.frame.size,
type: type, delegate: self)
window?.beginSheet(artworkSelectorController.window!, completionHandler: nil)
artworkSelector = artworkSelectorController
}
@@ -324,7 +325,7 @@ import Cocoa
result.artworks.add(MP42Image(data: data, type: MP42_ART_JPEG))
}
// Hack, download smaller iTunes version if big iTunes version is not available
else if artwork.providerName == iTunesStore().name,
else if artwork.service == iTunesStore().name,
let data = URLSession.data(from: artwork.url.deletingPathExtension().appendingPathExtension("600x600bb.jpg")) {
result.artworks.add(MP42Image(data: data, type: MP42_ART_JPEG))
}
+12 -7
View File
@@ -40,21 +40,26 @@ import Foundation
private func indexOfArtwork(type: QueueMetadataActionPreferredArtwork, provider: String, artworks: [RemoteImage]) -> Int? {
var preferredTypeName: String = ""
var artworkService: String = ""
var artworkType: String = ""
switch type {
case .iTunes:
preferredTypeName = "iTunes"
artworkService = "iTunes"
case .Episode:
preferredTypeName = "\(provider)|episode"
artworkService = provider
artworkType = "episode"
case .Season:
preferredTypeName = "\(provider)|season"
artworkService = provider
artworkType = "season"
default:
preferredTypeName = "\(provider)|poster"
artworkService = provider
artworkType = "poster"
}
for (index, image) in artworks.enumerated() {
if image.providerName.hasPrefix(preferredTypeName) {
let filteredByService = artworks.filter { $0.service == artworkService }
for (index, image) in filteredByService.enumerated() {
if image.type.hasPrefix(artworkType) {
return index
}
}