OSDN Git Service

コメントを追加
[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 { return Logger.shared.log("Can not load image", value: nil) }
26         return image
27     }()
28     
29     @objc var creationDate: Date? {
30         
31         let attr = try? url.resourceValues(forKeys: [.creationDateKey])
32         
33         return attr?.creationDate
34     }
35     @objc var tags: [String]? {
36         
37         get {
38             let attr = try? url.resourceValues(forKeys: [.tagNamesKey])
39             
40             return attr?.tagNames
41         }
42         set {
43             let url = self.url as NSURL
44             do {
45                 
46                 if let array = newValue {
47                     
48                     try url.setResourceValue(array as NSArray, forKey: .tagNamesKey)
49                     
50                 } else {
51                     
52                     try url.setResourceValue([] as NSArray, forKey: .tagNamesKey)
53                 }
54                 
55             } catch {
56                 
57                 print("Can not set tagNames")
58             }
59         }
60     }
61     
62     private(set) var version: Int
63     
64     init(url: URL, version: Int = 0) {
65         
66         self.url = url
67         self.version = version
68         
69         super.init()
70     }
71     
72     func incrementVersion() { version = version + 1 }
73     
74     // MARK: - NSCoding
75     struct CodingKey {
76         
77         static let url = "Url"
78     }
79     
80     required convenience init?(coder aDecoder: NSCoder) {
81         
82         guard let u = aDecoder.decodeObject(forKey: CodingKey.url) as? URL else { return nil }
83         
84         self.init(url: u)
85     }
86     
87     func encode(with aCoder: NSCoder) {
88         
89         aCoder.encode(url, forKey: CodingKey.url)
90     }
91 }
92
93 private let dateFormatter: DateFormatter = {
94     
95     let f = DateFormatter()
96     f.dateStyle = .short
97     f.timeStyle = .short
98     f.doesRelativeDateFormatting = true
99     
100     return f
101 }()
102
103 extension ScreenshotInformation {
104     
105     @objc var name: String? {
106         
107         let attr = try? url.resourceValues(forKeys: [.localizedNameKey])
108         
109         return attr?.localizedName
110     }
111     
112     @objc var creationDateString: String? {
113         
114         return creationDate.map(dateFormatter.string(from:))
115     }
116 }