From 7dc21628af0c5d21969eae3b8bea9cfa07179acd Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Wed, 3 Aug 2022 10:31:18 -0500 Subject: [PATCH] cleaned up more code Signed-off-by: Matt Bruce --- VDS/Components/Checkbox/VDSCheckbox.swift | 30 +++--- VDS/Components/Toggle/VDSToggle.swift | 126 ++++++++++------------ 2 files changed, 67 insertions(+), 89 deletions(-) diff --git a/VDS/Components/Checkbox/VDSCheckbox.swift b/VDS/Components/Checkbox/VDSCheckbox.swift index 7ccb9ef5..f0804f2c 100644 --- a/VDS/Components/Checkbox/VDSCheckbox.swift +++ b/VDS/Components/Checkbox/VDSCheckbox.swift @@ -191,7 +191,6 @@ import Combine checkboxHeightConstraint?.constant = checkboxSize.height checkboxWidthConstraint?.constant = checkboxSize.width - updateCheckbox(viewModel: model) } public override func setupView() { @@ -215,7 +214,7 @@ import Combine checkboxWidthConstraint = checkboxView.widthAnchor.constraint(equalToConstant: checkboxSize.width) checkboxWidthConstraint?.isActive = true - updateCheckbox(viewModel: model) + updateCheckbox(model) mainStackView.topAnchor.constraint(equalTo: topAnchor).isActive = true mainStackView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true @@ -224,7 +223,7 @@ import Combine } - func ensureLabel(viewModel: ModelType) { + func updateLabels(_ viewModel: ModelType) { //deal with labels if model.shouldShowLabels { @@ -268,7 +267,7 @@ import Combine public override func reset() { super.reset() - updateCheckbox(viewModel: model) + updateCheckbox(model) setAccessibilityLabel() onChange = nil } @@ -279,7 +278,7 @@ import Combine /// Manages the appearance of the checkbox. private var shapeLayer: CAShapeLayer? - private func getCheckboxBackgroundColor(viewModel: ModelType) -> UIColor { + private func getCheckboxBackgroundColor(_ viewModel: ModelType) -> UIColor { var colors: (on: UIColor, off: UIColor) if viewModel.disabled { if viewModel.surface == .light { @@ -297,7 +296,7 @@ import Combine return viewModel.on ? colors.on : colors.off } - private func getCheckboxBorderColor(viewModel: ModelType) -> UIColor { + private func getCheckboxBorderColor(_ viewModel: ModelType) -> UIColor { var colors: (on: UIColor, off: UIColor) if viewModel.disabled { if viewModel.surface == .light { @@ -315,7 +314,7 @@ import Combine return viewModel.on ? colors.on : colors.off } - private func getCheckboxCheckColor(viewModel: ModelType) -> UIColor { + private func getCheckboxCheckColor(_ viewModel: ModelType) -> UIColor { var color: UIColor if disabled { if surface == .light { @@ -333,11 +332,11 @@ import Combine return viewModel.on ? color : .clear } - private func updateCheckbox(viewModel: ModelType) { + private func updateCheckbox(_ viewModel: ModelType) { //get the colors - let backgroundColor = getCheckboxBackgroundColor(viewModel: viewModel) - let borderColor = getCheckboxBorderColor(viewModel: viewModel) - let checkColor = getCheckboxCheckColor(viewModel: viewModel) + let backgroundColor = getCheckboxBackgroundColor(viewModel) + let borderColor = getCheckboxBorderColor(viewModel) + let checkColor = getCheckboxCheckColor(viewModel) if let shapeLayer = shapeLayer, let sublayers = layer.sublayers, sublayers.contains(shapeLayer) { shapeLayer.removeFromSuperlayer() @@ -388,7 +387,6 @@ import Combine //-------------------------------------------------- // MARK: - Actions //-------------------------------------------------- - open override func sendAction(_ action: Selector, to target: Any?, for event: UIEvent?) { super.sendAction(action, to: target, for: event) toggleAndAction() @@ -414,8 +412,7 @@ import Combine //-------------------------------------------------- // MARK: - UIResponder - //-------------------------------------------------- - + //-------------------------------------------------- open override func touchesEnded(_ touches: Set, with event: UIEvent?) { sendActions(for: .touchUpInside) @@ -424,14 +421,13 @@ import Combine //-------------------------------------------------- // MARK: - State //-------------------------------------------------- - /// Follow the SwiftUI View paradigm /// - Parameter viewModel: state open override func onStateChange(viewModel: ModelType) { let enabled = !viewModel.disabled - ensureLabel(viewModel: viewModel) - updateCheckbox(viewModel: viewModel) + updateLabels(viewModel) + updateCheckbox(viewModel) setAccessibilityHint(enabled) setAccessibilityValue(viewModel.on) setAccessibilityLabel(viewModel.on) diff --git a/VDS/Components/Toggle/VDSToggle.swift b/VDS/Components/Toggle/VDSToggle.swift index 7d31c7e2..ca3412fd 100644 --- a/VDS/Components/Toggle/VDSToggle.swift +++ b/VDS/Components/Toggle/VDSToggle.swift @@ -25,11 +25,6 @@ import Combine //-------------------------------------------------- // MARK: - Private Properties //-------------------------------------------------- - - private var showTextSpacing: CGFloat { - showText ? 12 : 0 - } - private var stackView: UIStackView = { let stackView = UIStackView() stackView.translatesAutoresizingMaskIntoConstraints = false @@ -60,7 +55,6 @@ import Combine //-------------------------------------------------- // MARK: - Static Properties //-------------------------------------------------- - // Sizes are from InVision design specs. public let toggleSize = CGSize(width: 52, height: 24) public let knobSize = CGSize(width: 20, height: 20) @@ -68,7 +62,6 @@ import Combine //-------------------------------------------------- // MARK: - Public Properties //-------------------------------------------------- - public var onChange: Blocks.ActionBlock? @Proxy(\.model.on) @@ -78,7 +71,7 @@ import Combine public var showText: Bool { didSet { if oldValue != showText { - ensureLabel() + updateLabel(model) } } } @@ -99,7 +92,7 @@ import Combine public var textPosition: VDSTextPosition { didSet { if oldValue != textPosition { - ensureLabel() + updateLabel(model) } } } @@ -146,7 +139,6 @@ import Combine //-------------------------------------------------- // MARK: - Computed Properties //-------------------------------------------------- - open override var isEnabled: Bool { get { !model.disabled } set { @@ -176,7 +168,6 @@ import Combine //-------------------------------------------------- // MARK: - Constraints //-------------------------------------------------- - private var knobLeadingConstraint: NSLayoutConstraint? private var knobTrailingConstraint: NSLayoutConstraint? private var knobHeightConstraint: NSLayoutConstraint? @@ -186,9 +177,9 @@ import Combine //functions //-------------------------------------------------- - // MARK: - Color + // MARK: - Toggle //-------------------------------------------------- - private func getToggleColor(viewModel: ModelType) -> UIColor { + private func getToggleColor(_ viewModel: ModelType) -> UIColor { var colors: (on: UIColor, off: UIColor) if viewModel.disabled { if viewModel.surface == .light { @@ -206,7 +197,7 @@ import Combine return viewModel.on ? colors.on : colors.off } - private func getKnobColor(viewModel: ModelType) -> UIColor { + private func getKnobColor(_ viewModel: ModelType) -> UIColor { var colors: (on: UIColor, off: UIColor) if viewModel.disabled { if viewModel.surface == .light { @@ -224,11 +215,48 @@ import Combine return viewModel.on ? colors.on : colors.off } + private func updateToggle(_ viewModel: ModelType) { + //private func + func constrainKnob(){ + self.knobLeadingConstraint?.isActive = false + self.knobTrailingConstraint?.isActive = false + if viewModel.on { + self.knobTrailingConstraint = self.toggleView.trailingAnchor.constraint(equalTo: self.knobView.trailingAnchor, constant: 2) + self.knobLeadingConstraint = self.knobView.leadingAnchor.constraint(greaterThanOrEqualTo: self.toggleView.leadingAnchor) + } else { + self.knobTrailingConstraint = self.toggleView.trailingAnchor.constraint(greaterThanOrEqualTo: self.knobView.trailingAnchor) + self.knobLeadingConstraint = self.knobView.leadingAnchor.constraint(equalTo: self.toggleView.leadingAnchor, constant: 2) + } + self.knobTrailingConstraint?.isActive = true + self.knobLeadingConstraint?.isActive = true + self.knobWidthConstraint?.constant = self.knobSize.width + self.layoutIfNeeded() + } + + let toggleColor = getToggleColor(viewModel) + let knobColor = getKnobColor(viewModel) + + if viewModel.disabled { + toggleView.backgroundColor = toggleColor + knobView.backgroundColor = knobColor + constrainKnob() + } else { + UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseIn, animations: { + self.toggleView.backgroundColor = toggleColor + self.knobView.backgroundColor = knobColor + }, completion: nil) + + UIView.animate(withDuration: 0.33, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: [], animations: { + constrainKnob() + }, completion: nil) + } + } + //-------------------------------------------------- - // MARK: - Label Helper + // MARK: - Labels //-------------------------------------------------- - func ensureLabel() { - stackView.spacing = showTextSpacing + private func updateLabel(_ viewModel: ModelType) { + stackView.spacing = viewModel.showText ? 12 : 0 if showText { if textPosition == .left { stackView.insertArrangedSubview(label, at: 0) @@ -243,7 +271,6 @@ import Combine //-------------------------------------------------- // MARK: - Lifecycle //-------------------------------------------------- - public override func updateView(_ size: CGFloat) { super.updateView(size) @@ -255,9 +282,6 @@ import Combine toggleView.layer.cornerRadius = toggleSize.height / 2.0 knobView.layer.cornerRadius = toggleSize.height / 2.0 - - ensureLabel() - } public override func setupView() { @@ -276,7 +300,7 @@ import Combine toggleView.layer.cornerRadius = toggleSize.height / 2.0 knobView.layer.cornerRadius = knobSize.height / 2.0 - toggleView.backgroundColor = getToggleColor(viewModel: model) + toggleView.backgroundColor = getToggleColor(model) toggleView.addSubview(knobView) @@ -288,7 +312,7 @@ import Combine knobView.topAnchor.constraint(greaterThanOrEqualTo: toggleView.topAnchor).isActive = true toggleView.bottomAnchor.constraint(greaterThanOrEqualTo: knobView.bottomAnchor).isActive = true - ensureLabel() + updateLabel(model) stackView.addArrangedSubview(toggleView) stackView.topAnchor.constraint(equalTo: topAnchor).isActive = true stackView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true @@ -299,8 +323,8 @@ import Combine public override func reset() { super.reset() - toggleView.backgroundColor = getToggleColor(viewModel: model) - knobView.backgroundColor = getKnobColor(viewModel: model) + toggleView.backgroundColor = getToggleColor(model) + knobView.backgroundColor = getKnobColor(model) setAccessibilityLabel() onChange = nil } @@ -308,7 +332,6 @@ import Combine //-------------------------------------------------- // MARK: - Actions //-------------------------------------------------- - open override func sendAction(_ action: Selector, to target: Any?, for event: UIEvent?) { super.sendAction(action, to: target, for: event) toggleAndAction() @@ -335,7 +358,6 @@ import Combine //-------------------------------------------------- // MARK: - UIResponder //-------------------------------------------------- - open override func touchesBegan(_ touches: Set, with event: UIEvent?) { UIView.animate(withDuration: 0.1, animations: { @@ -362,7 +384,6 @@ import Combine //-------------------------------------------------- // MARK: - Animations //-------------------------------------------------- - public func knobReformAnimation() { UIView.animate(withDuration: 0.1, animations: { self.knobWidthConstraint?.constant = self.knobSize.width @@ -373,56 +394,17 @@ import Combine //-------------------------------------------------- // MARK: - State //-------------------------------------------------- - /// Follow the SwiftUI View paradigm /// - Parameter viewModel: state open override func onStateChange(viewModel: ModelType) { - let enabled = !viewModel.disabled - label.set(with: viewModel.label) - - //private func - func constrainKnob(){ - self.knobLeadingConstraint?.isActive = false - self.knobTrailingConstraint?.isActive = false - if viewModel.on { - self.knobTrailingConstraint = self.toggleView.trailingAnchor.constraint(equalTo: self.knobView.trailingAnchor, constant: 2) - self.knobLeadingConstraint = self.knobView.leadingAnchor.constraint(greaterThanOrEqualTo: self.toggleView.leadingAnchor) - } else { - self.knobTrailingConstraint = self.toggleView.trailingAnchor.constraint(greaterThanOrEqualTo: self.knobView.trailingAnchor) - self.knobLeadingConstraint = self.knobView.leadingAnchor.constraint(equalTo: self.toggleView.leadingAnchor, constant: 2) - } - self.knobTrailingConstraint?.isActive = true - self.knobLeadingConstraint?.isActive = true - self.knobWidthConstraint?.constant = self.knobSize.width - self.layoutIfNeeded() - } - - let toggleColor = getToggleColor(viewModel: viewModel) - let knobColor = getKnobColor(viewModel: viewModel) - - if viewModel.disabled { - toggleView.backgroundColor = toggleColor - knobView.backgroundColor = knobColor - constrainKnob() - } else { - UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseIn, animations: { - self.toggleView.backgroundColor = toggleColor - self.knobView.backgroundColor = knobColor - }, completion: nil) - - UIView.animate(withDuration: 0.33, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: [], animations: { - constrainKnob() - }, completion: nil) - } - - backgroundColor = viewModel.surface.color - isUserInteractionEnabled = !viewModel.disabled - - setAccessibilityHint(enabled) + updateLabel(viewModel) + updateToggle(viewModel) + setAccessibilityHint(!viewModel.disabled) setAccessibilityValue(viewModel.on) setAccessibilityLabel(viewModel.on) - + backgroundColor = viewModel.surface.color + isUserInteractionEnabled = !viewModel.disabled setNeedsLayout() layoutIfNeeded() }