refactored models

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2024-07-09 10:21:55 -05:00
parent aa773dc8f5
commit 8a12bf12c2
2 changed files with 42 additions and 53 deletions

View File

@ -6,6 +6,7 @@
// Copyright © 2020 Verizon Wireless. All rights reserved. // Copyright © 2020 Verizon Wireless. All rights reserved.
// //
import MVMCore import MVMCore
import VDS
@objcMembers public class RadioBoxModel: MoleculeModelProtocol, EnableableModelProtocol { @objcMembers public class RadioBoxModel: MoleculeModelProtocol, EnableableModelProtocol {
//-------------------------------------------------- //--------------------------------------------------
@ -17,15 +18,17 @@ import MVMCore
public var text: String public var text: String
public var subText: String? public var subText: String?
public var subTextRight: String?
public var backgroundColor: Color? public var backgroundColor: Color?
public var accessibilityIdentifier: String? public var accessibilityIdentifier: String?
public var selectedAccentColor: Color?
public var selected: Bool = false public var selected: Bool = false
public var enabled: Bool = true public var enabled: Bool = true
public var readOnly: Bool = false public var readOnly: Bool = false
public var strikethrough: Bool = false public var strikethrough: Bool = false
public var fieldValue: String? public var fieldValue: String?
public var action: ActionModelProtocol? public var action: ActionModelProtocol?
public var inverted: Bool = false
public var surface: Surface { inverted ? .dark : .light }
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Keys // MARK: - Keys
@ -36,7 +39,7 @@ import MVMCore
case moleculeName case moleculeName
case text case text
case subText case subText
case selectedAccentColor case subTextRight
case backgroundColor case backgroundColor
case accessibilityIdentifier case accessibilityIdentifier
case selected case selected
@ -45,6 +48,7 @@ import MVMCore
case fieldValue case fieldValue
case action case action
case readOnly case readOnly
case inverted
} }
//-------------------------------------------------- //--------------------------------------------------
@ -65,8 +69,7 @@ import MVMCore
id = try typeContainer.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString 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)
selectedAccentColor = try typeContainer.decodeIfPresent(Color.self, forKey: .selectedAccentColor) subTextRight = try typeContainer.decodeIfPresent(String.self, forKey: .subTextRight)
backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
accessibilityIdentifier = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityIdentifier) 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) {
@ -80,6 +83,10 @@ import MVMCore
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)
} }
@ -90,8 +97,7 @@ import MVMCore
try container.encode(moleculeName, forKey: .moleculeName) 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(selectedAccentColor, forKey: .selectedAccentColor) try container.encodeIfPresent(subTextRight, forKey: .subTextRight)
try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
try container.encodeIfPresent(accessibilityIdentifier, forKey: .accessibilityIdentifier) 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(enabled, forKey: .enabled)

View File

