From 71343b9e72869fac43abcf8a3fd31994fc89fcb7 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Mon, 19 Aug 2019 18:09:38 -0500 Subject: [PATCH] Add Share context menu for timeline --- iOS/AppAssets.swift | 4 ++ .../MasterTimelineViewController.swift | 47 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/iOS/AppAssets.swift b/iOS/AppAssets.swift index 3d5cc7871..ea549d4e6 100644 --- a/iOS/AppAssets.swift +++ b/iOS/AppAssets.swift @@ -92,6 +92,10 @@ struct AppAssets { return UIImage(named: "settingsImage")! }() + static var shareImage: UIImage = { + return UIImage(systemName: "square.and.arrow.up")! + }() + static var smartFeedColor: UIColor = { return UIColor(named: "smartFeedColor")! }() diff --git a/iOS/MasterTimeline/MasterTimelineViewController.swift b/iOS/MasterTimeline/MasterTimelineViewController.swift index 7ff2f371f..3a9e66f2d 100644 --- a/iOS/MasterTimeline/MasterTimelineViewController.swift +++ b/iOS/MasterTimeline/MasterTimelineViewController.swift @@ -176,6 +176,10 @@ class MasterTimelineViewController: UITableViewController, UndoableCommandRunner alert.addAction(action) } + if let action = self.shareAlertAction(indexPath: indexPath, completionHandler: completionHandler) { + alert.addAction(action) + } + let cancelTitle = NSLocalizedString("Cancel", comment: "Cancel") alert.addAction(UIAlertAction(title: cancelTitle, style: .cancel) { _ in completionHandler(true) @@ -218,6 +222,10 @@ class MasterTimelineViewController: UITableViewController, UndoableCommandRunner actions.append(action) } + if let action = self.shareAction(indexPath: indexPath) { + actions.append(action) + } + return UIMenu(title: "", children: actions) }) @@ -640,4 +648,43 @@ private extension MasterTimelineViewController { return action } + func shareDialogForTableCell(indexPath: IndexPath, url: URL, title: String?) { + let itemSource = ArticleActivityItemSource(url: url, subject: title) + let activityViewController = UIActivityViewController(activityItems: [itemSource], applicationActivities: nil) + + guard let cell = tableView.cellForRow(at: indexPath) else { return } + let popoverController = activityViewController.popoverPresentationController + popoverController?.sourceView = cell + popoverController?.sourceRect = CGRect(x: 0, y: 0, width: cell.frame.size.width, height: cell.frame.size.height) + + present(activityViewController, animated: true) + } + + func shareAction(indexPath: IndexPath) -> UIAction? { + let article = coordinator.articles[indexPath.row] + guard let preferredLink = article.preferredLink, let url = URL(string: preferredLink) else { + return nil + } + + let title = NSLocalizedString("Share", comment: "Share") + let action = UIAction(title: title, image: AppAssets.shareImage) { [weak self] action in + self?.shareDialogForTableCell(indexPath: indexPath, url: url, title: article.title) + } + return action + } + + func shareAlertAction(indexPath: IndexPath, completionHandler: @escaping (Bool) -> Void) -> UIAlertAction? { + let article = coordinator.articles[indexPath.row] + guard let preferredLink = article.preferredLink, let url = URL(string: preferredLink) else { + return nil + } + + let title = NSLocalizedString("Share", comment: "Share") + let action = UIAlertAction(title: title, style: .default) { [weak self] action in + completionHandler(true) + self?.shareDialogForTableCell(indexPath: indexPath, url: url, title: article.title) + } + return action + } + }