diff --git a/MVMCoreUI/Atoms/Views/RadioButtonModel.swift b/MVMCoreUI/Atoms/Views/RadioButtonModel.swift index c8443691..d26e1671 100644 --- a/MVMCoreUI/Atoms/Views/RadioButtonModel.swift +++ b/MVMCoreUI/Atoms/Views/RadioButtonModel.swift @@ -13,5 +13,39 @@ public class RadioButtonModel: MoleculeModelProtocol { public static var identifier: String = "radioButton" public var backgroundColor: Color? public var state: Bool = false + public var enabled: Bool = true public var fieldKey: String? + + private enum CodingKeys: String, CodingKey { + case moleculeName + case backgroundColor + case state + case enabled + case fieldKey + } + + public init(_ state: Bool) { + self.state = state + } + + required public init(from decoder: Decoder) throws { + let typeContainer = try decoder.container(keyedBy: CodingKeys.self) + if let state = try typeContainer.decodeIfPresent(Bool.self, forKey: .state) { + self.state = state + } + if let enabled = try typeContainer.decodeIfPresent(Bool.self, forKey: .enabled) { + self.enabled = enabled + } + backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) + fieldKey = try typeContainer.decodeIfPresent(String.self, forKey: .fieldKey) + } + + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor) + try container.encode(moleculeName, forKey: .moleculeName) + try container.encode(state, forKey: .state) + try container.encode(enabled, forKey: .enabled) + try container.encodeIfPresent(fieldKey, forKey: .fieldKey) + } } diff --git a/MVMCoreUI/Atoms/Views/ToggleModel.swift b/MVMCoreUI/Atoms/Views/ToggleModel.swift index 04b45fe2..948a8e96 100644 --- a/MVMCoreUI/Atoms/Views/ToggleModel.swift +++ b/MVMCoreUI/Atoms/Views/ToggleModel.swift @@ -49,7 +49,7 @@ public class ToggleModel: MoleculeModelProtocol { try container.encodeModelIfPresent(action, forKey: .action) try container.encodeModelIfPresent(alternateAction, forKey: .alternateAction) try container.encode(moleculeName, forKey: .moleculeName) - try container.encodeIfPresent(state, forKey: .state) + try container.encode(state, forKey: .state) try container.encodeIfPresent(required, forKey: .required) try container.encodeIfPresent(fieldKey, forKey: .fieldKey) }