Add Share context menu for timeline

This commit is contained in:
Maurice Parker 2019-08-19 18:09:38 -05:00
parent badc2d3e25
commit 71343b9e72
2 changed files with 51 additions and 0 deletions

View File

@ -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")!
}()

View File

@ -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
}
}