// // TuskerNavigationDelegate.swift // Tusker // // Created by Shadowfacts on 9/30/18. // Copyright © 2018 Shadowfacts. All rights reserved. // import UIKit import SafariServices import Pachyderm protocol TuskerNavigationDelegate { func selected(account accountID: String) func selected(mention: Mention) func selected(tag: Hashtag) func selected(url: URL) func selected(status statusID: String) func reply(to statusID: String) func showLargeImage(_ image: UIImage, description: String?, animatingFrom originView: UIView) func showMoreOptions(forStatus statusID: String) } extension TuskerNavigationDelegate where Self: UIViewController { func selected(account accountID: String) { // don't open if the account is the same as the current one if let profileController = self as? ProfileTableViewController, profileController.accountID == accountID { return } guard let navigationController = navigationController else { fatalError("Can't show profile VC when not in navigation controller") } let vc = ProfileTableViewController.create(for: accountID) navigationController.pushViewController(vc, animated: true) } func selected(mention: Mention) { guard let navigationController = navigationController else { fatalError("Can't show profile VC from mention when not in navigation controller") } let vc = ProfileTableViewController.create(for: mention.id) navigationController.pushViewController(vc, animated: true) } func selected(tag: Hashtag) { guard let navigationController = navigationController else { fatalError("Can't show hashtag timeline when not in navigation controller") } let timeline = Timeline.tag(hashtag: tag.name) let vc = TimelineTableViewController.create(for: timeline) navigationController.pushViewController(vc, animated: true) } func selected(url: URL) { let vc = SFSafariViewController(url: url) present(vc, animated: true) } func selected(status statusID: String) { // don't open if the conversation is the same as the current one if let conversationController = self as? ConversationViewController, conversationController.mainStatusID == statusID { return } guard let navigationController = navigationController else { fatalError("Can't show conversation VC when not in navigation controller") } let vc = ConversationViewController.create(for: statusID) navigationController.pushViewController(vc, animated: true) } func reply(to statusID: String) { let vc = ComposeViewController.create(inReplyTo: statusID) present(vc, animated: true) } func showLargeImage(_ image: UIImage, description: String?, animatingFrom originView: UIView) { guard let self = self as? UIViewController & LargeImageViewControllerDelegate else { return } let vc = LargeImageViewController.create(image: image, description: description) vc.delegate = self var frame = originView.convert(originView.bounds, to: view) if let scrollView = view as? UIScrollView { let scale = scrollView.zoomScale let width = frame.width * scale let height = frame.height * scale let x = frame.minX * scale - scrollView.contentOffset.x + scrollView.frame.minX let y = frame.minY * scale - scrollView.contentOffset.y + scrollView.frame.minY frame = CGRect(x: x, y: y, width: width, height: height) } vc.originFrame = frame vc.originCornerRadius = originView.layer.cornerRadius vc.transitioningDelegate = self present(vc, animated: true) } func showMoreOptions(forStatus statusID: String) { 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: "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)) present(alert, animated: true) } }