converted to use FormFieldModel subclass

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2024-08-21 14:08:57 -05:00
parent 56b4142c14
commit c41cf3f18e

View File

@ -8,47 +8,34 @@
import MVMCore import MVMCore
import VDS import VDS
@objcMembers public class RadioBoxModel: MoleculeModelProtocol, EnableableModelProtocol { @objcMembers public class RadioBoxModel: FormFieldModel {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Properties // MARK: - Properties
//-------------------------------------------------- //--------------------------------------------------
public static var identifier: String = "radioBox" public override static var identifier: String { "radioBox" }
public var id: String = UUID().uuidString
public var text: String public var text: String
public var subText: String? public var subText: String?
public var subTextRight: String? public var subTextRight: String?
public var backgroundColor: Color?
public var accessibilityIdentifier: String?
public var selected: Bool = false public var selected: Bool = false
public var enabled: Bool = true
public var readOnly: Bool = false
public var strikethrough: Bool = false public var strikethrough: Bool = false
public var fieldValue: String?
public var action: ActionModelProtocol? public var action: ActionModelProtocol?
public var inverted: Bool = false public var fieldValue: String?
public var surface: Surface { inverted ? .dark : .light }
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Keys // MARK: - Keys
//-------------------------------------------------- //--------------------------------------------------
private enum CodingKeys: String, CodingKey { private enum CodingKeys: String, CodingKey {
case id
case moleculeName
case text case text
case subText case subText
case subTextRight case subTextRight
case backgroundColor case backgroundColor
case accessibilityIdentifier
case selected case selected
case enabled
case strikethrough case strikethrough
case fieldValue
case action case action
case readOnly case fieldValue
case inverted
} }
//-------------------------------------------------- //--------------------------------------------------
@ -57,6 +44,17 @@ import VDS
public init(text: String) { public init(text: String) {
self.text = text 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
} }
//-------------------------------------------------- //--------------------------------------------------
@ -66,45 +64,30 @@ import VDS
required public init(from decoder: Decoder) throws { required public init(from decoder: Decoder) throws {
let typeContainer = try decoder.container(keyedBy: CodingKeys.self) 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) text = try typeContainer.decode(String.self, forKey: .text)
subText = try typeContainer.decodeIfPresent(String.self, forKey: .subText) subText = try typeContainer.decodeIfPresent(String.self, forKey: .subText)
subTextRight = try typeContainer.decodeIfPresent(String.self, forKey: .subTextRight) 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) { if let isSelected = try typeContainer.decodeIfPresent(Bool.self, forKey: .selected) {
selected = isSelected 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) { if let isStrikeTrough = try typeContainer.decodeIfPresent(Bool.self, forKey: .strikethrough) {
strikethrough = isStrikeTrough strikethrough = isStrikeTrough
} }
if let inverted = try typeContainer.decodeIfPresent(Bool.self, forKey: .inverted) {
self.inverted = inverted
}
fieldValue = try typeContainer.decodeIfPresent(String.self, forKey: .fieldValue) fieldValue = try typeContainer.decodeIfPresent(String.self, forKey: .fieldValue)
action = try typeContainer.decodeModelIfPresent(codingKey: .action) 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) 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.encode(text, forKey: .text)
try container.encodeIfPresent(subText, forKey: .subText) try container.encodeIfPresent(subText, forKey: .subText)
try container.encodeIfPresent(subTextRight, forKey: .subTextRight) try container.encodeIfPresent(subTextRight, forKey: .subTextRight)
try container.encodeIfPresent(accessibilityIdentifier, forKey: .accessibilityIdentifier)
try container.encode(selected, forKey: .selected) 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.encode(strikethrough, forKey: .strikethrough)
try container.encodeIfPresent(fieldValue, forKey: .fieldValue)
try container.encodeModelIfPresent(action, forKey: .action) try container.encodeModelIfPresent(action, forKey: .action)
try container.encode(inverted, forKey: .inverted)
} }
} }