Tusker/Tusker/TuskerNavigationDelegate.swift

182 lines
7.0 KiB
Swift
Raw Normal View History

//
// TuskerNavigationDelegate.swift
// Tusker
//
// Created by Shadowfacts on 9/30/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import UIKit
import SafariServices
import Pachyderm
protocol TuskerNavigationDelegate {
2018-10-12 01:20:58 +00:00
func viewController(forAccount accountID: String) -> UIViewController
func selected(account accountID: String)
2018-10-12 01:20:58 +00:00
func viewController(forMention mention: Mention) -> UIViewController
func selected(mention: Mention)
2018-10-12 01:20:58 +00:00
func viewController(forTag tag: Hashtag) -> UIViewController
func selected(tag: Hashtag)
2018-10-12 01:20:58 +00:00
func viewController(forURL url: URL) -> UIViewController
func selected(url: URL)
2018-10-12 01:20:58 +00:00
func viewController(forStatus statusID: String) -> UIViewController
func selected(status statusID: String)
2018-10-20 16:03:18 +00:00
func compose()
func reply(to statusID: String)
2018-10-12 01:20:58 +00:00
func viewController(forImage image: UIImage, description: String?, animatingFrom originView: UIView) -> UIViewController
func showLargeImage(_ image: UIImage, description: String?, animatingFrom originView: UIView)
func showMoreOptions(forStatus statusID: String)
2018-10-12 02:04:32 +00:00
func showMoreOptions(forURL url: URL)
}
extension TuskerNavigationDelegate where Self: UIViewController {
2018-10-12 01:20:58 +00:00
func viewController(forAccount accountID: String) -> UIViewController {
2018-10-20 16:03:18 +00:00
return ProfileTableViewController(accountID: accountID)
2018-10-12 01:20:58 +00:00
}
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")
}
2018-10-12 01:20:58 +00:00
let vc = viewController(forAccount: accountID)
navigationController.pushViewController(vc, animated: true)
}
2018-10-12 01:20:58 +00:00
func viewController(forMention mention: Mention) -> UIViewController {
2018-10-20 16:03:18 +00:00
return ProfileTableViewController(accountID: mention.id)
2018-10-12 01:20:58 +00:00
}
func selected(mention: Mention) {
2018-10-01 01:12:23 +00:00
guard let navigationController = navigationController else {
fatalError("Can't show profile VC from mention when not in navigation controller")
}
2018-10-12 01:20:58 +00:00
let vc = viewController(forMention: mention)
2018-10-01 01:12:23 +00:00
navigationController.pushViewController(vc, animated: true)
}
2018-10-12 01:20:58 +00:00
func viewController(forTag tag: Hashtag) -> UIViewController {
let timeline = Timeline.tag(hashtag: tag.name)
2018-10-20 16:03:18 +00:00
return TimelineTableViewController(for: timeline)
2018-10-12 01:20:58 +00:00
}
func selected(tag: Hashtag) {
guard let navigationController = navigationController else {
fatalError("Can't show hashtag timeline when not in navigation controller")
}
2018-10-12 01:20:58 +00:00
let vc = viewController(forTag: tag)
navigationController.pushViewController(vc, animated: true)
}
2018-10-12 01:20:58 +00:00
func viewController(forURL url: URL) -> UIViewController {
return SFSafariViewController(url: url)
}
func selected(url: URL) {
2018-10-12 01:20:58 +00:00
let vc = viewController(forURL: url)
present(vc, animated: true)
}
2018-10-12 01:20:58 +00:00
func viewController(forStatus statusID: String) -> UIViewController {
2018-10-20 16:03:18 +00:00
return ConversationTableViewController(for: statusID)
2018-10-12 01:20:58 +00:00
}
func selected(status statusID: String) {
// don't open if the conversation is the same as the current one
2018-10-20 16:03:18 +00:00
if let conversationController = self as? ConversationTableViewController,
conversationController.mainStatusID == statusID {
return
}
guard let navigationController = navigationController else {
fatalError("Can't show conversation VC when not in navigation controller")
}
2018-10-12 01:20:58 +00:00
let vc = viewController(forStatus: statusID)
navigationController.pushViewController(vc, animated: true)
}
2018-10-20 16:03:18 +00:00
func compose() {
let vc = UINavigationController(rootViewController: ComposeViewController())
present(vc, animated: true)
}
func reply(to statusID: String) {
2018-10-20 16:03:18 +00:00
let vc = UINavigationController(rootViewController: ComposeViewController(inReplyTo: statusID))
present(vc, animated: true)
}
2018-10-12 01:20:58 +00:00
func viewController(forImage image: UIImage, description: String?, animatingFrom originView: UIView) -> UIViewController {
guard let self = self as? UIViewController & LargeImageViewControllerDelegate else {
fatalError("Can't create large image view controller unless self is LargeImageViewControllerDelegate")
}
2018-10-20 16:03:18 +00:00
let vc = LargeImageViewController(image: image, description: description, sourceView: originView, sourceViewController: self)
2018-10-12 01:20:58 +00:00
vc.delegate = self
return vc
}
func showLargeImage(_ image: UIImage, description: String?, animatingFrom originView: UIView) {
let vc = viewController(forImage: image, description: description, animatingFrom: originView)
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 {
2018-10-12 02:04:32 +00:00
alert.addAction(UIAlertAction(title: "Open in Safari", style: .default, handler: { (_) in
let vc = SFSafariViewController(url: url)
self.present(vc, animated: true)
}))
2018-10-12 02:04:32 +00:00
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))
present(alert, animated: true)
}
2018-10-12 02:04:32 +00:00
func showMoreOptions(forURL url: URL) {
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))
present(alert, animated: true)
}
}