vds_ios/VDS/Components/Line/Line.swift
Matt Bruce e3f791960a documentation update
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2023-08-30 13:29:52 -05:00

98 lines
3.3 KiB
Swift

//
// Line.swift
// VDS
//
// Created by Nadigadda, Sumanth on 24/03/23.
//
import Foundation
import UIKit
import VDSColorTokens
/// A line visually separates content sections or elements in lists, tables and layouts to indicate content hierarchy.
@objc(VDSLine)
open class Line: View {
//--------------------------------------------------
// MARK: - Initializers
//--------------------------------------------------
required public init() {
super.init(frame: .zero)
}
public override init(frame: CGRect) {
super.init(frame: .zero)
}
public required init?(coder: NSCoder) {
super.init(coder: coder)
}
//--------------------------------------------------
// MARK: - Enums
//--------------------------------------------------
/// Enum used to describe the style the line that will be displayed.
public enum Style: String, CaseIterable {
case primary, secondary
}
/// Enum used to describe the orientation that will be displayed.
public enum Orientation: String, CaseIterable {
case horizontal, vertical
}
//--------------------------------------------------
// MARK: - Public Properties
//--------------------------------------------------
/// Style of the line that will be displayed.
open var style: Style = .primary { didSet { setNeedsUpdate() } }
/// Allows to render the line vertically.
open var orientation: Orientation = .horizontal { didSet { setNeedsUpdate() } }
/// The natural size for the receiving view, considering only properties of the view itself.
open override var intrinsicContentSize: CGSize {
if orientation == .vertical {
return .init(width: 1, height: bounds.height)
} else {
return .init(width: bounds.width, height: 1)
}
}
//--------------------------------------------------
// MARK: - Configuration
//--------------------------------------------------
/// Color configuration use for text color. This is a configuration that will provide a color based
/// of local properties such as style and surface.
open var lineViewColorConfiguration: AnyColorable = {
let config = KeyedColorConfiguration<Line, Style>(keyPath: \.style)
config.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forKey: .primary)
config.setSurfaceColors(VDSColor.elementsLowcontrastOnlight, VDSColor.elementsLowcontrastOndark, forKey: .secondary)
return config.eraseToAnyColorable()
}()
//--------------------------------------------------
// MARK: - Overrides
//--------------------------------------------------
/// Called once when a view is initialized and is used to Setup additional UI or other constants and configurations.
open override func setup() {
super.setup()
}
/// Used to make changes to the View based off a change events or from local properties.
open override func updateView() {
super.updateView()
backgroundColor = lineViewColorConfiguration.getColor(self)
invalidateIntrinsicContentSize()
}
/// Resets to default settings.
open override func reset() {
super.reset()
style = .primary
orientation = .horizontal
}
}