Archived
1
This repository has been archived on 2020-06-04. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
tagtunes/TagTunes/AlbumTableCellView.swift
2015-09-03 00:22:33 +02:00

178 lines
6.4 KiB
Swift

//
// AlbumTableCellView.swift
// TagTunes
//
// Created by Kim Wittenburg on 28.08.15.
// Copyright © 2015 Kim Wittenburg. All rights reserved.
//
import Cocoa
import AppKitPlus
/// A table cell view to represent an `Album`. This view can be initialized using
/// `initWithFrame`.
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")
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
}()
/// Intended to be used as accessory view.
///
/// Displayed for search results.
@IBOutlet public lazy var button: NSButton! = {
let button = NSButton()
button.setButtonType(NSButtonType.MomentaryPushInButton)
button.bezelStyle = NSBezelStyle.RoundedBezelStyle
button.controlSize = .SmallControlSize
button.setContentHuggingPriority(NSLayoutPriorityDefaultHigh, forOrientation: .Horizontal)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
@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:
/// - album: The album to be displayed.
/// - loading: `true` if a loading indicator should be displayed at the
/// album view.
public func setupForAlbum(album: Album, loading: Bool, error: NSError?) {
textField?.stringValue = album.name
secondaryTextField?.stringValue = album.artistName
asyncImageView.downloadImageFromURL(album.artwork.hiResURL)
if loading {
textField?.textColor = NSColor.disabledControlTextColor()
rightAccessoryView = loadingIndicator
} else if error != nil {
textField?.textColor = NSColor.redColor()
rightAccessoryView = errorButton
} else {
textField?.textColor = NSColor.controlTextColor()
if album.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: 17)
setRightAccessoryView(secondaryImageView, withConstraints: [aspectRatioConstraint, widthConstraint])
} else {
rightAccessoryView = nil
}
}
}
/// Configures the receiver to display the specified `searchResult`.
///
/// - parameters:
/// - searchResult: The search result to be displayed.
/// - selectable: `true` if the search result can be selected, `false`
/// otherwise.
public func setupForSearchResult(searchResult: SearchResult, selectable: Bool) {
textField?.stringValue = searchResult.name
textField?.textColor = NSColor.controlTextColor()
secondaryTextField?.stringValue = searchResult.artistName
asyncImageView.downloadImageFromURL(searchResult.artwork.hiResURL)
if selectable {
button.title = NSLocalizedString("Select", comment: "Button title for 'selecting a search result'")
button.enabled = true
} else {
button.title = NSLocalizedString("Added", comment: "Button title for a search result that is already present")
button.enabled = false
}
rightAccessoryView = button
}
}