@ -6,32 +6,38 @@
// Copyright © 2020 Verizon Wireless. All rights reserved. // Copyright © 2020 Verizon Wireless. All rights reserved.
// //
import MVMCore import MVMCore
import VDS
@objcMembers public class RadioBoxesModel: MoleculeModelProtocol, FormFieldProtocol { @objcMembers public class RadioBoxesModel: FormFieldModel {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Properties // MARK: - Properties
//-------------------------------------------------- //--------------------------------------------------
public static var identifier: String = "radioBoxes" public override static var identifier: String { "radioBoxes" }
public var id: String = UUID().uuidString
public var boxes: [RadioBoxModel] public var boxes: [RadioBoxModel]
public var backgroundColor: Color? public var inverted: Bool = false
public var accessibilityIdentifier: String? public var surface: Surface { inverted ? .dark : .light }
public var selectedAccentColor: Color?
public var boxesColor: Color? public var selectorModels: [VDS.RadioBoxGroup.RadioBoxItemModel] {
public var fieldKey: String? boxes.compactMap({ item in
public var groupName: String = FormValidator.defaultGroupName var radioBox = RadioBoxGroup.RadioBoxItemModel()
public var baseValue: AnyHashable? radioBox.text = item.text
public var enabled: Bool = true radioBox.subText = item.subText
public var readOnly: Bool = false radioBox.subTextRight = item.subTextRight
radioBox.surface = surface
radioBox.selected = item.selected
radioBox.strikethrough = item.strikethrough
radioBox.disabled = !(item.enabled && !item.readOnly)
return radioBox
})
}
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Form Validation // MARK: - Form Validation
//-------------------------------------------------- //--------------------------------------------------
/// Returns the fieldValue of the selected box, otherwise the text of the selected box. /// Returns the fieldValue of the selected box, otherwise the text of the selected box.
public func formFieldValue() -> AnyHashable? { public override func formFieldValue() -> AnyHashable? {
guard enabled else { return nil } guard enabled else { return nil }
let selectedBox = boxes.first { (box) -> Bool in let selectedBox = boxes.first { (box) -> Bool in
return box.selected return box.selected
@ -42,7 +48,7 @@ import MVMCore
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Server Value // MARK: - Server Value
//-------------------------------------------------- //--------------------------------------------------
open func formFieldServerValue() -> AnyHashable? { open override func formFieldServerValue() -> AnyHashable? {
return formFieldValue() return formFieldValue()
} }
@ -51,17 +57,8 @@ import MVMCore
//-------------------------------------------------- //--------------------------------------------------
private enum CodingKeys: String, CodingKey { private enum CodingKeys: String, CodingKey {
case id
case moleculeName
case enabled
case readOnly
case selectedAccentColor
case backgroundColor
case accessibilityIdentifier
case boxesColor
case boxes case boxes
case fieldKey case inverted
case groupName
} }
//-------------------------------------------------- //--------------------------------------------------
@ -69,7 +66,8 @@ import MVMCore
//-------------------------------------------------- //--------------------------------------------------
public init(with boxes: [RadioBoxModel]){ public init(with boxes: [RadioBoxModel]){
self.boxes = boxes self.boxes = boxes
super.init()
} }
//-------------------------------------------------- //--------------------------------------------------
@ -78,32 +76,17 @@ import MVMCore
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
selectedAccentColor = try typeContainer.decodeIfPresent(Color.self, forKey: .selectedAccentColor)
backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
accessibilityIdentifier = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityIdentifier)
boxesColor = try typeContainer.decodeIfPresent(Color.self, forKey: .boxesColor)
boxes = try typeContainer.decode([RadioBoxModel].self, forKey: .boxes) boxes = try typeContainer.decode([RadioBoxModel].self, forKey: .boxes)
fieldKey = try typeContainer.decodeIfPresent(String.self, forKey: .fieldKey) if let inverted = try typeContainer.decodeIfPresent(Bool.self, forKey: .inverted) {
if let groupName = try typeContainer.decodeIfPresent(String.self, forKey: .groupName) { self.inverted = inverted
self.groupName = groupName
} }
enabled = try typeContainer.decodeIfPresent(Bool.self, forKey: .enabled) ?? true try super.init(from: decoder)
readOnly = try typeContainer.decodeIfPresent(Bool.self, forKey: .readOnly) ?? false
baseValue = formFieldValue()
} }
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(boxes, forKey: .boxes) try container.encode(boxes, forKey: .boxes)
try container.encodeIfPresent(selectedAccentColor, forKey: .selectedAccentColor) try container.encode(inverted, forKey: .inverted)
try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
try container.encodeIfPresent(accessibilityIdentifier, forKey: .accessibilityIdentifier)
try container.encodeIfPresent(fieldKey, forKey: .fieldKey)
try container.encode(groupName, forKey: .groupName)
try container.encode(enabled, forKey: .enabled)
try container.encode(readOnly, forKey: .readOnly)
} }
} }