diff --git a/MVMCoreUI/Atoms/Buttons/Link.swift b/MVMCoreUI/Atoms/Buttons/Link.swift index b4c00c94..5dbbed16 100644 --- a/MVMCoreUI/Atoms/Buttons/Link.swift +++ b/MVMCoreUI/Atoms/Buttons/Link.swift @@ -36,6 +36,10 @@ import UIKit context?.strokePath() } + //-------------------------------------------------- + // MARK: - ModelMoleculeViewProtocol + //-------------------------------------------------- + public override func setWithModel(_ model: MoleculeModelProtocol?, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) { super.setWithModel(model, delegateObject, additionalData) guard let model = model as? LinkModel else { return } diff --git a/MVMCoreUI/Atoms/Buttons/LinkModel.swift b/MVMCoreUI/Atoms/Buttons/LinkModel.swift index f4fab34c..28fc8596 100644 --- a/MVMCoreUI/Atoms/Buttons/LinkModel.swift +++ b/MVMCoreUI/Atoms/Buttons/LinkModel.swift @@ -9,7 +9,12 @@ import UIKit public class LinkModel: ButtonModelProtocol, MoleculeModelProtocol { + //-------------------------------------------------- + // MARK: - Properties + //-------------------------------------------------- + public static var identifier: String = "link" + public var backgroundColor: Color? public var title: String public var action: ActionModelProtocol @@ -17,12 +22,21 @@ public class LinkModel: ButtonModelProtocol, MoleculeModelProtocol { public var textColor = Color(uiColor: .mvmBlack) public var disabledColor = Color(uiColor: .mvmCoolGray6) + //-------------------------------------------------- + // MARK: - Initializer + //-------------------------------------------------- + public init(title: String, action: ActionModelProtocol) { self.title = title self.action = action } + //-------------------------------------------------- + // MARK: - Keys + //-------------------------------------------------- + private enum CodingKeys: String, CodingKey { + case moleculeName case backgroundColor case title case action @@ -31,11 +45,16 @@ public class LinkModel: ButtonModelProtocol, MoleculeModelProtocol { case disabledColor } + //-------------------------------------------------- + // MARK: - Codec + //-------------------------------------------------- + required public init(from decoder: Decoder) throws { let typeContainer = try decoder.container(keyedBy: CodingKeys.self) backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) title = try typeContainer.decode(String.self, forKey: .title) action = try typeContainer.decodeModel(codingKey: .action, typeCodingKey: ActionCodingKey.actionType) + if let enabled = try typeContainer.decodeIfPresent(Bool.self, forKey: .enabled) { self.enabled = enabled } @@ -54,6 +73,7 @@ public class LinkModel: ButtonModelProtocol, MoleculeModelProtocol { try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor) try container.encodeModel(action, forKey: .action) try container.encode(enabled, forKey: .enabled) + try container.encode(moleculeName, forKey: .moleculeName) try container.encode(textColor, forKey: .textColor) try container.encode(disabledColor, forKey: .disabledColor) } diff --git a/MVMCoreUI/BaseClasses/Button.swift b/MVMCoreUI/BaseClasses/Button.swift index 60e0ab1a..c0ff0e81 100644 --- a/MVMCoreUI/BaseClasses/Button.swift +++ b/MVMCoreUI/BaseClasses/Button.swift @@ -12,6 +12,7 @@ public typealias ButtonAction = (Button) -> () //-------------------------------------------------- // MARK: - Properties //-------------------------------------------------- + open var model: MoleculeModelProtocol? open var actionModel: ActionModelProtocol? @@ -67,7 +68,7 @@ public typealias ButtonAction = (Button) -> () addTarget(self, action: #selector(callActionBlock(_:)), for: event) } - @objc private func callActionBlock(_ sender: Button) { + @objc func callActionBlock(_ sender: Button) { buttonAction?(self) } @@ -88,11 +89,13 @@ public typealias ButtonAction = (Button) -> () // MARK:- ModelMoleculeViewProtocol open func setWithModel(_ model: MoleculeModelProtocol?, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) { self.model = model + if let backgroundColor = model?.backgroundColor { self.backgroundColor = backgroundColor.uiColor } guard let model = model as? ButtonModelProtocol else { return } + isEnabled = model.enabled set(with: model.action, delegateObject: delegateObject, additionalData: additionalData) } @@ -113,7 +116,7 @@ public typealias ButtonAction = (Button) -> () // MARK: - MVMCoreViewProtocol extension Button: MVMCoreViewProtocol { - open func updateView(_ size: CGFloat) {} + open func updateView(_ size: CGFloat) { } /// Will be called only once. open func setupView() { @@ -126,6 +129,7 @@ extension Button: MVMCoreViewProtocol { // MARK: - MVMCoreUIMoleculeViewProtocol extension Button: MVMCoreUIMoleculeViewProtocol { + open func reset() { backgroundColor = .clear } @@ -133,6 +137,7 @@ extension Button: MVMCoreUIMoleculeViewProtocol { // 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/Models/ModelProtocols/MoleculeModelProtocol.swift b/MVMCoreUI/Models/ModelProtocols/MoleculeModelProtocol.swift index 1ec0d13c..cec7b90a 100644 --- a/MVMCoreUI/Models/ModelProtocols/MoleculeModelProtocol.swift +++ b/MVMCoreUI/Models/ModelProtocols/MoleculeModelProtocol.swift @@ -3,7 +3,7 @@ import Foundation public protocol MoleculeModelProtocol: Model { var moleculeName: String? { get } - var backgroundColor: Color? { get set} + var backgroundColor: Color? { get set } } public extension MoleculeModelProtocol {