Files
GitHawk/Classes/Repository/RepositoryViewController.swift
Ivan Magda f8d0cc7f12 Move duplicate share UIAlertActions to AlertAction struct #359 (#431)
* Move duplicate share UIAlertActions to AlertActions struct #359

Create helper AlertActions struct for convenient UIAlertAction's instantiation.

* Create AlertActionBuilder #359

The AlertActionBuilder is used to create custom UIAlertAction's with constituent parts.

* Create UIAlertController+Action #359

UIAlertController+Action extension provide a convinient way for adding actions into the UIAlertController.

* Move additional UIAlertAction duplicates into AlertAction #359

* NewIssueTableViewController: Use correct titles for alert actions #359

Use "Go back" and "Discard" titles instead of "No" and "Yes".

* IssueCommentSectionController: Refactor alert actions #359

* SettingsViewController: Refactor onSignOut alert actions #359
2017-09-29 16:21:17 -04:00

131 lines
4.4 KiB
Swift

//
// RepositoryViewController.swift
// Freetime
//
// Created by Ryan Nystrom on 9/20/17.
// Copyright © 2017 Ryan Nystrom. All rights reserved.
//
import UIKit
import Tabman
import Pageboy
import TUSafariActivity
import SafariServices
class RepositoryViewController: TabmanViewController,
PageboyViewControllerDataSource,
NewIssueTableViewControllerDelegate {
private let repo: RepositoryDetails
private let client: GithubClient
private let controllers: [UIViewController]
init(client: GithubClient, repo: RepositoryDetails) {
self.repo = repo
self.client = client
var controllers: [UIViewController] = [RepositoryOverviewViewController(client: client, repo: repo)]
if repo.hasIssuesEnabled {
controllers.append(RepositoryIssuesViewController(client: client, repo: repo, type: .issues))
}
controllers.append(RepositoryIssuesViewController(client: client, repo: repo, type: .pullRequests))
self.controllers = controllers
super.init(nibName: nil, bundle: nil)
self.title = "\(repo.owner)/\(repo.name)"
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil)
dataSource = self
bar.items = controllers.map { Item(title: $0.title ?? "" ) }
bar.appearance = TabmanBar.Appearance({ appearance in
appearance.text.font = Styles.Fonts.button
appearance.state.color = Styles.Colors.Gray.light.color
appearance.state.selectedColor = Styles.Colors.Blue.medium.color
appearance.indicator.color = Styles.Colors.Blue.medium.color
})
let rightItem = UIBarButtonItem(
image: UIImage(named: "bullets-hollow"),
style: .plain,
target: self,
action: #selector(IssuesViewController.onMore(sender:))
)
rightItem.accessibilityLabel = NSLocalizedString("More options", comment: "")
navigationItem.rightBarButtonItem = rightItem
}
// MARK: Private API
var repoUrl: URL {
return URL(string: "https://github.com/\(repo.owner)/\(repo.name)")!
}
func newIssueAction() -> UIAlertAction? {
guard let newIssueViewController = NewIssueTableViewController.create(
client: client,
owner: repo.owner,
repo: repo.name,
signature: .sentWithGitHawk)
else {
StatusBar.showGenericError()
return nil
}
newIssueViewController.delegate = self
weak var weakSelf = self
return AlertAction(AlertActionBuilder { $0.rootViewController = weakSelf })
.newIssue(issueController: newIssueViewController)
}
func onMore(sender: UIBarButtonItem) {
let alert = UIAlertController.configured(preferredStyle: .actionSheet)
weak var weakSelf = self
let alertBuilder = AlertActionBuilder { $0.rootViewController = weakSelf }
alert.addActions([
repo.hasIssuesEnabled ? newIssueAction() : nil,
AlertAction(alertBuilder).share([repoUrl], activities: [TUSafariActivity()]) { $0.popoverPresentationController?.barButtonItem = sender },
AlertAction(alertBuilder).openInSafari(url: repoUrl),
AlertAction(alertBuilder).view(owner: URL(string: "https://github.com/\(repo.owner)")!),
AlertAction.cancel(),
])
alert.popoverPresentationController?.barButtonItem = sender
present(alert, animated: true)
}
// MARK: PageboyViewControllerDataSource
func numberOfViewControllers(in pageboyViewController: PageboyViewController) -> Int {
return controllers.count
}
func viewController(for pageboyViewController: PageboyViewController, at index: PageboyViewController.PageIndex) -> UIViewController? {
return controllers[index]
}
func defaultPage(for pageboyViewController: PageboyViewController) -> PageboyViewController.Page? {
return nil
}
// MARK: NewIssueTableViewControllerDelegate
func didDismissAfterCreatingIssue(model: IssueDetailsModel) {
let issuesViewController = IssuesViewController(client: client, model: model)
show(issuesViewController, sender: self)
}
}