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

Commit 5c94d98

Browse files
Ashley Ngrnystrom
authored andcommitted
Adds more share actions when browsing a repository (#2161) (#2237)
* add additional share actions * fix share URLs
1 parent e2b2bc9 commit 5c94d98

File tree

5 files changed

+102
-12
lines changed

5 files changed

+102
-12
lines changed

Classes/Issues/Comments/IssueCommentSectionController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ final class IssueCommentSectionController:
9393
weak var weakSelf = self
9494

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

9999
var deleteAction: UIAlertAction? {

Classes/Repository/RepositoryCodeBlobViewController.swift

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import UIKit
1010
import Squawk
11+
import TUSafariActivity
1112

1213
final class RepositoryCodeBlobViewController: UIViewController {
1314

@@ -19,9 +20,13 @@ final class RepositoryCodeBlobViewController: UIViewController {
1920
private let feedRefresh = FeedRefresh()
2021
private let emptyView = EmptyView()
2122
private var sharingPayload: Any?
22-
private lazy var sharingButton: UIBarButtonItem = {
23+
private var repoUrl: URL {
24+
return URL(string: "https://github.com/\(repo.owner)/\(repo.name)/blob/\(branch)/\(path.path)")!
25+
}
26+
27+
private lazy var moreOptionsItem: UIBarButtonItem = {
2328
let barButtonItem = UIBarButtonItem(
24-
barButtonSystemItem: .action,
29+
image: UIImage(named: "bullets-hollow"),
2530
target: self,
2631
action: #selector(RepositoryCodeBlobViewController.onShare(sender:)))
2732
barButtonItem.isEnabled = false
@@ -61,7 +66,7 @@ final class RepositoryCodeBlobViewController: UIViewController {
6166
codeView.refreshControl = feedRefresh.refreshControl
6267
feedRefresh.refreshControl.addTarget(self, action: #selector(onRefresh), for: .valueChanged)
6368

64-
navigationItem.rightBarButtonItem = sharingButton
69+
navigationItem.rightBarButtonItem = moreOptionsItem
6570

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

8994
func didFetchPayload(_ payload: Any) {
9095
sharingPayload = payload
91-
sharingButton.isEnabled = true
96+
moreOptionsItem.isEnabled = true
9297
}
9398

9499
@objc func onRefresh() {
95100
fetch()
96101
}
97102

98-
@objc func onShare(sender: UIBarButtonItem) {
99-
guard let payload = sharingPayload else { return }
100-
let activityController = UIActivityViewController(activityItems: [payload], applicationActivities: nil)
101-
activityController.popoverPresentationController?.barButtonItem = sender
103+
@objc func onShare(sender: UIButton) {
104+
let alertTitle = "\(repo.owner)/\(repo.name):\(branch)"
105+
let alert = UIAlertController.configured(title: alertTitle, preferredStyle: .actionSheet)
106+
107+
weak var weakSelf = self
108+
let alertBuilder = AlertActionBuilder { $0.rootViewController = weakSelf }
109+
var actions = [
110+
AlertAction(alertBuilder).share([path.path], activities: nil, type: .shareFilePath) {
111+
$0.popoverPresentationController?.setSourceView(sender)
112+
},
113+
AlertAction(alertBuilder).share([repoUrl], activities: [TUSafariActivity()], type: .shareUrl) {
114+
$0.popoverPresentationController?.setSourceView(sender)
115+
},
116+
AlertAction.cancel()
117+
]
118+
119+
if let name = self.path.components.last {
120+
actions.insert(AlertAction(alertBuilder).share([name], activities: nil, type: .shareFileName) {
121+
$0.popoverPresentationController?.setSourceView(sender)
122+
}, at: 1)
123+
}
124+
if let payload = self.sharingPayload {
125+
actions.insert(AlertAction(alertBuilder).share([payload], activities: nil, type: .shareContent) {
126+
$0.popoverPresentationController?.setSourceView(sender)
127+
}, at: actions.endIndex - 1)
128+
}
102129

103-
present(activityController, animated: trueUnlessReduceMotionEnabled)
130+
alert.addActions(actions)
131+
alert.popoverPresentationController?.setSourceView(sender)
132+
present(alert, animated: trueUnlessReduceMotionEnabled)
104133
}
105134

106135
func fetch() {

Classes/Repository/RepositoryCodeDirectoryViewController.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import UIKit
1010
import IGListKit
11+
import TUSafariActivity
1112

1213
final class RepositoryCodeDirectoryViewController: BaseListViewController<NSNumber>,
1314
BaseListViewControllerDataSource,
@@ -20,6 +21,18 @@ RepositoryBranchUpdatable
2021
private let path: FilePath
2122
private let repo: RepositoryDetails
2223
private var files = [RepositoryFile]()
24+
private var repoUrl: URL {
25+
return URL(string: "https://github.com/\(repo.owner)/\(repo.name)/tree/\(branch)/\(path.path)")!
26+
}
27+
private lazy var moreOptionsItem: UIBarButtonItem = {
28+
let barButtonItem = UIBarButtonItem(
29+
image: UIImage(named: "bullets-hollow"),
30+
target: self,
31+
action: #selector(RepositoryCodeDirectoryViewController.onShare(sender:)))
32+
barButtonItem.isEnabled = false
33+
34+
return barButtonItem
35+
}()
2336

2437
init(
2538
client: GithubClient,
@@ -50,6 +63,7 @@ RepositoryBranchUpdatable
5063
super.viewDidLoad()
5164
configureTitle(filePath: path, target: self, action: #selector(onFileNavigationTitle(sender:)))
5265
makeBackBarItemEmpty()
66+
navigationItem.rightBarButtonItem = moreOptionsItem
5367
}
5468

5569
// MARK: Public API
@@ -73,6 +87,33 @@ RepositoryBranchUpdatable
7387
showAlert(filePath: path, sender: sender)
7488
}
7589

90+
@objc func onShare(sender: UIButton) {
91+
let alertTitle = "\(repo.owner)/\(repo.name):\(branch)"
92+
let alert = UIAlertController.configured(title: alertTitle, preferredStyle: .actionSheet)
93+
94+
weak var weakSelf = self
95+
let alertBuilder = AlertActionBuilder { $0.rootViewController = weakSelf }
96+
var actions = [
97+
AlertAction(alertBuilder).share([path.path], activities: nil, type: .shareFilePath) {
98+
$0.popoverPresentationController?.setSourceView(sender)
99+
},
100+
AlertAction(alertBuilder).share([repoUrl], activities: [TUSafariActivity()], type: .shareUrl) {
101+
$0.popoverPresentationController?.setSourceView(sender)
102+
},
103+
AlertAction.cancel()
104+
]
105+
106+
if let name = self.path.components.last {
107+
actions.insert(AlertAction(alertBuilder).share([name], activities: nil, type: .shareFileName) {
108+
$0.popoverPresentationController?.setSourceView(sender)
109+
}, at: 1)
110+
}
111+
112+
alert.addActions(actions)
113+
alert.popoverPresentationController?.setSourceView(sender)
114+
present(alert, animated: trueUnlessReduceMotionEnabled)
115+
}
116+
76117
// MARK: Overrides
77118

78119
override func fetch(page: NSNumber?) {
@@ -88,6 +129,7 @@ RepositoryBranchUpdatable
88129
case .success(let files):
89130
self?.files = files
90131
self?.update(animated: trueUnlessReduceMotionEnabled)
132+
self?.moreOptionsItem.isEnabled = true
91133
}
92134
}
93135
}

Classes/Repository/RepositoryViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ ContextMenuDelegate {
169169

170170
alert.addActions([
171171
repo.hasIssuesEnabled ? newIssueAction() : nil,
172-
AlertAction(alertBuilder).share([repoUrl], activities: [TUSafariActivity()]) {
172+
AlertAction(alertBuilder).share([repoUrl], activities: [TUSafariActivity()], type: .shareUrl) {
173173
$0.popoverPresentationController?.setSourceView(sender)
174174
},
175175
switchBranchAction(),

Classes/Utility/AlertAction.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@ struct AlertAction {
2121
let title: String?
2222
let style: UIAlertActionStyle
2323

24+
enum AlertShareType {
25+
case shareUrl
26+
case shareContent
27+
case shareFilePath
28+
case shareFileName
29+
case `default`
30+
31+
var localizedString: String {
32+
switch self {
33+
case .shareUrl: return NSLocalizedString("Share URL", comment: "")
34+
case .shareContent: return NSLocalizedString("Share Content", comment: "")
35+
case .shareFilePath: return NSLocalizedString("Copy Path", comment: "")
36+
case .shareFileName: return NSLocalizedString("Copy Name", comment: "")
37+
default: return NSLocalizedString("Share", comment: "")
38+
}
39+
}
40+
}
41+
2442
// MARK: Init
2543

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

3856
func share(_ items: [Any],
3957
activities: [UIActivity]?,
58+
type: AlertShareType = .default,
4059
buildActivityBlock: ((UIActivityViewController) -> Void)?) -> UIAlertAction {
41-
return UIAlertAction(title: NSLocalizedString("Share", comment: ""), style: .default) { _ in
60+
return UIAlertAction(title: type.localizedString, style: .default) { _ in
4261
let activityController = UIActivityViewController(activityItems: items, applicationActivities: activities)
4362
buildActivityBlock?(activityController)
4463
self.rootViewController?.present(activityController, animated: trueUnlessReduceMotionEnabled)

0 commit comments

Comments
 (0)