From 847daee32ff4f737a6cc160fbe64f9b201b452b2 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Mon, 1 Jul 2024 14:55:25 -0500 Subject: [PATCH] - refactored more code - CheckboxLabel to VDS.CheckboxItem Signed-off-by: Matt Bruce --- .../Atomic/Atoms/Selectors/Checkbox.swift | 9 +- .../Atomic/Atoms/Views/CheckboxLabel.swift | 193 ++++++++---------- .../Atoms/Views/CheckboxLabelModel.swift | 8 +- 3 files changed, 95 insertions(+), 115 deletions(-) diff --git a/MVMCoreUI/Atomic/Atoms/Selectors/Checkbox.swift b/MVMCoreUI/Atomic/Atoms/Selectors/Checkbox.swift index d6e65968..1b8c9a84 100644 --- a/MVMCoreUI/Atomic/Atoms/Selectors/Checkbox.swift +++ b/MVMCoreUI/Atomic/Atoms/Selectors/Checkbox.swift @@ -159,12 +159,15 @@ import VDS public func viewModelDidUpdate() { + //forms FormValidator.setupValidation(for: viewModel, delegate: delegateObject?.formHolderDelegate) - + groupName = viewModel.groupName if let fieldKey = viewModel.fieldKey { self.fieldKey = fieldKey } + //properties + isEnabled = viewModel.enabled && !viewModel.readOnly isAnimated = viewModel.animated if viewModel.selected { updateSelectionOnly = true @@ -172,6 +175,7 @@ import VDS updateSelectionOnly = false } + //events viewModel.updateUI = { MVMCoreDispatchUtility.performBlock(onMainThread: { [weak self] in guard let self = self else { return } @@ -179,8 +183,7 @@ import VDS }) } - isEnabled = viewModel.enabled && !viewModel.readOnly - + //onChange if (viewModel.action != nil || viewModel.offAction != nil) { actionBlock = { [weak self] in guard let self = self else { return } diff --git a/MVMCoreUI/Atomic/Atoms/Views/CheckboxLabel.swift b/MVMCoreUI/Atomic/Atoms/Views/CheckboxLabel.swift index a10abbac..3a1722e0 100644 --- a/MVMCoreUI/Atomic/Atoms/Views/CheckboxLabel.swift +++ b/MVMCoreUI/Atomic/Atoms/Views/CheckboxLabel.swift @@ -5,130 +5,103 @@ // Created by Kevin Christiano on 9/13/19. // Copyright © 2019 Verizon Wireless. All rights reserved. // +import VDS - -@objcMembers open class CheckboxLabel: View { - //-------------------------------------------------- - // MARK: - Outlets - //-------------------------------------------------- - - public let checkbox = Checkbox() - public let label = Label(fontStyle: .RegularBodySmall) - private var observation: NSKeyValueObservation? = nil - - //-------------------------------------------------- +@objcMembers open class CheckboxLabel: VDS.CheckboxItem, VDSMoleculeViewProtocol { + //------------------------------------------------------ // MARK: - Properties - //-------------------------------------------------- - - public var checkboxPosition: CheckboxPosition = .center - - //-------------------------------------------------- - // MARK: - Constraints - //-------------------------------------------------- + //------------------------------------------------------ + open var viewModel: CheckboxLabelModel! + open var delegateObject: MVMCoreUIDelegateObject? + open var additionalData: [AnyHashable : Any]? + + // Form Validation + var fieldKey: String? + var fieldValue: JSONValue? + var groupName: String? + + private var updateSelectionOnly: Bool = false + override open var isSelected: Bool { + didSet { + if !updateSelectionOnly { + viewModel.checkbox.selected = isSelected + _ = FormValidator.validate(delegate: delegateObject?.formHolderDelegate) + } + } + } - public var checkboxTopConstraint: NSLayoutConstraint? - public var checkboxBottomConstraint: NSLayoutConstraint? - public var checkboxCenterYConstraint: NSLayoutConstraint? - //-------------------------------------------------- // MARK: - Life Cycle //-------------------------------------------------- - - override open func setupView() { - super.setupView() - - guard subviews.isEmpty else { return } - - addSubview(checkbox) - addSubview(label) - - label.text = "" - - checkbox.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor).isActive = true - - checkboxBottomConstraint = layoutMarginsGuide.bottomAnchor.constraint(equalTo: checkbox.bottomAnchor) - checkboxBottomConstraint?.isActive = true - - checkboxTopConstraint = checkbox.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor) - checkboxTopConstraint?.isActive = true - - checkboxCenterYConstraint = checkbox.centerYAnchor.constraint(equalTo: centerYAnchor) - - label.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor).isActive = true - layoutMarginsGuide.trailingAnchor.constraint(equalTo: label.trailingAnchor).isActive = true - label.leadingAnchor.constraint(equalTo: checkbox.trailingAnchor, constant: PaddingTwo).isActive = true - - layoutMarginsGuide.bottomAnchor.constraint(greaterThanOrEqualTo: label.bottomAnchor).isActive = true - let bottomLabelConstraint = layoutMarginsGuide.bottomAnchor.constraint(equalTo: label.bottomAnchor) - bottomLabelConstraint.priority = .defaultLow - bottomLabelConstraint.isActive = true - - alignCheckbox(.center) - isAccessibilityElement = false - accessibilityElements = [checkbox, label] - } - - @objc override open func updateView(_ size: CGFloat) { - super.updateView(size) - - label.updateView(size) - checkbox.updateView(size) - layoutIfNeeded() - } - - //-------------------------------------------------- - // MARK: - Methods - //-------------------------------------------------- - - /// Aligns Checkbox and Label relative to the desired position of the Checkbox. - private func alignCheckbox(_ position: CheckboxPosition) { - checkboxPosition = position - - switch position { - case .center: - checkboxBottomConstraint?.isActive = false - checkboxTopConstraint?.isActive = false - checkboxCenterYConstraint?.isActive = true - - case .top: - checkboxBottomConstraint?.isActive = false - checkboxTopConstraint?.isActive = true - checkboxCenterYConstraint?.isActive = false - - case .bottom: - checkboxBottomConstraint?.isActive = true - checkboxTopConstraint?.isActive = false - checkboxCenterYConstraint?.isActive = false - } - } + @objc open func updateView(_ size: CGFloat) {} //-------------------------------------------------- // MARK: - Atomic //-------------------------------------------------- - open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) { - guard let checkBoxWithLabelModel = model as? CheckboxLabelModel else { return } - - if let checkboxAlignment = checkBoxWithLabelModel.checkboxAlignment { - alignCheckbox(checkboxAlignment) + public func viewModelDidUpdate() { + updateCheckbox() + + //primary label + labelText = viewModel.label.text + if let attributes = viewModel.label.attributes?.toVDSLabelAttributeModel(delegateObject: delegateObject, additionalData: additionalData) { + labelTextAttributes = attributes } - checkbox.set(with: checkBoxWithLabelModel.checkbox, delegateObject, additionalData) - label.set(with: checkBoxWithLabelModel.label, delegateObject, additionalData) + //secondary label + if let subtitleModel = viewModel.subtitle { + childText = subtitleModel.text + if let attributes = subtitleModel.attributes?.toVDSLabelAttributeModel(delegateObject: delegateObject, additionalData: additionalData) { + childTextAttributes = attributes + } + } } - open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? { + private func performCheckboxAction(with actionModel: ActionModelProtocol, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) { + MVMCoreUIActionHandler.performActionUnstructured(with: actionModel, sourceModel: viewModel.checkbox, additionalData: additionalData, delegateObject: delegateObject) + } + + public func updateCheckbox() { + //forms + FormValidator.setupValidation(for: viewModel.checkbox, delegate: delegateObject?.formHolderDelegate) + groupName = viewModel.checkbox.groupName + if let fieldKey = viewModel.checkbox.fieldKey { + self.fieldKey = fieldKey + } + + //properties + isAnimated = viewModel.checkbox.animated + isEnabled = viewModel.checkbox.enabled && !viewModel.checkbox.readOnly + if viewModel.checkbox.selected { + updateSelectionOnly = false + isSelected = viewModel.checkbox.selected + updateSelectionOnly = true + } + + //events + viewModel.checkbox.updateUI = { + MVMCoreDispatchUtility.performBlock(onMainThread: { [weak self] in + guard let self = self else { return } + isEnabled = viewModel.checkbox.enabled + }) + } + + //onChange + if (viewModel.checkbox.action != nil || viewModel.checkbox.offAction != nil) { + onChange = { [weak self] control in + guard let self = self else { return } + + if let offAction = viewModel.checkbox.offAction, !isSelected { + performCheckboxAction(with: offAction, delegateObject: delegateObject, additionalData: additionalData) + + } else if let action = viewModel.checkbox.action { + performCheckboxAction(with: action, delegateObject: delegateObject, additionalData: additionalData) + } + } + } + + } + + open class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? { return 200 } - - open override func reset() { - super.reset() - - label.text = "" - checkbox.reset() - alignCheckbox(.center) - } - - override open func accessibilityActivate() -> Bool { - checkbox.accessibilityActivate() - } } diff --git a/MVMCoreUI/Atomic/Atoms/Views/CheckboxLabelModel.swift b/MVMCoreUI/Atomic/Atoms/Views/CheckboxLabelModel.swift index 9618cfab..31cdf46d 100644 --- a/MVMCoreUI/Atomic/Atoms/Views/CheckboxLabelModel.swift +++ b/MVMCoreUI/Atomic/Atoms/Views/CheckboxLabelModel.swift @@ -21,11 +21,15 @@ public enum CheckboxPosition: String, Codable { @DecodableDefault.UUIDString public var id: String public var backgroundColor: Color? - public var checkboxAlignment: CheckboxPosition? public var checkbox: CheckboxModel public var label: LabelModel + public var subtitle: LabelModel? + + public var children: [MoleculeModelProtocol] { + guard let subtitle else { return [checkbox, label] } + return [checkbox, label, subtitle] + } - public var children: [MoleculeModelProtocol] { [checkbox, label] } //-------------------------------------------------- // MARK: - Initializer //--------------------------------------------------