first cut for the conversion
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
aba99b887b
commit
938a5d1192
@ -6,152 +6,61 @@
|
||||
// Created by Christiano, Kevin on 3/18/19.
|
||||
// Copyright © 2019 Verizon Wireless. All rights reserved.
|
||||
//
|
||||
import VDS
|
||||
|
||||
open class CaretLink: VDS.TextLinkCaret, VDSMoleculeViewProtocol {
|
||||
|
||||
open class CaretLink: Button, MVMCoreUIViewConstrainingProtocol {
|
||||
//------------------------------------------------------
|
||||
// MARK: - Constants
|
||||
//------------------------------------------------------
|
||||
//--------------------------------------------------
|
||||
// MARK: - Public Properties
|
||||
//--------------------------------------------------
|
||||
open var viewModel: CaretLinkModel!
|
||||
open var delegateObject: MVMCoreUIDelegateObject?
|
||||
open var additionalData: [AnyHashable : Any]?
|
||||
|
||||
private let CARET_VIEW_HEIGHT: Float = 10.5
|
||||
private let CARET_VIEW_WIDTH: Float = 6.5
|
||||
|
||||
//------------------------------------------------------
|
||||
// MARK: - Properties
|
||||
//------------------------------------------------------
|
||||
|
||||
@objc public var rightView: UIView?
|
||||
@objc public var rightViewHeight: NSNumber?
|
||||
@objc public var rightViewWidth: NSNumber?
|
||||
|
||||
@objc public var enabledColor: UIColor = .mvmBlack {
|
||||
didSet { changeCaretColor() }
|
||||
}
|
||||
|
||||
@objc public var disabledColor: UIColor = .mvmCoolGray3 {
|
||||
didSet { changeCaretColor() }
|
||||
}
|
||||
|
||||
private var caretSpacingConstraint: NSLayoutConstraint?
|
||||
|
||||
//------------------------------------------------------
|
||||
// MARK: - Inits
|
||||
//------------------------------------------------------
|
||||
|
||||
// Default values for view.
|
||||
public required init(model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
|
||||
super.init(frame: .zero)
|
||||
backgroundColor = .clear
|
||||
translatesAutoresizingMaskIntoConstraints = false
|
||||
setTitleColor(enabledColor, for: .normal)
|
||||
setTitleColor(disabledColor, for: .disabled)
|
||||
}
|
||||
|
||||
public override init(frame: CGRect) {
|
||||
super.init(frame: frame)
|
||||
}
|
||||
|
||||
public required init?(coder: NSCoder) {
|
||||
super.init(coder: coder)
|
||||
}
|
||||
|
||||
//------------------------------------------------------
|
||||
// MARK: - Lifecycle
|
||||
//------------------------------------------------------
|
||||
|
||||
override public var isEnabled: Bool {
|
||||
didSet { changeCaretColor() }
|
||||
}
|
||||
|
||||
public override func updateView(_ size: CGFloat) {
|
||||
titleLabel?.font = MFStyler.fontBoldBodySmall()
|
||||
}
|
||||
|
||||
//------------------------------------------------------
|
||||
// MARK: - Methods
|
||||
//------------------------------------------------------
|
||||
|
||||
private func changeCaretColor() {
|
||||
|
||||
setTitleColor(enabledColor, for: .normal)
|
||||
setTitleColor(disabledColor, for: .disabled)
|
||||
|
||||
if let rightCaretView = rightView as? CaretView {
|
||||
rightCaretView.enabledColor = enabledColor
|
||||
rightCaretView.disabledColor = disabledColor
|
||||
rightCaretView.isEnabled = isEnabled
|
||||
//--------------------------------------------------
|
||||
// MARK: - Public Functions
|
||||
//--------------------------------------------------
|
||||
open func viewModelDidUpdate() {
|
||||
isEnabled = viewModel.enabled
|
||||
iconPosition = viewModel.iconPosition
|
||||
text = viewModel.title
|
||||
surface = viewModel.surface
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
private func createCaretView() -> CaretView {
|
||||
let caret = CaretView()
|
||||
caret.lineWidth = 1.5
|
||||
return caret
|
||||
}
|
||||
|
||||
private func addCaretImageView() {
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - Overrides
|
||||
//--------------------------------------------------
|
||||
open override func updateAccessibility() {
|
||||
super.updateAccessibility()
|
||||
|
||||
rightView?.removeFromSuperview()
|
||||
|
||||
let width = CGFloat(rightViewWidth?.floatValue ?? CARET_VIEW_WIDTH)
|
||||
let height = CGFloat(rightViewHeight?.floatValue ?? CARET_VIEW_HEIGHT)
|
||||
|
||||
contentEdgeInsets.right = 4 + width
|
||||
|
||||
let caretView: UIView = rightView ?? createCaretView()
|
||||
rightView = caretView
|
||||
rightView?.translatesAutoresizingMaskIntoConstraints = false
|
||||
addSubview(caretView)
|
||||
|
||||
caretView.widthAnchor.constraint(equalToConstant: width).isActive = true
|
||||
caretView.heightAnchor.constraint(equalToConstant: height).isActive = true
|
||||
|
||||
let caretLabelSpacing = NSLayoutConstraint(item: caretView, attribute: .left, relatedBy: .equal, toItem: titleLabel, attribute: .right, multiplier: 1.0, constant: 7)
|
||||
caretLabelSpacing.isActive = true
|
||||
caretSpacingConstraint = caretLabelSpacing
|
||||
|
||||
caretView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
|
||||
trailingAnchor.constraint(greaterThanOrEqualTo: caretView.trailingAnchor).isActive = true
|
||||
caretView.topAnchor.constraint(greaterThanOrEqualTo: topAnchor).isActive = true
|
||||
bottomAnchor.constraint(greaterThanOrEqualTo: caretView.bottomAnchor).isActive = true
|
||||
contentHorizontalAlignment = .left
|
||||
|
||||
// Set correct color after layout
|
||||
changeCaretColor()
|
||||
}
|
||||
|
||||
public func updateCaretSpacing(_ spacing: CGFloat) {
|
||||
caretSpacingConstraint?.constant = spacing
|
||||
}
|
||||
|
||||
//------------------------------------------------------
|
||||
// MARK: - Atomization
|
||||
//------------------------------------------------------
|
||||
|
||||
public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
|
||||
addCaretImageView()
|
||||
guard let model = model as? CaretLinkModel else { return }
|
||||
|
||||
if let color = model.backgroundColor {
|
||||
backgroundColor = color.uiColor
|
||||
guard let viewModel = viewModel else { return }
|
||||
if let accessibilityText = viewModel.accessibilityText {
|
||||
self.accessibilityLabel = accessibilityText
|
||||
}
|
||||
|
||||
enabledColor = (model.inverted ? model.enabledColor_inverted : model.enabledColor).uiColor
|
||||
disabledColor = (model.inverted ? model.disabledColor_inverted : model.disabledColor).uiColor
|
||||
|
||||
isEnabled = model.enabled
|
||||
set(with: model.action, delegateObject: delegateObject, additionalData: additionalData)
|
||||
setTitle(model.title, for: .normal)
|
||||
}
|
||||
|
||||
open override func reset() {
|
||||
super.reset()
|
||||
rightView?.removeFromSuperview()
|
||||
if let accessibilityIdentifier = viewModel.accessibilityIdentifier {
|
||||
self.accessibilityIdentifier = accessibilityIdentifier
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - MVMCoreViewProtocol
|
||||
//--------------------------------------------------
|
||||
public func updateView(_ size: CGFloat) {}
|
||||
|
||||
open class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? { 10.5 }
|
||||
}
|
||||
|
||||
// MARK: - MVMCoreUIViewConstrainingProtocol
|
||||
extension CaretLink: MVMCoreUIViewConstrainingProtocol {
|
||||
public func needsToBeConstrained() -> Bool { true }
|
||||
|
||||
open func horizontalAlignment() -> UIStackView.Alignment { .leading }
|
||||
|
||||
open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? { 10.5 }
|
||||
}
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
|
||||
import Foundation
|
||||
import MVMCore
|
||||
|
||||
import VDS
|
||||
|
||||
public class CaretLinkModel: ButtonModelProtocol, MoleculeModelProtocol, EnableableModelProtocol {
|
||||
//--------------------------------------------------
|
||||
@ -20,13 +20,11 @@ public class CaretLinkModel: ButtonModelProtocol, MoleculeModelProtocol, Enablea
|
||||
public var backgroundColor: Color?
|
||||
public var accessibilityIdentifier: String?
|
||||
public var title: String
|
||||
public var accessibilityText: String?
|
||||
public var action: ActionModelProtocol
|
||||
public var enabledColor: Color = Color(uiColor: .mvmBlack)
|
||||
public var disabledColor: Color = Color(uiColor: .mvmCoolGray6)
|
||||
public var enabledColor_inverted: Color = Color(uiColor: .mvmWhite)
|
||||
public var disabledColor_inverted: Color = Color(uiColor: .mvmCoolGray10)
|
||||
public var enabled = true
|
||||
public var inverted = false
|
||||
public var iconPosition: TextLinkCaret.IconPosition = .right
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - Initializer
|
||||
@ -45,12 +43,10 @@ public class CaretLinkModel: ButtonModelProtocol, MoleculeModelProtocol, Enablea
|
||||
case id
|
||||
case backgroundColor
|
||||
case accessibilityIdentifier
|
||||
case accessibilityText
|
||||
case iconPosition
|
||||
case title
|
||||
case action
|
||||
case enabledColor_inverted
|
||||
case disabledColor_inverted
|
||||
case enabledColor
|
||||
case disabledColor
|
||||
case enabled
|
||||
case inverted
|
||||
case moleculeName
|
||||
@ -66,22 +62,11 @@ public class CaretLinkModel: ButtonModelProtocol, MoleculeModelProtocol, Enablea
|
||||
id = try typeContainer.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString
|
||||
backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
|
||||
accessibilityIdentifier = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityIdentifier)
|
||||
accessibilityText = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityText)
|
||||
title = try typeContainer.decode(String.self, forKey: .title)
|
||||
|
||||
if let enabledColor_inverted = try typeContainer.decodeIfPresent(Color.self, forKey: .enabledColor_inverted) {
|
||||
self.enabledColor_inverted = enabledColor_inverted
|
||||
}
|
||||
|
||||
if let disabledColor_inverted = try typeContainer.decodeIfPresent(Color.self, forKey: .disabledColor_inverted) {
|
||||
self.disabledColor_inverted = disabledColor_inverted
|
||||
}
|
||||
|
||||
if let color = try typeContainer.decodeIfPresent(Color.self, forKey: .enabledColor) {
|
||||
enabledColor = color
|
||||
}
|
||||
|
||||
if let color = try typeContainer.decodeIfPresent(Color.self, forKey: .disabledColor) {
|
||||
disabledColor = color
|
||||
|
||||
if let iconPosition = try typeContainer.decodeIfPresent(TextLinkCaret.IconPosition.self, forKey: .iconPosition) {
|
||||
self.iconPosition = iconPosition
|
||||
}
|
||||
|
||||
if let enabled = try typeContainer.decodeIfPresent(Bool.self, forKey: .enabled) {
|
||||
@ -99,15 +84,17 @@ public class CaretLinkModel: ButtonModelProtocol, MoleculeModelProtocol, Enablea
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(id, forKey: .id)
|
||||
try container.encode(moleculeName, forKey: .moleculeName)
|
||||
try container.encode(iconPosition, forKey: .iconPosition)
|
||||
try container.encode(title, forKey: .title)
|
||||
try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
|
||||
try container.encodeIfPresent(accessibilityIdentifier, forKey: .accessibilityIdentifier)
|
||||
try container.encodeIfPresent(accessibilityText, forKey: .accessibilityText)
|
||||
try container.encodeModel(action, forKey: .action)
|
||||
try container.encode(enabledColor, forKey: .enabledColor)
|
||||
try container.encodeIfPresent(disabledColor, forKey: .disabledColor)
|
||||
try container.encode(enabled, forKey: .enabled)
|
||||
try container.encode(enabledColor_inverted, forKey: .enabledColor_inverted)
|
||||
try container.encode(disabledColor_inverted, forKey: .disabledColor_inverted)
|
||||
try container.encode(inverted, forKey: .inverted)
|
||||
}
|
||||
}
|
||||
|
||||
extension CaretLinkModel {
|
||||
public var surface: Surface { inverted ? .dark : .light }
|
||||
}
|
||||
|
||||
@ -13,6 +13,7 @@ extension Surface: Codable {}
|
||||
extension Badge.FillColor: Codable {}
|
||||
extension Icon.Name: Codable {}
|
||||
extension Icon.Size: Codable {}
|
||||
extension TextLinkCaret.IconPosition: Codable {}
|
||||
extension TileContainer.BackgroundColor: Codable {}
|
||||
extension TileContainer.Padding: Codable {}
|
||||
extension TileContainer.AspectRatio: Codable {}
|
||||
@ -20,7 +21,8 @@ extension TextLink.Size: Codable {}
|
||||
extension VDS.Line.Style: Codable {}
|
||||
extension VDS.Line.Orientation: Codable {}
|
||||
extension Use: Codable {}
|
||||
|
||||
extension VDS.Button.Size: RawRepresentableCodable {
|
||||
public static var mapping: [String : VDS.Button.Size] { ["standard": .large, "tiny": .small] }
|
||||
public static var defaultValue: VDS.Button.Size? { nil }
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user