vds_ios_sample/VDSSample/ViewControllers/TableViewTestController.swift
Matt Bruce 0a4560cffc updated test
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2023-08-22 08:59:39 -05:00

224 lines
7.9 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
}
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: 0, bottom: 5, right: 16))
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)
])
items.forEach { menuItem in
if let componentable = menuItem.viewController as? ComponentSampleable.Type {
components.append(componentable.makeSample())
}
}
tableView.allowsSelection = false
tableView.estimatedRowHeight = 32
tableView.rowHeight = UITableView.automaticDimension
tableView.separatorStyle = .singleLine
tableView.delegate = self
tableView.dataSource = self
tableView.reloadData()
}
public func refresh() {
DispatchQueue.main.async { [self] in
UIView.performWithoutAnimation {
tableView.beginUpdates()
tableView.endUpdates()
}
}
}
var items: [MenuComponent] {
batch1
}
var all: [MenuComponent] {
MenuViewController.items
}
var batch1: [MenuComponent] {
[
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: "Label", completed: true, viewController: LabelViewController.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),
]
}
}
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 {
let sample = components[indexPath.row]
var component = sample.component
component.surface = surface
let cell = UITableViewCell()
cell.contentView.addSubview(component)
sample.pin(edgeInset: .init(top: 16, left: 16, bottom: 16, right: 16))
cell.backgroundColor = surface.color
cell.layoutIfNeeded()
return cell
}
}