// // DraftsTableViewController.swift // Tusker // // Created by Shadowfacts on 10/22/18. // Copyright © 2018 Shadowfacts. All rights reserved. // import UIKit protocol DraftsTableViewControllerDelegate { func draftSelectionCanceled() func draftSelected(_ draft: DraftsManager.Draft) } class DraftsTableViewController: UITableViewController { var delegate: DraftsTableViewControllerDelegate? init() { super.init(nibName: "DraftsTableViewController", bundle: nil) title = "Drafts" navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancelPressed)) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewDidLoad() { super.viewDidLoad() tableView.register(UINib(nibName: "DraftTableViewCell", bundle: nil), forCellReuseIdentifier: "draftCell") } func draft(for indexPath: IndexPath) -> DraftsManager.Draft { return DraftsManager.shared.sorted[indexPath.row] } // MARK: - Table View Data Source override func numberOfSections(in tableView: UITableView) -> Int { return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return DraftsManager.shared.drafts.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { guard let cell = tableView.dequeueReusableCell(withIdentifier: "draftCell", for: indexPath) as? DraftTableViewCell else { fatalError() } cell.updateUI(for: draft(for: indexPath)) return cell } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { delegate?.draftSelected(draft(for: indexPath)) dismiss(animated: true) } @objc func cancelPressed() { delegate?.draftSelectionCanceled() dismiss(animated: true) } }