cleaned up more code

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2022-08-03 10:31:18 -05:00
parent 3b2d52dcd3
commit 7dc21628af
2 changed files with 67 additions and 89 deletions

View File

@ -191,7 +191,6 @@ import Combine
checkboxHeightConstraint?.constant = checkboxSize.height checkboxHeightConstraint?.constant = checkboxSize.height
checkboxWidthConstraint?.constant = checkboxSize.width checkboxWidthConstraint?.constant = checkboxSize.width
updateCheckbox(viewModel: model)
} }
public override func setupView() { public override func setupView() {
@ -215,7 +214,7 @@ import Combine
checkboxWidthConstraint = checkboxView.widthAnchor.constraint(equalToConstant: checkboxSize.width) checkboxWidthConstraint = checkboxView.widthAnchor.constraint(equalToConstant: checkboxSize.width)
checkboxWidthConstraint?.isActive = true checkboxWidthConstraint?.isActive = true
updateCheckbox(viewModel: model) updateCheckbox(model)
mainStackView.topAnchor.constraint(equalTo: topAnchor).isActive = true mainStackView.topAnchor.constraint(equalTo: topAnchor).isActive = true
mainStackView.leadingAnchor.constraint(equalTo: leadingAnchor).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 //deal with labels
if model.shouldShowLabels { if model.shouldShowLabels {
@ -268,7 +267,7 @@ import Combine
public override func reset() { public override func reset() {
super.reset() super.reset()
updateCheckbox(viewModel: model) updateCheckbox(model)
setAccessibilityLabel() setAccessibilityLabel()
onChange = nil onChange = nil
} }
@ -279,7 +278,7 @@ import Combine
/// Manages the appearance of the checkbox. /// Manages the appearance of the checkbox.
private var shapeLayer: CAShapeLayer? private var shapeLayer: CAShapeLayer?
private func getCheckboxBackgroundColor(viewModel: ModelType) -> UIColor { private func getCheckboxBackgroundColor(_ viewModel: ModelType) -> UIColor {
var colors: (on: UIColor, off: UIColor) var colors: (on: UIColor, off: UIColor)
if viewModel.disabled { if viewModel.disabled {
if viewModel.surface == .light { if viewModel.surface == .light {
@ -297,7 +296,7 @@ import Combine
return viewModel.on ? colors.on : colors.off 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) var colors: (on: UIColor, off: UIColor)
if viewModel.disabled { if viewModel.disabled {
if viewModel.surface == .light { if viewModel.surface == .light {
@ -315,7 +314,7 @@ import Combine
return viewModel.on ? colors.on : colors.off return viewModel.on ? colors.on : colors.off
} }
private func getCheckboxCheckColor(viewModel: ModelType) -> UIColor { private func getCheckboxCheckColor(_ viewModel: ModelType) -> UIColor {
var color: UIColor var color: UIColor
if disabled { if disabled {
if surface == .light { if surface == .light {
@ -333,11 +332,11 @@ import Combine
return viewModel.on ? color : .clear return viewModel.on ? color : .clear
} }
private func updateCheckbox(viewModel: ModelType) { private func updateCheckbox(_ viewModel: ModelType) {
//get the colors //get the colors
let backgroundColor = getCheckboxBackgroundColor(viewModel: viewModel) let backgroundColor = getCheckboxBackgroundColor(viewModel)
let borderColor = getCheckboxBorderColor(viewModel: viewModel) let borderColor = getCheckboxBorderColor(viewModel)
let checkColor = getCheckboxCheckColor(viewModel: viewModel) let checkColor = getCheckboxCheckColor(viewModel)
if let shapeLayer = shapeLayer, let sublayers = layer.sublayers, sublayers.contains(shapeLayer) { if let shapeLayer = shapeLayer, let sublayers = layer.sublayers, sublayers.contains(shapeLayer) {
shapeLayer.removeFromSuperlayer() shapeLayer.removeFromSuperlayer()
@ -388,7 +387,6 @@ import Combine
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Actions // MARK: - Actions
//-------------------------------------------------- //--------------------------------------------------
open override func sendAction(_ action: Selector, to target: Any?, for event: UIEvent?) { open override func sendAction(_ action: Selector, to target: Any?, for event: UIEvent?) {
super.sendAction(action, to: target, for: event) super.sendAction(action, to: target, for: event)
toggleAndAction() toggleAndAction()
@ -414,8 +412,7 @@ import Combine
//-------------------------------------------------- //--------------------------------------------------
// MARK: - UIResponder // MARK: - UIResponder
//-------------------------------------------------- //--------------------------------------------------
open override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { open override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
sendActions(for: .touchUpInside) sendActions(for: .touchUpInside)
@ -424,14 +421,13 @@ import Combine
//-------------------------------------------------- //--------------------------------------------------
// MARK: - State // MARK: - State
//-------------------------------------------------- //--------------------------------------------------
/// Follow the SwiftUI View paradigm /// Follow the SwiftUI View paradigm
/// - Parameter viewModel: state /// - Parameter viewModel: state
open override func onStateChange(viewModel: ModelType) { open override func onStateChange(viewModel: ModelType) {
let enabled = !viewModel.disabled let enabled = !viewModel.disabled
ensureLabel(viewModel: viewModel) updateLabels(viewModel)
updateCheckbox(viewModel: viewModel) updateCheckbox(viewModel)
setAccessibilityHint(enabled) setAccessibilityHint(enabled)
setAccessibilityValue(viewModel.on) setAccessibilityValue(viewModel.on)
setAccessibilityLabel(viewModel.on) setAccessibilityLabel(viewModel.on)

View File

@ -25,11 +25,6 @@ import Combine
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Private Properties // MARK: - Private Properties
//-------------------------------------------------- //--------------------------------------------------
private var showTextSpacing: CGFloat {
showText ? 12 : 0
}
private var stackView: UIStackView = { private var stackView: UIStackView = {
let stackView = UIStackView() let stackView = UIStackView()
stackView.translatesAutoresizingMaskIntoConstraints = false stackView.translatesAutoresizingMaskIntoConstraints = false
@ -60,7 +55,6 @@ import Combine
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Static Properties // MARK: - Static Properties
//-------------------------------------------------- //--------------------------------------------------
// Sizes are from InVision design specs. // Sizes are from InVision design specs.
public let toggleSize = CGSize(width: 52, height: 24) public let toggleSize = CGSize(width: 52, height: 24)
public let knobSize = CGSize(width: 20, height: 20) public let knobSize = CGSize(width: 20, height: 20)
@ -68,7 +62,6 @@ import Combine
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Public Properties // MARK: - Public Properties
//-------------------------------------------------- //--------------------------------------------------
public var onChange: Blocks.ActionBlock? public var onChange: Blocks.ActionBlock?
@Proxy(\.model.on) @Proxy(\.model.on)
@ -78,7 +71,7 @@ import Combine
public var showText: Bool { public var showText: Bool {
didSet { didSet {
if oldValue != showText { if oldValue != showText {
ensureLabel() updateLabel(model)
} }
} }
} }
@ -99,7 +92,7 @@ import Combine
public var textPosition: VDSTextPosition { public var textPosition: VDSTextPosition {
didSet { didSet {
if oldValue != textPosition { if oldValue != textPosition {
ensureLabel() updateLabel(model)
} }
} }
} }
@ -146,7 +139,6 @@ import Combine
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Computed Properties // MARK: - Computed Properties
//-------------------------------------------------- //--------------------------------------------------
open override var isEnabled: Bool { open override var isEnabled: Bool {
get { !model.disabled } get { !model.disabled }
set { set {
@ -176,7 +168,6 @@ import Combine
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Constraints // MARK: - Constraints
//-------------------------------------------------- //--------------------------------------------------
private var knobLeadingConstraint: NSLayoutConstraint? private var knobLeadingConstraint: NSLayoutConstraint?
private var knobTrailingConstraint: NSLayoutConstraint? private var knobTrailingConstraint: NSLayoutConstraint?
private var knobHeightConstraint: NSLayoutConstraint? private var knobHeightConstraint: NSLayoutConstraint?
@ -186,9 +177,9 @@ import Combine
//functions //functions
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Color // MARK: - Toggle
//-------------------------------------------------- //--------------------------------------------------
private func getToggleColor(viewModel: ModelType) -> UIColor { private func getToggleColor(_ viewModel: ModelType) -> UIColor {
var colors: (on: UIColor, off: UIColor) var colors: (on: UIColor, off: UIColor)
if viewModel.disabled { if viewModel.disabled {
if viewModel.surface == .light { if viewModel.surface == .light {
@ -206,7 +197,7 @@ import Combine
return viewModel.on ? colors.on : colors.off 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) var colors: (on: UIColor, off: UIColor)
if viewModel.disabled { if viewModel.disabled {
if viewModel.surface == .light { if viewModel.surface == .light {
@ -224,11 +215,48 @@ import Combine
return viewModel.on ? colors.on : colors.off 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() { private func updateLabel(_ viewModel: ModelType) {
stackView.spacing = showTextSpacing stackView.spacing = viewModel.showText ? 12 : 0
if showText { if showText {
if textPosition == .left { if textPosition == .left {
stackView.insertArrangedSubview(label, at: 0) stackView.insertArrangedSubview(label, at: 0)
@ -243,7 +271,6 @@ import Combine
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Lifecycle // MARK: - Lifecycle
//-------------------------------------------------- //--------------------------------------------------
public override func updateView(_ size: CGFloat) { public override func updateView(_ size: CGFloat) {
super.updateView(size) super.updateView(size)
@ -255,9 +282,6 @@ import Combine
toggleView.layer.cornerRadius = toggleSize.height / 2.0 toggleView.layer.cornerRadius = toggleSize.height / 2.0
knobView.layer.cornerRadius = toggleSize.height / 2.0 knobView.layer.cornerRadius = toggleSize.height / 2.0
ensureLabel()
} }
public override func setupView() { public override func setupView() {
@ -276,7 +300,7 @@ import Combine
toggleView.layer.cornerRadius = toggleSize.height / 2.0 toggleView.layer.cornerRadius = toggleSize.height / 2.0
knobView.layer.cornerRadius = knobSize.height / 2.0 knobView.layer.cornerRadius = knobSize.height / 2.0
toggleView.backgroundColor = getToggleColor(viewModel: model) toggleView.backgroundColor = getToggleColor(model)
toggleView.addSubview(knobView) toggleView.addSubview(knobView)
@ -288,7 +312,7 @@ import Combine
knobView.topAnchor.constraint(greaterThanOrEqualTo: toggleView.topAnchor).isActive = true knobView.topAnchor.constraint(greaterThanOrEqualTo: toggleView.topAnchor).isActive = true
toggleView.bottomAnchor.constraint(greaterThanOrEqualTo: knobView.bottomAnchor).isActive = true toggleView.bottomAnchor.constraint(greaterThanOrEqualTo: knobView.bottomAnchor).isActive = true
ensureLabel() updateLabel(model)
stackView.addArrangedSubview(toggleView) stackView.addArrangedSubview(toggleView)
stackView.topAnchor.constraint(equalTo: topAnchor).isActive = true stackView.topAnchor.constraint(equalTo: topAnchor).isActive = true
stackView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true stackView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
@ -299,8 +323,8 @@ import Combine
public override func reset() { public override func reset() {
super.reset() super.reset()
toggleView.backgroundColor = getToggleColor(viewModel: model) toggleView.backgroundColor = getToggleColor(model)
knobView.backgroundColor = getKnobColor(viewModel: model) knobView.backgroundColor = getKnobColor(model)
setAccessibilityLabel() setAccessibilityLabel()
onChange = nil onChange = nil
} }
@ -308,7 +332,6 @@ import Combine
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Actions // MARK: - Actions
//-------------------------------------------------- //--------------------------------------------------
open override func sendAction(_ action: Selector, to target: Any?, for event: UIEvent?) { open override func sendAction(_ action: Selector, to target: Any?, for event: UIEvent?) {
super.sendAction(action, to: target, for: event) super.sendAction(action, to: target, for: event)
toggleAndAction() toggleAndAction()
@ -335,7 +358,6 @@ import Combine
//-------------------------------------------------- //--------------------------------------------------
// MARK: - UIResponder // MARK: - UIResponder
//-------------------------------------------------- //--------------------------------------------------
open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
UIView.animate(withDuration: 0.1, animations: { UIView.animate(withDuration: 0.1, animations: {
@ -362,7 +384,6 @@ import Combine
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Animations // MARK: - Animations
//-------------------------------------------------- //--------------------------------------------------
public func knobReformAnimation() { public func knobReformAnimation() {
UIView.animate(withDuration: 0.1, animations: { UIView.animate(withDuration: 0.1, animations: {
self.knobWidthConstraint?.constant = self.knobSize.width self.knobWidthConstraint?.constant = self.knobSize.width
@ -373,56 +394,17 @@ import Combine
//-------------------------------------------------- //--------------------------------------------------
// MARK: - State // MARK: - State
//-------------------------------------------------- //--------------------------------------------------
/// Follow the SwiftUI View paradigm /// Follow the SwiftUI View paradigm
/// - Parameter viewModel: state /// - Parameter viewModel: state
open override func onStateChange(viewModel: ModelType) { open override func onStateChange(viewModel: ModelType) {
let enabled = !viewModel.disabled
label.set(with: viewModel.label) label.set(with: viewModel.label)
updateLabel(viewModel)
//private func updateToggle(viewModel)
func constrainKnob(){ setAccessibilityHint(!viewModel.disabled)
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)
setAccessibilityValue(viewModel.on) setAccessibilityValue(viewModel.on)
setAccessibilityLabel(viewModel.on) setAccessibilityLabel(viewModel.on)
backgroundColor = viewModel.surface.color
isUserInteractionEnabled = !viewModel.disabled
setNeedsLayout() setNeedsLayout()
layoutIfNeeded() layoutIfNeeded()
} }