4 Copyright (c) 2017, Hori, Masaki.
7 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
9 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
13 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 Created by 1024jp on 2017-02-14.
26 ------------------------------------------------------------------------------
30 Licensed under the Apache License, Version 2.0 (the "License");
31 you may not use this file except in compliance with the License.
32 You may obtain a copy of the License at
34 https://www.apache.org/licenses/LICENSE-2.0
36 Unless required by applicable law or agreed to in writing, software
37 distributed under the License is distributed on an "AS IS" BASIS,
38 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
39 See the License for the specific language governing permissions and
40 limitations under the License.
48 enum DefaultsKeyError: Error {
54 protocol DefaultValuePrimitive { }
55 extension Bool: DefaultValuePrimitive { }
56 extension Int: DefaultValuePrimitive { }
57 extension UInt: DefaultValuePrimitive { }
58 extension Float: DefaultValuePrimitive { }
59 extension Double: DefaultValuePrimitive { }
60 extension CGFloat: DefaultValuePrimitive { }
61 extension Data: DefaultValuePrimitive { }
62 extension String: DefaultValuePrimitive { }
63 extension URL: DefaultValuePrimitive { }
64 extension Array: DefaultValuePrimitive { }
65 extension Dictionary: DefaultValuePrimitive { }
68 // FIXME: value [["key": Date()]] returns true but it can not into UserDefaults.
69 func isDefaultValuePrimitive(_ value: Any) -> Bool {
75 case _ as [DefaultValuePrimitive]: return true
82 if value is [AnyHashable: Any] {
86 case _ as [String: DefaultValuePrimitive]: return true
93 if value is DefaultValuePrimitive {
101 // FIXME: value [["key": <Any Struct>]] returns true but it can not KeyedUnarchive.
102 func canCoding(_ value: Any) -> Bool {
108 case _ as [NSCoding]: return true
110 default: return false
115 if value is [AnyHashable: Any] {
119 case _ as [String: NSCoding]: return true
121 default: return false
126 if value is NSCoding {
134 extension DefaultKeys {
136 static let settings: [DefaultKeys: Any?] = [
138 .slotItemSortDescriptors: nil,
139 .shipviewSortDescriptors: nil,
140 .powerupSupportSortDecriptors: nil,
142 .showsDebugMenu: false,
143 .degugPrintLevel: nil,
145 .prevReloadDate: nil,
147 .hideMaxKaryoku: false,
148 .hideMaxLucky: false,
149 .hideMaxRaisou: false,
150 .hideMaxSoukou: false,
151 .hideMaxTaiku: false,
153 .showsPlanColor: true,
154 .plan01Color: #colorLiteral(red: 0.000, green: 0.043, blue: 0.518, alpha: 1),
155 .plan02Color: #colorLiteral(red: 0.800, green: 0.223, blue: 0.000, alpha: 1),
156 .plan03Color: #colorLiteral(red: 0.539, green: 0.012, blue: 0.046, alpha: 1),
157 .plan04Color: #colorLiteral(red: 0.000, green: 0.535, blue: 0.535, alpha: 1),
158 .plan05Color: #colorLiteral(red: 0.376, green: 0.035, blue: 0.535, alpha: 1),
159 .plan06Color: #colorLiteral(red: 0.535, green: 0.535, blue: 0.000, alpha: 1),
161 .minimumColoredShipCount: 0,
163 .playFinishMissionSound: true,
164 .playFinishNyukyoSound: true,
165 .playFinishKenzoSound: true,
167 .showLevelOneShipInUpgradableList: true,
168 .showsExcludedShipInUpgradableList: true,
170 .showEquipmentType: nil,
172 .fleetViewPosition: nil,
173 .fleetViewShipOrder: nil,
176 .lastHasCombinedView: false,
177 .autoCombinedView: true,
178 .useSwipeChangeCombinedView: false,
180 .appendKanColleTag: true,
181 .showsListWindowAtScreenshot: false,
182 .screenshotPreviewZoomValue: 0.4,
183 .screenshotEditorColumnCount: 2,
184 .scrennshotEditorType: nil,
185 .screenshotButtonSize: nil,
187 .screenShotBorderWidth: 0.0,
188 .screenShotSaveDirectory: nil,
190 .cleanOldHistoryItems: false,
193 .notifyTimeSignal: false,
194 .notifyTimeBeforeTimeSignal: 5,
195 .playNotifyTimeSignalSound: false,
197 .sakutekiCalclationSterategy: nil,
198 .formula33Factor: 1.0
202 static let defaults: [String: Any] = settings
203 .compactMap { (k: DefaultKeys, v: Any?) -> (key: String, value: Any)? in
205 guard let value = v else {
210 if isDefaultValuePrimitive(value) {
212 return (key: k.rawValue, value: value)
215 if canCoding(value) {
217 return (key: k.rawValue, value: NSKeyedArchiver.archivedData(withRootObject: value))
220 print("DefaultKeys can not store this type", type(of: value))
224 .reduce(into: [String: Any]()) { (dict: inout [String: Any], pair) in
226 dict[pair.key] = pair.value