diff --git a/MVMCoreUI/Atomic/Atoms/Buttons/Link/Link.swift b/MVMCoreUI/Atomic/Atoms/Buttons/Link/Link.swift index 4f4a8b34..8c1e5f4a 100644 --- a/MVMCoreUI/Atomic/Atoms/Buttons/Link/Link.swift +++ b/MVMCoreUI/Atomic/Atoms/Buttons/Link/Link.swift @@ -8,83 +8,53 @@ import UIKit import VDSColorTokens +import VDS + +@objcMembers open class Link: VDS.TextLink, VDSMoleculeViewProtocol { -@objcMembers open class Link: Button { //-------------------------------------------------- - // MARK: - Draw + // MARK: - Public Properties //-------------------------------------------------- + open var viewModel: LinkModel! + open var delegateObject: MVMCoreUIDelegateObject? + open var additionalData: [AnyHashable : Any]? - open override func draw(_ rect: CGRect) { + //-------------------------------------------------- + // MARK: - Public Functions + //-------------------------------------------------- + open func viewModelDidUpdate() { + isEnabled = viewModel.enabled + size = viewModel.size + text = viewModel.title - guard let textRect = titleLabel?.frame, - let context = UIGraphicsGetCurrentContext() - else { return } - - // Set line to the same color as the text - if let color = titleLabel?.textColor?.cgColor { - context.setStrokeColor(color) + onClick = { [weak self] control in + guard let self else { return } + MVMCoreUIActionHandler.performActionUnstructured(with: self.viewModel.action, + sourceModel: self.viewModel, + additionalData: self.additionalData, + delegateObject: self.delegateObject) } - - // x should be according to the text, not the button - let x = textRect.origin.x - - // Line is 0 point below the text - let y = textRect.origin.y + textRect.size.height - - context.move(to: CGPoint(x: x, y: y)) - context.addLine(to: CGPoint(x: x + textRect.size.width, y: y)) - context.strokePath() } - - open override var intrinsicContentSize: CGSize { - guard let size = titleLabel?.intrinsicContentSize else { return super.intrinsicContentSize } - return CGSize(width: size.width, height: size.height + 1) - } - + //-------------------------------------------------- - // MARK: - MoleculeViewProtocol + // MARK: - Overrides //-------------------------------------------------- - - public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) { - super.set(with: model, delegateObject, additionalData) + open override func updateAccessibility() { + super.updateAccessibility() - guard let model = model as? LinkModel else { return } - - setTitle(model.title, for: .normal) - if let accessibilityText = model.accessibilityText { + if let viewModel, let accessibilityText = viewModel.accessibilityText { accessibilityLabel = accessibilityText } - setTitleColor((model.inverted ? model.enabledColor_inverted : model.enabledColor).uiColor, for: .normal) - setTitleColor((model.inverted ? model.disabledColor_inverted : model.disabledColor).uiColor, for: .disabled) - setTitleColor((model.inverted ? model.activeColor_inverted : model.activeColor).uiColor, for: .highlighted) - isEnabled = model.enabled - titleLabel?.font = model.getFont(model.size) - set(with: model.action, delegateObject: delegateObject, additionalData: additionalData) } - open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? { 31 } -} + //-------------------------------------------------- + // MARK: - MVMCoreViewProtocol + //-------------------------------------------------- + open class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? { 31 } -// MARK: - MVMCoreViewProtocol -extension Link { + open func updateView(_ size: CGFloat) { } - open override func updateView(_ size: CGFloat) { - super.updateView(size) - } - - open override func setupView() { - super.setupView() - backgroundColor = .clear - contentMode = .redraw - setTitleColor(VDSColor.elementsPrimaryOnlight, for: .normal) - setTitleColor(VDSColor.interactiveDisabledOnlight, for: .disabled) - setTitleColor(VDSColor.interactiveActiveOnlight, for: .highlighted) - titleLabel?.numberOfLines = 1 - titleLabel?.lineBreakMode = .byTruncatingTail - titleLabel?.textAlignment = .left - contentHorizontalAlignment = .left - contentVerticalAlignment = .top - } + open func setupView() {} } // MARK: - MVMCoreUIViewConstrainingProtocol diff --git a/MVMCoreUI/Atomic/Atoms/Buttons/Link/LinkModel.swift b/MVMCoreUI/Atomic/Atoms/Buttons/Link/LinkModel.swift index 62ba0422..7e9a4640 100644 --- a/MVMCoreUI/Atomic/Atoms/Buttons/Link/LinkModel.swift +++ b/MVMCoreUI/Atomic/Atoms/Buttons/Link/LinkModel.swift @@ -7,7 +7,7 @@ // import UIKit -import VDSColorTokens +import VDS open class LinkModel: ButtonModelProtocol, MoleculeModelProtocol, EnableableModelProtocol { //-------------------------------------------------- @@ -23,15 +23,9 @@ open class LinkModel: ButtonModelProtocol, MoleculeModelProtocol, EnableableMode public var accessibilityText: String? public var action: ActionModelProtocol public var enabled = true - public var enabledColor = Color(uiColor: VDSColor.elementsPrimaryOnlight) - public var enabledColor_inverted = Color(uiColor: VDSColor.elementsPrimaryOndark) - public var disabledColor = Color(uiColor: VDSColor.interactiveDisabledOnlight) - public var disabledColor_inverted = Color(uiColor: VDSColor.interactiveDisabledOndark) - public var activeColor = Color(uiColor: VDSColor.interactiveActiveOnlight) - public var activeColor_inverted = Color(uiColor: VDSColor.interactiveActiveOndark) public var inverted = false - public var size:linkFontSize = linkFontSize.small + public var size: TextLink.Size = .small public var shouldMaskRecordedView: Bool? = false @@ -57,34 +51,10 @@ open class LinkModel: ButtonModelProtocol, MoleculeModelProtocol, EnableableMode case title case action case enabled - case enabledColor - case enabledColor_inverted - case disabledColor - case disabledColor_inverted - case activeColor - case activeColor_inverted case inverted case size case shouldMaskRecordedView } - - public enum linkFontSize: String, Codable { - case small - case large - } - - //-------------------------------------------------- - // MARK: - Method - //-------------------------------------------------- - - func getFont(_ type: linkFontSize) -> UIFont { - switch type { - case .small: - return MFStyler.fontRegularBodySmall() - case .large: - return MFStyler.fontRegularBodyLarge() - } - } //-------------------------------------------------- // MARK: - Codec @@ -108,30 +78,7 @@ open class LinkModel: ButtonModelProtocol, MoleculeModelProtocol, EnableableMode self.inverted = inverted } - if let enabledColor = try typeContainer.decodeIfPresent(Color.self, forKey: .enabledColor) { - self.enabledColor = enabledColor - } - - if let enabledColor_inverted = try typeContainer.decodeIfPresent(Color.self, forKey: .enabledColor_inverted) { - self.enabledColor_inverted = enabledColor_inverted - } - - if let disabledColor = try typeContainer.decodeIfPresent(Color.self, forKey: .disabledColor) { - self.disabledColor = disabledColor - } - - if let disabledColor_inverted = try typeContainer.decodeIfPresent(Color.self, forKey: .disabledColor_inverted) { - self.disabledColor_inverted = disabledColor_inverted - } - - if let activeColor = try typeContainer.decodeIfPresent(Color.self, forKey: .activeColor) { - self.activeColor = activeColor - } - - if let activeColor_inverted = try typeContainer.decodeIfPresent(Color.self, forKey: .activeColor_inverted) { - self.activeColor_inverted = activeColor_inverted - } - if let size = try typeContainer.decodeIfPresent(linkFontSize.self, forKey: .size) { + if let size = try typeContainer.decodeIfPresent(TextLink.Size.self, forKey: .size) { self.size = size } @@ -148,13 +95,11 @@ open class LinkModel: ButtonModelProtocol, MoleculeModelProtocol, EnableableMode try container.encodeModel(action, forKey: .action) try container.encode(inverted, forKey: .inverted) try container.encode(enabled, forKey: .enabled) - try container.encode(enabledColor, forKey: .enabledColor) - try container.encode(enabledColor_inverted, forKey: .enabledColor_inverted) - try container.encode(disabledColor, forKey: .disabledColor) - try container.encode(disabledColor_inverted, forKey: .disabledColor_inverted) - try container.encode(activeColor, forKey: .activeColor) - try container.encode(activeColor_inverted, forKey: .activeColor_inverted) try container.encodeIfPresent(size, forKey: .size) try container.encode(shouldMaskRecordedView, forKey: .shouldMaskRecordedView) } } + +extension LinkModel { + public var surface: Surface { inverted ? .dark : .light } +}