Adding protocols, comments, and mild reworks.

This commit is contained in:
Kevin G Christiano 2020-01-08 14:19:37 -05:00
parent 468f58b88e
commit e674fe4d94
2 changed files with 108 additions and 23 deletions

View File

@ -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)
}
}

View File

@ -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 {