Skip to content

Instantly share code, notes, and snippets.

@RoyalIcing
Created July 22, 2015 22:07
Show Gist options
  • Save RoyalIcing/c9eb2ff1b91c86823b10 to your computer and use it in GitHub Desktop.
Save RoyalIcing/c9eb2ff1b91c86823b10 to your computer and use it in GitHub Desktop.
internal enum FileInfoIdentifier: String {
case DisplayNameAndIcon = "displayNameAndIcon"
case DateModified = "dateModified"
var sortDescriptor: NSSortDescriptor {
switch self {
case .DisplayNameAndIcon:
return NSSortDescriptor(key:NSURLLocalizedNameKey, ascending:true)
case .DateModified:
return NSSortDescriptor(key:NSURLContentModificationDateKey, ascending:false)
}
}
}
func textForInfoIdentifier(identifier: FileInfoIdentifier, fileURL: NSURL) -> String? {
switch identifier {
case .DisplayNameAndIcon:
return fileInfoRetriever.resourceValueForKey(NSURLLocalizedNameKey, forURL: fileURL) as? String
case .DateModified:
if let dateModified = fileInfoRetriever.resourceValueForKey(NSURLContentModificationDateKey, forURL: fileURL) as? NSDate {
return dateFormatter.stringFromDate(dateModified)
}
}
return nil
}
func imageForInfoIdentifier(identifier: FileInfoIdentifier, fileURL: NSURL) -> NSImage? {
switch identifier {
case .DisplayNameAndIcon:
return fileInfoRetriever.resourceValueForKey(NSURLEffectiveIconKey, forURL: fileURL) as? NSImage
default:
return nil
}
}
func tableCellViewForTableView(tableView: NSTableView, tableColumn: NSTableColumn?, fileURL: NSURL) -> NSTableCellView? {
if let identifier = tableColumn?.identifier {
let cellView = tableView.makeViewWithIdentifier(identifier, owner: nil) as! NSTableCellView
let fileInfoRetriever = self.fileInfoRetriever
var text: String?
var image: NSImage?
let hasImageView = (cellView.imageView != nil)
if let infoIdentifier = FileInfoIdentifier(rawValue: identifier) {
text = textForInfoIdentifier(infoIdentifier, fileURL: fileURL)
image = imageForInfoIdentifier(infoIdentifier, fileURL: fileURL)
}
cellView.textField?.stringValue = text ?? "Loading…"
cellView.imageView?.image = image
return cellView
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment