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 VDS
@objcMembers open class Line: View {
@objcMembers open class Line: VDS.View, VDSMoleculeViewProtocol {
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
var lineModel: LineModel? {
get { return model as? LineModel }
}
//--------------------------------------------------
// MARK: - Constraints
//--------------------------------------------------
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
open var line = VDS.Line()
open var viewModel: LineModel!
open var delegateObject: MVMCoreUIDelegateObject?
open var additionalData: [AnyHashable : Any]?
open var orientation: VDS.Line.Orientation = .horizontal {
didSet {
viewModel.orientation = orientation
update(viewModel: viewModel)
}
}
//--------------------------------------------------
// MARK: - Initializer
//--------------------------------------------------
//--------------------------------------------------
public convenience init(pinTo view: UIView, edge: UIRectEdge, useMargin: Bool) {
self.init(frame: .zero)
addLine(to: view, edge: edge, useMargin: useMargin)
}
public init() {
super.init(frame: .zero)
model = LineModel(type: .secondary)
setStyle(.secondary)
public required init() {
super.init()
viewModel = LineModel(type: .primary)
}
public override init(frame: CGRect) {
super.init(frame: frame)
model = LineModel(type: .secondary)
setStyle(.secondary)
viewModel = LineModel(type: .primary)
}
public required init?(coder: NSCoder) {
super.init(coder: coder)
model = LineModel(type: .secondary)
setStyle(.secondary)
viewModel = LineModel(type: .primary)
}
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
//--------------------------------------------------
open func setStyle(_ style: LineModel.Style) {
lineModel?.type = style
backgroundColor = lineModel?.backgroundColor?.uiColor
updateLineConstraints(constant: lineModel?.thickness ?? 1)
}
open func shouldBeVisible() -> Bool {
guard let type = lineModel?.type else { return false }
guard let type = viewModel?.type else { return false }
return type != .none
}
//--------------------------------------------------
// MARK: - MoleculeViewProtocol
//--------------------------------------------------
open func setStyle(_ style: LineModel.Style) {
viewModel.type = style
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))
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() {
super.setupView()
heightConstraint = heightAnchor.constraint(equalToConstant: 1)
heightConstraint?.isActive = true
widthConstraint = widthAnchor.constraint(equalToConstant: 1)
widthConstraint?.isActive = false
//--------------------------------------------------
// MARK: - VDSMoleculeViewProtocol
//--------------------------------------------------
open func viewModelDidUpdate() {
surface = viewModel.surface
line.style = VDS.Line.Style(rawValue: viewModel.type.rawValue) ?? .primary
line.orientation = viewModel.orientation
}
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)
}
//--------------------------------------------------
// MARK: - MoleculeViewProtocol
//--------------------------------------------------
public func updateView(_ size: CGFloat) {
setNeedsDisplay()
}
open override func reset() {
setStyle(.secondary)
}
public override static func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
return (model as? LineModel)?.thickness ?? 1
public static func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
return 1
}
}
//--------------------------------------------------
// MARK: - MVMCoreUIViewConstrainingProtocol
//--------------------------------------------------
extension Line: MVMCoreUIViewConstrainingProtocol {
open func needsToBeConstrained() -> Bool {

View File

@ -8,8 +8,9 @@
import UIKit
import VDSColorTokens
import VDS
@objcMembers public class LineModel: MoleculeModelProtocol {
@objcMembers public class LineModel: MoleculeModelProtocol, Invertable {
//--------------------------------------------------
// MARK: - Enums
//--------------------------------------------------
@ -28,23 +29,9 @@ import VDSColorTokens
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 {
case secondary
case primary
case standard
case thin
case medium
case heavy
case none
}
@ -54,53 +41,14 @@ import VDSColorTokens
public static var identifier: String = "line"
public var id: String = UUID().uuidString
public var type: Style = .secondary
public var backgroundColor: Color?
public var type: Style = .primary
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
// Use this to show vertical line
// Default is false
public var useVerticalLine: Bool?
public var orientation: VDS.Line.Orientation = .horizontal
//--------------------------------------------------
// MARK: - Initializer
@ -108,13 +56,10 @@ import VDSColorTokens
public init(type: Style) {
self.type = type
self.useVerticalLine = false
}
public init(verticalLineOf type: Style, backgroundColor: Color? = nil) {
self.type = type
self.backgroundColor = backgroundColor
self.useVerticalLine = true
}
//--------------------------------------------------
@ -125,13 +70,10 @@ import VDSColorTokens
case id
case moleculeName
case type
case backgroundColor
case backgroundColor_inverted
case color
case frequency
case inverted
case useVerticalLine
case thickness
case orientation
}
//--------------------------------------------------
@ -155,9 +97,12 @@ import VDSColorTokens
self.inverted = inverted
}
backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
useVerticalLine = try typeContainer.decodeIfPresent(Bool.self, forKey: .useVerticalLine)
_thickness = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .thickness)
/// adding code to look for the old useVerticalLine or the new orientation
if let useVerticalLine = try typeContainer.decodeIfPresent(Bool.self, forKey: .useVerticalLine) {
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 {
@ -167,8 +112,6 @@ import VDSColorTokens
try container.encode(type, forKey: .type)
try container.encode(inverted, forKey: .inverted)
try container.encodeIfPresent(frequency, forKey: .frequency)
try container.encodeIfPresent(_backgroundColor, forKey: .backgroundColor)
try container.encodeIfPresent(useVerticalLine, forKey: .useVerticalLine)
try container.encodeIfPresent(_thickness, forKey: .thickness)
try container.encodeIfPresent(orientation == .vertical, forKey: .useVerticalLine)
}
}