2018-11-10 16:48:09 +00:00
|
|
|
//
|
|
|
|
// EnhancedTableViewController.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 11/10/18.
|
|
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2019-06-04 21:04:37 +00:00
|
|
|
import SafariServices
|
2018-11-10 16:48:09 +00:00
|
|
|
|
|
|
|
class EnhancedTableViewController: UITableViewController {
|
|
|
|
|
|
|
|
var prevScrollToTopOffset: CGPoint? = nil
|
|
|
|
|
|
|
|
private var topOffset: CGPoint {
|
|
|
|
// when scrolled to top, the content offset is negative the height of the UI above the scroll view (i.e. the nav and status bars)
|
2019-08-06 03:08:00 +00:00
|
|
|
let windowScene = view.window!.windowScene!
|
2019-06-04 17:31:05 +00:00
|
|
|
let barOffset = -1 * (navigationController!.navigationBar.frame.height + windowScene.statusBarManager!.statusBarFrame.height)
|
2018-11-10 16:48:09 +00:00
|
|
|
// add one so it's not technically all the way at the top, and scrollViewWShouldScrollToTop is still called to trigger undo
|
|
|
|
return CGPoint(x: 0, y: barOffset + 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
override func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {
|
|
|
|
if let offset = prevScrollToTopOffset {
|
|
|
|
tableView.setContentOffset(offset, animated: true)
|
|
|
|
prevScrollToTopOffset = nil
|
|
|
|
} else {
|
|
|
|
prevScrollToTopOffset = tableView.contentOffset
|
|
|
|
tableView.setContentOffset(topOffset, animated: true)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
|
|
|
|
prevScrollToTopOffset = nil
|
|
|
|
}
|
2019-12-14 16:59:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
if let cell = tableView.cellForRow(at: indexPath) as? SelectableTableViewCell {
|
|
|
|
cell.didSelectCell()
|
|
|
|
}
|
|
|
|
}
|
2018-11-10 16:48:09 +00:00
|
|
|
|
|
|
|
}
|
2019-06-04 21:04:37 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2019-11-17 15:19:50 +00:00
|
|
|
let actionProvider: UIContextMenuActionProvider = { (_) in
|
2019-11-17 23:49:48 +00:00
|
|
|
let suggested = self.getSuggestedContextMenuActions(tableView: tableView, indexPath: indexPath, point: point)
|
|
|
|
return UIMenu(title: "", image: nil, identifier: nil, options: [], children: suggested + actionsProvider())
|
2019-06-04 21:04:37 +00:00
|
|
|
}
|
|
|
|
return UIContextMenuConfiguration(identifier: nil, previewProvider: previewProvider, actionProvider: actionProvider)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-17 23:49:48 +00:00
|
|
|
// todo: replace this with the UIKit suggested actions, if possible
|
|
|
|
@objc open func getSuggestedContextMenuActions(tableView: UITableView, indexPath: IndexPath, point: CGPoint) -> [UIAction] {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
2019-09-14 20:48:43 +00:00
|
|
|
override func tableView(_ tableView: UITableView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) {
|
|
|
|
if let viewController = animator.previewViewController {
|
|
|
|
animator.preferredCommitStyle = .pop
|
2019-06-04 21:04:37 +00:00
|
|
|
animator.addCompletion {
|
2019-09-14 20:48:43 +00:00
|
|
|
if viewController is LargeImageViewController || viewController is GalleryViewController || viewController is SFSafariViewController {
|
2019-06-04 21:04:37 +00:00
|
|
|
self.present(viewController, animated: true)
|
|
|
|
} else {
|
|
|
|
self.show(viewController, sender: nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|