409 lines
14 KiB
Swift
409 lines
14 KiB
Swift
//
|
|
// Tilet.swift
|
|
// VDS
|
|
//
|
|
// Created by Matt Bruce on 12/19/22.
|
|
//
|
|
|
|
import Foundation
|
|
import Foundation
|
|
import VDSColorTokens
|
|
import UIKit
|
|
|
|
@objc(VDSTilelet)
|
|
open class Tilelet: TileContainer {
|
|
//--------------------------------------------------
|
|
// MARK: - Enums
|
|
//--------------------------------------------------
|
|
public enum TextPosition: String, CaseIterable {
|
|
case top
|
|
case bottom
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// 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: - Private Properties
|
|
//--------------------------------------------------
|
|
private var stackView = UIStackView().with {
|
|
$0.translatesAutoresizingMaskIntoConstraints = false
|
|
$0.axis = .vertical
|
|
$0.distribution = .fill
|
|
$0.spacing = 5
|
|
}
|
|
|
|
private var titleLockupContainerView = UIView().with {
|
|
$0.translatesAutoresizingMaskIntoConstraints = false
|
|
}
|
|
|
|
private var badgeContainerView = UIView().with {
|
|
$0.translatesAutoresizingMaskIntoConstraints = false
|
|
}
|
|
|
|
private let iconContainerView = UIView().with {
|
|
$0.translatesAutoresizingMaskIntoConstraints = false
|
|
$0.backgroundColor = .clear
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Public Properties
|
|
//--------------------------------------------------
|
|
open var titleLockup = TitleLockup().with {
|
|
let configs = [
|
|
TextStyle.DeviceSpacingConfig([.titleSmall, .boldTitleSmall],
|
|
neighboring: [
|
|
.bodySmall, .boldBodySmall,
|
|
.bodyMedium, .boldBodyMedium
|
|
],
|
|
spacing: 8.0,
|
|
deviceType: .iPhone),
|
|
|
|
TextStyle.DeviceSpacingConfig([.titleMedium, .boldTitleMedium,
|
|
.titleLarge, .boldTitleLarge],
|
|
neighboring: [
|
|
.bodySmall, .boldBodySmall,
|
|
.bodyMedium, .boldBodyMedium,
|
|
.bodyLarge, .boldBodyLarge],
|
|
spacing: 8.0,
|
|
deviceType: .iPhone),
|
|
|
|
TextStyle.DeviceSpacingConfig([.titleXLarge, .boldTitleXLarge],
|
|
neighboring: [
|
|
.bodySmall, .boldBodySmall,
|
|
.bodyMedium, .boldBodyMedium,
|
|
.bodyLarge, .boldBodyLarge,
|
|
.titleMedium, .boldTitleMedium
|
|
],
|
|
spacing: 12.0,
|
|
deviceType: .iPhone),
|
|
|
|
TextStyle.DeviceSpacingConfig([.titleSmall, .boldTitleSmall,
|
|
.titleMedium, .boldTitleMedium],
|
|
neighboring: [
|
|
.bodySmall, .boldBodySmall,
|
|
.bodyMedium, .boldBodyMedium,
|
|
.bodyLarge, .boldBodyLarge
|
|
],
|
|
spacing: 8.0,
|
|
deviceType: .iPad),
|
|
|
|
TextStyle.DeviceSpacingConfig([.titleLarge, .boldTitleLarge],
|
|
neighboring: [
|
|
.bodySmall, .boldBodySmall,
|
|
.bodyMedium, .boldBodyMedium,
|
|
.bodyLarge, .boldBodyLarge,
|
|
.titleSmall, .boldTitleSmall
|
|
],
|
|
spacing: 12.0,
|
|
deviceType: .iPad),
|
|
|
|
TextStyle.DeviceSpacingConfig([.titleXLarge, .boldTitleXLarge],
|
|
neighboring: [
|
|
.bodyLarge, .boldBodyLarge,
|
|
.titleMedium, .boldTitleMedium
|
|
],
|
|
spacing: 16.0,
|
|
deviceType: .iPad)
|
|
|
|
]
|
|
|
|
$0.bottomTextStyleSpacingConfig = TextStyle.SpacingConfig(configs: configs)
|
|
}
|
|
|
|
open var badge = Badge().with {
|
|
$0.fillColor = .red
|
|
}
|
|
|
|
open var descriptiveIcon = Icon()
|
|
|
|
open var directionalIcon = Icon().with {
|
|
$0.name = .rightArrow
|
|
}
|
|
|
|
private var _textWidth: CGFloat?
|
|
open var textWidth: CGFloat? {
|
|
get { _textWidth }
|
|
set {
|
|
if let newValue, newValue > 44.0 {
|
|
_textWidth = newValue
|
|
if _textPercentage != nil {
|
|
_textPercentage = nil
|
|
}
|
|
} else {
|
|
_textWidth = nil
|
|
}
|
|
didChange()
|
|
}
|
|
|
|
}
|
|
|
|
private var _textPercentage: CGFloat?
|
|
open var textPercentage: CGFloat? {
|
|
get { _textPercentage }
|
|
set {
|
|
if let newValue, newValue >= 5 && newValue <= 100.0 {
|
|
_textPercentage = newValue
|
|
if textWidth != nil {
|
|
_textWidth = nil
|
|
}
|
|
} else {
|
|
_textPercentage = nil
|
|
}
|
|
didChange()
|
|
}
|
|
}
|
|
|
|
open var textPostion: TextPosition = .top { didSet { didChange() }}
|
|
|
|
//models
|
|
public var badgeModel: BadgeModel? { didSet { didChange() }}
|
|
public var titleModel: TitleModel? { didSet { didChange() }}
|
|
public var subTitleModel: SubTitleModel? { didSet { didChange() }}
|
|
|
|
//only 1 Icon can be active
|
|
private var _descriptiveIconModel: DescriptiveIcon?
|
|
public var descriptiveIconModel: DescriptiveIcon? {
|
|
get { _descriptiveIconModel }
|
|
set {
|
|
_descriptiveIconModel = newValue;
|
|
_directionalIconModel = nil
|
|
didChange()
|
|
}
|
|
}
|
|
|
|
private var _directionalIconModel: DirectionalIcon?
|
|
public var directionalIconModel: DirectionalIcon? {
|
|
get { _directionalIconModel }
|
|
set {
|
|
_directionalIconModel = newValue;
|
|
_descriptiveIconModel = nil
|
|
didChange()
|
|
}
|
|
}
|
|
//icons
|
|
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Constraints
|
|
//--------------------------------------------------
|
|
internal var titleLockupWidthConstraint: NSLayoutConstraint?
|
|
internal var titleLockupTrailingConstraint: NSLayoutConstraint?
|
|
//functions
|
|
//--------------------------------------------------
|
|
// MARK: - Lifecycle
|
|
//--------------------------------------------------
|
|
|
|
open override func setup() {
|
|
super.setup()
|
|
width = 100
|
|
aspectRatio = .none
|
|
color = .black
|
|
addContentView(stackView)
|
|
|
|
isAccessibilityElement = true
|
|
accessibilityTraits = .staticText
|
|
|
|
//badge
|
|
badgeContainerView.addSubview(badge)
|
|
badge
|
|
.pinTop()
|
|
.pinLeading()
|
|
.pinBottom()
|
|
badge.trailingAnchor.constraint(lessThanOrEqualTo: badgeContainerView.trailingAnchor).isActive = true
|
|
|
|
titleLockupContainerView.addSubview(titleLockup)
|
|
titleLockup
|
|
.pinTop()
|
|
.pinLeading()
|
|
.pinBottom()
|
|
titleLockupTrailingConstraint = titleLockup.trailingAnchor.constraint(equalTo: titleLockupContainerView.trailingAnchor)
|
|
titleLockupTrailingConstraint?.isActive = true
|
|
|
|
iconContainerView.addSubview(descriptiveIcon)
|
|
iconContainerView.addSubview(directionalIcon)
|
|
|
|
descriptiveIcon
|
|
.pinLeading()
|
|
.pinTop()
|
|
.pinBottom()
|
|
|
|
directionalIcon
|
|
.pinTrailing()
|
|
.pinTop()
|
|
.pinBottom()
|
|
|
|
}
|
|
|
|
open override func reset() {
|
|
aspectRatio = .none
|
|
color = .black
|
|
|
|
//models
|
|
badgeModel = nil
|
|
titleModel = nil
|
|
subTitleModel = nil
|
|
descriptiveIconModel = nil
|
|
directionalIconModel = nil
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - State
|
|
//--------------------------------------------------
|
|
fileprivate func updateBadge() {
|
|
if let badgeModel {
|
|
badge.text = badgeModel.text
|
|
badge.fillColor = badgeModel.fillColor
|
|
badge.numberOfLines = badgeModel.numberOfLines
|
|
badge.surface = badgeModel.surface
|
|
badge.maxWidth = badgeModel.maxWidth
|
|
if badgeContainerView.superview == nil {
|
|
stackView.insertArrangedSubview(badgeContainerView, at: 0)
|
|
setNeedsLayout()
|
|
}
|
|
} else {
|
|
removeFromSuperview(badgeContainerView)
|
|
}
|
|
}
|
|
|
|
fileprivate func updateTitleLockup() {
|
|
|
|
var showTitleLockup = false
|
|
|
|
if let titleModel, !titleModel.text.isEmpty {
|
|
showTitleLockup = true
|
|
}
|
|
|
|
if let subTitleModel, !subTitleModel.text.isEmpty {
|
|
showTitleLockup = true
|
|
}
|
|
|
|
if showTitleLockup {
|
|
//flip the surface for the titleLockup
|
|
titleLockup.surface = color == .black ? Surface.dark : Surface.light
|
|
|
|
//titleLockup
|
|
if let textWidth {
|
|
titleLockupTrailingConstraint?.isActive = false
|
|
titleLockupWidthConstraint?.isActive = false
|
|
titleLockupWidthConstraint = titleLockup.widthAnchor.constraint(equalToConstant: textWidth)
|
|
titleLockupWidthConstraint?.isActive = true
|
|
|
|
} else if let textPercentage {
|
|
titleLockupTrailingConstraint?.isActive = false
|
|
titleLockupWidthConstraint?.isActive = false
|
|
titleLockupWidthConstraint = NSLayoutConstraint(item: titleLockup,
|
|
attribute: .width,
|
|
relatedBy: .equal,
|
|
toItem: containerView,
|
|
attribute: .width,
|
|
multiplier: textPercentage / 100,
|
|
constant: 0.0)
|
|
titleLockupWidthConstraint?.isActive = true
|
|
|
|
} else {
|
|
titleLockupWidthConstraint?.isActive = false
|
|
titleLockupTrailingConstraint?.isActive = true
|
|
}
|
|
|
|
//set models
|
|
titleLockup.titleModel = titleModel?.toTitleLockupTitleModel()
|
|
titleLockup.subTitleModel = subTitleModel?.toTitleLockupSubTitleModel()
|
|
|
|
if let style = subTitleModel?.textStyle.value {
|
|
titleLockup.otherTextStyle = style
|
|
}
|
|
|
|
if titleLockupContainerView.superview == nil {
|
|
stackView.insertArrangedSubview(titleLockupContainerView, at: badgeContainerView.superview == nil ? 0 : 1)
|
|
setNeedsLayout()
|
|
}
|
|
} else {
|
|
removeFromSuperview(titleLockupContainerView)
|
|
}
|
|
|
|
accessibilityTraits = enabledHighlight ? .link : .staticText
|
|
accessibilityLabel = [titleModel?.text, subTitleModel?.text].compactMap({$0}).joined(separator: ", ")
|
|
}
|
|
|
|
fileprivate func updateIcons() {
|
|
//icons
|
|
var showIconContainerView = false
|
|
if let descriptiveIconModel {
|
|
descriptiveIcon.name = descriptiveIconModel.name
|
|
descriptiveIcon.size = descriptiveIconModel.size
|
|
descriptiveIcon.surface = descriptiveIconModel.surface
|
|
showIconContainerView = true
|
|
}
|
|
|
|
if let directionalIconModel {
|
|
directionalIcon.size = directionalIconModel.size
|
|
directionalIcon.surface = directionalIconModel.surface
|
|
showIconContainerView = true
|
|
}
|
|
|
|
//iconContainer
|
|
descriptiveIcon.isHidden = descriptiveIconModel == nil
|
|
directionalIcon.isHidden = directionalIconModel == nil
|
|
|
|
if showIconContainerView {
|
|
//spacing before iconContainerView
|
|
var view: UIView?
|
|
if badgeContainerView.superview != nil {
|
|
view = badgeContainerView
|
|
}
|
|
if titleLockupContainerView.superview != nil {
|
|
view = titleLockupContainerView
|
|
}
|
|
if let view {
|
|
stackView.setCustomSpacing(padding.tiletSpacing, after: view)
|
|
}
|
|
if iconContainerView.superview == nil {
|
|
stackView.addArrangedSubview(iconContainerView)
|
|
setNeedsDisplay()
|
|
}
|
|
} else {
|
|
removeFromSuperview(iconContainerView)
|
|
}
|
|
}
|
|
|
|
open override func updateView() {
|
|
super.updateView()
|
|
|
|
updateBadge()
|
|
updateTitleLockup()
|
|
updateIcons()
|
|
|
|
layoutIfNeeded()
|
|
}
|
|
}
|
|
|
|
extension TileContainer.Padding {
|
|
fileprivate var tiletSpacing: CGFloat {
|
|
switch self {
|
|
case .padding2X:
|
|
return 16
|
|
case .padding4X:
|
|
return 24
|
|
case .padding6X:
|
|
return 32
|
|
case .padding8X:
|
|
return 48
|
|
default:
|
|
return 16
|
|
}
|
|
}
|
|
}
|