Created
August 27, 2020 09:42
-
-
Save hsleedevelop/ee2f017e87e781a56782478d8d6bbbac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ShopFilterParams: NSObject, Codable, DictionaryMapping { | |
private var config: [String: Any]? | |
private var defaults: [String: Any]? | |
private var list: [String: Any]? | |
var showAreaFilter: Bool? | |
var showServiceFilter: Bool? | |
var defaultAreaId: String? | |
var defaultService: String? | |
var services: [String: Any]? | |
enum CodingKeys: String, CodingKey { | |
case config | |
case defaults = "default" | |
case list | |
} | |
required public init(from decoder: Decoder) throws { | |
let container = try decoder.container(keyedBy: CodingKeys.self) | |
config = try? container.decode([String: Any].self, forKey: .config) | |
defaults = try? container.decode([String: Any].self, forKey: .defaults) | |
list = try? container.decode([String: Any].self, forKey: .list) | |
showAreaFilter = config?["showAreaFilter"] as? Bool ?? false | |
showServiceFilter = config?["showServiceFilter"] as? Bool ?? false | |
defaultAreaId = (defaults?["areaId"] as? Int)?.s ?? "9" | |
defaultService = defaults?["service"] as? String ?? "" | |
services = list?["service"] as? [String: Any] | |
} | |
@objc required init(dictionary: [String : Any]!) { | |
if let config = dictionary["config"] as? [String : Any] { | |
showAreaFilter = config["showAreaFilter"] as? Bool ?? false | |
showServiceFilter = config["showServiceFilter"] as? Bool ?? false | |
} | |
if let defaults = dictionary["default"] as? [String : Any] { | |
defaultAreaId = (defaults["areaId"] as? Int)?.s ?? "9" | |
defaultService = defaults["service"] as? String ?? "" | |
} | |
if let list = dictionary["list"] as? [String : Any] { | |
services = list["service"] as? [String: Any] | |
} | |
} | |
func encode(to encoder: Encoder) throws { | |
var container = encoder.container(keyedBy: CodingKeys.self) | |
var configContainer = container.nestedContainer(keyedBy: DynamicKey.self, forKey: .config) | |
try configContainer.encodeDynamicKeyValues(dictionary: config) | |
var defaultsContainer = container.nestedContainer(keyedBy: DynamicKey.self, forKey: .defaults) | |
try defaultsContainer.encodeDynamicKeyValues(dictionary: defaults) | |
var listContainer = container.nestedContainer(keyedBy: DynamicKey.self, forKey: .list) | |
try listContainer.encodeDynamicKeyValues(dictionary: list) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment