Commit 0ba48212 by Daniel Dahan

added smart optional variables for TransitionControllers

parent dc110fd1
......@@ -31,28 +31,33 @@
import UIKit
public struct Application {
/// A reference to the main UIWindow.
public static var keyWindow: UIWindow? {
/// An optional reference to the main UIWindow.
static var keyWindow: UIWindow? {
return UIApplication.shared.keyWindow
}
/// A Boolean indicating if the device is in Landscape mode.
public static var isLandscape: Bool {
/// An optional reference to the top most view controller.
static var rootViewController: UIViewController? {
return keyWindow?.rootViewController
}
/// A boolean indicating if the device is in Landscape mode.
static var isLandscape: Bool {
return UIApplication.shared.statusBarOrientation.isLandscape
}
/// A Boolean indicating if the device is in Portrait mode.
public static var isPortrait: Bool {
/// A boolean indicating if the device is in Portrait mode.
static var isPortrait: Bool {
return !isLandscape
}
/// The current UIInterfaceOrientation value.
public static var orientation: UIInterfaceOrientation {
static var orientation: UIInterfaceOrientation {
return UIApplication.shared.statusBarOrientation
}
/// Retrieves the device status bar style.
public static var statusBarStyle: UIStatusBarStyle {
static var statusBarStyle: UIStatusBarStyle {
get {
return UIApplication.shared.statusBarStyle
}
......@@ -62,7 +67,7 @@ public struct Application {
}
/// Retrieves the device status bar hidden state.
public static var isStatusBarHidden: Bool {
static var isStatusBarHidden: Bool {
get {
return UIApplication.shared.isStatusBarHidden
}
......@@ -75,12 +80,12 @@ public struct Application {
A boolean that indicates based on iPhone rules if the
status bar should be shown.
*/
public static var shouldStatusBarBeHidden: Bool {
static var shouldStatusBarBeHidden: Bool {
return isLandscape && .phone == Device.userInterfaceIdiom
}
/// A reference to the user interface layout direction.
public static var userInterfaceLayoutDirection: UIUserInterfaceLayoutDirection {
static var userInterfaceLayoutDirection: UIUserInterfaceLayoutDirection {
return UIApplication.shared.userInterfaceLayoutDirection
}
}
......@@ -43,11 +43,11 @@ extension UIViewController {
through child UIViewControllers.
*/
public var fabMenuController: FABMenuController? {
return findViewControllerType()
return traverseViewControllerHierarchyForClassType()
}
}
open class FABMenuController: RootController {
open class FABMenuController: TransitionController {
/// Reference to the MenuView.
@IBInspectable
open let fabMenu = FABMenu()
......
/*
* Copyright (C) 2015 - 2017, Daniel Dahan and CosmicMind, Inc. <http://cosmicmind.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of CosmicMind nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import UIKit
internal extension UIViewController {
/**
Finds a view controller with a given type based on
the view controller subclass.
- Returns: An optional of type T.
*/
func findViewControllerType<T: UIViewController>() -> T? {
var v: UIViewController? = self
while nil != v {
if v is T {
return v as? T
}
v = v?.parent
}
return nil
}
}
......@@ -43,7 +43,7 @@ extension UIViewController {
through child UIViewControllers.
*/
public var navigationDrawerController: NavigationDrawerController? {
return findViewControllerType()
return traverseViewControllerHierarchyForClassType()
}
}
......@@ -136,7 +136,7 @@ public protocol NavigationDrawerControllerDelegate {
}
@objc(NavigationDrawerController)
open class NavigationDrawerController: RootController {
open class NavigationDrawerController: TransitionController {
/**
A CGFloat property that is used internally to track
the original (x) position of the container view when panning.
......
......@@ -37,7 +37,7 @@ public extension UIViewController {
through child UIViewControllers.
*/
var searchBarController: SearchBarController? {
return findViewControllerType()
return traverseViewControllerHierarchyForClassType()
}
}
......
......@@ -78,11 +78,11 @@ extension UIViewController {
through child UIViewControllers.
*/
public var snackbarController: SnackbarController? {
return findViewControllerType()
return traverseViewControllerHierarchyForClassType()
}
}
open class SnackbarController: RootController {
open class SnackbarController: TransitionController {
/// Reference to the Snackbar.
open let snackbar = Snackbar()
......
......@@ -37,11 +37,11 @@ extension UIViewController {
through child UIViewControllers.
*/
public var statusBarController: StatusBarController? {
return findViewControllerType()
return traverseViewControllerHierarchyForClassType()
}
}
open class StatusBarController: RootController {
open class StatusBarController: TransitionController {
/**
A Display value to indicate whether or not to
display the rootViewController to the full view
......
......@@ -60,7 +60,7 @@ extension UIViewController {
through child UIViewControllers.
*/
public var tabsController: TabsController? {
return findViewControllerType()
return traverseViewControllerHierarchyForClassType()
}
}
......
......@@ -37,7 +37,7 @@ public extension UIViewController {
through child UIViewControllers.
*/
var toolbarController: ToolbarController? {
return findViewControllerType()
return traverseViewControllerHierarchyForClassType()
}
}
......
......@@ -30,7 +30,40 @@
import UIKit
open class RootController: UIViewController {
internal extension UIViewController {
/**
Finds a view controller with a given type based on
the view controller subclass.
- Returns: An optional of type T.
*/
func traverseViewControllerHierarchyForClassType<T: UIViewController>() -> T? {
var v: UIViewController? = self
while nil != v {
if v is T {
return v as? T
}
v = v?.parent as? TransitionController
}
return Application.rootViewController?.traverseTransitionViewControllerHierarchyForClassType()
}
/**
Retrieves a flattened hierarchy of view controllers for a given type.
- Returns: An Array of type T.
*/
func traverseTransitionViewControllerHierarchyForClassType<T: UIViewController>() -> T? {
if let v = self as? T {
return v
} else if let v = self as? TransitionController {
return v.rootViewController.traverseTransitionViewControllerHierarchyForClassType()
}
return nil
}
}
open class TransitionController: UIViewController {
/**
A Boolean property used to enable and disable interactivity
with the rootViewController.
......@@ -146,7 +179,7 @@ open class RootController: UIViewController {
}
}
internal extension RootController {
internal extension TransitionController {
/// Prepares the container view.
func prepareContainer() {
container.frame = view.bounds
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment