From a84f502a0ec70422abd8b475f90306f7d2cd1c00 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Wed, 3 Jul 2024 09:37:00 -0500 Subject: [PATCH] updated checkbox/label Signed-off-by: Matt Bruce --- .../Atomic/Atoms/Selectors/Checkbox.swift | 5 +- .../Atoms/Selectors/CheckboxModel.swift | 65 +++++-------------- .../Atomic/Atoms/Views/CheckboxLabel.swift | 3 + .../Atoms/Views/CheckboxLabelModel.swift | 6 +- 4 files changed, 27 insertions(+), 52 deletions(-) diff --git a/MVMCoreUI/Atomic/Atoms/Selectors/Checkbox.swift b/MVMCoreUI/Atomic/Atoms/Selectors/Checkbox.swift index 1b8c9a84..ed89af67 100644 --- a/MVMCoreUI/Atomic/Atoms/Selectors/Checkbox.swift +++ b/MVMCoreUI/Atomic/Atoms/Selectors/Checkbox.swift @@ -142,7 +142,7 @@ import VDS self.isSelected = isSelected } } - + //-------------------------------------------------- // MARK: - Molecular //-------------------------------------------------- @@ -179,7 +179,10 @@ import VDS viewModel.updateUI = { MVMCoreDispatchUtility.performBlock(onMainThread: { [weak self] in guard let self = self else { return } + let isValid = viewModel.isValid ?? true + showError = !isValid isEnabled = viewModel.enabled + }) } diff --git a/MVMCoreUI/Atomic/Atoms/Selectors/CheckboxModel.swift b/MVMCoreUI/Atomic/Atoms/Selectors/CheckboxModel.swift index 98e0cbbd..7c3d3ee0 100644 --- a/MVMCoreUI/Atomic/Atoms/Selectors/CheckboxModel.swift +++ b/MVMCoreUI/Atomic/Atoms/Selectors/CheckboxModel.swift @@ -14,46 +14,29 @@ import VDS var selected: Bool { get set } } -@objcMembers public class CheckboxModel: MoleculeModelProtocol, SelectableMoleculeModelProtocol, FormFieldProtocol, UIUpdatableModelProtocol { +@objcMembers public class CheckboxModel: FormFieldModel, SelectableMoleculeModelProtocol{ //-------------------------------------------------- // MARK: - Properties //-------------------------------------------------- - - public static var identifier: String = "checkbox" - public var id: String = UUID().uuidString - public var backgroundColor: Color? - public var accessibilityIdentifier: String? + public static override var identifier: String { "checkbox" } public var selected: Bool = false - public var enabled: Bool = true - public var readOnly: Bool = false public var animated: Bool = true public var inverted: Bool = false public var action: ActionModelProtocol? public var offAction: ActionModelProtocol? public var surface: Surface { inverted ? .dark : .light } - public var fieldKey: String? - public var groupName: String = FormValidator.defaultGroupName - public var baseValue: AnyHashable? - public var updateUI: ActionBlock? - + //-------------------------------------------------- // MARK: - Keys //-------------------------------------------------- private enum CodingKeys: String, CodingKey { - case id - case moleculeName - case accessibilityIdentifier case checked - case enabled - case readOnly case inverted case animated case action - case fieldKey - case groupName case offAction } @@ -61,16 +44,17 @@ import VDS // MARK: - Form Validation //-------------------------------------------------- - public func formFieldValue() -> AnyHashable? { + open override func formFieldValue() -> AnyHashable? { guard enabled else { return nil } return selected } - //-------------------------------------------------- - // MARK: - Server Value - //-------------------------------------------------- - open func formFieldServerValue() -> AnyHashable? { - return formFieldValue() + open override func setValidity(_ valid: Bool, errorMessage: String?) { + if let ruleErrorMessage = errorMessage, fieldKey != nil { + self.errorMessage = ruleErrorMessage + } + isValid = valid + updateUI?() } //-------------------------------------------------- @@ -78,7 +62,8 @@ import VDS //-------------------------------------------------- public init(isChecked: Bool = false) { - self.selected = isChecked + super.init() + selected = isChecked baseValue = isChecked } @@ -87,12 +72,9 @@ import VDS //-------------------------------------------------- required public init(from decoder: Decoder) throws { + try super.init(from: decoder) + let typeContainer = try decoder.container(keyedBy: CodingKeys.self) - - id = try typeContainer.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString - - accessibilityIdentifier = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityIdentifier) - if let checked = try typeContainer.decodeIfPresent(Bool.self, forKey: .checked) { self.selected = checked } @@ -107,31 +89,18 @@ import VDS self.inverted = inverted } - enabled = try typeContainer.decodeIfPresent(Bool.self, forKey: .enabled) ?? true - readOnly = try typeContainer.decodeIfPresent(Bool.self, forKey: .readOnly) ?? false action = try typeContainer.decodeModelIfPresent(codingKey: .action) - fieldKey = try typeContainer.decodeIfPresent(String.self, forKey: .fieldKey) - - if let groupName = try typeContainer.decodeIfPresent(String.self, forKey: .groupName) { - self.groupName = groupName - } offAction = try typeContainer.decodeModelIfPresent(codingKey: .offAction) + } - 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.encodeIfPresent(groupName, forKey: .groupName) - try container.encodeIfPresent(fieldKey, forKey: .fieldKey) try container.encode(selected, forKey: .checked) try container.encode(inverted, forKey: .inverted) - try container.encodeIfPresent(accessibilityIdentifier, forKey: .accessibilityIdentifier) try container.encodeIfPresent(animated, forKey: .animated) - try container.encode(enabled, forKey: .enabled) - try container.encode(readOnly, forKey: .readOnly) try container.encodeModelIfPresent(action, forKey: .action) - try container.encodeIfPresent(groupName, forKey: .groupName) try container.encodeModelIfPresent(offAction, forKey: .offAction) } } diff --git a/MVMCoreUI/Atomic/Atoms/Views/CheckboxLabel.swift b/MVMCoreUI/Atomic/Atoms/Views/CheckboxLabel.swift index e21f9895..d4c25889 100644 --- a/MVMCoreUI/Atomic/Atoms/Views/CheckboxLabel.swift +++ b/MVMCoreUI/Atomic/Atoms/Views/CheckboxLabel.swift @@ -83,6 +83,9 @@ import VDS viewModel.checkbox.updateUI = { MVMCoreDispatchUtility.performBlock(onMainThread: { [weak self] in guard let self = self else { return } + let isValid = viewModel.checkbox.isValid ?? true + showError = !isValid + errorText = viewModel.checkbox.errorMessage isEnabled = viewModel.checkbox.enabled }) } diff --git a/MVMCoreUI/Atomic/Atoms/Views/CheckboxLabelModel.swift b/MVMCoreUI/Atomic/Atoms/Views/CheckboxLabelModel.swift index 1592bd02..ea78a174 100644 --- a/MVMCoreUI/Atomic/Atoms/Views/CheckboxLabelModel.swift +++ b/MVMCoreUI/Atomic/Atoms/Views/CheckboxLabelModel.swift @@ -25,9 +25,9 @@ public enum CheckboxPosition: String, Codable { public var checkbox: CheckboxModel public var label: LabelModel public var subtitle: LabelModel? - public var inverted: Bool = false - public var surface: Surface { inverted ? .dark : .light } - + public var inverted: Bool? = false + public var surface: Surface { inverted ?? false ? .dark : .light } + public var children: [MoleculeModelProtocol] { guard let subtitle else { return [checkbox, label] } return [checkbox, label, subtitle]