OSDN Git Service

UAをVersion/10.0.3 Safari/602.4.8に変更
[kcd/KCD.git] / KCD / UserDefaultsExtension.swift
1 //
2 //  UserDefaultsExtension.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
12 extension UserDefaults {
13     
14     static var plan01Color: NSColor { return NSColor(calibratedRed: 0.000, green: 0.043, blue: 0.518, alpha: 1.0) }
15     static var plan02Color: NSColor { return NSColor(calibratedRed: 0.800, green: 0.223, blue: 0.000, alpha: 1.0) }
16     static var plan03Color: NSColor { return NSColor(calibratedRed: 0.539, green: 0.012, blue: 0.046, alpha: 1.0) }
17     static var plan04Color: NSColor { return NSColor(calibratedRed: 0.000, green: 0.535, blue: 0.535, alpha: 1.0) }
18     static var plan05Color: NSColor { return NSColor(calibratedRed: 0.376, green: 0.035, blue: 0.535, alpha: 1.0) }
19     static var plan06Color: NSColor { return NSColor(calibratedRed: 0.535, green: 0.535, blue: 0.000, alpha: 1.0) }
20     
21     func keyedArchivedObject(forKey key: String) -> Any? {
22         guard let data = object(forKey: key) as? Data else { return nil }
23         return NSKeyedUnarchiver.unarchiveObject(with: data)
24     }
25     func setKeyedArchived(_ object: Any?, forKey key: String) {
26         guard let object = object else {
27             removeObject(forKey: key)
28             return
29         }
30         let data = NSKeyedArchiver.archivedData(withRootObject: object)
31         set(data, forKey: key)
32     }
33     
34     class func registerAllDefaults() {
35         standard.register(defaults:
36             [
37                 "screenShotBorderWidth": 0.0,
38                 "plan01Color": NSKeyedArchiver.archivedData(withRootObject: plan01Color),
39                 "plan02Color": NSKeyedArchiver.archivedData(withRootObject: plan02Color),
40                 "plan03Color": NSKeyedArchiver.archivedData(withRootObject: plan03Color),
41                 "plan04Color": NSKeyedArchiver.archivedData(withRootObject: plan04Color),
42                 "plan05Color": NSKeyedArchiver.archivedData(withRootObject: plan05Color),
43                 "plan06Color": NSKeyedArchiver.archivedData(withRootObject: plan06Color),
44                 "screenshotPreviewZoomValue": 0.4,
45                 "showEquipmentType": -1,
46                 "fleetViewPosition": 1,
47                 "autoCombinedView": true,
48                 "screenshotEditorColumnCount": 2,
49                 "cleanSiceDays": 90,
50                 "notifyTimeSignal": false,
51                 "notifyTimeBeforeTimeSignal": 5,
52                 ]
53         )
54     }
55 }
56 extension UserDefaults {
57     var slotItemSortDescriptors: [NSSortDescriptor] {
58         get {
59             return keyedArchivedObject(forKey: "slotItemSortKey2") as? [NSSortDescriptor] ?? []
60         }
61         set {
62             setKeyedArchived(newValue, forKey: "slotItemSortKey2")
63         }
64     }
65     var shipviewSortDescriptors: [NSSortDescriptor] {
66         get {
67             return keyedArchivedObject(forKey: "shipviewsortdescriptor") as? [NSSortDescriptor] ?? []
68         }
69         set {
70             setKeyedArchived(newValue, forKey: "shipviewsortdescriptor")
71         }
72     }
73     var powerupSupportSortDecriptors: [NSSortDescriptor] {
74         get {
75             return keyedArchivedObject(forKey: "powerupsupportsortdecriptor") as? [NSSortDescriptor] ?? []
76         }
77         set {
78             setKeyedArchived(newValue, forKey: "powerupsupportsortdecriptor")
79         }
80     }
81     
82     // MARK: -
83     var showsDebugMenu: Bool {
84         get { return bool(forKey: "ShowsDebugMenu") }
85         set { set(newValue, forKey: "ShowsDebugMenu") }
86     }
87     
88     // MARK: -
89     var appendKanColleTag: Bool {
90         get { return bool(forKey: "appendKanColleTag") }
91         set { set(newValue, forKey: "appendKanColleTag") }
92     }
93     
94     // MARK: -
95     var hideMaxKaryoku: Bool {
96         get { return bool(forKey: "hideMaxKaryoku") }
97         set { set(newValue, forKey: "hideMaxKaryoku") }
98     }
99     var hideMaxLucky: Bool {
100         get { return bool(forKey: "hideMaxLucky") }
101         set { set(newValue, forKey: "hideMaxLucky") }
102     }
103     var hideMaxRaisou: Bool {
104         get { return bool(forKey: "hideMaxRaisou") }
105         set { set(newValue, forKey: "hideMaxRaisou") }
106     }
107     var hideMaxSoukou: Bool {
108         get { return bool(forKey: "hideMaxSoukou") }
109         set { set(newValue, forKey: "hideMaxSoukou") }
110     }
111     var hideMaxTaiku: Bool {
112         get { return bool(forKey: "hideMaxTaiku") }
113         set { set(newValue, forKey: "hideMaxTaiku") }
114     }
115     
116     // MARK: - plan color
117     var showsPlanColor: Bool {
118         get { return bool(forKey: "showsPlanColor") }
119         set { set(newValue, forKey: "showsPlanColor") }
120     }
121     var plan01Color: NSColor {
122         get {
123             return keyedArchivedObject(forKey: "plan01Color") as? NSColor ?? UserDefaults.plan01Color
124         }
125         set { setKeyedArchived(newValue, forKey: "plan01Color") }
126     }
127     var plan02Color: NSColor {
128         get {
129             return keyedArchivedObject(forKey: "plan02Color") as? NSColor ?? UserDefaults.plan02Color
130         }
131         set { setKeyedArchived(newValue, forKey: "plan02Color") }
132     }
133     var plan03Color: NSColor {
134         get {
135             return keyedArchivedObject(forKey: "plan03Color") as? NSColor ?? UserDefaults.plan03Color
136         }
137         set { setKeyedArchived(newValue, forKey: "plan03Color") }
138     }
139     var plan04Color: NSColor {
140         get {
141             return keyedArchivedObject(forKey: "plan04Color") as? NSColor ?? UserDefaults.plan04Color
142         }
143         set { setKeyedArchived(newValue, forKey: "plan04Color") }
144     }
145     var plan05Color: NSColor {
146         get {
147             return keyedArchivedObject(forKey: "plan05Color") as? NSColor ?? UserDefaults.plan05Color
148         }
149         set { setKeyedArchived(newValue, forKey: "plan05Color") }
150     }
151     var plan06Color: NSColor {
152         get {
153             return keyedArchivedObject(forKey: "plan06Color") as? NSColor ?? UserDefaults.plan06Color
154         }
155         set { setKeyedArchived(newValue, forKey: "plan06Color") }
156     }
157     
158     // MARK: -
159     var minimumColoredShipCount: Int {
160         get { return integer(forKey: "minimumColoredShipCount") }
161         set { set(newValue, forKey: "minimumColoredShipCount") }
162     }
163     
164     // MARK: - screenshot
165     var useMask: Bool {
166         get { return bool(forKey: "useMask") }
167         set { set(newValue, forKey: "useMask") }
168     }
169     var screenShotBorderWidth: CGFloat {
170         get {
171             let r = double(forKey: "screenShotBorderWidth")
172             if r < 0 { return 0.0 }
173             return r > 20 ? 20.0 : CGFloat(r)
174         }
175         set {
176             if 0.0..<20 ~= newValue { return }
177             set(newValue, forKey: "screenShotBorderWidth")
178         }
179     }
180     var screenShotSaveDirectory: String? {
181         get { return object(forKey: "screenShotSaveDirectory") as? String }
182         set { set(newValue, forKey: "screenShotSaveDirectory") }
183     }
184     var screenShotSaveDirectoryURL: URL? {
185         guard let path = screenShotSaveDirectory else { return nil }
186         return URL(fileURLWithPath: path, isDirectory: true)
187     }
188     var showsListWindowAtScreenshot: Bool {
189         get { return bool(forKey: "showsListWindowAtScreenshot") }
190         set { set(newValue, forKey: "showsListWindowAtScreenshot") }
191     }
192     var screenshotPreviewZoomValue: Double {
193         get { return double(forKey: "screenshotPreviewZoomValue") }
194         set { set(newValue, forKey: "screenshotPreviewZoomValue") }
195     }
196     var screenshotEditorColumnCount: Int {
197         get { return integer(forKey: "screenshotEditorColumnCount") }
198         set { set(newValue, forKey: "screenshotEditorColumnCount") }
199     }
200     var scrennshotEditorType: Int {
201         get { return integer(forKey: "scrennshotEditorType") }
202         set { set(newValue, forKey: "scrennshotEditorType") }
203     }
204     var screenshotButtonSize: NSControlSize {
205         get {
206             return NSControlSize(rawValue: UInt(integer(forKey: "screenshotButtonSize"))) ?? .regular
207         
208         }
209         set { set(newValue.rawValue, forKey: "screenshotButtonSize") }
210     }
211     
212     // MARK: - Notify Sound
213     var playFinishMissionSound: Bool {
214         get { return bool(forKey: "playFinishMissionSound") }
215         set { set(newValue, forKey: "playFinishMissionSound") }
216     }
217     var playFinishNyukyoSound: Bool {
218         get { return bool(forKey: "playFinishNyukyoSound") }
219         set { set(newValue, forKey: "playFinishNyukyoSound") }
220     }
221     var playFinishKenzoSound: Bool {
222         get { return bool(forKey: "playFinishKenzoSound") }
223         set { set(newValue, forKey: "playFinishKenzoSound") }
224     }
225     
226     // MARK: - Upgradable List
227     var showLevelOneShipInUpgradableList: Bool {
228         get { return bool(forKey: "showLevelOneShipInUpgradableList") }
229         set { set(newValue, forKey: "showLevelOneShipInUpgradableList") }
230     }
231     var showsExcludedShipInUpgradableList: Bool {
232         get { return bool(forKey: "showsExcludedShipInUpgradableList") }
233         set { set(newValue, forKey: "showsExcludedShipInUpgradableList") }
234     }
235     
236     // MARK: - Equipment List
237     var showEquipmentType: SlotItemWindowController.ShowType {
238         get {
239             let t = integer(forKey: "showEquipmentType")
240             let type = SlotItemWindowController.ShowType(rawValue: t)
241             return type ?? .all
242         }
243         set { set(newValue.rawValue, forKey: "showEquipmentType") }
244     }
245     
246     // MARK: - FleetView
247     var fleetViewPosition: BroserWindowController.FleetViewPosition {
248         get {
249             let t = integer(forKey: "fleetViewPosition")
250             let position = BroserWindowController.FleetViewPosition(rawValue: t)
251             return position ?? .above
252         }
253         set { set(newValue.rawValue, forKey: "fleetViewPosition") }
254     }
255     var fleetViewShipOrder: FleetViewController.ShipOrder {
256         get {
257             let o = integer(forKey: "fleetViewShipOrder")
258             let order = FleetViewController.ShipOrder(rawValue: o)
259             return order ?? .doubleLine
260         }
261         set { set(newValue.rawValue, forKey: "fleetViewShipOrder") }
262     }
263     var repairTime: Date {
264         get { return keyedArchivedObject(forKey: "repairTime") as? Date ?? Date() }
265         set { setKeyedArchived(newValue, forKey: "repairTime") }
266     }
267     
268     // MARK: - combined view
269     var lastHasCombinedView: Bool {
270         get { return bool(forKey: "lastHasCombinedView") }
271         set { set(newValue, forKey: "lastHasCombinedView") }
272     }
273     var autoCombinedView: Bool {
274         get { return bool(forKey: "autoCombinedView") }
275         set { set(newValue, forKey: "autoCombinedView") }
276     }
277     var useSwipeChangeCombinedView: Bool {
278         get { return bool(forKey: "useSwipeChangeCombinedView") }
279         set { set(newValue, forKey: "useSwipeChangeCombinedView") }
280     }
281     
282     // MARK: - Old History Item Clean
283     var cleanOldHistoryItems: Bool {
284         get { return bool(forKey: "cleanOldHistoryItems") }
285         set { set(newValue, forKey: "cleanOldHistoryItems") }
286     }
287     var cleanSiceDays: Int {
288         get { return integer(forKey: "cleanSiceDays") }
289         set { set(newValue, forKey: "cleanSiceDays") }
290     }
291     
292     // MARK: - Notify time signal
293     var notifyTimeSignal: Bool {
294         get { return bool(forKey: "notifyTimeSignal") }
295         set { set(newValue, forKey: "notifyTimeSignal") }
296     }
297     var notifyTimeBeforeTimeSignal: Int {
298         get { return integer(forKey: "notifyTimeBeforeTimeSignal") }
299         set {
300             if newValue < 1 {
301                 set(1, forKey: "notifyTimeBeforeTimeSignal")
302             } else if newValue > 59 {
303                 set(59, forKey: "notifyTimeBeforeTimeSignal")
304             } else {
305                 set(newValue, forKey: "notifyTimeBeforeTimeSignal")
306             }
307         }
308     }
309     var playNotifyTimeSignalSound: Bool {
310         get { return bool(forKey: "playNotifyTimeSignalSound") }
311         set { set(newValue, forKey: "playNotifyTimeSignalSound") }
312     }
313     
314     // MARK: - Debug print
315     var degugPrintLevel: Debug.Level {
316         return Debug.Level(rawValue: integer(forKey: "DebugPrintLevel")) ?? .none
317     }
318 }
319
320 extension UserDefaults {
321     private var dateFormatter: DateFormatter {
322         let f = DateFormatter()
323         f.dateFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss' 'Z"
324         return f
325     }
326     var prevReloadDate: Date? {
327         get {
328             guard let p = object(forKey: "previousReloadDateString") as? String else { return nil }
329             return dateFormatter.date(from: p)
330         }
331         set {
332             guard let newValue = newValue else {
333                 removeObject(forKey: "previousReloadDateString")
334                 return
335             }
336             let data = dateFormatter.string(from: newValue)
337             set(data, forKey: "previousReloadDateString")
338         }
339     }
340 }