Files
GitHawk/Classes/Repository/RepositoryViewController.swift
Hesham Salman 1e2e0c1698 Chore/bookmark rewrite (#865)
* Unified store behavior, new bookmark model

- Added a `Store` protocol to the project with default implementations
  that should be sufficient for most models. (#855)
- Switched images in the notification type category to #imageLiterals,
  so we can avoid the optionality of the image.

* Implemented BookMark ViewModel

* Created Bookmark Collection Cell

* Created Bookmark View Controller

Done:
  - Displays empty state
  - Displays bookmarks
  - Allows for adding of new bookmarks
  - Navigating to bookmarks

In flight:
  - Clear All

TODO:
  - Search
  - Cleanup old files & remove SwipeCell hacks

* Unified search and clear all behavior

The search bar and clear all buttons between the search and bookmarks
page had different styles and behavior. This PR:
  - Makes a generic clear-all header
  - Gives one to each VC
  - Makes them behave the same to the user re: visibility etc

* Re-implemented filter

* Deleted old files

* Attributed String Sizing

* Removed bookmark store tests

* Reloading bookmarks & namespacing

Can now reload bookmarks through the listener pattern (#773, #783).
Bookmark store is now correctly namespaced again.
2017-11-06 09:03:29 -05:00

155 lines
5.1 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 += [
RepositoryIssuesViewController(client: client, repo: repo, type: .pullRequests),
RepositoryCodeDirectoryViewController(client: client, repo: repo, branch: repo.defaultBranch, path: "", isRoot: true)
]
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()
makeBackBarItemEmpty()
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
navigationItem.configure(title: repo.name, subtitle: repo.owner)
}
override func viewSafeAreaInsetsDidChange() {
if #available(iOS 11.0, *) {
super.viewSafeAreaInsetsDidChange()
}
setNeedsScrollViewInsetUpdate()
}
// 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 {
ToastManager.showGenericError()
return nil
}
newIssueViewController.delegate = self
weak var weakSelf = self
return AlertAction(AlertActionBuilder { $0.rootViewController = weakSelf })
.newIssue(issueController: newIssueViewController)
}
func bookmarkAction() -> UIAlertAction? {
guard let store = client.bookmarksStore else { return nil }
let bookmark = Bookmark(
type: .repo,
name: repo.name,
owner: repo.owner,
hasIssueEnabled: repo.hasIssuesEnabled,
defaultBranch: repo.defaultBranch
)
return AlertAction.toggleBookmark(store: store, model: bookmark)
}
@objc
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).view(owner: repo.owner, url: repo.ownerURL),
bookmarkAction(),
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)
}
}