231 lines
8.2 KiB
Swift
231 lines
8.2 KiB
Swift
// TableViewTestController.swift
|
|
// VDSSample
|
|
//
|
|
// Created by Matt Bruce on 5/3/23.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
import VDS
|
|
import VDSColorTokens
|
|
|
|
public typealias ComponentSampleView = UIView & Surfaceable
|
|
|
|
public struct ComponentSample {
|
|
public var component: ComponentSampleView
|
|
public enum GreaterThanPinningType: String {
|
|
case none, equalTo, greaterThanOrEqual
|
|
}
|
|
|
|
public enum LessThanPinningType: String {
|
|
case none, equalTo, lessThanOrEqual
|
|
}
|
|
|
|
public var top: GreaterThanPinningType = .equalTo
|
|
public var leading: GreaterThanPinningType = .equalTo
|
|
public var trailing: LessThanPinningType = .equalTo
|
|
public var bottom: LessThanPinningType = .equalTo
|
|
|
|
public init(component: ComponentSampleView,
|
|
topPinningType: GreaterThanPinningType = .equalTo,
|
|
leadingPinningType: GreaterThanPinningType = .equalTo,
|
|
trailingPinningType: LessThanPinningType = .equalTo,
|
|
bottomPinningType: LessThanPinningType = .equalTo) {
|
|
|
|
self.component = component
|
|
self.top = topPinningType
|
|
self.leading = leadingPinningType
|
|
self.trailing = trailingPinningType
|
|
self.bottom = bottomPinningType
|
|
}
|
|
|
|
public func pin(edgeInset: UIEdgeInsets = .zero) {
|
|
|
|
guard let superview = component.superview else { return }
|
|
switch top {
|
|
case .equalTo:
|
|
component.pinTop(anchor: superview.topAnchor, constant: edgeInset.top)
|
|
|
|
case .greaterThanOrEqual:
|
|
component.pinTopGreaterThanOrEqualTo(anchor: superview.topAnchor, constant: edgeInset.top)
|
|
|
|
case .none: break
|
|
}
|
|
|
|
switch leading {
|
|
case .equalTo:
|
|
component.pinLeading(anchor: superview.leadingAnchor, constant: edgeInset.left)
|
|
|
|
case .greaterThanOrEqual:
|
|
component.pinLeadingGreaterThanOrEqualTo(anchor: superview.leadingAnchor, constant: edgeInset.left)
|
|
|
|
case .none: break
|
|
|
|
}
|
|
|
|
switch trailing {
|
|
case .equalTo:
|
|
component.pinTrailing(anchor: superview.trailingAnchor, constant: edgeInset.right)
|
|
|
|
case .lessThanOrEqual:
|
|
component.pinTrailingLessThanOrEqualTo(anchor: superview.trailingAnchor, constant: edgeInset.right)
|
|
|
|
case .none: break
|
|
}
|
|
|
|
switch bottom {
|
|
case .equalTo:
|
|
component.pinBottom(anchor: superview.bottomAnchor, constant: edgeInset.bottom)
|
|
|
|
case .lessThanOrEqual:
|
|
component.pinBottomLessThanOrEqualTo(anchor: superview.bottomAnchor, constant: edgeInset.bottom)
|
|
|
|
case .none: break
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
protocol ComponentSampleable {
|
|
static func makeSample() -> ComponentSample
|
|
}
|
|
|
|
public class TableViewTestController: UIViewController, Initable, Surfaceable {
|
|
|
|
var components:[ComponentSample] = []
|
|
lazy var tableView = UITableView().with {
|
|
$0.translatesAutoresizingMaskIntoConstraints = false
|
|
$0.delegate = self
|
|
$0.dataSource = self
|
|
}
|
|
|
|
public var surface: Surface = .light {
|
|
didSet {
|
|
tableView.reloadData()
|
|
}
|
|
}
|
|
//--------------------------------------------------
|
|
// MARK: - Initializers
|
|
//--------------------------------------------------
|
|
required public init() {
|
|
super.init(nibName: nil, bundle: nil)
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
public override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
let toggle = Toggle().with {
|
|
$0.showText = true
|
|
//$0.textPosition = .right
|
|
$0.offText = "Light"
|
|
$0.onText = "Dark"
|
|
$0.onChange = { [weak self] toggle in
|
|
self?.surface = toggle.isOn ? .dark : .light
|
|
}
|
|
}
|
|
|
|
let toggleSample = ComponentSample(component: toggle, leadingPinningType: .none)
|
|
let wrapper = UIView().with { $0.translatesAutoresizingMaskIntoConstraints = false }
|
|
wrapper.addSubview(toggle)
|
|
toggleSample.pin(edgeInset: .init(top: 5, left: 5, bottom: 5, right: 5))
|
|
|
|
let stackView = UIStackView(arrangedSubviews: [wrapper, tableView]).with {
|
|
$0.translatesAutoresizingMaskIntoConstraints = false
|
|
$0.axis = .vertical
|
|
$0.spacing = 4
|
|
$0.distribution = .fill
|
|
}
|
|
view.backgroundColor = Surface.light.color
|
|
view.addSubview(stackView)
|
|
|
|
NSLayoutConstraint.activate([
|
|
stackView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
|
|
stackView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
|
|
stackView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
|
|
stackView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
|
|
])
|
|
|
|
//var items = MenuViewController.items
|
|
let items = [
|
|
MenuComponent(title: "Badge", completed: true, viewController: BadgeViewController.self),
|
|
MenuComponent(title: "Button", completed: true, viewController: ButtonViewController.self),
|
|
MenuComponent(title: "ButtonGroup", completed: true, viewController: ButtonGroupViewController.self),
|
|
MenuComponent(title: "Icon", completed: true, viewController: IconViewController.self),
|
|
MenuComponent(title: "Line", completed: true, viewController: LineViewController.self),
|
|
MenuComponent(title: "Loader", completed: true, viewController: LoaderViewController.self),
|
|
MenuComponent(title: "Tabs", completed: true, viewController: TabsViewController.self),
|
|
MenuComponent(title: "TextLink", completed: true, viewController: TextLinkViewController.self),
|
|
MenuComponent(title: "TextLinkCaret", completed: true, viewController: TextLinkCaretViewController.self),
|
|
MenuComponent(title: "TitleLockup", completed: true, viewController: TitleLockupViewController.self),
|
|
MenuComponent(title: "Tooltip", completed: true, viewController: TooltipViewController.self)
|
|
]
|
|
|
|
items.forEach { menuItem in
|
|
if let componentable = menuItem.viewController as? ComponentSampleable.Type {
|
|
components.append(componentable.makeSample())
|
|
}
|
|
}
|
|
|
|
tableView.register(VDSCell.self, forCellReuseIdentifier: "cell")
|
|
tableView.allowsSelection = false
|
|
tableView.estimatedRowHeight = 45
|
|
tableView.separatorStyle = .singleLine
|
|
}
|
|
|
|
public override func viewDidAppear(_ animated: Bool) {
|
|
super.viewDidAppear(animated)
|
|
tableView.reloadData()
|
|
}
|
|
|
|
public func refresh() {
|
|
DispatchQueue.main.async { [self] in
|
|
tableView.beginUpdates()
|
|
tableView.endUpdates()
|
|
}
|
|
}
|
|
}
|
|
|
|
extension TableViewTestController : UITableViewDelegate, UITableViewDataSource {
|
|
|
|
public func numberOfSections(in tableView: UITableView) -> Int {
|
|
1
|
|
}
|
|
|
|
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
components.count
|
|
}
|
|
|
|
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? VDSCell else { return UITableViewCell() }
|
|
cell.prepareForReuse()
|
|
cell.surface = surface
|
|
cell.sample = components[indexPath.row]
|
|
return cell
|
|
}
|
|
}
|
|
|
|
public class VDSCell: UITableViewCell, Surfaceable {
|
|
public var surface: Surface = .light
|
|
public var sample: ComponentSample? {
|
|
didSet {
|
|
contentView.subviews.forEach { $0.removeFromSuperview() }
|
|
|
|
guard let sample else { return }
|
|
|
|
var component = sample.component
|
|
component.surface = surface
|
|
|
|
contentView.addSubview(component)
|
|
sample.pin(edgeInset: .init(top: 16, left: 16, bottom: 16, right: 16))
|
|
|
|
backgroundColor = surface.color
|
|
}
|
|
}
|
|
}
|
|
|
|
|