2018-09-30 23:29:52 +00:00
|
|
|
//
|
|
|
|
// TuskerNavigationDelegate.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 9/30/18.
|
|
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import SafariServices
|
|
|
|
import Pachyderm
|
|
|
|
|
2020-08-15 21:47:33 +00:00
|
|
|
protocol TuskerNavigationDelegate: UIViewController {
|
2020-01-05 20:25:07 +00:00
|
|
|
var apiController: MastodonController { get }
|
2018-09-30 23:29:52 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 21:47:33 +00:00
|
|
|
extension TuskerNavigationDelegate {
|
2018-09-30 23:29:52 +00:00
|
|
|
|
2019-09-07 21:10:58 +00:00
|
|
|
func show(_ vc: UIViewController) {
|
2020-01-18 21:00:38 +00:00
|
|
|
if vc is LargeImageViewController || vc is GalleryViewController || vc is SFSafariViewController {
|
|
|
|
present(vc, animated: true)
|
|
|
|
} else {
|
|
|
|
show(vc, sender: self)
|
|
|
|
}
|
2019-09-07 21:10:58 +00:00
|
|
|
}
|
|
|
|
|
2018-09-30 23:29:52 +00:00
|
|
|
func selected(account accountID: String) {
|
|
|
|
// don't open if the account is the same as the current one
|
2020-07-05 20:17:56 +00:00
|
|
|
if let profileController = self as? ProfileViewController,
|
2018-09-30 23:29:52 +00:00
|
|
|
profileController.accountID == accountID {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-05 20:17:56 +00:00
|
|
|
show(ProfileViewController(accountID: accountID, mastodonController: apiController), sender: self)
|
2018-10-12 01:20:58 +00:00
|
|
|
}
|
|
|
|
|
2018-09-30 23:29:52 +00:00
|
|
|
func selected(mention: Mention) {
|
2020-07-05 20:17:56 +00:00
|
|
|
show(ProfileViewController(accountID: mention.id, mastodonController: apiController), sender: self)
|
2018-10-12 01:20:58 +00:00
|
|
|
}
|
|
|
|
|
2018-09-30 23:29:52 +00:00
|
|
|
func selected(tag: Hashtag) {
|
2020-01-05 20:25:07 +00:00
|
|
|
show(HashtagTimelineViewController(for: tag, mastodonController: apiController), sender: self)
|
2018-10-12 01:20:58 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 21:48:58 +00:00
|
|
|
func selected(url: URL, allowUniversalLinks: Bool = true) {
|
2019-11-15 00:53:27 +00:00
|
|
|
func openSafari() {
|
|
|
|
if Preferences.shared.useInAppSafari {
|
|
|
|
let config = SFSafariViewController.Configuration()
|
|
|
|
config.entersReaderIfAvailable = Preferences.shared.inAppSafariAutomaticReaderMode
|
|
|
|
present(SFSafariViewController(url: url, configuration: config), animated: true)
|
|
|
|
} else {
|
|
|
|
UIApplication.shared.open(url, options: [:])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-15 21:48:58 +00:00
|
|
|
if allowUniversalLinks && Preferences.shared.openLinksInApps {
|
2019-02-22 15:20:22 +00:00
|
|
|
UIApplication.shared.open(url, options: [.universalLinksOnly: true]) { (success) in
|
|
|
|
if (!success) {
|
2019-11-15 00:53:27 +00:00
|
|
|
openSafari()
|
2019-02-22 15:20:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2019-11-15 00:53:27 +00:00
|
|
|
openSafari()
|
2019-02-22 15:20:22 +00:00
|
|
|
}
|
2018-10-12 01:20:58 +00:00
|
|
|
}
|
|
|
|
|
2018-09-30 23:29:52 +00:00
|
|
|
func selected(status statusID: String) {
|
2019-11-28 23:36:58 +00:00
|
|
|
self.selected(status: statusID, state: .unknown)
|
|
|
|
}
|
|
|
|
|
|
|
|
func selected(status statusID: String, state: StatusState) {
|
|
|
|
// todo: is this necessary? should the conversation main status cell prevent this
|
2018-09-30 23:29:52 +00:00
|
|
|
// 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,
|
2018-09-30 23:29:52 +00:00
|
|
|
conversationController.mainStatusID == statusID {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-05 20:25:07 +00:00
|
|
|
show(ConversationTableViewController(for: statusID, state: state, mastodonController: apiController), sender: self)
|
2018-09-30 23:29:52 +00:00
|
|
|
}
|
|
|
|
|
2020-09-01 01:39:36 +00:00
|
|
|
func compose(editing draft: Draft) {
|
2020-08-31 23:28:50 +00:00
|
|
|
let compose = ComposeHostingController(draft: draft, mastodonController: apiController)
|
|
|
|
|
2019-06-13 21:54:55 +00:00
|
|
|
let vc = UINavigationController(rootViewController: compose)
|
|
|
|
vc.presentationController?.delegate = compose
|
2019-01-19 19:31:31 +00:00
|
|
|
present(vc, animated: true)
|
2018-10-21 02:07:04 +00:00
|
|
|
}
|
2019-01-19 19:31:31 +00:00
|
|
|
|
2020-09-01 01:39:36 +00:00
|
|
|
func compose(inReplyToID: String? = nil, mentioningAcct: String? = nil) {
|
|
|
|
let draft = apiController.createDraft(inReplyToID: inReplyToID, mentioningAcct: mentioningAcct)
|
|
|
|
compose(editing: draft)
|
|
|
|
}
|
|
|
|
|
2020-03-18 01:42:09 +00:00
|
|
|
func loadingLargeImage(url: URL, cache: ImageCache, description: String?, animatingFrom sourceView: UIImageView) -> LoadingLargeImageViewController {
|
|
|
|
let vc = LoadingLargeImageViewController(url: url, cache: cache, imageDescription: description)
|
2020-03-26 03:10:48 +00:00
|
|
|
vc.animationSourceView = sourceView
|
2020-03-18 01:42:09 +00:00
|
|
|
vc.transitioningDelegate = self
|
|
|
|
return vc
|
|
|
|
}
|
|
|
|
|
|
|
|
func showLoadingLargeImage(url: URL, cache: ImageCache, description: String?, animatingFrom sourceView: UIImageView) {
|
|
|
|
present(loadingLargeImage(url: url, cache: cache, description: description, animatingFrom: sourceView), animated: true)
|
|
|
|
}
|
|
|
|
|
2019-09-10 16:25:50 +00:00
|
|
|
func gallery(attachments: [Attachment], sourceViews: [UIImageView?], startIndex: Int) -> GalleryViewController {
|
2020-03-26 03:10:48 +00:00
|
|
|
let vc = GalleryViewController(attachments: attachments, sourceViews: sourceViews, startIndex: startIndex)
|
2019-06-15 00:23:03 +00:00
|
|
|
vc.transitioningDelegate = self
|
|
|
|
return vc
|
|
|
|
}
|
|
|
|
|
2019-09-10 16:25:50 +00:00
|
|
|
func showGallery(attachments: [Attachment], sourceViews: [UIImageView?], startIndex: Int) {
|
2019-06-15 00:23:03 +00:00
|
|
|
present(gallery(attachments: attachments, sourceViews: sourceViews, startIndex: startIndex), animated: true)
|
|
|
|
}
|
|
|
|
|
2020-01-18 02:29:53 +00:00
|
|
|
private func moreOptions(forURL url: URL) -> UIActivityViewController {
|
2019-09-05 17:40:10 +00:00
|
|
|
let customActivites: [UIActivity] = [
|
|
|
|
OpenInSafariActivity()
|
|
|
|
]
|
|
|
|
let activityController = UIActivityViewController(activityItems: [url], applicationActivities: customActivites)
|
2020-09-13 19:51:06 +00:00
|
|
|
activityController.completionWithItemsHandler = OpenInSafariActivity.completionHandler(navigator: self, url: url)
|
2019-09-05 17:40:10 +00:00
|
|
|
return activityController
|
|
|
|
}
|
|
|
|
|
2020-01-18 02:29:53 +00:00
|
|
|
private func moreOptions(forStatus statusID: String) -> UIActivityViewController {
|
2020-05-02 23:52:35 +00:00
|
|
|
guard let status = apiController.persistentContainer.status(for: statusID) else { fatalError("Missing cached status \(statusID)") }
|
2019-01-19 19:31:31 +00:00
|
|
|
guard let url = status.url else { fatalError("Missing url for status \(statusID)") }
|
2020-05-15 02:00:58 +00:00
|
|
|
|
2020-06-27 04:22:14 +00:00
|
|
|
// on iOS 14+, all these custom actions are in the context menu and don't need to be in the share sheet
|
|
|
|
if #available(iOS 14.0, *) {
|
|
|
|
return UIActivityViewController(activityItems: [url, StatusActivityItemSource(status)], applicationActivities: nil)
|
|
|
|
} else {
|
|
|
|
var customActivites: [UIActivity] = [
|
|
|
|
OpenInSafariActivity(),
|
|
|
|
(status.bookmarked ?? false) ? UnbookmarkStatusActivity() : BookmarkStatusActivity(),
|
|
|
|
status.muted ? UnmuteConversationActivity() : MuteConversationActivity(),
|
|
|
|
]
|
|
|
|
|
|
|
|
if apiController.account != nil, status.account.id == apiController.account.id {
|
|
|
|
let pinned = status.pinned ?? false
|
|
|
|
customActivites.insert(pinned ? UnpinStatusActivity() : PinStatusActivity(), at: 1)
|
|
|
|
}
|
2020-05-02 23:52:35 +00:00
|
|
|
|
2020-06-27 04:22:14 +00:00
|
|
|
let activityController = UIActivityViewController(activityItems: [url, StatusActivityItemSource(status)], applicationActivities: customActivites)
|
2020-09-13 19:51:06 +00:00
|
|
|
activityController.completionWithItemsHandler = OpenInSafariActivity.completionHandler(navigator: self, url: url)
|
2020-06-27 04:22:14 +00:00
|
|
|
return activityController
|
2020-01-05 00:31:35 +00:00
|
|
|
}
|
2018-11-09 20:48:08 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 02:29:53 +00:00
|
|
|
private func moreOptions(forAccount accountID: String) -> UIActivityViewController {
|
2020-05-02 23:52:35 +00:00
|
|
|
guard let account = apiController.persistentContainer.account(for: accountID) else { fatalError("Missing cached account \(accountID)") }
|
2020-05-15 02:30:45 +00:00
|
|
|
|
2020-06-27 04:22:14 +00:00
|
|
|
if #available(iOS 14.0, *) {
|
|
|
|
return UIActivityViewController(activityItems: [account.url, AccountActivityItemSource(account)], applicationActivities: nil)
|
|
|
|
} else {
|
|
|
|
let customActivities: [UIActivity] = [
|
|
|
|
OpenInSafariActivity(),
|
|
|
|
]
|
|
|
|
|
|
|
|
let activityController = UIActivityViewController(activityItems: [account.url, AccountActivityItemSource(account)], applicationActivities: customActivities)
|
2020-09-13 19:51:06 +00:00
|
|
|
activityController.completionWithItemsHandler = OpenInSafariActivity.completionHandler(navigator: self, url: account.url)
|
2020-06-27 04:22:14 +00:00
|
|
|
return activityController
|
|
|
|
}
|
2019-12-14 18:36:05 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 02:29:53 +00:00
|
|
|
func showMoreOptions(forStatus statusID: String, sourceView: UIView?) {
|
|
|
|
let vc = moreOptions(forStatus: statusID)
|
|
|
|
vc.popoverPresentationController?.sourceView = sourceView
|
|
|
|
present(vc, animated: true)
|
2018-09-30 23:29:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 02:29:53 +00:00
|
|
|
func showMoreOptions(forURL url: URL, sourceView: UIView?) {
|
|
|
|
let vc = moreOptions(forURL: url)
|
|
|
|
vc.popoverPresentationController?.sourceView = sourceView
|
|
|
|
present(vc, animated: true)
|
2018-10-12 02:04:32 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 02:29:53 +00:00
|
|
|
func showMoreOptions(forAccount accountID: String, sourceView: UIView?) {
|
|
|
|
let vc = moreOptions(forAccount: accountID)
|
|
|
|
vc.popoverPresentationController?.sourceView = sourceView
|
|
|
|
present(vc, animated: true)
|
2019-12-14 18:36:05 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 02:30:58 +00:00
|
|
|
func showFollowedByList(accountIDs: [String]) {
|
2020-01-05 20:25:07 +00:00
|
|
|
let vc = AccountListTableViewController(accountIDs: accountIDs, mastodonController: apiController)
|
2019-09-06 02:30:58 +00:00
|
|
|
vc.title = NSLocalizedString("Followed By", comment: "followed by accounts list title")
|
|
|
|
show(vc, sender: self)
|
|
|
|
}
|
|
|
|
|
2019-11-28 23:36:58 +00:00
|
|
|
func statusActionAccountList(action: StatusActionAccountListTableViewController.ActionType, statusID: String, statusState state: StatusState, accountIDs: [String]?) -> StatusActionAccountListTableViewController {
|
2020-01-05 20:25:07 +00:00
|
|
|
return StatusActionAccountListTableViewController(actionType: action, statusID: statusID, statusState: state, accountIDs: accountIDs, mastodonController: apiController)
|
2019-09-06 03:16:45 +00:00
|
|
|
}
|
|
|
|
|
2018-09-30 23:29:52 +00:00
|
|
|
}
|