Tusker/Tusker/AppRouter.swift

140 lines
5.8 KiB
Swift

//
// 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
init(root: UIViewController) {
self.rootViewController = root
}
private func getTopViewController(parent vc: UIViewController) -> UIViewController {
if let vc = vc as? UINavigationController,
let top = vc.topViewController {
return getTopViewController(parent: top)
} else if let vc = vc as? UITabBarController,
let selected = vc.selectedViewController {
return getTopViewController(parent: selected)
} else if let presented = vc.presentedViewController {
return getTopViewController(parent: presented)
} else {
return vc
}
}
private func getNavController(parent vc: UIViewController) -> UINavigationController {
if let vc = vc as? UINavigationController {
return vc
} else if let vc = vc as? UITabBarController,
let selected = vc.selectedViewController {
return getNavController(parent: selected)
} else {
fatalError()
}
}
func present(_ vc: UIViewController, animated: Bool) {
let top = getTopViewController(parent: rootViewController)
top.present(vc, animated: animated)
}
func push(_ vc: UIViewController, animated: Bool) {
let nav = getNavController(parent: rootViewController)
nav.pushViewController(vc, animated: true)
}
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 drafts() -> DraftsTableViewController {
return DraftsTableViewController()
}
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 largeImage(gifData: Data, description: String?, sourceFrame: CGRect, sourceCornerRadius: CGFloat, transitioningDelegate: UIViewControllerTransitioningDelegate?) -> LargeImageViewController {
let vc = LargeImageViewController(image: UIImage(data: gifData)!, description: description, sourceFrame: sourceFrame, sourceCornerRadius: sourceCornerRadius, router: self)
vc.gifData = gifData
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
}
}