185 lines
6.6 KiB
Swift
185 lines
6.6 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 label = Label().with {
|
|
$0.setContentCompressionResistancePriority(.required, for: .vertical)
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// 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
|
|
addSubview(label)
|
|
|
|
layer.cornerRadius = 2
|
|
label.adjustsFontSizeToFitWidth = false
|
|
label.lineBreakMode = .byTruncatingTail
|
|
|
|
label.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 4).isActive = true
|
|
label.topAnchor.constraint(equalTo: topAnchor, constant: 2).isActive = true
|
|
trailingAnchor.constraint(greaterThanOrEqualTo: label.trailingAnchor, constant: 4).isActive = true
|
|
bottomAnchor.constraint(greaterThanOrEqualTo: label.bottomAnchor, constant: 2).isActive = true
|
|
|
|
maxWidthConstraint = label.widthAnchor.constraint(lessThanOrEqualToConstant: 100)
|
|
minWidthConstraint = label.widthAnchor.constraint(greaterThanOrEqualToConstant: 23)
|
|
minWidthConstraint?.isActive = true
|
|
}
|
|
|
|
public override func reset() {
|
|
super.reset()
|
|
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() {
|
|
backgroundColor = backgroundColor()
|
|
label.textColorConfiguration = textColorConfiguration()
|
|
label.numberOfLines = numberOfLines
|
|
label.textPosition = .left
|
|
label.typograpicalStyle = .BoldBodySmall
|
|
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()
|
|
}
|
|
|
|
}
|
|
|