85 lines
3.3 KiB
Swift
85 lines
3.3 KiB
Swift
//
|
|
// PreviewViewControllerProvider.swift
|
|
// Tusker
|
|
//
|
|
// Created by Shadowfacts on 10/10/18.
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
enum PreviewCommitType {
|
|
case nav
|
|
case modal
|
|
}
|
|
|
|
protocol PreviewViewControllerProvider {
|
|
|
|
func getPreviewViewController(forLocation location: CGPoint, sourceViewController: UIViewController) -> UIViewController?
|
|
|
|
func getPreviewCommitType(forViewController viewController: UIViewController) -> PreviewCommitType
|
|
|
|
}
|
|
|
|
extension PreviewViewControllerProvider {
|
|
func getPreviewCommitType(forViewController viewController: UIViewController) -> PreviewCommitType {
|
|
return viewController is LargeImageViewController ? .modal : .nav
|
|
}
|
|
}
|
|
|
|
@objc extension UITableViewController: UIViewControllerPreviewingDelegate {
|
|
public func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
|
|
if let indexPath = tableView.indexPathForRow(at: location),
|
|
let cell = tableView.cellForRow(at: indexPath) as? UITableViewCell & PreviewViewControllerProvider {
|
|
let cellLocation = cell.convert(location, from: tableView)
|
|
if let vc = cell.getPreviewViewController(forLocation: cellLocation, sourceViewController: self) {
|
|
previewingContext.sourceRect = tableView.rectForRow(at: indexPath)
|
|
return vc
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
public func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
|
|
if viewControllerToCommit is LargeImageViewController {
|
|
present(viewControllerToCommit, animated: false)
|
|
} else {
|
|
navigationController!.pushViewController(viewControllerToCommit, animated: false)
|
|
}
|
|
}
|
|
}
|
|
|
|
//class PreviewingController: NSObject, UIViewControllerPreviewingDelegate {
|
|
//
|
|
// var currentCommitType: PreviewCommitType?
|
|
// var owner: UIViewController?
|
|
//
|
|
// init(owner: UIViewController) {
|
|
// self.owner = owner
|
|
// }
|
|
//
|
|
// func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
|
|
// if let owner = owner as? UITableViewController,
|
|
// let indexPath = owner.tableView.indexPathForRow(at: location),
|
|
// let cell = owner.tableView.cellForRow(at: indexPath) as? UITableViewCell & PreviewViewControllerProvider {
|
|
// let cellLocation = cell.convert(location, from: owner.tableView)
|
|
// if let vc = cell.getPreviewViewController(forLocation: cellLocation, sourceViewController: owner) {
|
|
// currentCommitType = cell.getPreviewCommitType(forViewController: vc)
|
|
// previewingContext.sourceRect = owner.tableView.rectForRow(at: indexPath)
|
|
// return vc
|
|
// }
|
|
// }
|
|
// return nil
|
|
// }
|
|
//
|
|
// func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
|
|
// switch currentCommitType ?? .nav {
|
|
// case .modal:
|
|
// owner?.present(viewControllerToCommit, animated: false)
|
|
// case .nav:
|
|
// owner?.navigationController!.pushViewController(viewControllerToCommit, animated: false)
|
|
// }
|
|
// }
|
|
//
|
|
//}
|