From 6b999703b1f8b5b0cf700a46a9037cc34ddc5c3b Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Mon, 12 Feb 2024 11:24:23 -0600 Subject: [PATCH] added some changes I put as comments, just save the code to check later Signed-off-by: Matt Bruce --- .../Icon/ButtonIcon/ButtonIcon.swift | 45 +++++++++++++++++-- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/VDS/Components/Icon/ButtonIcon/ButtonIcon.swift b/VDS/Components/Icon/ButtonIcon/ButtonIcon.swift index 247be717..c4e18328 100644 --- a/VDS/Components/Icon/ButtonIcon/ButtonIcon.swift +++ b/VDS/Components/Icon/ButtonIcon/ButtonIcon.swift @@ -8,12 +8,13 @@ import Foundation import UIKit import VDSColorTokens +import Combine /// A button icon is an interactive element that visually communicates the action it triggers via an icon. /// It usually represents a supplementary or utilitarian action. A button icon can stand alone, but often /// exists in a group when there are several actions that can be performed. @objc(VDSButtonIcon) -open class ButtonIcon: Control { +open class ButtonIcon: Control, Changeable, FormFieldable { //-------------------------------------------------- // MARK: - Initializers @@ -77,7 +78,7 @@ open class ButtonIcon: Control { private var badgeIndicatorCenterXConstraint: NSLayoutConstraint? private var badgeIndicatorCenterYConstraint: NSLayoutConstraint? private var currentIconName: Icon.Name? { - if let selectedIconName, selectable { + if let selectedIconName, selectable, isSelected { return selectedIconName } else { return iconName @@ -144,14 +145,28 @@ open class ButtonIcon: Control { open var showBadgeIndicator: Bool = false { didSet { setNeedsUpdate() } } /// If true, button will be rendered as selected, when it is selectable. - open var selectable: Bool = false { didSet { setNeedsUpdate() } } - + open var selectable: Bool = false { + didSet { + //unselect + if !selectable && isSelected { + isSelected = false + } + setNeedsUpdate() + } + } + /// Used to move the icon inside the button in both x and y axis. open var iconOffset: CGPoint = .init(x: 0, y: 0) { didSet { setNeedsUpdate() } } /// Applies expand direction to Badge Indicator if shows badge indicator. open var expandDirection: ExpandDirection = .right { didSet { setNeedsUpdate() } } + open var onChangeSubscriber: AnyCancellable? + + open var inputId: String? { didSet { setNeedsUpdate() } } + + open var value: AnyHashable? { didSet { setNeedsUpdate() } } + //-------------------------------------------------- // MARK: - Configuration //-------------------------------------------------- @@ -187,6 +202,7 @@ open class ButtonIcon: Control { $0.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forState: .normal) $0.setSurfaceColors(VDSColor.interactiveActiveOnlight, VDSColor.interactiveActiveOndark, forState: .highlighted) $0.setSurfaceColors(VDSColor.interactiveDisabledOnlight, VDSColor.interactiveDisabledOndark, forState: .disabled) + $0.setSurfaceColors(VDSColor.interactiveDisabledOnlight, VDSColor.interactiveDisabledOndark, forState: [.selected, .disabled]) }.eraseToAnyColorable() }() @@ -195,6 +211,7 @@ open class ButtonIcon: Control { $0.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forState: .normal) $0.setSurfaceColors(VDSColor.interactiveActiveOnlight, VDSColor.interactiveActiveOndark, forState: .highlighted) $0.setSurfaceColors(VDSColor.interactiveDisabledOnlight, VDSColor.paletteBlack.withAlphaComponent(0.70), forState: .disabled) + $0.setSurfaceColors(VDSColor.interactiveDisabledOnlight, VDSColor.paletteBlack.withAlphaComponent(0.70), forState: [.selected, .disabled]) }.eraseToAnyColorable() }() @@ -272,6 +289,7 @@ open class ButtonIcon: Control { $0.setSurfaceColors(VDSColor.backgroundPrimaryDark, VDSColor.backgroundPrimaryLight, forState: .normal) $0.setSurfaceColors(VDSColor.interactiveActiveOnlight, VDSColor.interactiveActiveOndark, forState: .highlighted) $0.setSurfaceColors(VDSColor.interactiveDisabledOnlight, VDSColor.interactiveDisabledOndark, forState: .disabled) + $0.setSurfaceColors(VDSColor.interactiveDisabledOnlight, VDSColor.interactiveDisabledOndark, forState: [.selected, .disabled]) }.eraseToAnyColorable() }() @@ -286,6 +304,7 @@ open class ButtonIcon: Control { $0.setSurfaceColors(VDSColor.paletteGray20, VDSColor.paletteWhite, forState: .normal) $0.setSurfaceColors(VDSColor.interactiveActiveOnlight, VDSColor.interactiveActiveOndark, forState: .highlighted) $0.setSurfaceColors(VDSColor.interactiveDisabledOnlight, VDSColor.interactiveDisabledOndark, forState: .disabled) + $0.setSurfaceColors(VDSColor.interactiveDisabledOnlight, VDSColor.interactiveDisabledOndark, forState: [.selected, .disabled]) }.eraseToAnyColorable() }() var shadowColorConfiguration: AnyColorable = { @@ -301,6 +320,17 @@ open class ButtonIcon: Control { //-------------------------------------------------- // MARK: - Overrides //-------------------------------------------------- + /// Executed on initialization for this View. + open override func initialSetup() { + super.initialSetup() + onClick = { control in + guard control.isEnabled else { return } + if let selectedIconName = control.selectedIconName, control.selectable { + control.toggle() + } + } + } + /// Called once when a view is initialized and is used to Setup additional UI or other constants and configurations. open override func setup() { super.setup() @@ -435,6 +465,13 @@ open class ButtonIcon: Control { } } + /// This will change the state of the Selector and execute the actionBlock if provided. + open func toggle() { + //removed error + isSelected.toggle() + sendActions(for: .valueChanged) + } + //-------------------------------------------------- // MARK: - Private Methods //--------------------------------------------------