updated LineModel and Line

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2023-09-20 13:26:31 -05:00
parent 470bb160d6
commit 099fce290e
2 changed files with 73 additions and 139 deletions

View File

@ -7,116 +7,107 @@
// //
import UIKit import UIKit
import VDS
@objcMembers open class Line: VDS.View, VDSMoleculeViewProtocol {
@objcMembers open class Line: View {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Properties // MARK: - Properties
//-------------------------------------------------- //--------------------------------------------------
open var line = VDS.Line()
var lineModel: LineModel? { open var viewModel: LineModel!
get { return model as? LineModel } open var delegateObject: MVMCoreUIDelegateObject?
} open var additionalData: [AnyHashable : Any]?
open var orientation: VDS.Line.Orientation = .horizontal {
//-------------------------------------------------- didSet {
// MARK: - Constraints viewModel.orientation = orientation
//-------------------------------------------------- update(viewModel: viewModel)
public var heightConstraint: NSLayoutConstraint?
public var widthConstraint: NSLayoutConstraint?
open func updateLineConstraints(constant: CGFloat) {
if let useVerticalLine = lineModel?.useVerticalLine, useVerticalLine {
heightConstraint?.isActive = false
widthConstraint?.isActive = true
widthConstraint?.constant = constant
} else {
widthConstraint?.isActive = false
heightConstraint?.isActive = true
heightConstraint?.constant = constant
} }
} }
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Initializer // MARK: - Initializer
//-------------------------------------------------- //--------------------------------------------------
public convenience init(pinTo view: UIView, edge: UIRectEdge, useMargin: Bool) { public convenience init(pinTo view: UIView, edge: UIRectEdge, useMargin: Bool) {
self.init(frame: .zero) self.init(frame: .zero)
addLine(to: view, edge: edge, useMargin: useMargin) addLine(to: view, edge: edge, useMargin: useMargin)
} }
public init() { public required init() {
super.init(frame: .zero) super.init()
model = LineModel(type: .secondary) viewModel = LineModel(type: .primary)
setStyle(.secondary)
} }
public override init(frame: CGRect) { public override init(frame: CGRect) {
super.init(frame: frame) super.init(frame: frame)
model = LineModel(type: .secondary) viewModel = LineModel(type: .primary)
setStyle(.secondary)
} }
public required init?(coder: NSCoder) { public required init?(coder: NSCoder) {
super.init(coder: coder) super.init(coder: coder)
model = LineModel(type: .secondary) viewModel = LineModel(type: .primary)
setStyle(.secondary)
} }
public required init(model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable : Any]?) { //--------------------------------------------------
super.init(model: model, delegateObject, additionalData) // MARK: - Overrides
//--------------------------------------------------
open override func setup() {
super.setup()
addSubview(line)
line.pinToSuperView()
} }
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Methods // MARK: - Methods
//-------------------------------------------------- //--------------------------------------------------
open func shouldBeVisible() -> Bool {
open func setStyle(_ style: LineModel.Style) { guard let type = viewModel?.type else { return false }
lineModel?.type = style return type != .none
backgroundColor = lineModel?.backgroundColor?.uiColor
updateLineConstraints(constant: lineModel?.thickness ?? 1)
} }
open func shouldBeVisible() -> Bool { open func setStyle(_ style: LineModel.Style) {
guard let type = lineModel?.type else { return false } viewModel.type = style
return type != .none update(viewModel: viewModel)
}
open func addLine(to view: UIView, edge: UIRectEdge, useMargin: Bool) {
view.addSubview(self)
NSLayoutConstraint.activate(
Array(
NSLayoutConstraint.pinView(toSuperview: self,
useMargins: useMargin,
pinTop: edge != .bottom,
pinBottom: edge != .top,
pinLeft: edge != .right,
pinRight: edge != .left).values
)
)
}
//--------------------------------------------------
// MARK: - VDSMoleculeViewProtocol
//--------------------------------------------------
open func viewModelDidUpdate() {
surface = viewModel.surface
line.style = VDS.Line.Style(rawValue: viewModel.type.rawValue) ?? .primary
line.orientation = viewModel.orientation
} }
//-------------------------------------------------- //--------------------------------------------------
// MARK: - MoleculeViewProtocol // MARK: - MoleculeViewProtocol
//-------------------------------------------------- //--------------------------------------------------
public func updateView(_ size: CGFloat) {
open func addLine(to view: UIView, edge: UIRectEdge, useMargin: Bool) { setNeedsDisplay()
view.addSubview(self)
NSLayoutConstraint.activate(Array(NSLayoutConstraint.pinView(toSuperview: self, useMargins: useMargin, pinTop: edge != .bottom, pinBottom: edge != .top, pinLeft: edge != .right, pinRight: edge != .left).values))
} }
open override func setupView() { public static func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
super.setupView() return 1
heightConstraint = heightAnchor.constraint(equalToConstant: 1)
heightConstraint?.isActive = true
widthConstraint = widthAnchor.constraint(equalToConstant: 1)
widthConstraint?.isActive = false
}
open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
super.set(with: model, delegateObject, additionalData)
if let lineModel = model as? LineModel {
setStyle(lineModel.type)
}
}
open override func reset() {
setStyle(.secondary)
}
public override static func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
return (model as? LineModel)?.thickness ?? 1
} }
} }
//--------------------------------------------------
// MARK: - MVMCoreUIViewConstrainingProtocol
//--------------------------------------------------
extension Line: MVMCoreUIViewConstrainingProtocol { extension Line: MVMCoreUIViewConstrainingProtocol {
open func needsToBeConstrained() -> Bool { open func needsToBeConstrained() -> Bool {

View File

@ -8,8 +8,9 @@
import UIKit import UIKit
import VDSColorTokens import VDSColorTokens
import VDS
@objcMembers public class LineModel: MoleculeModelProtocol { @objcMembers public class LineModel: MoleculeModelProtocol, Invertable {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Enums // MARK: - Enums
//-------------------------------------------------- //--------------------------------------------------
@ -28,23 +29,9 @@ import VDSColorTokens
case between case between
} }
/**
The style of the line:
- secondary (1 height, silver)
- primary (1 height, black)
- standard (1 height, silver) - deprecated
- thin (1 height, black) - deprecated
- medium (2 height, black)
- heavy (4 height, black)
- none (hidden)
*/
public enum Style: String, Codable { public enum Style: String, Codable {
case secondary case secondary
case primary case primary
case standard
case thin
case medium
case heavy
case none case none
} }
@ -54,53 +41,14 @@ import VDSColorTokens
public static var identifier: String = "line" public static var identifier: String = "line"
public var id: String = UUID().uuidString public var id: String = UUID().uuidString
public var backgroundColor: Color?
public var type: Style = .secondary public var type: Style = .primary
public var frequency: Frequency? = .allExceptTop public var frequency: Frequency? = .allExceptTop
//TODO: use color insted of backgroundColor. Needs server changes
// public var color: Color?
private var _backgroundColor: Color?
public var backgroundColor: Color? {
get {
if let backgroundColor = _backgroundColor { return backgroundColor }
if inverted {
if type == .secondary || type == .standard { return Color(uiColor: VDSColor.paletteGray20) }
return Color(uiColor: VDSColor.elementsPrimaryOndark)
}
if type == .secondary || type == .standard { return Color(uiColor: VDSColor.paletteGray85) }
return Color(uiColor: VDSColor.elementsPrimaryOnlight)
}
set {
_backgroundColor = newValue
}
}
private var _thickness: CGFloat?
public var thickness: CGFloat {
get {
if let thickness = _thickness { return thickness }
switch type {
case .heavy:
return 4
case .medium:
return 2
case .none:
return 0
default:
return 1
}
}
set {
_thickness = newValue
}
}
public var inverted: Bool = false public var inverted: Bool = false
// Use this to show vertical line public var orientation: VDS.Line.Orientation = .horizontal
// Default is false
public var useVerticalLine: Bool?
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Initializer // MARK: - Initializer
@ -108,13 +56,10 @@ import VDSColorTokens
public init(type: Style) { public init(type: Style) {
self.type = type self.type = type
self.useVerticalLine = false
} }
public init(verticalLineOf type: Style, backgroundColor: Color? = nil) { public init(verticalLineOf type: Style, backgroundColor: Color? = nil) {
self.type = type self.type = type
self.backgroundColor = backgroundColor
self.useVerticalLine = true
} }
//-------------------------------------------------- //--------------------------------------------------
@ -125,13 +70,10 @@ import VDSColorTokens
case id case id
case moleculeName case moleculeName
case type case type
case backgroundColor
case backgroundColor_inverted
case color
case frequency case frequency
case inverted case inverted
case useVerticalLine case useVerticalLine
case thickness case orientation
} }
//-------------------------------------------------- //--------------------------------------------------
@ -155,9 +97,12 @@ import VDSColorTokens
self.inverted = inverted self.inverted = inverted
} }
backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) /// adding code to look for the old useVerticalLine or the new orientation
useVerticalLine = try typeContainer.decodeIfPresent(Bool.self, forKey: .useVerticalLine) if let useVerticalLine = try typeContainer.decodeIfPresent(Bool.self, forKey: .useVerticalLine) {
_thickness = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .thickness) orientation = useVerticalLine ? .vertical : .horizontal
}else if let orientation = try typeContainer.decodeIfPresent(VDS.Line.Orientation.self, forKey: .orientation) {
self.orientation = orientation
}
} }
public func encode(to encoder: Encoder) throws { public func encode(to encoder: Encoder) throws {
@ -167,8 +112,6 @@ import VDSColorTokens
try container.encode(type, forKey: .type) try container.encode(type, forKey: .type)
try container.encode(inverted, forKey: .inverted) try container.encode(inverted, forKey: .inverted)
try container.encodeIfPresent(frequency, forKey: .frequency) try container.encodeIfPresent(frequency, forKey: .frequency)
try container.encodeIfPresent(_backgroundColor, forKey: .backgroundColor) try container.encodeIfPresent(orientation == .vertical, forKey: .useVerticalLine)
try container.encodeIfPresent(useVerticalLine, forKey: .useVerticalLine)
try container.encodeIfPresent(_thickness, forKey: .thickness)
} }
} }