OSDN Git Service

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