// // AppRouter.swift // Tusker // // Created by Shadowfacts on 10/20/18. // Copyright © 2018 Shadowfacts. All rights reserved. // import UIKit import Pachyderm import SafariServices class AppRouter { let rootViewController: UIViewController var currentViewController: UIViewController { return getContentViewController(from: rootViewController) } init(root: UIViewController) { self.rootViewController = root } private func getContentViewController(from vc: UIViewController) -> UIViewController { if let vc = vc as? UITabBarController, let selected = vc.selectedViewController { return getContentViewController(from: selected) } else if let vc = vc as? UINavigationController, let top = vc.topViewController { return getContentViewController(from: top) } else { return vc } } func present(_ vc: UIViewController, animated: Bool) { if currentViewController.presentingViewController != nil { currentViewController.dismiss(animated: animated) { self.present(vc, animated: animated) } } currentViewController.present(vc, animated: animated) } func push(_ vc: UIViewController, animated: Bool) { if currentViewController.presentingViewController != nil { currentViewController.dismiss(animated: animated) { self.push(vc, animated: animated) } } guard let nav = currentViewController.navigationController else { fatalError("Can't push view controller while not in navigation controller") } nav.pushViewController(vc, animated: animated) } func profile(for accountID: String) -> ProfileTableViewController { return ProfileTableViewController(accountID: accountID, router: self) } func profile(for mention: Mention) -> ProfileTableViewController { return ProfileTableViewController(accountID: mention.id, router: self) } func timeline(for timeline: Timeline) -> TimelineTableViewController { return TimelineTableViewController(for: timeline, router: self) } func timeline(tag: Hashtag) -> TimelineTableViewController { return timeline(for: .tag(hashtag: tag.name)) } func safariViewController(for url: URL) -> SFSafariViewController { return SFSafariViewController(url: url) } func conversation(for statusID: String) -> ConversationTableViewController { return ConversationTableViewController(for: statusID, router: self) } func compose(inReplyTo inReplyToID: String? = nil, mentioningAcct: String? = nil, text: String? = nil) -> ComposeViewController { return ComposeViewController(inReplyTo: inReplyToID, mentioningAcct: mentioningAcct, text: text, router: self) } func largeImage(_ image: UIImage, description: String?, sourceFrame: CGRect, sourceCornerRadius: CGFloat, transitioningDelegate: UIViewControllerTransitioningDelegate?) -> LargeImageViewController { let vc = LargeImageViewController(image: image, description: description, sourceFrame: sourceFrame, sourceCornerRadius: sourceCornerRadius, router: self) vc.transitioningDelegate = transitioningDelegate return vc } func moreOptions(forStatus statusID: String) -> UIAlertController { guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID)") } let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) if let url = status.url { alert.addAction(UIAlertAction(title: "Open in Safari", style: .default, handler: { (_) in let vc = SFSafariViewController(url: url) self.present(vc, animated: true) })) alert.addAction(UIAlertAction(title: "Copy", style: .default, handler: { (_) in UIPasteboard.general.url = url })) alert.addAction(UIAlertAction(title: "Share...", style: .default, handler: { (_) in let vc = UIActivityViewController(activityItems: [url], applicationActivities: nil) self.present(vc, animated: true) })) } alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) return alert } func moreOptions(forURL url: URL) -> UIAlertController { let alert = UIAlertController(title: nil, message: url.absoluteString, preferredStyle: .actionSheet) alert.addAction(UIAlertAction(title: "Open in Safari", style: .default, handler: { (_) in let vc = SFSafariViewController(url: url) self.present(vc, animated: true) })) alert.addAction(UIAlertAction(title: "Copy", style: .default, handler: { (_) in UIPasteboard.general.url = url })) alert.addAction(UIAlertAction(title: "Share...", style: .default, handler: { (_) in let vc = UIActivityViewController(activityItems: [url], applicationActivities: nil) self.present(vc, animated: true) })) alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) return alert } }