173 lines
6.6 KiB
Swift
173 lines
6.6 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 class ComponentSample {
|
|
public var component: ComponentSampleView
|
|
public var model: LayoutAnchorModel
|
|
|
|
public init(component: ComponentSampleView,
|
|
model: LayoutAnchorModel = .init()) {
|
|
self.model = model
|
|
self.component = component
|
|
}
|
|
|
|
public func pin(){
|
|
component.pinToSuperview(with: model)
|
|
}
|
|
}
|
|
|
|
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 model = ContainerManager.ContainerModel(horizontal: .init(alignment: .trailing,
|
|
leading: .init(),
|
|
trailing: .init(padding: 16)),
|
|
vertical: .init(alignment: .fill,
|
|
top: .init(padding: 5),
|
|
bottom: .init(padding: 5)))
|
|
|
|
let toggleSample = ComponentSample(component: toggle, model: .init(layoutType: .right, insets: .init(top: 5, left: 0, bottom: 5, right: 16), priorities: [.bottom: .defaultLow]))
|
|
let wrapper = UIView().with { $0.translatesAutoresizingMaskIntoConstraints = false }
|
|
wrapper.addSubview(toggle)
|
|
toggleSample.pin()
|
|
|
|
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)
|
|
|
|
stackView.pin(to: view.safeAreaLayoutGuide, with: .fill)
|
|
|
|
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] {
|
|
all
|
|
}
|
|
|
|
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),
|
|
MenuComponent(title: "TrailingTooltip", completed: true, viewController: TrailingTooltipLabelViewController.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 insets = UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
|
|
let cell = UITableViewCell()
|
|
cell.contentView.addSubview(component)
|
|
component.pinToSuperview(with: sample.model.layoutType, edgeInsets: insets, priorities: sample.model.priorities)
|
|
// if let model = sample.model, let horizontal = model.horizontalAlignment, let vertical = model.verticalAlignment {
|
|
// sample.model = ContainerManager.model(for: horizontal.alignment, vertical: vertical.alignment, inset: .init(top: 16, left: 16, bottom: 16, right: 16))
|
|
// sample.pin()
|
|
// } else {
|
|
// component.pinToSuperView()
|
|
// }
|
|
cell.backgroundColor = surface.color
|
|
cell.layoutIfNeeded()
|
|
return cell
|
|
}
|
|
|
|
}
|