From e674fe4d94290c98e0accc15076640d53780bd92 Mon Sep 17 00:00:00 2001 From: Kevin G Christiano Date: Wed, 8 Jan 2020 14:19:37 -0500 Subject: [PATCH] Adding protocols, comments, and mild reworks. --- MVMCoreUI/Atoms/Buttons/Button.swift | 115 +++++++++++++++++++--- MVMCoreUI/Atoms/Buttons/CaretButton.swift | 16 +-- 2 files changed, 108 insertions(+), 23 deletions(-) diff --git a/MVMCoreUI/Atoms/Buttons/Button.swift b/MVMCoreUI/Atoms/Buttons/Button.swift index 2845e66a..21a9b227 100644 --- a/MVMCoreUI/Atoms/Buttons/Button.swift +++ b/MVMCoreUI/Atoms/Buttons/Button.swift @@ -6,38 +6,129 @@ // Copyright © 2019 Verizon Wireless. All rights reserved. // -public typealias ButtonBlock = (Button) -> Void +public typealias ButtonBlock = (Button) -> () + @objcMembers open class Button: UIButton, MFButtonProtocol { + //-------------------------------------------------- + // MARK: - Properties + //-------------------------------------------------- + public var json: [AnyHashable: Any]? public var actionMap: [AnyHashable: Any]? - public weak var buttonDelegate: ButtonDelegateProtocol? + + private var initialSetupPerformed = false + private var buttonBlock: ButtonBlock? - public func addBlock(_ buttonBlock: @escaping ButtonBlock, event: Event) { + //-------------------------------------------------- + // MARK: - Delegate + //-------------------------------------------------- + + public weak var buttonDelegate: ButtonDelegateProtocol? + + //-------------------------------------------------- + // MARK: - Initializers + //-------------------------------------------------- + + public override init(frame: CGRect) { + super.init(frame: .zero) + initialSetup() + } + + public convenience init() { + self.init(frame: .zero) + initialSetup() + } + + public required init?(coder: NSCoder) { + super.init(coder: coder) + fatalError("Button does not support xib.") + } + + //-------------------------------------------------- + // MARK: - Setup + //-------------------------------------------------- + + public func initialSetup() { + + if !initialSetupPerformed { + initialSetupPerformed = true + setupView() + } + } + + //-------------------------------------------------- + // MARK: - Methods + //-------------------------------------------------- + + public func addBlock( event: Event, _ buttonBlock: @escaping ButtonBlock) { self.buttonBlock = buttonBlock addTarget(self, action: #selector(callBlock(_:)), for: event) } func callBlock(_ sender: Button) { - guard let buttonBlock = buttonBlock else { return } - buttonBlock(self) + buttonBlock?(self) } public func setWithActionMap(_ actionMap: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) { self.actionMap = actionMap + titleLabel?.numberOfLines = 0 titleLabel?.lineBreakMode = .byWordWrapping buttonDelegate = delegateObject?.buttonDelegate - addBlock({ [weak self] (sender) in - guard let self = self, - let performAction = self.buttonDelegate?.button?(self, shouldPerformActionWithMap: actionMap, additionalData: additionalData), - performAction else { return } + addBlock(event: .touchUpInside) { [weak self] sender in + guard let self = self else { return } - MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegateObject: delegateObject) - - }, event: .touchUpInside) + if self.buttonDelegate?.button?(self, shouldPerformActionWithMap: actionMap, additionalData: additionalData) ?? true { + MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegateObject: delegateObject) + } + } + } +} + +// MARK: - MVMCoreUIMoleculeViewProtocol +extension Button: MVMCoreUIMoleculeViewProtocol { + + public func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) { + self.json = json + + guard let dictionary = json else { return } + + if let backgroundColorString = dictionary[KeyBackgroundColor] as? String { + backgroundColor = UIColor.mfGet(forHex: backgroundColorString) + } + + if let title = dictionary[KeyTitle] as? String { + setTitle(title, for: .normal) + } + } + + public func reset() { + backgroundColor = .clear + } +} + + +// MARK: - MVMCoreViewProtocol +extension Button: MVMCoreViewProtocol { + + public func updateView(_ size: CGFloat) {} + + /// Will be called only once. + public func setupView() { + + translatesAutoresizingMaskIntoConstraints = false + insetsLayoutMarginsFromSafeArea = false + } +} + +// MARK: AppleGuidelinesProtocol +extension Button: AppleGuidelinesProtocol { + + override open func point(inside point: CGPoint, with event: UIEvent?) -> Bool { + return Self.acceptablyOutsideBounds(point: point, bounds: bounds) } } diff --git a/MVMCoreUI/Atoms/Buttons/CaretButton.swift b/MVMCoreUI/Atoms/Buttons/CaretButton.swift index 22bc3cd5..5311df31 100644 --- a/MVMCoreUI/Atoms/Buttons/CaretButton.swift +++ b/MVMCoreUI/Atoms/Buttons/CaretButton.swift @@ -17,7 +17,6 @@ open class CaretButton: Button, MVMCoreUIMoleculeViewProtocol, MVMCoreUIViewCons private let CARET_VIEW_HEIGHT: Float = 10.5 private let CARET_VIEW_WIDTH: Float = 6.5 - //------------------------------------------------------ // MARK: - Properties //------------------------------------------------------ @@ -47,13 +46,10 @@ open class CaretButton: Button, MVMCoreUIMoleculeViewProtocol, MVMCoreUIViewCons } override public var isEnabled: Bool { - didSet { - changeCaretColor() - } + didSet { changeCaretColor() } } - public func updateView(_ size: CGFloat) { - } + public func updateView(_ size: CGFloat) { } //------------------------------------------------------ // MARK: - Functions @@ -86,7 +82,7 @@ open class CaretButton: Button, MVMCoreUIMoleculeViewProtocol, MVMCoreUIViewCons NSLayoutConstraint(item: caretView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: width).isActive = true NSLayoutConstraint(item: caretView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: height).isActive = true - let caretLabelSpacing = NSLayoutConstraint(item: caretView, attribute: .left, relatedBy: .equal, toItem: titleLabel, attribute: .right, multiplier: 1.0, constant: 4.0) + let caretLabelSpacing = NSLayoutConstraint(item: caretView, attribute: .left, relatedBy: .equal, toItem: titleLabel, attribute: .right, multiplier: 1.0, constant: 4) caretLabelSpacing.isActive = true caretSpacingConstraint = caretLabelSpacing @@ -123,9 +119,7 @@ open class CaretButton: Button, MVMCoreUIMoleculeViewProtocol, MVMCoreUIViewCons setTitle(title, for: .normal) } - if let disableButtonAsAny = dictionary[KeyDisableButton], - let isDisabled = disableButtonAsAny as? Bool { - + if let disableButtonAsAny = dictionary[KeyDisableButton], let isDisabled = disableButtonAsAny as? Bool { isEnabled = !isDisabled } @@ -147,7 +141,7 @@ open class CaretButton: Button, MVMCoreUIMoleculeViewProtocol, MVMCoreUIViewCons } open func horizontalAlignment() -> UIStackView.Alignment { - return UIStackView.Alignment.leading; + return .leading } public class func estimatedHeight(forRow json: [AnyHashable : Any]?, delegateObject: MVMCoreUIDelegateObject?) -> CGFloat {