Adding protocols, comments, and mild reworks.
This commit is contained in:
parent
468f58b88e
commit
e674fe4d94
@ -6,38 +6,129 @@
|
|||||||
// Copyright © 2019 Verizon Wireless. All rights reserved.
|
// Copyright © 2019 Verizon Wireless. All rights reserved.
|
||||||
//
|
//
|
||||||
|
|
||||||
public typealias ButtonBlock = (Button) -> Void
|
public typealias ButtonBlock = (Button) -> ()
|
||||||
|
|
||||||
|
|
||||||
@objcMembers open class Button: UIButton, MFButtonProtocol {
|
@objcMembers open class Button: UIButton, MFButtonProtocol {
|
||||||
|
//--------------------------------------------------
|
||||||
|
// MARK: - Properties
|
||||||
|
//--------------------------------------------------
|
||||||
|
|
||||||
|
public var json: [AnyHashable: Any]?
|
||||||
public var actionMap: [AnyHashable: Any]?
|
public var actionMap: [AnyHashable: Any]?
|
||||||
public weak var buttonDelegate: ButtonDelegateProtocol?
|
|
||||||
|
private var initialSetupPerformed = false
|
||||||
|
|
||||||
private var buttonBlock: ButtonBlock?
|
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
|
self.buttonBlock = buttonBlock
|
||||||
addTarget(self, action: #selector(callBlock(_:)), for: event)
|
addTarget(self, action: #selector(callBlock(_:)), for: event)
|
||||||
}
|
}
|
||||||
|
|
||||||
func callBlock(_ sender: Button) {
|
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]?) {
|
public func setWithActionMap(_ actionMap: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
|
||||||
self.actionMap = actionMap
|
self.actionMap = actionMap
|
||||||
|
|
||||||
titleLabel?.numberOfLines = 0
|
titleLabel?.numberOfLines = 0
|
||||||
titleLabel?.lineBreakMode = .byWordWrapping
|
titleLabel?.lineBreakMode = .byWordWrapping
|
||||||
|
|
||||||
buttonDelegate = delegateObject?.buttonDelegate
|
buttonDelegate = delegateObject?.buttonDelegate
|
||||||
|
|
||||||
addBlock({ [weak self] (sender) in
|
addBlock(event: .touchUpInside) { [weak self] sender in
|
||||||
guard let self = self,
|
guard let self = self else { return }
|
||||||
let performAction = self.buttonDelegate?.button?(self, shouldPerformActionWithMap: actionMap, additionalData: additionalData),
|
|
||||||
performAction else { return }
|
|
||||||
|
|
||||||
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegateObject: delegateObject)
|
if self.buttonDelegate?.button?(self, shouldPerformActionWithMap: actionMap, additionalData: additionalData) ?? true {
|
||||||
|
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegateObject: delegateObject)
|
||||||
}, event: .touchUpInside)
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,7 +17,6 @@ open class CaretButton: Button, MVMCoreUIMoleculeViewProtocol, MVMCoreUIViewCons
|
|||||||
private let CARET_VIEW_HEIGHT: Float = 10.5
|
private let CARET_VIEW_HEIGHT: Float = 10.5
|
||||||
private let CARET_VIEW_WIDTH: Float = 6.5
|
private let CARET_VIEW_WIDTH: Float = 6.5
|
||||||
|
|
||||||
|
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
// MARK: - Properties
|
// MARK: - Properties
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
@ -47,13 +46,10 @@ open class CaretButton: Button, MVMCoreUIMoleculeViewProtocol, MVMCoreUIViewCons
|
|||||||
}
|
}
|
||||||
|
|
||||||
override public var isEnabled: Bool {
|
override public var isEnabled: Bool {
|
||||||
didSet {
|
didSet { changeCaretColor() }
|
||||||
changeCaretColor()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func updateView(_ size: CGFloat) {
|
public func updateView(_ size: CGFloat) { }
|
||||||
}
|
|
||||||
|
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
// MARK: - Functions
|
// 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: .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
|
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
|
caretLabelSpacing.isActive = true
|
||||||
caretSpacingConstraint = caretLabelSpacing
|
caretSpacingConstraint = caretLabelSpacing
|
||||||
|
|
||||||
@ -123,9 +119,7 @@ open class CaretButton: Button, MVMCoreUIMoleculeViewProtocol, MVMCoreUIViewCons
|
|||||||
setTitle(title, for: .normal)
|
setTitle(title, for: .normal)
|
||||||
}
|
}
|
||||||
|
|
||||||
if let disableButtonAsAny = dictionary[KeyDisableButton],
|
if let disableButtonAsAny = dictionary[KeyDisableButton], let isDisabled = disableButtonAsAny as? Bool {
|
||||||
let isDisabled = disableButtonAsAny as? Bool {
|
|
||||||
|
|
||||||
isEnabled = !isDisabled
|
isEnabled = !isDisabled
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,7 +141,7 @@ open class CaretButton: Button, MVMCoreUIMoleculeViewProtocol, MVMCoreUIViewCons
|
|||||||
}
|
}
|
||||||
|
|
||||||
open func horizontalAlignment() -> UIStackView.Alignment {
|
open func horizontalAlignment() -> UIStackView.Alignment {
|
||||||
return UIStackView.Alignment.leading;
|
return .leading
|
||||||
}
|
}
|
||||||
|
|
||||||
public class func estimatedHeight(forRow json: [AnyHashable : Any]?, delegateObject: MVMCoreUIDelegateObject?) -> CGFloat {
|
public class func estimatedHeight(forRow json: [AnyHashable : Any]?, delegateObject: MVMCoreUIDelegateObject?) -> CGFloat {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user