vds_ios/VDS/Components/Badge/Badge.swift
Matt Bruce 0b63f5abea updated with internal wrapper view so the object won't stretch and mess up alignments
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2022-11-18 10:02:07 -06:00

211 lines
7.2 KiB
Swift

//
// Badge.swift
// VDS
//
// Created by Matt Bruce on 9/22/22.
//
import Foundation
import UIKit
import VDSColorTokens
import VDSFormControlsTokens
import Combine
public enum BadgeFillColor: String, Codable, CaseIterable {
case red, yellow, green, orange, blue, black, white
}
@objc(VDSBadge)
public class Badge: View, Accessable {
private var contentView = UIView().with {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.layer.cornerRadius = 2
}
private var label = Label().with {
$0.setContentCompressionResistancePriority(.required, for: .vertical)
$0.adjustsFontSizeToFitWidth = false
$0.lineBreakMode = .byTruncatingTail
$0.textPosition = .left
$0.typograpicalStyle = .BoldBodySmall
}
//--------------------------------------------------
// MARK: - Public Properties
//--------------------------------------------------
open var fillColor: BadgeFillColor = .red { didSet { didChange() }}
open var text: String = "" { didSet { didChange() }}
open var maxWidth: CGFloat? { didSet { didChange() }}
open var numberOfLines: Int = 1 { didSet { didChange() }}
open var accessibilityHintEnabled: String? { didSet { didChange() }}
open var accessibilityHintDisabled: String? { didSet { didChange() }}
open var accessibilityValueEnabled: String? { didSet { didChange() }}
open var accessibilityValueDisabled: String? { didSet { didChange() }}
open var accessibilityLabelEnabled: String? { didSet { didChange() }}
open var accessibilityLabelDisabled: String? { didSet { didChange() }}
//--------------------------------------------------
// MARK: - Constraints
//--------------------------------------------------
private var maxWidthConstraint: NSLayoutConstraint?
private var minWidthConstraint: NSLayoutConstraint?
//functions
//--------------------------------------------------
// MARK: - Lifecycle
//--------------------------------------------------
open override func setup() {
super.setup()
isAccessibilityElement = true
accessibilityTraits = .staticText
contentView.addSubview(label)
addSubview(contentView)
contentView
.pinTop()
.pinBottom()
.pinLeading()
contentView.trailingAnchor.constraint(lessThanOrEqualTo: trailingAnchor).isActive = true
label.pinToSuperView(.init(top: 2, left: 4, bottom: 2, right: 4))
maxWidthConstraint = label.widthAnchor.constraint(lessThanOrEqualToConstant: 100)
minWidthConstraint = label.widthAnchor.constraint(greaterThanOrEqualToConstant: 23)
minWidthConstraint?.isActive = true
}
public override func reset() {
super.reset()
label.reset()
label.lineBreakMode = .byTruncatingTail
label.textPosition = .left
label.typograpicalStyle = .BoldBodySmall
fillColor = .red
text = ""
maxWidth = nil
numberOfLines = 1
accessibilityHintEnabled = nil
accessibilityHintDisabled = nil
accessibilityValueEnabled = nil
accessibilityValueDisabled = nil
accessibilityLabelEnabled = nil
accessibilityLabelDisabled = nil
setAccessibilityLabel()
}
//--------------------------------------------------
// MARK: - Configuration
//--------------------------------------------------
public func backgroundColor() -> UIColor {
var config: SurfaceColorConfiguration
switch fillColor {
case .red:
config = SurfaceColorConfiguration().with {
$0.lightColor = VDSColor.backgroundBrandhighlight
$0.darkColor = VDSColor.backgroundBrandhighlight
}
case .yellow:
config = SurfaceColorConfiguration().with {
$0.lightColor = VDSColor.paletteYellow62
$0.darkColor = VDSColor.paletteYellow62
}
case .green:
config = SurfaceColorConfiguration().with {
$0.lightColor = VDSColor.paletteGreen26
$0.darkColor = VDSColor.paletteGreen34
}
case .orange:
config = SurfaceColorConfiguration().with {
$0.lightColor = VDSColor.paletteOrange39
$0.darkColor = VDSColor.paletteOrange46
}
case .blue:
config = SurfaceColorConfiguration().with {
$0.lightColor = VDSColor.paletteBlue35
$0.darkColor = VDSColor.paletteBlue45
}
case .black:
config = SurfaceColorConfiguration().with {
$0.lightColor = VDSColor.paletteBlack
$0.darkColor = VDSColor.paletteBlack
}
case .white:
config = SurfaceColorConfiguration().with {
$0.lightColor = VDSColor.paletteWhite
$0.darkColor = VDSColor.paletteWhite
}
}
return config.getColor(self)
}
public func textColorConfiguration() -> AnyColorable {
switch fillColor {
case .red, .black:
return DisabledSurfaceColorConfiguration().with {
$0.disabled.lightColor = VDSColor.elementsPrimaryOndark
$0.disabled.darkColor = VDSColor.elementsPrimaryOndark
$0.enabled.lightColor = VDSColor.elementsPrimaryOndark
$0.enabled.darkColor = VDSColor.elementsPrimaryOndark
}.eraseToAnyColorable()
case .yellow, .white:
return DisabledSurfaceColorConfiguration().with {
$0.disabled.lightColor = VDSColor.elementsPrimaryOnlight
$0.disabled.darkColor = VDSColor.elementsPrimaryOnlight
$0.enabled.lightColor = VDSColor.elementsPrimaryOnlight
$0.enabled.darkColor = VDSColor.elementsPrimaryOnlight
}.eraseToAnyColorable()
case .orange, .green, .blue:
return DisabledSurfaceColorConfiguration().with {
$0.disabled.lightColor = VDSColor.elementsPrimaryOndark
$0.disabled.darkColor = VDSColor.elementsPrimaryOnlight
$0.enabled.lightColor = VDSColor.elementsPrimaryOndark
$0.enabled.darkColor = VDSColor.elementsPrimaryOnlight
}.eraseToAnyColorable()
}
}
//--------------------------------------------------
// MARK: - State
//--------------------------------------------------
open override func updateView() {
contentView.backgroundColor = backgroundColor()
label.textColorConfiguration = textColorConfiguration()
label.numberOfLines = numberOfLines
label.text = text
label.surface = surface
label.disabled = disabled
if let maxWidth = maxWidth, let minWidth = minWidthConstraint?.constant, maxWidth > minWidth {
maxWidthConstraint?.constant = maxWidth
maxWidthConstraint?.isActive = true
} else {
maxWidthConstraint?.isActive = false
}
setAccessibilityLabel()
}
}