From 98997bfa8046cc86d9958e5d8923570c862a28b5 Mon Sep 17 00:00:00 2001 From: Kevin G Christiano Date: Wed, 11 Dec 2019 15:58:18 -0500 Subject: [PATCH] Comments, logical updates, refactoring. --- MVMCoreUI/Atoms/Views/Toggle.swift | 34 +++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/MVMCoreUI/Atoms/Views/Toggle.swift b/MVMCoreUI/Atoms/Views/Toggle.swift index 0ee42937..b49104c5 100644 --- a/MVMCoreUI/Atoms/Views/Toggle.swift +++ b/MVMCoreUI/Atoms/Views/Toggle.swift @@ -14,6 +14,8 @@ public typealias ActionBlockConfirmation = () -> (Bool) /** A custom implementation of Apple's UISwitch. + By default this class begins in the off state. + Container: The background of the toggle control. Knob: The circular indicator that slides on the container. */ @@ -29,6 +31,8 @@ public typealias ActionBlockConfirmation = () -> (Bool) public var isAnimated = true public var didToggleAction: ActionBlock? + + /// Executes logic before state change. If false, then toggle state will not change and the didToggleAction will not execute. public var shouldToggleAction: ActionBlockConfirmation? = { return { return true } }() @@ -51,8 +55,9 @@ public typealias ActionBlockConfirmation = () -> (Bool) open override var isEnabled: Bool { didSet { isUserInteractionEnabled = isEnabled - backgroundColor = isEnabled ? containerTintColor?.on : disabledTintColor?.container - knobView.backgroundColor = isEnabled ? knobTintColor?.on : disabledTintColor?.knob + changeStateNoAnimation(isEnabled) + backgroundColor = isEnabled ? containerTintColor?.off : disabledTintColor?.container + knobView.backgroundColor = isEnabled ? knobTintColor?.off : disabledTintColor?.knob } } @@ -116,8 +121,8 @@ public typealias ActionBlockConfirmation = () -> (Bool) private func constrainKnob() { - self.knobLeadingConstraint?.isActive = !isOn - self.knobTrailingConstraint?.isActive = isOn + knobLeadingConstraint?.isActive = !isOn + knobTrailingConstraint?.isActive = isOn } //-------------------------------------------------- @@ -133,17 +138,21 @@ public typealias ActionBlockConfirmation = () -> (Bool) self.init(frame: .zero) } - public convenience init(isOn: Bool, didToggleAction: ActionBlock?) { + public convenience init(isOn: Bool) { self.init(frame: .zero) self.isOn = isOn - self.didToggleAction = didToggleAction } - public convenience init(didToggleAction: ActionBlock?) { + /// - parameter isOn: Bool to set the state of the toggle. + /// - parameter didToggleAction: A closure which is executed after the toggle changes states. + public convenience init(isOn: Bool = false, didToggleAction: ActionBlock?) { self.init(frame: .zero) + changeStateNoAnimation(isOn) self.didToggleAction = didToggleAction } + /// - parameter shouldToggleAction: Takes a closure that returns a boolean. + /// - parameter didToggleAction: A closure which is executed after the toggle changes states. public convenience init(shouldToggleAction: ActionBlockConfirmation?, didToggleAction: ActionBlock?) { self.init(frame: .zero) self.didToggleAction = didToggleAction @@ -246,6 +255,13 @@ public typealias ActionBlockConfirmation = () -> (Bool) } } + private func changeStateNoAnimation(_ state: Bool) { + + isAnimated = false + isOn = state + isAnimated = true + } + //-------------------------------------------------- // MARK: - UIResponder //-------------------------------------------------- @@ -357,9 +373,7 @@ extension Toggle { } if let state = dictionary["state"] as? Bool { - isAnimated = false - isOn = state - isAnimated = true + changeStateNoAnimation(state) } if let actionMap = dictionary.optionalDictionaryForKey("actionMap") {