Skip to content

Commit

Permalink
Merge pull request #28 from OakCityLabs/master
Browse files Browse the repository at this point in the history
Add shouldAutorotate to the Config class.
  • Loading branch information
wtmoose authored Oct 12, 2016
2 parents 3d493f2 + d34390e commit 7b5636e
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 37 deletions.
10 changes: 10 additions & 0 deletions Demo/Demo/Assets.xcassets/AppIcon.appiconset/Contents.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
{
"images" : [
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "29x29",
Expand Down
69 changes: 49 additions & 20 deletions Demo/Demo/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Demo/Demo/ExploreViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ class ExploreViewController: UITableViewController, UITextFieldDelegate {
default:
break
}

config.shouldAutorotate = self.autoRotate.isOn

config.interactiveHide = interactiveHide.isOn

Expand Down Expand Up @@ -147,6 +149,7 @@ class ExploreViewController: UITableViewController, UITextFieldDelegate {
@IBOutlet weak var layout: UISegmentedControl!
@IBOutlet weak var theme: UISegmentedControl!
@IBOutlet weak var iconStyle: UISegmentedControl!
@IBOutlet weak var autoRotate: UISwitch!
@IBOutlet weak var dropShadow: UISwitch!
@IBOutlet weak var titleText: UITextField!
@IBOutlet weak var bodyText: UITextField!
Expand Down
9 changes: 3 additions & 6 deletions SwiftMessages/Presenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,21 @@ class Presenter: NSObject, UIGestureRecognizerDelegate {
func getPresentationContext() throws -> UIViewController {

func newWindowViewController(_ windowLevel: UIWindowLevel) -> UIViewController {
let viewController = WindowViewController(windowLevel: windowLevel)
if windowLevel == UIWindowLevelNormal {
viewController.statusBarStyle = config.preferredStatusBarStyle
}
let viewController = WindowViewController(windowLevel: windowLevel, config: config)
return viewController
}

switch config.presentationContext {
case .automatic:
if let rootViewController = UIApplication.shared.keyWindow?.rootViewController {
return rootViewController.sm_selectPresentationContextTopDown(config.presentationStyle)
return rootViewController.sm_selectPresentationContextTopDown(config)
} else {
throw SwiftMessagesError.noRootViewController
}
case .window(let level):
return newWindowViewController(level)
case .viewController(let viewController):
return viewController.sm_selectPresentationContextBottomUp(config.presentationStyle)
return viewController.sm_selectPresentationContextBottomUp(config)
}
}

Expand Down
8 changes: 8 additions & 0 deletions SwiftMessages/SwiftMessages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ open class SwiftMessages: PresenterDelegate {
the current one. The default is `.Default`.
*/
public var preferredStatusBarStyle: UIStatusBarStyle?

/**
If a view controller is created to host the message view, should the view
controller auto rotate? The default is 'true', meaning it should auto
rotate.
*/
public var shouldAutorotate: Bool = true

}

/**
Expand Down
18 changes: 10 additions & 8 deletions SwiftMessages/UIViewController+Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ private let fullScreenStyles: [UIModalPresentationStyle] = [.fullScreen, .overFu

extension UIViewController {

func sm_selectPresentationContextTopDown(_ presentationStyle: SwiftMessages.PresentationStyle) -> UIViewController {
func sm_selectPresentationContextTopDown(_ config: SwiftMessages.Config) -> UIViewController {
let presentationStyle = config.presentationStyle
if let presented = sm_presentedFullScreenViewController() {
return presented.sm_selectPresentationContextTopDown(presentationStyle)
return presented.sm_selectPresentationContextTopDown(config)
} else if case .top = presentationStyle, let navigationController = sm_selectNavigationControllerTopDown() {
return navigationController
} else if case .bottom = presentationStyle, let tabBarController = sm_selectTabBarControllerTopDown() {
return tabBarController
}
return WindowViewController(windowLevel: self.view.window?.windowLevel ?? UIWindowLevelNormal)
return WindowViewController(windowLevel: self.view.window?.windowLevel ?? UIWindowLevelNormal, config: config)
}

fileprivate func sm_selectNavigationControllerTopDown() -> UINavigationController? {
Expand Down Expand Up @@ -58,27 +59,28 @@ extension UIViewController {
return nil
}

func sm_selectPresentationContextBottomUp(_ presentationStyle: SwiftMessages.PresentationStyle) -> UIViewController {
func sm_selectPresentationContextBottomUp(_ config: SwiftMessages.Config) -> UIViewController {
let presentationStyle = config.presentationStyle
if let parent = parent {
if let navigationController = parent as? UINavigationController {
if case .top = presentationStyle, navigationController.sm_isVisible(view: navigationController.navigationBar) {
return navigationController
}
return navigationController.sm_selectPresentationContextBottomUp(presentationStyle)
return navigationController.sm_selectPresentationContextBottomUp(config)
} else if let tabBarController = parent as? UITabBarController {
if case .bottom = presentationStyle, tabBarController.sm_isVisible(view: tabBarController.tabBar) {
return tabBarController
}
return tabBarController.sm_selectPresentationContextBottomUp(presentationStyle)
return tabBarController.sm_selectPresentationContextBottomUp(config)
}
}
if self.view is UITableView {
// Never select scroll view as presentation context
// because, you know, it scrolls.
if let parent = self.parent {
return parent.sm_selectPresentationContextBottomUp(presentationStyle)
return parent.sm_selectPresentationContextBottomUp(config)
} else {
return WindowViewController(windowLevel: self.view.window?.windowLevel ?? UIWindowLevelNormal)
return WindowViewController(windowLevel: self.view.window?.windowLevel ?? UIWindowLevelNormal, config: config)
}
}
return self
Expand Down
11 changes: 8 additions & 3 deletions SwiftMessages/WindowViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ class WindowViewController: UIViewController
fileprivate var window: UIWindow?

let windowLevel: UIWindowLevel
var statusBarStyle: UIStatusBarStyle?
let config: SwiftMessages.Config

init(windowLevel: UIWindowLevel = UIWindowLevelNormal)
override var shouldAutorotate: Bool {
return config.shouldAutorotate
}

init(windowLevel: UIWindowLevel = UIWindowLevelNormal, config: SwiftMessages.Config)
{
self.windowLevel = windowLevel
self.config = config
let window = PassthroughWindow(frame: UIScreen.main.bounds)
self.window = window
super.init(nibName: nil, bundle: nil)
Expand All @@ -41,7 +46,7 @@ class WindowViewController: UIViewController
}

override public var preferredStatusBarStyle: UIStatusBarStyle {
return statusBarStyle ?? UIApplication.shared.statusBarStyle
return config.preferredStatusBarStyle ?? UIApplication.shared.statusBarStyle
}

override var prefersStatusBarHidden: Bool {
Expand Down

0 comments on commit 7b5636e

Please sign in to comment.