79 lines
3.5 KiB
Swift
79 lines
3.5 KiB
Swift
//
|
|
// 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])
|
|
|
|
var navigationDelegate: TuskerNavigationDelegate? { get }
|
|
|
|
func getPreviewProviders(for location: CGPoint, sourceViewController: UIViewController) -> PreviewProviders?
|
|
|
|
}
|
|
|
|
extension MenuPreviewProvider {
|
|
|
|
private var mastodonController: MastodonController? { navigationDelegate?.apiController }
|
|
|
|
func actionsForProfile(accountID: String, sourceView: UIView?) -> [UIAction] {
|
|
guard let mastodonController = mastodonController,
|
|
let account = mastodonController.cache.account(for: accountID) else { return [] }
|
|
return [
|
|
createAction(identifier: "sendmessage", title: "Send Message", systemImageName: "envelope", handler: { (_) in
|
|
self.navigationDelegate?.compose(mentioning: account.acct)
|
|
}),
|
|
createAction(identifier: "openinsafari", title: "Open in Safari", systemImageName: "safari", handler: { (_) in
|
|
self.navigationDelegate?.selected(url: account.url)
|
|
}),
|
|
createAction(identifier: "share", title: "Share...", systemImageName: "square.and.arrow.up", handler: { (_) in
|
|
self.navigationDelegate?.showMoreOptions(forAccount: accountID, sourceView: sourceView)
|
|
})
|
|
]
|
|
}
|
|
|
|
func actionsForURL(_ url: URL, sourceView: UIView?) -> [UIAction] {
|
|
return [
|
|
createAction(identifier: "openinsafari", title: "Open in Safari", systemImageName: "safari", handler: { (_) in
|
|
self.navigationDelegate?.selected(url: url)
|
|
}),
|
|
createAction(identifier: "share", title: "Share...", systemImageName: "square.and.arrow.up", handler: { (_) in
|
|
self.navigationDelegate?.showMoreOptions(forURL: url, sourceView: sourceView)
|
|
})
|
|
]
|
|
}
|
|
|
|
func actionsForHashtag(_ hashtag: Hashtag, sourceView: UIView?) -> [UIAction] {
|
|
return actionsForURL(hashtag.url, sourceView: sourceView)
|
|
}
|
|
|
|
func actionsForStatus(statusID: String, sourceView: UIView?) -> [UIAction] {
|
|
guard let mastodonController = mastodonController,
|
|
let status = mastodonController.cache.status(for: statusID) else { return [] }
|
|
return [
|
|
createAction(identifier: "reply", title: "Reply", systemImageName: "arrowshape.turn.up.left", handler: { (_) in
|
|
self.navigationDelegate?.reply(to: statusID)
|
|
}),
|
|
createAction(identifier: "openinsafari", title: "Open in Safari", systemImageName: "safari", handler: { (_) in
|
|
self.navigationDelegate?.selected(url: status.url!)
|
|
}),
|
|
createAction(identifier: "share", title: "Share...", systemImageName: "square.and.arrow.up", handler: { (_) in
|
|
self.navigationDelegate?.showMoreOptions(forStatus: statusID, sourceView: sourceView)
|
|
})
|
|
]
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
}
|