// // EnhancedTableViewController.swift // Tusker // // Created by Shadowfacts on 11/10/18. // Copyright © 2018 Shadowfacts. All rights reserved. // import UIKit import SafariServices class EnhancedTableViewController: UITableViewController { private var prevScrollToTopOffset: CGPoint? = nil private(set) var isCurrentlyScrollingToTop = false private var prevScrollViewContentOffset: CGPoint? private(set) var scrollViewDirection: CGFloat = 0 // MARK: Scroll View Delegate override func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool { if let offset = prevScrollToTopOffset { tableView.setContentOffset(offset, animated: true) prevScrollToTopOffset = nil return false } else { prevScrollToTopOffset = tableView.contentOffset isCurrentlyScrollingToTop = true return true } } override func scrollViewDidScrollToTop(_ scrollView: UIScrollView) { isCurrentlyScrollingToTop = false // add one so it's not technically scrolled all the way to the top, // otherwise there's no way of detecting a status bar press to scroll back down tableView.contentOffset.y -= 0.5 } override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) { prevScrollToTopOffset = nil } override func scrollViewDidScroll(_ scrollView: UIScrollView) { if let prev = prevScrollViewContentOffset { scrollViewDirection = scrollView.contentOffset.y - prev.y } prevScrollViewContentOffset = scrollView.contentOffset } // MARK: Table View Delegate override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if let cell = tableView.cellForRow(at: indexPath) as? SelectableTableViewCell { cell.didSelectCell() } } } extension EnhancedTableViewController { override func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? { if let cell = tableView.cellForRow(at: indexPath) as? UITableViewCell & MenuPreviewProvider { let cellLocation = cell.convert(point, from: tableView) guard let (previewProvider, actionsProvider) = cell.getPreviewProviders(for: cellLocation, sourceViewController: self) else { return nil } let actionProvider: UIContextMenuActionProvider = { (_) in let suggested = self.getSuggestedContextMenuActions(tableView: tableView, indexPath: indexPath, point: point) return UIMenu(title: "", image: nil, identifier: nil, options: [], children: suggested + actionsProvider()) } return UIContextMenuConfiguration(identifier: nil, previewProvider: previewProvider, actionProvider: actionProvider) } else { return nil } } // todo: replace this with the UIKit suggested actions, if possible @objc open func getSuggestedContextMenuActions(tableView: UITableView, indexPath: IndexPath, point: CGPoint) -> [UIAction] { return [] } override func tableView(_ tableView: UITableView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) { if let viewController = animator.previewViewController { animator.preferredCommitStyle = .pop animator.addCompletion { if let customPresenting = viewController as? CustomPreviewPresenting { customPresenting.presentFromPreview(presenter: self) } else { self.show(viewController, sender: nil) } } } } } extension EnhancedTableViewController: TabBarScrollableViewController { func tabBarScrollToTop() { if scrollViewShouldScrollToTop(tableView) { let topOffset = CGPoint(x: 0, y: -tableView.adjustedContentInset.top) tableView.setContentOffset(topOffset, animated: true) scrollViewDidScrollToTop(tableView) } } }