167 lines
6.3 KiB
Swift
167 lines
6.3 KiB
Swift
//
|
|
// Badge.swift
|
|
// VDS
|
|
//
|
|
// Created by Matt Bruce on 9/22/22.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
import VDSColorTokens
|
|
import VDSFormControlsTokens
|
|
import Combine
|
|
|
|
/// Badges are visual labels used to convey status or highlight supplemental information.
|
|
@objc(VDSBadge)
|
|
public class Badge: View, Accessable {
|
|
//--------------------------------------------------
|
|
// MARK: - Enums
|
|
//--------------------------------------------------
|
|
public enum FillColor: String, Codable, CaseIterable {
|
|
case red, yellow, green, orange, blue, black, white
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Private Properties
|
|
//--------------------------------------------------
|
|
private var label = Label().with {
|
|
$0.setContentCompressionResistancePriority(.required, for: .vertical)
|
|
$0.adjustsFontSizeToFitWidth = false
|
|
$0.lineBreakMode = .byTruncatingTail
|
|
$0.textPosition = .left
|
|
$0.textStyle = .BoldBodySmall
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Public Properties
|
|
//--------------------------------------------------
|
|
open var fillColor: FillColor = .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?
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Lifecycle
|
|
//--------------------------------------------------
|
|
|
|
open override func setup() {
|
|
super.setup()
|
|
|
|
isAccessibilityElement = true
|
|
accessibilityTraits = .staticText
|
|
layer.cornerRadius = VDSFormControls.borderradius
|
|
|
|
addSubview(label)
|
|
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.textStyle = .BoldBodySmall
|
|
|
|
fillColor = .red
|
|
text = ""
|
|
maxWidth = nil
|
|
numberOfLines = 1
|
|
accessibilityHintEnabled = nil
|
|
accessibilityHintDisabled = nil
|
|
accessibilityValueEnabled = nil
|
|
accessibilityValueDisabled = nil
|
|
accessibilityLabelEnabled = nil
|
|
accessibilityLabelDisabled = nil
|
|
|
|
setAccessibilityLabel()
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Configuration
|
|
//--------------------------------------------------
|
|
private var backgroundColorConfig: StateColorConfiguration<FillColor> = {
|
|
let config = StateColorConfiguration<FillColor>()
|
|
config.setSurfaceColors(VDSColor.backgroundBrandhighlight, VDSColor.backgroundBrandhighlight, forState: .red)
|
|
config.setSurfaceColors(VDSColor.paletteYellow62, VDSColor.paletteYellow62, forState: .yellow)
|
|
config.setSurfaceColors(VDSColor.paletteGreen26, VDSColor.paletteGreen34, forState: .green)
|
|
config.setSurfaceColors(VDSColor.paletteOrange39, VDSColor.paletteOrange46, forState: .orange)
|
|
config.setSurfaceColors(VDSColor.paletteBlue35, VDSColor.paletteBlue45, forState: .blue)
|
|
config.setSurfaceColors(VDSColor.paletteBlack, VDSColor.paletteBlack, forState: .black)
|
|
config.setSurfaceColors(VDSColor.paletteWhite, VDSColor.paletteWhite, forState: .white)
|
|
return config
|
|
}()
|
|
|
|
private var textColorConfig = ViewColorConfiguration()
|
|
|
|
public func updateTextColorConfig() {
|
|
textColorConfig.reset()
|
|
|
|
switch fillColor {
|
|
|
|
case .red, .black:
|
|
textColorConfig.setSurfaceColors(VDSColor.elementsPrimaryOndark, VDSColor.elementsPrimaryOndark, forDisabled: false)
|
|
textColorConfig.setSurfaceColors(VDSColor.elementsPrimaryOndark, VDSColor.elementsPrimaryOndark, forDisabled: true)
|
|
|
|
case .yellow, .white:
|
|
textColorConfig.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOnlight, forDisabled: false)
|
|
textColorConfig.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOnlight, forDisabled: true)
|
|
|
|
case .orange, .green, .blue:
|
|
textColorConfig.setSurfaceColors(VDSColor.elementsPrimaryOndark, VDSColor.elementsPrimaryOnlight, forDisabled: false)
|
|
textColorConfig.setSurfaceColors(VDSColor.elementsPrimaryOndark, VDSColor.elementsPrimaryOnlight, forDisabled: true)
|
|
}
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - State
|
|
//--------------------------------------------------
|
|
open override func updateView() {
|
|
updateTextColorConfig()
|
|
|
|
backgroundColor = backgroundColorConfig.getColor(surface, forState: fillColor)
|
|
|
|
label.textColorConfiguration = textColorConfig.eraseToAnyColorable()
|
|
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()
|
|
}
|
|
|
|
}
|
|
|