From c41cf3f18e4820000e9608658ba76bfc77b3ba2e Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Wed, 21 Aug 2024 14:08:57 -0500 Subject: [PATCH] converted to use FormFieldModel subclass Signed-off-by: Matt Bruce --- .../Atoms/Selectors/RadioBoxModel.swift | 55 +++++++------------ 1 file changed, 19 insertions(+), 36 deletions(-) diff --git a/MVMCoreUI/Atomic/Atoms/Selectors/RadioBoxModel.swift b/MVMCoreUI/Atomic/Atoms/Selectors/RadioBoxModel.swift index cd7623c2..8b9282a9 100644 --- a/MVMCoreUI/Atomic/Atoms/Selectors/RadioBoxModel.swift +++ b/MVMCoreUI/Atomic/Atoms/Selectors/RadioBoxModel.swift @@ -8,47 +8,34 @@ import MVMCore import VDS -@objcMembers public class RadioBoxModel: MoleculeModelProtocol, EnableableModelProtocol { +@objcMembers public class RadioBoxModel: FormFieldModel { //-------------------------------------------------- // MARK: - Properties //-------------------------------------------------- - public static var identifier: String = "radioBox" - public var id: String = UUID().uuidString + public override static var identifier: String { "radioBox" } public var text: String public var subText: String? public var subTextRight: String? - public var backgroundColor: Color? - public var accessibilityIdentifier: String? public var selected: Bool = false - public var enabled: Bool = true - public var readOnly: Bool = false public var strikethrough: Bool = false - public var fieldValue: String? public var action: ActionModelProtocol? - public var inverted: Bool = false - public var surface: Surface { inverted ? .dark : .light } - + public var fieldValue: String? + //-------------------------------------------------- // MARK: - Keys //-------------------------------------------------- private enum CodingKeys: String, CodingKey { - case id - case moleculeName case text case subText case subTextRight case backgroundColor - case accessibilityIdentifier case selected - case enabled case strikethrough - case fieldValue case action - case readOnly - case inverted + case fieldValue } //-------------------------------------------------- @@ -57,8 +44,19 @@ import VDS public init(text: String) { self.text = text + super.init() } + //-------------------------------------------------- + // MARK: - Form Validation + //-------------------------------------------------- + + /// Returns the fieldValue of the selected box, otherwise the text of the selected box. + public override func formFieldValue() -> AnyHashable? { + guard enabled else { return nil } + return fieldValue + } + //-------------------------------------------------- // MARK: - Codec //-------------------------------------------------- @@ -66,45 +64,30 @@ import VDS required public init(from decoder: Decoder) throws { let typeContainer = try decoder.container(keyedBy: CodingKeys.self) - id = try typeContainer.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString text = try typeContainer.decode(String.self, forKey: .text) subText = try typeContainer.decodeIfPresent(String.self, forKey: .subText) subTextRight = try typeContainer.decodeIfPresent(String.self, forKey: .subTextRight) - accessibilityIdentifier = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityIdentifier) if let isSelected = try typeContainer.decodeIfPresent(Bool.self, forKey: .selected) { selected = isSelected } - enabled = try typeContainer.decodeIfPresent(Bool.self, forKey: .enabled) ?? true - readOnly = try typeContainer.decodeIfPresent(Bool.self, forKey: .readOnly) ?? false - if let isStrikeTrough = try typeContainer.decodeIfPresent(Bool.self, forKey: .strikethrough) { strikethrough = isStrikeTrough } - - if let inverted = try typeContainer.decodeIfPresent(Bool.self, forKey: .inverted) { - self.inverted = inverted - } - fieldValue = try typeContainer.decodeIfPresent(String.self, forKey: .fieldValue) action = try typeContainer.decodeModelIfPresent(codingKey: .action) + try super.init(from: decoder) } - public func encode(to encoder: Encoder) throws { + public override func encode(to encoder: Encoder) throws { + try super.encode(to: encoder) var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(moleculeName, forKey: .moleculeName) try container.encode(text, forKey: .text) try container.encodeIfPresent(subText, forKey: .subText) try container.encodeIfPresent(subTextRight, forKey: .subTextRight) - try container.encodeIfPresent(accessibilityIdentifier, forKey: .accessibilityIdentifier) try container.encode(selected, forKey: .selected) - try container.encode(enabled, forKey: .enabled) - try container.encode(readOnly, forKey: .readOnly) try container.encode(strikethrough, forKey: .strikethrough) - try container.encodeIfPresent(fieldValue, forKey: .fieldValue) try container.encodeModelIfPresent(action, forKey: .action) - try container.encode(inverted, forKey: .inverted) } }