// // DraftsTableViewController.swift // Tusker // // Created by Shadowfacts on 10/22/18. // Copyright © 2018 Shadowfacts. All rights reserved. // import UIKit protocol DraftsTableViewControllerDelegate: AnyObject { func draftSelectionCanceled() func shouldSelectDraft(_ draft: Draft, completion: @escaping (Bool) -> Void) func draftSelected(_ draft: Draft) func draftSelectionCompleted() } class DraftsTableViewController: UITableViewController { let account: LocalData.UserAccountInfo let excludedDraft: Draft? weak var delegate: DraftsTableViewControllerDelegate? var drafts = [Draft]() init(account: LocalData.UserAccountInfo, exclude: Draft? = nil) { self.account = account self.excludedDraft = exclude 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") tableView.dragDelegate = self drafts = DraftsManager.shared.sorted.filter { (draft) in draft.accountID == account.id && draft != excludedDraft } } func draft(for indexPath: IndexPath) -> 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)) drafts.remove(at: indexPath.row) tableView.deleteRows(at: [indexPath], with: .automatic) } // MARK: - Interaction @objc func cancelPressed() { delegate?.draftSelectionCanceled() dismiss(animated: true) } } extension DraftsTableViewController: UITableViewDragDelegate { func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] { let draft = self.draft(for: indexPath) let activity = UserActivityManager.editDraftActivity(id: draft.id, accountID: account.id) let provider = NSItemProvider(object: activity) return [UIDragItem(itemProvider: provider)] } }