2 // ScreenshotInformation.swift
5 // Created by Hori,Masaki on 2017/01/28.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
11 final class ScreenshotModel: NSObject {
13 @objc dynamic var screenshots: [ScreenshotInformation] = []
14 @objc dynamic var sortDescriptors: [NSSortDescriptor]?
15 @objc dynamic var selectedIndexes: IndexSet?
16 @objc dynamic var filterPredicate: NSPredicate?
19 final class ScreenshotInformation: NSObject, NSCoding {
23 @objc lazy var image: NSImage? = {
25 guard let image = NSImage(contentsOf: url) else { return Logger.shared.log("Can not load image", value: nil) }
29 @objc var creationDate: Date? {
31 let attr = try? url.resourceValues(forKeys: [.creationDateKey])
33 return attr?.creationDate
35 @objc var tags: [String]? {
38 let attr = try? url.resourceValues(forKeys: [.tagNamesKey])
43 let url = self.url as NSURL
46 if let array = newValue {
48 try url.setResourceValue(array as NSArray, forKey: .tagNamesKey)
52 try url.setResourceValue([] as NSArray, forKey: .tagNamesKey)
57 print("Can not set tagNames")
62 private(set) var version: Int
64 init(url: URL, version: Int = 0) {
67 self.version = version
72 func incrementVersion() { version = version + 1 }
77 static let url = "Url"
80 required convenience init?(coder aDecoder: NSCoder) {
82 guard let u = aDecoder.decodeObject(forKey: CodingKey.url) as? URL else { return nil }
87 func encode(with aCoder: NSCoder) {
89 aCoder.encode(url, forKey: CodingKey.url)
93 private let dateFormatter: DateFormatter = {
95 let f = DateFormatter()
98 f.doesRelativeDateFormatting = true
103 extension ScreenshotInformation {
105 @objc var name: String? {
107 let attr = try? url.resourceValues(forKeys: [.localizedNameKey])
109 return attr?.localizedName
112 @objc var creationDateString: String? {
114 return creationDate.map(dateFormatter.string(from:))