// // DraftsTableViewController.swift // Tusker // // Created by Shadowfacts on 10/22/18. // Copyright © 2018 Shadowfacts. All rights reserved. // import UIKit protocol DraftsTableViewControllerDelegate: class { func draftSelectionCanceled() func shouldSelectDraft(_ draft: DraftsManager.Draft, completion: @escaping (Bool) -> Void) func draftSelected(_ draft: DraftsManager.Draft) func draftSelectionCompleted() } class DraftsTableViewController: UITableViewController { let account: LocalData.UserAccountInfo weak var delegate: DraftsTableViewControllerDelegate? var drafts = [DraftsManager.Draft]() init(account: LocalData.UserAccountInfo) { self.account = account 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.rowHeight = UITableView.automaticDimension tableView.estimatedRowHeight = 140 tableView.register(UINib(nibName: "DraftTableViewCell", bundle: nil), forCellReuseIdentifier: "draftCell") drafts = DraftsManager.shared.sorted.filter { (draft) in draft.accountID == account.id } } func draft(for indexPath: IndexPath) -> DraftsManager.Draft { return drafts[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 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) { let draft = self.draft(for: indexPath) func select() { delegate?.draftSelected(draft) dismiss(animated: true) { self.delegate?.draftSelectionCompleted() } } if let delegate = delegate { delegate.shouldSelectDraft(draft) { (shouldSelect) in if shouldSelect { select() } else { tableView.selectRow(at: nil, animated: true, scrollPosition: .none) } } } else { select() } } override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle { return .delete } override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { return true } override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { guard editingStyle == .delete else { return } DraftsManager.shared.remove(draft(for: indexPath)) tableView.deleteRows(at: [indexPath], with: .automatic) } // MARK: - Interaction @objc func cancelPressed() { delegate?.draftSelectionCanceled() dismiss(animated: true) } }