// // MenuViewController.swift // VDSSample // // Created by Matt Bruce on 8/1/22. // import Foundation import UIKit import VDS import VDSColorTokens struct MenuComponent { var title: String var completed: Bool var viewController: UIViewController.Type } protocol Initable { init() } class MenuCell: UITableViewCell { var completed: Bool = true { didSet { if completed { statusImage.image = UIImage(systemName: "checkmark.circle") statusImage.tintColor = VDSColor.paletteGreen26 } else { statusImage.image = UIImage(systemName: "xmark.circle") statusImage.tintColor = VDSColor.paletteRed } } } var statusImage = UIImageView().with { $0.translatesAutoresizingMaskIntoConstraints = false } var label = Label().with { $0.typograpicalStyle = .BodyLarge } override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) setup() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } func setup(){ addSubview(statusImage) addSubview(label) statusImage.pinLeading(16) label.pinLeading(statusImage.trailingAnchor, 10) label.pinTop() label.pinBottom() label.pinTrailing() statusImage.width(20) statusImage.height(20) statusImage.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true } } class MenuViewController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() overrideUserInterfaceStyle = .light tableView.register(MenuCell.self, forCellReuseIdentifier: "cell") } let items: [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: "Checkbox", completed: true, viewController: CheckboxViewController.self), MenuComponent(title: "CheckboxGroup", completed: true, viewController: CheckboxGroupViewController.self), MenuComponent(title: "Icon", completed: true, viewController: IconViewController.self), MenuComponent(title: "InputField", completed: false, viewController: InputFieldViewController.self), MenuComponent(title: "Label", completed: true, viewController: LabelViewController.self), MenuComponent(title: "RadioBoxGroup", completed: true, viewController: RadioBoxGroupViewController.self), MenuComponent(title: "RadioButtonGroup", completed: true, viewController: RadioButtonViewController.self), MenuComponent(title: "RadioSwatchGroup", completed: true, viewController: RadioSwatchGroupViewController.self), MenuComponent(title: "TextLink", completed: true, viewController: TextLinkViewController.self), MenuComponent(title: "TextLinkCaret", completed: true, viewController: TextLinkCaretViewController.self), MenuComponent(title: "TileContainer", completed: false, viewController: TileContainerViewController.self), MenuComponent(title: "Tilet", completed: false, viewController: TiletViewController.self), MenuComponent(title: "TitleLockup", completed: true, viewController: TitleLockupViewController.self), MenuComponent(title: "Toggle", completed: true, viewController: ToggleViewController.self) ] override func numberOfSections(in tableView: UITableView) -> Int { 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { items.count } override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 50.0 } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { guard items.count > indexPath.row, let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? MenuCell else { return UITableViewCell() } let item = items[indexPath.row] cell.label.text = item.title cell.completed = item.completed cell.accessoryType = .disclosureIndicator return cell } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { guard items.count > indexPath.row else { return } let item = items[indexPath.row] if let split = splitViewController { if let type = item.viewController as? StoryboardInitable.Type { let viewController = type.instantiate() viewController.title = item.title split.showDetailViewController(viewController, sender: nil) } else if let type = item.viewController as? Initable.Type { if let viewController = type.init() as? UIViewController { viewController.title = item.title split.showDetailViewController(viewController, sender: nil) } } } else { if let type = item.viewController as? StoryboardInitable.Type { let viewController = type.instantiate() viewController.title = item.title self.navigationController?.pushViewController(viewController, animated: true) } else if let type = item.viewController as? Initable.Type { if let viewController = type.init() as? UIViewController { viewController.title = item.title self.navigationController?.pushViewController(viewController, animated: true) } } } } }