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
|
|
|
|
|
|
|
|
protocol TuskerNavigationDelegate {
|
2018-10-12 01:20:58 +00:00
|
|
|
|
2019-09-07 21:10:58 +00:00
|
|
|
func show(_ vc: UIViewController)
|
|
|
|
|
2018-10-21 02:07:04 +00:00
|
|
|
func selected(account accountID: String)
|
2018-10-12 01:20:58 +00:00
|
|
|
|
2018-09-30 23:29:52 +00:00
|
|
|
func selected(mention: Mention)
|
|
|
|
|
|
|
|
func selected(tag: Hashtag)
|
|
|
|
|
|
|
|
func selected(url: URL)
|
|
|
|
|
|
|
|
func selected(status statusID: String)
|
|
|
|
|
2019-11-28 23:36:58 +00:00
|
|
|
func selected(status statusID: String, state: StatusState)
|
|
|
|
|
2018-10-20 16:03:18 +00:00
|
|
|
func compose()
|
|
|
|
|
2019-12-14 18:36:05 +00:00
|
|
|
func compose(mentioning: String?)
|
|
|
|
|
2018-09-30 23:29:52 +00:00
|
|
|
func reply(to statusID: String)
|
|
|
|
|
2019-09-10 16:25:50 +00:00
|
|
|
func largeImage(_ image: UIImage, description: String?, sourceView: UIImageView) -> LargeImageViewController
|
2018-10-12 01:20:58 +00:00
|
|
|
|
2019-09-10 16:25:50 +00:00
|
|
|
func largeImage(gifData: Data, description: String?, sourceView: UIImageView) -> LargeImageViewController
|
2018-11-09 20:48:08 +00:00
|
|
|
|
2019-09-10 16:25:50 +00:00
|
|
|
func showLargeImage(_ image: UIImage, description: String?, animatingFrom sourceView: UIImageView)
|
2018-09-30 23:29:52 +00:00
|
|
|
|
2019-09-10 16:25:50 +00:00
|
|
|
func showLargeImage(gifData: Data, description: String?, animatingFrom sourceView: UIImageView)
|
2018-11-09 20:48:08 +00:00
|
|
|
|
2019-09-10 16:25:50 +00:00
|
|
|
func gallery(attachments: [Attachment], sourceViews: [UIImageView?], startIndex: Int) -> GalleryViewController
|
2019-06-15 00:23:03 +00:00
|
|
|
|
2019-09-10 16:25:50 +00:00
|
|
|
func showGallery(attachments: [Attachment], sourceViews: [UIImageView?], startIndex: Int)
|
2019-06-15 00:23:03 +00:00
|
|
|
|
2018-09-30 23:29:52 +00:00
|
|
|
func showMoreOptions(forStatus statusID: String)
|
2018-10-12 02:04:32 +00:00
|
|
|
|
2019-12-14 18:36:05 +00:00
|
|
|
func showMoreOptions(forAccount accountID: String)
|
|
|
|
|
2018-10-12 02:04:32 +00:00
|
|
|
func showMoreOptions(forURL url: URL)
|
2019-09-06 02:30:58 +00:00
|
|
|
|
|
|
|
func showFollowedByList(accountIDs: [String])
|
2019-09-06 03:16:45 +00:00
|
|
|
|
2019-11-28 23:36:58 +00:00
|
|
|
func statusActionAccountList(action: StatusActionAccountListTableViewController.ActionType, statusID: String, statusState state: StatusState, accountIDs: [String]?) -> StatusActionAccountListTableViewController
|
2018-09-30 23:29:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension TuskerNavigationDelegate where Self: UIViewController {
|
|
|
|
|
2019-09-07 21:10:58 +00:00
|
|
|
func show(_ vc: UIViewController) {
|
|
|
|
show(vc, sender: self)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
if let profileController = self as? ProfileTableViewController,
|
|
|
|
profileController.accountID == accountID {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-19 19:31:31 +00:00
|
|
|
show(ProfileTableViewController(accountID: accountID), sender: self)
|
2018-10-12 01:20:58 +00:00
|
|
|
}
|
|
|
|
|
2018-09-30 23:29:52 +00:00
|
|
|
func selected(mention: Mention) {
|
2019-01-19 19:31:31 +00:00
|
|
|
show(ProfileTableViewController(accountID: mention.id), sender: self)
|
2018-10-12 01:20:58 +00:00
|
|
|
}
|
|
|
|
|
2018-09-30 23:29:52 +00:00
|
|
|
func selected(tag: Hashtag) {
|
2019-12-20 02:20:29 +00:00
|
|
|
show(HashtagTimelineViewController(for: tag), sender: self)
|
2018-10-12 01:20:58 +00:00
|
|
|
}
|
|
|
|
|
2018-09-30 23:29:52 +00:00
|
|
|
func selected(url: URL) {
|
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: [:])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 15:20:22 +00:00
|
|
|
if (Preferences.shared.openLinksInApps) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-11-28 23:36:58 +00:00
|
|
|
show(ConversationTableViewController(for: statusID, state: state), sender: self)
|
2018-09-30 23:29:52 +00:00
|
|
|
}
|
|
|
|
|
2019-12-14 18:36:05 +00:00
|
|
|
// protocols can't have parameter defaults, so this stub is necessary to fulfill the protocol req
|
2018-10-20 16:03:18 +00:00
|
|
|
func compose() {
|
2019-12-14 18:36:05 +00:00
|
|
|
compose(mentioning: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func compose(mentioning: String? = nil) {
|
|
|
|
let compose = ComposeViewController( mentioningAcct: mentioning)
|
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-20 16:03:18 +00:00
|
|
|
}
|
|
|
|
|
2018-09-30 23:29:52 +00:00
|
|
|
func reply(to statusID: String) {
|
2019-06-13 21:54:55 +00:00
|
|
|
let compose = ComposeViewController(inReplyTo: statusID)
|
|
|
|
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-09-10 16:25:50 +00:00
|
|
|
private func sourceViewInfo(_ sourceView: UIImageView?) -> LargeImageViewController.SourceInfo? {
|
2019-06-17 02:39:46 +00:00
|
|
|
guard let sourceView = sourceView else { return nil }
|
|
|
|
|
2018-10-21 02:07:04 +00:00
|
|
|
var sourceFrame = sourceView.convert(sourceView.bounds, to: view)
|
|
|
|
if let scrollView = view as? UIScrollView {
|
|
|
|
let scale = scrollView.zoomScale
|
|
|
|
let width = sourceFrame.width * scale
|
|
|
|
let height = sourceFrame.height * scale
|
|
|
|
let x = sourceFrame.minX * scale - scrollView.contentOffset.x + scrollView.frame.minX
|
|
|
|
let y = sourceFrame.minY * scale - scrollView.contentOffset.y + scrollView.frame.minY
|
|
|
|
sourceFrame = CGRect(x: x, y: y, width: width, height: height)
|
2018-09-30 23:29:52 +00:00
|
|
|
}
|
2019-09-10 16:25:50 +00:00
|
|
|
return (image: sourceView.image!, frame: sourceFrame, cornerRadius: sourceView.layer.cornerRadius)
|
2019-06-15 00:23:03 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 16:25:50 +00:00
|
|
|
func largeImage(_ image: UIImage, description: String?, sourceView: UIImageView) -> LargeImageViewController {
|
2019-06-17 02:39:46 +00:00
|
|
|
let vc = LargeImageViewController(image: image, description: description, sourceInfo: sourceViewInfo(sourceView))
|
2019-01-19 19:31:31 +00:00
|
|
|
vc.transitioningDelegate = self
|
|
|
|
return vc
|
2018-10-12 01:20:58 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 16:25:50 +00:00
|
|
|
func largeImage(gifData: Data, description: String?, sourceView: UIImageView) -> LargeImageViewController {
|
2019-06-17 02:39:46 +00:00
|
|
|
let vc = LargeImageViewController(image: UIImage(data: gifData)!, description: description, sourceInfo: sourceViewInfo(sourceView))
|
2019-01-19 19:31:31 +00:00
|
|
|
vc.transitioningDelegate = self
|
|
|
|
vc.gifData = gifData
|
|
|
|
return vc
|
2018-11-09 20:48:08 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 16:25:50 +00:00
|
|
|
func showLargeImage(_ image: UIImage, description: String?, animatingFrom sourceView: UIImageView) {
|
2019-01-19 19:31:31 +00:00
|
|
|
present(largeImage(image, description: description, sourceView: sourceView), animated: true)
|
2018-09-30 23:29:52 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 16:25:50 +00:00
|
|
|
func showLargeImage(gifData: Data, description: String?, animatingFrom sourceView: UIImageView) {
|
2019-01-19 19:31:31 +00:00
|
|
|
present(largeImage(gifData: gifData, description: description, sourceView: sourceView), animated: true)
|
|
|
|
}
|
|
|
|
|
2019-09-10 16:25:50 +00:00
|
|
|
func gallery(attachments: [Attachment], sourceViews: [UIImageView?], startIndex: Int) -> GalleryViewController {
|
2019-06-15 00:23:03 +00:00
|
|
|
let sourcesInfo = sourceViews.map(sourceViewInfo)
|
|
|
|
let vc = GalleryViewController(attachments: attachments, sourcesInfo: sourcesInfo, startIndex: startIndex)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-09-05 17:40:10 +00:00
|
|
|
private func moreOptions(forURL url: URL) -> UIViewController {
|
|
|
|
let customActivites: [UIActivity] = [
|
|
|
|
OpenInSafariActivity()
|
|
|
|
]
|
|
|
|
let activityController = UIActivityViewController(activityItems: [url], applicationActivities: customActivites)
|
|
|
|
activityController.completionWithItemsHandler = OpenInSafariActivity.completionHandler(viewController: self, url: url)
|
|
|
|
return activityController
|
|
|
|
}
|
|
|
|
|
|
|
|
private func moreOptions(forStatus statusID: String) -> UIViewController {
|
2019-01-19 19:31:31 +00:00
|
|
|
guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID)") }
|
|
|
|
guard let url = status.url else { fatalError("Missing url for status \(statusID)") }
|
2019-12-14 18:36:05 +00:00
|
|
|
var customActivites: [UIActivity] = [OpenInSafariActivity()]
|
2019-12-14 17:40:50 +00:00
|
|
|
|
|
|
|
if let bookmarked = status.bookmarked {
|
2019-12-14 18:36:05 +00:00
|
|
|
customActivites.insert(bookmarked ? UnbookmarkStatusActivity() : BookmarkStatusActivity(), at: 0)
|
2019-12-14 17:40:50 +00:00
|
|
|
}
|
|
|
|
|
2020-01-05 00:31:35 +00:00
|
|
|
if status.account == MastodonController.account,
|
|
|
|
let pinned = status.pinned {
|
|
|
|
customActivites.insert(pinned ? UnpinStatusActivity() : PinStatusActivity(), at: 1)
|
|
|
|
}
|
|
|
|
|
2019-12-14 17:40:50 +00:00
|
|
|
let activityController = UIActivityViewController(activityItems: [url, status], applicationActivities: customActivites)
|
|
|
|
activityController.completionWithItemsHandler = OpenInSafariActivity.completionHandler(viewController: self, url: url)
|
|
|
|
return activityController
|
2018-11-09 20:48:08 +00:00
|
|
|
}
|
|
|
|
|
2019-12-14 18:36:05 +00:00
|
|
|
private func moreOptions(forAccount accountID: String) -> UIViewController {
|
|
|
|
guard let account = MastodonCache.account(for: accountID) else { fatalError("Missing cached account \(accountID)") }
|
|
|
|
return moreOptions(forURL: account.url)
|
|
|
|
}
|
|
|
|
|
2018-09-30 23:29:52 +00:00
|
|
|
func showMoreOptions(forStatus statusID: String) {
|
2019-01-19 19:31:31 +00:00
|
|
|
present(moreOptions(forStatus: statusID), animated: true)
|
2018-09-30 23:29:52 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 02:04:32 +00:00
|
|
|
func showMoreOptions(forURL url: URL) {
|
2019-01-19 19:31:31 +00:00
|
|
|
present(moreOptions(forURL: url), animated: true)
|
2018-10-12 02:04:32 +00:00
|
|
|
}
|
|
|
|
|
2019-12-14 18:36:05 +00:00
|
|
|
func showMoreOptions(forAccount accountID: String) {
|
|
|
|
present(moreOptions(forAccount: accountID), animated: true)
|
|
|
|
}
|
|
|
|
|
2019-09-06 02:30:58 +00:00
|
|
|
func showFollowedByList(accountIDs: [String]) {
|
|
|
|
let vc = AccountListTableViewController(accountIDs: accountIDs)
|
|
|
|
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 {
|
|
|
|
return StatusActionAccountListTableViewController(actionType: action, statusID: statusID, statusState: state, accountIDs: accountIDs)
|
2019-09-06 03:16:45 +00:00
|
|
|
}
|
|
|
|
|
2018-09-30 23:29:52 +00:00
|
|
|
}
|