Skip to content

Commit

Permalink
add SAHistoryExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
marty-suzuki committed Jan 28, 2017
1 parent fb9959a commit a1c1716
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 75 deletions.
48 changes: 30 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,33 @@ In addition, set module to SAHistoryNavigationViewController.

#### Code

You can use SAHistoryNavigationViewController as `self.navigationController` in ViewController, bacause implemented `extension UINavigationController` as below codes and override those methods in SAHistoryNavigationViewController.
You can use SAHistoryNavigationViewController as `self.sah.navigationController` in any ViewController, bacause implemented `extension SAHistoryExtension` as below codes.

```swift
extension UINavigationController {
public weak var historyDelegate: SAHistoryNavigationViewControllerDelegate? {
set {
willSetHistoryDelegate(newValue)
}
get {
return willGetHistoryDelegate()
}
}
public func showHistory() {}
public func setHistoryBackgroundColor(color: UIColor) {}
public func contentView() -> UIView? { return nil }
func willSetHistoryDelegate(delegate: SAHistoryNavigationViewControllerDelegate?) {}
func willGetHistoryDelegate() -> SAHistoryNavigationViewControllerDelegate? { return nil }
public protocol SAHistoryCompatible {
associatedtype CompatibleType
var sah: CompatibleType { get }
}

public extension SAHistoryCompatible {
public var sah: SAHistoryExtension<Self> {
return SAHistoryExtension(self)
}
}

public final class SAHistoryExtension<Base> {
public let base: Base
public init(_ base: Base) {
self.base = base
}
}

extension UIViewController: SAHistoryCompatible {}

extension SAHistoryExtension where Base: UIViewController {
public var navigationController: SAHistoryNavigationViewController? {
return base.navigationController as? SAHistoryNavigationViewController
}
}
```

Expand All @@ -86,16 +96,18 @@ presentViewController(navigationController, animated: true, completion: nil)
If you want to launch Navigation History without long tap action, use this code.

```swift
navigationController?.showHistory()
//In any UIViewController
self.sah.navigationController?.showHistory()
```

## Customize

If you want to customize background of Navigation History, you can use those methods.

```swift
navigationController?.contentView()
navigationController?.setHistoryBackgroundColor(color: UIColor)
//In any UIViewController
self.sah.navigationController?.contentView
self.sah.navigationController?.historyBackgroundColor
```

This is delegate methods.
Expand Down
2 changes: 1 addition & 1 deletion SAHistoryNavigationViewController.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

Pod::Spec.new do |s|
s.name = "SAHistoryNavigationViewController"
s.version = "3.0.1"
s.version = "3.1.0"
s.summary = "SAHistoryNavigationViewController realizes iOS task manager like UI in UINavigationContoller."

s.homepage = "https://github.com/marty-suzuki/SAHistoryNavigationViewController"
Expand Down
35 changes: 35 additions & 0 deletions SAHistoryNavigationViewController/SAHistoryExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// SAHistoryExtension.swift
// SAHistoryNavigationViewController
//
// Created by 鈴木大貴 on 2017/01/28.
// Copyright (c) 2015年 鈴木大貴. All rights reserved.
//

import UIKit

public protocol SAHistoryCompatible {
associatedtype CompatibleType
var sah: CompatibleType { get }
}

public extension SAHistoryCompatible {
public var sah: SAHistoryExtension<Self> {
return SAHistoryExtension(self)
}
}

public final class SAHistoryExtension<Base> {
public let base: Base
public init(_ base: Base) {
self.base = base
}
}

extension UIViewController: SAHistoryCompatible {}

