289 lines
11 KiB
Swift
289 lines
11 KiB
Swift
//
|
|
// Tilet.swift
|
|
// VDS
|
|
//
|
|
// Created by Matt Bruce on 12/19/22.
|
|
//
|
|
|
|
import Foundation
|
|
import Foundation
|
|
import VDSColorTokens
|
|
import UIKit
|
|
|
|
@objc(VDSTilet)
|
|
open class Tilet: TileContainer {
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Initializers
|
|
//--------------------------------------------------
|
|
required public init() {
|
|
super.init(frame: .zero)
|
|
initialSetup()
|
|
}
|
|
|
|
public override init(frame: CGRect) {
|
|
super.init(frame: .zero)
|
|
initialSetup()
|
|
}
|
|
|
|
public required init?(coder: NSCoder) {
|
|
super.init(coder: coder)
|
|
initialSetup()
|
|
}
|
|
//--------------------------------------------------
|
|
// 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 titleLockup = TitleLockup().with {
|
|
let configs = [
|
|
TypographicalStyleDeviceSpacingConfig([.TitleSmall, .BoldTitleSmall],
|
|
neighboring: [
|
|
.BodySmall, .BoldBodySmall,
|
|
.BodyMedium, .BoldBodyMedium
|
|
],
|
|
spacing: 8.0,
|
|
deviceType: .iPhone),
|
|
|
|
TypographicalStyleDeviceSpacingConfig([.TitleMedium, .BoldTitleMedium,
|
|
.TitleLarge, .BoldTitleLarge],
|
|
neighboring: [
|
|
.BodySmall, .BoldBodySmall,
|
|
.BodyMedium, .BoldBodyMedium,
|
|
.BodyLarge, .BoldBodyLarge],
|
|
spacing: 8.0,
|
|
deviceType: .iPhone),
|
|
|
|
TypographicalStyleDeviceSpacingConfig([.TitleXLarge, .BoldTitleXLarge],
|
|
neighboring: [
|
|
.BodySmall, .BoldBodySmall,
|
|
.BodyMedium, .BoldBodyMedium,
|
|
.BodyLarge, .BoldBodyLarge,
|
|
.TitleMedium, .BoldTitleMedium
|
|
],
|
|
spacing: 12.0,
|
|
deviceType: .iPhone),
|
|
|
|
TypographicalStyleDeviceSpacingConfig([.TitleSmall, .BoldTitleSmall,
|
|
.TitleMedium, .BoldTitleMedium],
|
|
neighboring: [
|
|
.BodySmall, .BoldBodySmall,
|
|
.BodyMedium, .BoldBodyMedium,
|
|
.BodyLarge, .BoldBodyLarge
|
|
],
|
|
spacing: 8.0,
|
|
deviceType: .iPad),
|
|
|
|
TypographicalStyleDeviceSpacingConfig([.TitleLarge, .BoldTitleLarge],
|
|
neighboring: [
|
|
.BodySmall, .BoldBodySmall,
|
|
.BodyMedium, .BoldBodyMedium,
|
|
.BodyLarge, .BoldBodyLarge,
|
|
.TitleSmall, .BoldTitleSmall
|
|
],
|
|
spacing: 12.0,
|
|
deviceType: .iPad),
|
|
|
|
TypographicalStyleDeviceSpacingConfig([.TitleXLarge, .BoldTitleXLarge],
|
|
neighboring: [
|
|
.BodyLarge, .BoldBodyLarge,
|
|
.TitleMedium, .BoldTitleMedium
|
|
],
|
|
spacing: 16.0,
|
|
deviceType: .iPad)
|
|
|
|
]
|
|
|
|
$0.bottomTypographicalStyleSpacingConfig = TypographicalStyleSpacingConfig(configs: configs)
|
|
}
|
|
|
|
private var badgeContainerView = UIView().with {
|
|
$0.translatesAutoresizingMaskIntoConstraints = false
|
|
}
|
|
|
|
private var badge = Badge().with {
|
|
$0.fillColor = .red
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Public Properties
|
|
//--------------------------------------------------
|
|
//style
|
|
private var _textWidth: CGFloat?
|
|
open var textWidth: CGFloat? {
|
|
get { _textWidth }
|
|
set {
|
|
if let newValue, newValue > 44.0 && newValue <= width {
|
|
_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()
|
|
}
|
|
}
|
|
|
|
//models
|
|
public var badgeModel: TiletBadgeModel? { didSet { didChange() }}
|
|
public var titleModel: TiletTitleModel? { didSet { didChange() }}
|
|
public var subTitleModel: TiletSubTitleModel? { didSet { 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
|
|
containerBackgroundColor = .black
|
|
|
|
addContentView(stackView)
|
|
|
|
//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
|
|
|
|
//stackView.addArrangedSubview(badgeContainerView)
|
|
//stackView.addArrangedSubview(titleLockupContainerView)
|
|
|
|
}
|
|
|
|
public override func reset() {
|
|
super.reset()
|
|
aspectRatio = .none
|
|
surface = .light
|
|
containerBackgroundColor = .black
|
|
|
|
//badge
|
|
badge.reset()
|
|
badgeModel = nil
|
|
|
|
//titleLockup
|
|
titleLockup.reset()
|
|
titleModel = nil
|
|
subTitleModel = nil
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - State
|
|
//--------------------------------------------------
|
|
|
|
open override func updateView() {
|
|
super.updateView()
|
|
|
|
//flip the surface for the titleLockup
|
|
let titleLockupSurface: Surface = containerBackgroundColor == .black ? .dark : .light
|
|
titleLockup.surface = titleLockupSurface
|
|
|
|
//update constraints
|
|
|
|
//badge
|
|
if let badgeModel {
|
|
if badgeContainerView.superview == nil {
|
|
stackView.insertArrangedSubview(badgeContainerView, at: 0)
|
|
}
|
|
badge.set(with: badgeModel)
|
|
} else {
|
|
badgeContainerView.removeFromSuperview()
|
|
}
|
|
|
|
var showTitleLockup = false
|
|
|
|
if let titleModel, !titleModel.text.isEmpty {
|
|
showTitleLockup = true
|
|
}
|
|
|
|
if let subTitleModel, !subTitleModel.text.isEmpty {
|
|
showTitleLockup = true
|
|
}
|
|
|
|
if showTitleLockup {
|
|
if titleLockupContainerView.superview == nil {
|
|
stackView.insertArrangedSubview(titleLockupContainerView, at: badgeContainerView.superview == nil ? 0 : 1)
|
|
}
|
|
|
|
//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
|
|
}
|
|
|
|
titleLockup.set(with: titleModel)
|
|
titleLockup.set(with: subTitleModel)
|
|
|
|
} else {
|
|
titleLockupContainerView.removeFromSuperview()
|
|
}
|
|
}
|
|
}
|
|
|
|
|