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 var creationDate: Date? {
25 let attr = try? url.resourceValues(forKeys: [.creationDateKey])
27 return attr?.creationDate
29 @objc var tags: [String]? {
32 let attr = try? url.resourceValues(forKeys: [.tagNamesKey])
37 let url = self.url as NSURL
40 if let array = newValue {
42 try url.setResourceValue(array as NSArray, forKey: .tagNamesKey)
46 try url.setResourceValue([] as NSArray, forKey: .tagNamesKey)
51 print("Can not set tagNames")
56 private(set) var version: Int
58 init(url: URL, version: Int = 0) {
61 self.version = version
66 func incrementVersion() { version = version + 1 }
71 static let url = "Url"
74 required convenience init?(coder aDecoder: NSCoder) {
76 guard let u = aDecoder.decodeObject(forKey: CodingKey.url) as? URL else { return nil }
81 func encode(with aCoder: NSCoder) {
83 aCoder.encode(url, forKey: CodingKey.url)
87 private let dateFormatter: DateFormatter = {
89 let f = DateFormatter()
92 f.doesRelativeDateFormatting = true
97 extension ScreenshotInformation {
99 @objc var name: String? {
101 let attr = try? url.resourceValues(forKeys: [.localizedNameKey])
103 return attr?.localizedName
106 @objc var creationDateString: String? {
108 return creationDate.map(dateFormatter.string(from:))