Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Adds more share actions when browsing a repository (#2161) #2237

Merged
merged 2 commits into from
Oct 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ final class IssueCommentSectionController:
weak var weakSelf = self

return AlertAction(AlertActionBuilder { $0.rootViewController = weakSelf?.viewController })
.share([url], activities: [TUSafariActivity()]) { $0.popoverPresentationController?.sourceView = sender }
.share([url], activities: [TUSafariActivity()], type: .shareUrl) { $0.popoverPresentationController?.sourceView = sender }
}

var deleteAction: UIAlertAction? {
Expand Down
47 changes: 38 additions & 9 deletions Classes/Repository/RepositoryCodeBlobViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import UIKit
import Squawk
import TUSafariActivity

final class RepositoryCodeBlobViewController: UIViewController {

Expand All @@ -19,9 +20,13 @@ final class RepositoryCodeBlobViewController: UIViewController {
private let feedRefresh = FeedRefresh()
private let emptyView = EmptyView()
private var sharingPayload: Any?
private lazy var sharingButton: UIBarButtonItem = {
private var repoUrl: URL {
return URL(string: "https://github.com/\(repo.owner)/\(repo.name)/blob/\(branch)/\(path.path)")!
}

private lazy var moreOptionsItem: UIBarButtonItem = {
let barButtonItem = UIBarButtonItem(
barButtonSystemItem: .action,
image: UIImage(named: "bullets-hollow"),
target: self,
action: #selector(RepositoryCodeBlobViewController.onShare(sender:)))
barButtonItem.isEnabled = false
Expand Down Expand Up @@ -61,7 +66,7 @@ final class RepositoryCodeBlobViewController: UIViewController {
codeView.refreshControl = feedRefresh.refreshControl
feedRefresh.refreshControl.addTarget(self, action: #selector(onRefresh), for: .valueChanged)

navigationItem.rightBarButtonItem = sharingButton
navigationItem.rightBarButtonItem = moreOptionsItem

fetch()
feedRefresh.beginRefreshing()
Expand All @@ -88,19 +93,43 @@ final class RepositoryCodeBlobViewController: UIViewController {

func didFetchPayload(_ payload: Any) {
sharingPayload = payload
sharingButton.isEnabled = true
moreOptionsItem.isEnabled = true
}

@objc func onRefresh() {
fetch()
}

@objc func onShare(sender: UIBarButtonItem) {
guard let payload = sharingPayload else { return }
let activityController = UIActivityViewController(activityItems: [payload], applicationActivities: nil)
activityController.popoverPresentationController?.barButtonItem = sender
@objc func onShare(sender: UIButton) {
let alertTitle = "\(repo.owner)/\(repo.name):\(branch)"
let alert = UIAlertController.configured(title: alertTitle, preferredStyle: .actionSheet)

weak var weakSelf = self
let alertBuilder = AlertActionBuilder { $0.rootViewController = weakSelf }
var actions = [
AlertAction(alertBuilder).share([path.path], activities: nil, type: .shareFilePath) {
$0.popoverPresentationController?.setSourceView(sender)
},
AlertAction(alertBuilder).share([repoUrl], activities: [TUSafariActivity()], type: .shareUrl) {
$0.popoverPresentationController?.setSourceView(sender)
},
AlertAction.cancel()
]

if let name = self.path.components.last {
actions.insert(AlertAction(alertBuilder).share([name], activities: nil, type: .shareFileName) {
$0.popoverPresentationController?.setSourceView(sender)
}, at: 1)
}
if let payload = self.sharingPayload {
actions.insert(AlertAction(alertBuilder).share([payload], activities: nil, type: .shareContent) {
$0.popoverPresentationController?.setSourceView(sender)
}, at: actions.endIndex - 1)
}

present(activityController, animated: trueUnlessReduceMotionEnabled)
alert.addActions(actions)
alert.popoverPresentationController?.setSourceView(sender)
present(alert, animated: trueUnlessReduceMotionEnabled)
}

func fetch() {
Expand Down
42 changes: 42 additions & 0 deletions Classes/Repository/RepositoryCodeDirectoryViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import UIKit
import IGListKit
import TUSafariActivity

final class RepositoryCodeDirectoryViewController: BaseListViewController<NSNumber>,
BaseListViewControllerDataSource,
Expand All @@ -20,6 +21,18 @@ RepositoryBranchUpdatable
private let path: FilePath
private let repo: RepositoryDetails
private var files = [RepositoryFile]()
private var repoUrl: URL {
return URL(string: "https://github.com/\(repo.owner)/\(repo.name)/tree/\(branch)/\(path.path)")!
}
private lazy var moreOptionsItem: UIBarButtonItem = {
let barButtonItem = UIBarButtonItem(
image: UIImage(named: "bullets-hollow"),
target: self,
action: #selector(RepositoryCodeDirectoryViewController.onShare(sender:)))
barButtonItem.isEnabled = false

return barButtonItem
}()

init(
client: GithubClient,
Expand Down Expand Up @@ -50,6 +63,7 @@ RepositoryBranchUpdatable
super.viewDidLoad()
configureTitle(filePath: path, target: self, action: #selector(onFileNavigationTitle(sender:)))
makeBackBarItemEmpty()
navigationItem.rightBarButtonItem = moreOptionsItem
}

// MARK: Public API
Expand All @@ -73,6 +87,33 @@ RepositoryBranchUpdatable
showAlert(filePath: path, sender: sender)
}

@objc func onShare(sender: UIButton) {
let alertTitle = "\(repo.owner)/\(repo.name):\(branch)"
let alert = UIAlertController.configured(title: alertTitle, preferredStyle: .actionSheet)

weak var weakSelf = self
let alertBuilder = AlertActionBuilder { $0.rootViewController = weakSelf }
var actions = [
AlertAction(alertBuilder).share([path.path], activities: nil, type: .shareFilePath) {
$0.popoverPresentationController?.setSourceView(sender)
},
AlertAction(alertBuilder).share([repoUrl], activities: [TUSafariActivity()], type: .shareUrl) {
$0.popoverPresentationController?.setSourceView(sender)
},
AlertAction.cancel()
]

if let name = self.path.components.last {
actions.insert(AlertAction(alertBuilder).share([name], activities: nil, type: .shareFileName) {
$0.popoverPresentationController?.setSourceView(sender)
}, at: 1)
}

alert.addActions(actions)
alert.popoverPresentationController?.setSourceView(sender)
present(alert, animated: trueUnlessReduceMotionEnabled)
}

// MARK: Overrides

override func fetch(page: NSNumber?) {
Expand All @@ -88,6 +129,7 @@ RepositoryBranchUpdatable
case .success(let files):
self?.files = files
self?.update(animated: trueUnlessReduceMotionEnabled)
self?.moreOptionsItem.isEnabled = true
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Classes/Repository/RepositoryViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ ContextMenuDelegate {

alert.addActions([
repo.hasIssuesEnabled ? newIssueAction() : nil,
AlertAction(alertBuilder).share([repoUrl], activities: [TUSafariActivity()]) {
AlertAction(alertBuilder).share([repoUrl], activities: [TUSafariActivity()], type: .shareUrl) {
$0.popoverPresentationController?.setSourceView(sender)
},
switchBranchAction(),
Expand Down
21 changes: 20 additions & 1 deletion Classes/Utility/AlertAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ struct AlertAction {
let title: String?
let style: UIAlertActionStyle

enum AlertShareType {
case shareUrl
case shareContent
case shareFilePath
case shareFileName
case `default`

var localizedString: String {
switch self {
case .shareUrl: return NSLocalizedString("Share URL", comment: "")
case .shareContent: return NSLocalizedString("Share Content", comment: "")
case .shareFilePath: return NSLocalizedString("Copy Path", comment: "")
case .shareFileName: return NSLocalizedString("Copy Name", comment: "")
default: return NSLocalizedString("Share", comment: "")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be case .default instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I figured it would be a good fallback if there were any other new sharing options to be added.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair, but that would mean it would be rather easy to overlook as the compiler won't help us if we do want to change it. Not the biggest deal either way, I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. I’ll be making another PR soon for #1958 and can add the change for this in there

}
}
}

// MARK: Init

init(_ builder: AlertActionBuilder) {
Expand All @@ -37,8 +55,9 @@ struct AlertAction {

func share(_ items: [Any],
activities: [UIActivity]?,
type: AlertShareType = .default,
buildActivityBlock: ((UIActivityViewController) -> Void)?) -> UIAlertAction {
return UIAlertAction(title: NSLocalizedString("Share", comment: ""), style: .default) { _ in
return UIAlertAction(title: type.localizedString, style: .default) { _ in
let activityController = UIActivityViewController(activityItems: items, applicationActivities: activities)
buildActivityBlock?(activityController)
self.rootViewController?.present(activityController, animated: trueUnlessReduceMotionEnabled)
Expand Down