This repository was archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 387
/
Copy pathRepositoryViewController.swift
342 lines (294 loc) · 11.8 KB
/
RepositoryViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//
// RepositoryViewController.swift
// Freetime
//
// Created by Ryan Nystrom on 9/20/17.
// Copyright © 2017 Ryan Nystrom. All rights reserved.
//
import UIKit
import XLPagerTabStrip
import TUSafariActivity
import Squawk
import ContextMenu
import DropdownTitleView
final class RepositoryViewController: ButtonBarPagerTabStripViewController,
NewIssueTableViewControllerDelegate,
ContextMenuDelegate,
EmptyViewDelegate {
private struct Details {
let hasIssuesEnabled: Bool
let defaultBranch: String
let graphQLID: String
let headerModel: RepositoryOverviewHeaderModel
}
private enum State {
case loading
case error
case value(Details)
}
private let repo: RepositoryDetails
private let client: GithubClient
private var controllers = [UIViewController]()
private var bookmarkNavController: BookmarkNavigationController?
public private(set) var branch: String?
private var state: State = .loading {
didSet {
if case .value(let details) = state {
branch = branch ?? details.defaultBranch
bookmarkNavController = BookmarkNavigationController(
store: client.bookmarkCloudStore,
graphQLID: details.graphQLID
)
updateRightBarItems()
}
}
}
init(client: GithubClient, repo: RepositoryDetails) {
self.repo = repo
self.client = client
super.init(nibName: nil, bundle: nil)
buildViewControllers()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
settings.style.buttonBarBackgroundColor = .white
settings.style.buttonBarItemBackgroundColor = .white
settings.style.selectedBarBackgroundColor = Styles.Colors.Blue.medium.color
settings.style.buttonBarItemFont = Styles.Text.body.preferredFont
settings.style.selectedBarHeight = 2.0
settings.style.buttonBarItemTitleColor = Styles.Colors.Gray.medium.color
settings.style.buttonBarItemsShouldFillAvailiableWidth = true
settings.style.buttonBarHeight = 44
pagerBehaviour = .progressive(skipIntermediateViewControllers: true, elasticIndicatorLimit: true)
changeCurrentIndex = { (oldCell, newCell, animated) in
oldCell?.label.textColor = Styles.Colors.Gray.medium.color
newCell?.label.textColor = Styles.Colors.Blue.medium.color
}
edgesForExtendedLayout = []
super.viewDidLoad()
navigationController?.navigationBar.backgroundColor = .white
view.backgroundColor = .white
makeBackBarItemEmpty()
delegate = self
updateRightBarItems()
let navigationTitle = DropdownTitleView()
navigationTitle.addTouchEffect()
navigationItem.titleView = navigationTitle
navigationTitle.addTarget(self, action: #selector(onNavigationTitle(sender:)), for: .touchUpInside)
let labelFormat = NSLocalizedString(
"Repository %@ by %@",
comment: "Accessibility label for a repository navigation item"
)
let accessibilityLabel = String(format: labelFormat, arguments: [repo.name, repo.owner])
navigationTitle.configure(title: repo.name, subtitle: repo.owner, accessibilityLabel: accessibilityLabel)
fetchDetails()
}
// MARK: Private API
private func updateRightBarItems() {
let moreItem = UIBarButtonItem(
image: UIImage(named: "bullets-hollow"),
target: self,
action: #selector(RepositoryViewController.onMore(sender:))
)
moreItem.accessibilityLabel = Constants.Strings.moreOptions
var items = [moreItem]
if let bookmarkItem = bookmarkNavController?.navigationItem {
items.append(bookmarkItem)
}
navigationItem.rightBarButtonItems = items
}
private func buildViewControllers() {
var controllers = [UIViewController]()
switch state {
case .error:
let controller = RepositoryErrorViewController()
controller.delegate = self
controllers.append(controller)
case .value(let details):
controllers.append(RepositoryOverviewViewController(
client: client,
repo: repo,
branch: branch ?? details.defaultBranch,
headerModel: details.headerModel
))
if details.hasIssuesEnabled {
controllers.append(RepositoryIssuesViewController(
client: client,
owner: repo.owner,
repo: repo.name,
type: .issues
))
}
controllers += [
RepositoryIssuesViewController(
client: client,
owner: repo.owner,
repo: repo.name,
type: .pullRequests
),
RepositoryCodeDirectoryViewController.createRoot(
client: client,
repo: repo,
branch: branch ?? details.defaultBranch
)
]
case .loading:
controllers.append(RepositoryLoadingViewController())
}
self.controllers = controllers
}
func fetchDetails() {
state = .loading
buildViewControllers()
reloadPagerTabStripView()
client.client.query(
RepositoryInfoQuery(owner: repo.owner, name: repo.name),
result: { $0 },
completion: { [weak self] result in
switch result {
case .failure(let error):
self?.state = .error
Squawk.show(error: error)
case .success(let data):
if let repo = data.repository {
let headerModel = RepositoryOverviewHeaderModel(
watchersCount: repo.watchers.totalCount,
starsCount: repo.stargazers.totalCount,
forksCount: repo.forkCount
)
let details = Details(
hasIssuesEnabled: repo.hasIssuesEnabled,
defaultBranch: repo.defaultBranchRef?.name ?? "master",
graphQLID: repo.id,
headerModel: headerModel
)
self?.state = .value(details)
} else {
self?.state = .error
}
}
self?.buildViewControllers()
self?.reloadPagerTabStripView()
})
}
@objc func onNavigationTitle(sender: UIView) {
let alert = UIAlertController.configured(preferredStyle: .actionSheet)
weak var weakSelf = self
alert.addActions([
AlertAction(AlertActionBuilder { $0.rootViewController = weakSelf })
.view(owner: repo.owner, icon: #imageLiteral(resourceName: "organization")),
AlertAction.cancel()
])
alert.popoverPresentationController?.setSourceView(sender)
present(alert, animated: trueUnlessReduceMotionEnabled)
}
var repoUrl: URL? {
return URLBuilder.github().add(paths: [repo.owner, repo.name]).url
}
var switchBranchAction: UIAlertAction? {
guard case .value(let details) = self.state else { return nil }
return UIAlertAction(
title: NSLocalizedString("Switch Branch", comment: ""),
style: .default
) { [weak self] _ in
guard let `self` = self,
let branch = self.branch
else { return }
let viewController =
RepositoryBranchesViewController(
defaultBranch: details.defaultBranch,
selectedBranch: branch,
owner: self.repo.owner,
repo: self.repo.name,
client: self.client
)
self.showContextualMenu(
viewController,
options: ContextMenu.Options(
containerStyle: ContextMenu.ContainerStyle(
backgroundColor: Styles.Colors.menuBackgroundColor.color
)
),
delegate: self
)
}
}
func newIssueAction() -> UIAlertAction? {
guard case .value(let details) = state,
details.hasIssuesEnabled,
let newIssueViewController = NewIssueTableViewController.create(
client: client,
owner: repo.owner,
repo: repo.name,
signature: .sentWithGitHawk)
else {
return nil
}
newIssueViewController.delegate = self
weak var weakSelf = self
return AlertAction(AlertActionBuilder { $0.rootViewController = weakSelf })
.newIssue(issueController: newIssueViewController)
}
func workingCopyAction() -> UIAlertAction? {
guard let remote = repoUrl?.absoluteString
.addingPercentEncoding(withAllowedCharacters: CharacterSet.alphanumerics)
else { return nil}
guard let url = URL(string: "working-copy://show?remote=\(remote)")
else { return nil }
guard UIApplication.shared.canOpenURL(url) else { return nil }
let title = NSLocalizedString("Working Copy", comment: "")
let action = UIAlertAction(title: title, style: .default,
handler: { _ in
UIApplication.shared.open(url)
})
return action
}
@objc func onMore(sender: UIButton) {
guard let branch = self.branch else { return }
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 }
alert.addActions([
viewHistoryAction(owner: repo.owner, repo: repo.name, branch: branch, client: client),
newIssueAction()
])
if let url = repoUrl {
alert.add(action: AlertAction(alertBuilder).share([url], activities: [TUSafariActivity()], type: .shareUrl) {
$0.popoverPresentationController?.setSourceView(sender)
})
}
alert.addActions([
switchBranchAction,
workingCopyAction(),
AlertAction.cancel()
])
alert.popoverPresentationController?.setSourceView(sender)
present(alert, animated: trueUnlessReduceMotionEnabled)
}
// MARK: ButtonBarPagerTabStripViewController Overrides
override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
return controllers
}
// MARK: NewIssueTableViewControllerDelegate
func didDismissAfterCreatingIssue(model: IssueDetailsModel) {
route_push(to: IssuesViewController(client: client, model: model))
}
// MARK: ContextMenuDelegate
func contextMenuWillDismiss(viewController: UIViewController, animated: Bool) {
guard let viewController = viewController as? RepositoryBranchesViewController else { return }
self.branch = viewController.selectedBranch
controllers.forEach {
if let branchUpdatable = $0 as? RepositoryBranchUpdatable {
branchUpdatable.updateBranch(to: viewController.selectedBranch)
}
}
}
func contextMenuDidDismiss(viewController: UIViewController, animated: Bool) {}
// MARK: EmptyViewDelegate
func didTapRetry(view: EmptyView) {
fetchDetails()
}
}