// // PreviewViewControllerProvider.swift // Tusker // // Created by Shadowfacts on 10/10/18. // Copyright © 2018 Shadowfacts. All rights reserved. // import UIKit import SafariServices import Pachyderm protocol MenuPreviewProvider { typealias PreviewProviders = (content: UIContextMenuContentPreviewProvider, actions: () -> [UIAction]) func getPreviewProviders(for location: CGPoint, sourceViewController: UIViewController) -> PreviewProviders? } extension MenuPreviewProvider { fileprivate func present(_ vc: UIViewController) { UIApplication.shared.keyWindow!.rootViewController!.present(vc, animated: true) } func actionsForProfile(accountID: String) -> [UIAction] { guard let account = MastodonCache.account(for: accountID) else { return [] } return [ createAction(identifier: "openinsafari", title: "Open in Safari", systemImageName: "safari", handler: { (_) in self.present(SFSafariViewController(url: account.url)) }), createAction(identifier: "sendmessage", title: "Send Message", systemImageName: "envelope", handler: { (_) in self.present(UINavigationController(rootViewController: ComposeViewController(mentioningAcct: account.acct))) }), createAction(identifier: "share", title: "Share...", systemImageName: "square.and.arrow.up", handler: { (_) in self.present(UIActivityViewController(activityItems: [account.url], applicationActivities: nil)) }) ] } func actionsForURL(_ url: URL) -> [UIAction] { return [ createAction(identifier: "openinsafari", title: "Open in Safari", systemImageName: "safari", handler: { (_) in self.present(SFSafariViewController(url: url)) }), createAction(identifier: "share", title: "Share...", systemImageName: "square.and.arrow.up", handler: { (_) in self.present(UIActivityViewController(activityItems: [url], applicationActivities: nil)) }) ] } func actionsForHashtag(_ hashtag: Hashtag) -> [UIAction] { return actionsForURL(hashtag.url) } func actionsForStatus(statusID: String) -> [UIAction] { guard let status = MastodonCache.status(for: statusID) else { return [] } return [ createAction(identifier: "reply", title: "Reply", systemImageName: "arrowshape.turn.up.left", handler: { (_) in self.present(UINavigationController(rootViewController: ComposeViewController(inReplyTo: statusID))) }), createAction(identifier: "openinsafari", title: "Open in Safari", systemImageName: "safari", handler: { (_) in self.present(SFSafariViewController(url: status.url!)) }), createAction(identifier: "share", title: "Share...", systemImageName: "square.and.arrow.up", handler: { (_) in self.present(UIActivityViewController(activityItems: [status.url!], applicationActivities: nil)) }) ] } private func createAction(identifier: String, title: String, systemImageName: String, handler: @escaping UIActionHandler) -> UIAction { return UIAction(title: title, image: UIImage(systemName: systemImageName), identifier: UIAction.Identifier(identifier), discoverabilityTitle: nil, attributes: [], state: .off, handler: handler) } }