extension SAHistoryExtension where Base: UIViewController {
public var navigationController: SAHistoryNavigationViewController? {
return base.navigationController as? SAHistoryNavigationViewController
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,18 @@ open class SAHistoryNavigationViewController: UINavigationController {
fileprivate var screenshots = [UIImage]()
fileprivate var historyViewController: SAHistoryViewController?
fileprivate let historyContentView = UIView()
fileprivate weak var _historyDelegate: SAHistoryNavigationViewControllerDelegate?
public weak var historyDelegate: SAHistoryNavigationViewControllerDelegate?
public var historyBackgroundColor: UIColor? {
get {
return historyContentView.backgroundColor
}
set {
historyContentView.backgroundColor = newValue
}
}
public var contentView: UIView? {
return historyContentView
}

//MARK: - Initializers
required public init(coder aDecoder: NSCoder) {
Expand Down Expand Up @@ -68,16 +79,8 @@ open class SAHistoryNavigationViewController: UINavigationController {
navigationBar.addGestureRecognizer(gestureRecognizer)
}

override func willSetHistoryDelegate(_ delegate: SAHistoryNavigationViewControllerDelegate?) {
_historyDelegate = delegate
}

override func willGetHistoryDelegate() -> SAHistoryNavigationViewControllerDelegate? {
return _historyDelegate
}

override open func pushViewController(_ viewController: UIViewController, animated: Bool) {
if let image = visibleViewController?.screenshotFromWindow(Const.imageScale) {
if let image = visibleViewController?.sah.screenshotFromWindow(Const.imageScale) {
screenshots += [image]
}
super.pushViewController(viewController, animated: animated)
Expand All @@ -100,7 +103,7 @@ open class SAHistoryNavigationViewController: UINavigationController {
super.setViewControllers(viewControllers, animated: animated)
for (currentIndex, viewController) in viewControllers.enumerated() {
if currentIndex == viewControllers.endIndex { break }
guard let image = viewController.screenshotFromWindow(Const.imageScale) else { continue }
guard let image = viewController.sah.screenshotFromWindow(Const.imageScale) else { continue }
screenshots += [image]
}
}
Expand All @@ -109,7 +112,7 @@ open class SAHistoryNavigationViewController: UINavigationController {
func handleThirdDimensionalTouch(_ gesture: SAThirdDimensionalTouchRecognizer) {
switch gesture.state {
case .began:
guard let image = visibleViewController?.screenshotFromWindow(Const.imageScale) else { return }
guard let image = visibleViewController?.sah.screenshotFromWindow(Const.imageScale) else { return }
screenshots += [image]

let historyViewController = createHistoryViewController()
Expand Down Expand Up @@ -150,8 +153,8 @@ open class SAHistoryNavigationViewController: UINavigationController {
return historyViewController
}

override public func showHistory() {
guard let image = visibleViewController?.screenshotFromWindow(Const.imageScale) else { return }
public func showHistory() {
guard let image = visibleViewController?.sah.screenshotFromWindow(Const.imageScale) else { return }
screenshots += [image]
let historyViewController = createHistoryViewController()
self.historyViewController = historyViewController
Expand All @@ -161,14 +164,6 @@ open class SAHistoryNavigationViewController: UINavigationController {
self.historyDelegate?.historyControllerDidShowHistory?(self, viewController: visibleViewController)
}
}

override public func setHistoryBackgroundColor(_ color: UIColor) {
historyContentView.backgroundColor = color
}

override public func contentView() -> UIView? {
return historyContentView
}
}

//MARK: - UINavigationBarDelegate
Expand Down

This file was deleted.

6 changes: 3 additions & 3 deletions SAHistoryNavigationViewController/UIView+Screenshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

import UIKit

extension UIView {
extension SAHistoryExtension where Base: UIView {
func screenshotImage(_ scale: CGFloat = 0.0) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(frame.size, false, scale)
drawHierarchy(in: bounds, afterScreenUpdates: true)
UIGraphicsBeginImageContextWithOptions(base.frame.size, false, scale)
base.drawHierarchy(in: base.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import UIKit

extension UIViewController {
extension SAHistoryExtension where Base: UIViewController {
func screenshotFromWindow(_ scale: CGFloat = 0.0) -> UIImage? {
guard let window = UIApplication.shared.windows.first else { return nil }
UIGraphicsBeginImageContextWithOptions(window.frame.size, false, scale)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]
UIApplication.shared.setStatusBarStyle(.lightContent, animated: false)

(window?.rootViewController as? UINavigationController)?.historyDelegate = self
(window?.rootViewController as? SAHistoryNavigationViewController)?.historyDelegate = self

return true
}
Expand Down

0 comments on commit a1c1716

Please sign in to comment.