OSDN Git Service

staticプロパティをインスタンスプロパティに変更
[kcd/KCD.git] / KCD / ScreenshotInformation.swift
1 //
2 //  ScreenshotInformation.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/01/28.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 final class ScreenshotModel: NSObject {
12     
13     @objc dynamic var screenshots: [ScreenshotInformation] = []
14     @objc dynamic var sortDescriptors: [NSSortDescriptor]?
15     @objc dynamic var selectedIndexes: IndexSet?
16     @objc dynamic var filterPredicate: NSPredicate?
17 }
18
19 final class ScreenshotInformation: NSObject, NSCoding {
20     
21     @objc let url: URL
22     
23     @objc lazy var image: NSImage? = {
24         
25         guard let image = NSImage(contentsOf: url) else {
26             
27             Logger.shared.log("Can not load image")
28             
29             return nil
30         }
31         
32         return image
33     }()
34     
35     @objc var creationDate: Date? {
36         
37         let attr = try? url.resourceValues(forKeys: [.creationDateKey])
38         
39         return attr?.creationDate
40     }
41     
42     @objc var tags: [String]? {
43         
44         get {
45             
46             let attr = try? url.resourceValues(forKeys: [.tagNamesKey])
47             
48             return attr?.tagNames
49         }
50         
51         set {
52             
53             let url = self.url as NSURL
54             do {
55                 
56                 if let array = newValue {
57                     
58                     try url.setResourceValue(array as NSArray, forKey: .tagNamesKey)
59                     
60                 } else {
61                     
62                     try url.setResourceValue([] as NSArray, forKey: .tagNamesKey)
63                 }
64                 
65             } catch {
66                 
67                 print("Can not set tagNames")
68             }
69         }
70     }
71     
72     private(set) var version: Int
73     
74     init(url: URL, version: Int = 0) {
75         
76         self.url = url
77         self.version = version
78         
79         super.init()
80     }
81     
82     func incrementVersion() { version = version + 1 }
83     
84     // MARK: - NSCoding
85     struct CodingKey {
86         
87         static let url = "Url"
88     }
89     
90     required convenience init?(coder aDecoder: NSCoder) {
91         
92         guard let u = aDecoder.decodeObject(forKey: CodingKey.url) as? URL else {
93             
94             return nil
95         }
96         
97         self.init(url: u)
98     }
99     
100     func encode(with aCoder: NSCoder) {
101         
102         aCoder.encode(url, forKey: CodingKey.url)
103     }
104 }
105
106 private let dateFormatter: DateFormatter = {
107     
108     let f = DateFormatter()
109     f.dateStyle = .short
110     f.timeStyle = .short
111     f.doesRelativeDateFormatting = true
112     
113     return f
114 }()
115
116 extension ScreenshotInformation {
117     
118     @objc var name: String? {
119         
120         let attr = try? url.resourceValues(forKeys: [.localizedNameKey])
121         
122         return attr?.localizedName
123     }
124     
125     @objc var creationDateString: String? {
126         
127         return creationDate.map(dateFormatter.string(from:))
128     }
129 }