// // AlbumTableCellView.swift // TagTunes // // Created by Kim Wittenburg on 28.08.15. // Copyright © 2015 Kim Wittenburg. All rights reserved. // import Cocoa import AppKitPlus import SearchAPI /// A table cell view to represent an `Album`. public class AlbumTableCellView: AdvancedTableCellView { // MARK: Types private struct Constants { static let secondaryImageViewWidth: CGFloat = 17 } // MARK: Properties /// Returns the receiver's `imageView` as an `AsyncImageView`. This property /// replaces the `imageView` property. /// /// Intended to be used as accessory view. @IBOutlet public var asyncImageView: AsyncImageView! = { let imageView = AsyncImageView() imageView.imageScaling = .ScaleProportionallyUpOrDown imageView.translatesAutoresizingMaskIntoConstraints = false return imageView }() /// Intended to be used as accessory view. /// /// Contains the *tick* image for saved albums. @IBOutlet public lazy var secondaryImageView: NSImageView! = { let secondaryImageView = NSImageView() secondaryImageView.image = NSImage(named: "Tick")?.imageByMaskingWithColor(NSColor.clearColor()) secondaryImageView.imageScaling = .ScaleProportionallyDown secondaryImageView.translatesAutoresizingMaskIntoConstraints = false return secondaryImageView }() /// Intended to be used as accessory view. /// /// Displayed for loading albums. @IBOutlet public lazy var loadingIndicator: NSProgressIndicator! = { let loadingIndicator = NSProgressIndicator() loadingIndicator.style = .SpinningStyle loadingIndicator.indeterminate = true loadingIndicator.controlSize = .SmallControlSize loadingIndicator.translatesAutoresizingMaskIntoConstraints = false return loadingIndicator }() @IBOutlet public lazy var errorButton: NSButton! = { let errorButton = NSButton() errorButton.setButtonType(NSButtonType.MomentaryChangeButton) errorButton.bezelStyle = NSBezelStyle.ShadowlessSquareBezelStyle errorButton.bordered = false errorButton.imagePosition = .ImageOnly errorButton.image = NSImage(named: NSImageNameCaution) errorButton.translatesAutoresizingMaskIntoConstraints = false return errorButton }() // MARK: Initializers override public func setupView() { style = .Subtitle primaryTextField?.font = NSFont.boldSystemFontOfSize(0) setLeftAccessoryView(asyncImageView, withConstraints: [NSLayoutConstraint( item: asyncImageView, attribute: .Width, relatedBy: .Equal, toItem: asyncImageView, attribute: .Height, multiplier: 1, constant: 0)]) super.setupView() } // MARK: Overrides /// The receiver's imageView. /// /// - attention: Use `asyncImageView` instead. override public var imageView: NSImageView? { set { asyncImageView = newValue as? AsyncImageView } get { return asyncImageView } } // MARK: Methods /// Configures the receiver to display the specified `album`. /// /// - parameters: /// - albumItem: The album to be displayed. /// - height: The height the cell view is going to have. This is used to /// apropriately scale the album's artwork. public func setupForAlbumItem(albumItem: AlbumItem, height: CGFloat) { textField?.stringValue = Preferences.sharedPreferences.useCensoredNames ? albumItem.album.censoredName : albumItem.album.name secondaryTextField?.stringValue = albumItem.album.artist.name asyncImageView.downloadImageFromURL(albumItem.album.artwork.optimalArtworkURLForImageSize(height)) switch albumItem.loadingState { case .Loading: textField?.textColor = NSColor.disabledControlTextColor() rightAccessoryView = loadingIndicator case .Error: textField?.textColor = NSColor.redColor() rightAccessoryView = errorButton case .Normal: textField?.textColor = NSColor.controlTextColor() if albumItem.saved { let aspectRatioConstraint = NSLayoutConstraint( item: secondaryImageView, attribute: .Width, relatedBy: .Equal, toItem: secondaryImageView, attribute: .Height, multiplier: 1, constant: 0) let widthConstraint = NSLayoutConstraint( item: secondaryImageView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .Width, multiplier: 1, constant: Constants.secondaryImageViewWidth) setRightAccessoryView(secondaryImageView, withConstraints: [aspectRatioConstraint, widthConstraint]) } else { rightAccessoryView = nil } } } /// Configures the cell to display the specified `album`. This method should /// be used to display search results. /// /// - parameters: /// - album: The `Album` to be displayed. /// - enabled: Wether the cell should appear *enabled*. If not it draws its /// text in a gray color. /// - height: The height the cell view is going to have. This is used to /// appropriately scale the album's artwork. public func setupForAlbum(album: Album, enabled: Bool, height: CGFloat) { textField?.stringValue = Preferences.sharedPreferences.useCensoredNames ? album.censoredName : album.name textField?.textColor = NSColor.controlTextColor() secondaryTextField?.stringValue = album.artist.name textField?.textColor = enabled ? NSColor.textColor() : NSColor.disabledControlTextColor() secondaryTextField?.textColor = enabled ? NSColor.secondaryLabelColor() : NSColor.disabledControlTextColor() asyncImageView.downloadImageFromURL(album.artwork.optimalArtworkURLForImageSize(height)) asyncImageView.enabled = enabled } }