Add documentation
This commit is contained in:
parent
1fc98b6a2c
commit
501ca25d7c
Binary file not shown.
|
@ -8,21 +8,36 @@
|
||||||
|
|
||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
|
/// Priority type of SModal
|
||||||
public typealias SModalPriority = Float
|
public typealias SModalPriority = Float
|
||||||
|
|
||||||
|
// MARK: - Priority default types
|
||||||
extension SModalPriority {
|
extension SModalPriority {
|
||||||
|
/// Low Priority
|
||||||
public static var Low: SModalPriority = 250
|
public static var Low: SModalPriority = 250
|
||||||
|
/// Required Priority
|
||||||
public static var Required: SModalPriority = 500
|
public static var Required: SModalPriority = 500
|
||||||
|
/// High Priority
|
||||||
public static var High: SModalPriority = 750
|
public static var High: SModalPriority = 750
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Presentation status of Your ViewController.
|
||||||
|
///
|
||||||
|
/// - presented: Means it's currently presented on screen.
|
||||||
|
/// - waitingInStack: Your controllers is waiting in queue.
|
||||||
|
/// - none: Your controller is not presented neither in queue
|
||||||
public enum SModalStatus {
|
public enum SModalStatus {
|
||||||
|
/// Means it's currently presented on screen.
|
||||||
case presented
|
case presented
|
||||||
|
/// Your controllers is waiting in queue.
|
||||||
case waitingInStack
|
case waitingInStack
|
||||||
|
/// Your controller is not presented neither in queue
|
||||||
case none
|
case none
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Main object responsible for presentation flow
|
||||||
public class SModal {
|
public class SModal {
|
||||||
|
/// Window use to present your view controllers
|
||||||
static let modalWindow: UIWindow = {
|
static let modalWindow: UIWindow = {
|
||||||
let window = UIWindow(frame: UIScreen.main.bounds)
|
let window = UIWindow(frame: UIScreen.main.bounds)
|
||||||
window.backgroundColor = .clear
|
window.backgroundColor = .clear
|
||||||
|
@ -31,26 +46,32 @@ public class SModal {
|
||||||
return window
|
return window
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
/// stack to store controllers in queue
|
||||||
static var stack: [SModalPresentation] = []
|
static var stack: [SModalPresentation] = []
|
||||||
|
|
||||||
|
/// Boolean value defining if window upon presentation should become a key window.
|
||||||
public static var shouldMakeKey: Bool {
|
public static var shouldMakeKey: Bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// window level on which should appear on top of windows
|
||||||
public static var windowLevel: UIWindowLevel {
|
public static var windowLevel: UIWindowLevel {
|
||||||
return UIWindowLevelAlert - 1
|
return UIWindowLevelAlert - 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// animation duration used to animated transition between presentations
|
||||||
public static var animationDuration: TimeInterval {
|
public static var animationDuration: TimeInterval {
|
||||||
return 0.2
|
return 0.2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Discard curent root view controller and hide window
|
||||||
fileprivate static func dropRootViewController() {
|
fileprivate static func dropRootViewController() {
|
||||||
modalWindow.isHidden = true
|
modalWindow.isHidden = true
|
||||||
modalWindow.rootViewController = nil
|
modalWindow.rootViewController = nil
|
||||||
modalWindow.resignKey()
|
modalWindow.resignKey()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Make our window key window.
|
||||||
fileprivate static func makeKey() {
|
fileprivate static func makeKey() {
|
||||||
if shouldMakeKey {
|
if shouldMakeKey {
|
||||||
SModal.modalWindow.makeKey()
|
SModal.modalWindow.makeKey()
|
||||||
|
@ -58,19 +79,33 @@ public class SModal {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Modal Stack protocol providing main information to operate with SWindow
|
||||||
public protocol SModalStack {
|
public protocol SModalStack {
|
||||||
var stack: [SModalPresentation] { get }
|
/// Define if other objects can discard this one while presenting
|
||||||
var modalStatus: SModalStatus { get }
|
|
||||||
var canDismiss: Bool { get }
|
var canDismiss: Bool { get }
|
||||||
|
/// Define priority of this object
|
||||||
var priority: SModalPriority { get }
|
var priority: SModalPriority { get }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Modal Presentation protocol, provide methods to present and dismiss object.
|
||||||
public protocol SModalPresentation: class, SModalStack {
|
public protocol SModalPresentation: class, SModalStack {
|
||||||
|
/// Present current object or adding it to the stack if presentation in this time is not avaiable
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - animated: Boolean value idicating if presentation should be animated
|
||||||
|
/// - completion: completion block called after presentation procedure or insert to stack
|
||||||
func sPresent(animated: Bool, completion: (() -> Void)?)
|
func sPresent(animated: Bool, completion: (() -> Void)?)
|
||||||
|
/// Dismiss current object from presentation or remove from stack.
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - animated: Boolean value idicating if operation should be animated
|
||||||
|
/// - completion: completion block called after dismiss procedure
|
||||||
func sWithdraw(animated: Bool, completion: (() -> Void)?)
|
func sWithdraw(animated: Bool, completion: (() -> Void)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: - Default implementation of Modal Presentation protocol
|
||||||
extension SModalPresentation {
|
extension SModalPresentation {
|
||||||
|
/// Provide information about current status
|
||||||
final var modalStatus: SModalStatus {
|
final var modalStatus: SModalStatus {
|
||||||
if SModal.modalWindow.rootViewController === self {
|
if SModal.modalWindow.rootViewController === self {
|
||||||
return .presented
|
return .presented
|
||||||
|
@ -81,20 +116,31 @@ extension SModalPresentation {
|
||||||
return .none
|
return .none
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return SWindow stack
|
||||||
final var stack: [SModalPresentation] {
|
final var stack: [SModalPresentation] {
|
||||||
return SModal.stack
|
return SModal.stack
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// By default other objects are not allow to dimiss this one upon presentation
|
||||||
public var canDismiss: Bool {
|
public var canDismiss: Bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Default priority set to Required
|
||||||
public var priority: SModalPriority {
|
public var priority: SModalPriority {
|
||||||
return .Required
|
return .Required
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// MARK: - Default implementation of Modal Stac method with additional helper methods
|
||||||
extension SModalPresentation where Self: UIViewController {
|
extension SModalPresentation where Self: UIViewController {
|
||||||
|
/// Helper method provide ability to replace current presented object with passed one in method argument with out touching stack.
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - controller: Controller to replace current presented one
|
||||||
|
/// - animated: Boolean value idicating if operation should be animated
|
||||||
|
/// - completion: Completion block called on the end of operation
|
||||||
public func sReplace<T: UIViewController>(with controller: T, animated: Bool = false, completion: (() -> Void)? = nil) where T: SModalPresentation {
|
public func sReplace<T: UIViewController>(with controller: T, animated: Bool = false, completion: (() -> Void)? = nil) where T: SModalPresentation {
|
||||||
if animated {
|
if animated {
|
||||||
SModal.modalWindow.isHidden = false
|
SModal.modalWindow.isHidden = false
|
||||||
|
@ -114,6 +160,11 @@ extension SModalPresentation where Self: UIViewController {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Default implementation, Object is presented if avaiable or placed in stack. If other object is current presented but can be dissmised SModal is replacing him the next one from stack.
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - animated: Boolean value idicating if operation should be animated
|
||||||
|
/// - completion: Completion block called on the end of operation
|
||||||
public func sPresent(animated: Bool = false, completion: (() -> Void)? = nil) {
|
public func sPresent(animated: Bool = false, completion: (() -> Void)? = nil) {
|
||||||
guard let currentPresented = SModal.modalWindow.rootViewController else {
|
guard let currentPresented = SModal.modalWindow.rootViewController else {
|
||||||
SModal.modalWindow.rootViewController = self
|
SModal.modalWindow.rootViewController = self
|
||||||
|
@ -140,6 +191,11 @@ extension SModalPresentation where Self: UIViewController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Default implementation, upon withdraw object is removed from presentation or stack and the next one from the stack is presented if avaiable.
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - animated: Boolean value idicating if operation should be animated
|
||||||
|
/// - completion: Completion block called on the end of operation
|
||||||
public func sWithdraw(animated: Bool = false, completion: (() -> Void)? = nil) {
|
public func sWithdraw(animated: Bool = false, completion: (() -> Void)? = nil) {
|
||||||
func completedProcedure() {
|
func completedProcedure() {
|
||||||
if let controller = SModal.stack.sorted(by: {$0.priority > $1.priority}).first {
|
if let controller = SModal.stack.sorted(by: {$0.priority > $1.priority}).first {
|
||||||
|
|
Loading…
Reference in New Issue