This commit is contained in:
xaoxuu 2019-08-08 09:53:54 +08:00
parent 34e7b0ffbb
commit c165d05de4
1 changed files with 30 additions and 67 deletions

View File

@ -8,6 +8,8 @@
import SnapKit import SnapKit
public typealias Guard = ProHUD.Guard
public extension ProHUD { public extension ProHUD {
class Guard: HUDController { class Guard: HUDController {
@ -51,11 +53,10 @@ public extension ProHUD {
// MARK: // MARK:
/// ///
/// - Parameter scene:
/// - Parameter title: /// - Parameter title:
/// - Parameter message: /// - Parameter message:
/// - Parameter icon: /// - Parameter actions:
public convenience init(title: String? = nil, message: String? = nil) { public convenience init(title: String? = nil, message: String? = nil, actions: ((Guard) -> Void)? = nil) {
self.init() self.init()
view.tintColor = cfg.guard.tintColor view.tintColor = cfg.guard.tintColor
@ -65,11 +66,12 @@ public extension ProHUD {
if let _ = message { if let _ = message {
add(message: message) add(message: message)
} }
actions?(self)
cfg.guard.loadSubviews(self) cfg.guard.loadSubviews(self)
cfg.guard.reloadData(self) cfg.guard.reloadData(self)
cfg.guard.reloadStack(self) cfg.guard.reloadStack(self)
// //
let tap = UITapGestureRecognizer(target: self, action: #selector(privDidTapped(_:))) let tap = UITapGestureRecognizer(target: self, action: #selector(privDidTapped(_:)))
view.addGestureRecognizer(tap) view.addGestureRecognizer(tap)
@ -78,11 +80,12 @@ public extension ProHUD {
} }
} }
// MARK: - // MARK: -
public extension ProHUD.Guard { public extension Guard {
// MARK: // MARK:
@ -90,7 +93,6 @@ public extension ProHUD.Guard {
/// - Parameter viewController: /// - Parameter viewController:
func push(to viewController: UIViewController? = nil) { func push(to viewController: UIViewController? = nil) {
func f(_ vc: UIViewController) { func f(_ vc: UIViewController) {
ProHUD.pop(guard: vc, animated: false)
view.layoutIfNeeded() view.layoutIfNeeded()
vc.addChild(self) vc.addChild(self)
vc.view.addSubview(view) vc.view.addSubview(view)
@ -185,14 +187,14 @@ public extension ProHUD.Guard {
/// - Parameter style: /// - Parameter style:
/// - Parameter title: /// - Parameter title:
/// - Parameter action: /// - Parameter action:
@discardableResult func add(action style: UIAlertAction.Style, title: String?, action: (() -> Void)?) -> UIButton { @discardableResult func add(action style: UIAlertAction.Style, title: String?, handler: (() -> Void)?) -> UIButton {
let btn = Button.actionButton(title: title) let btn = Button.actionButton(title: title)
btn.titleLabel?.font = cfg.guard.buttonFont btn.titleLabel?.font = cfg.guard.buttonFont
actionStack.addArrangedSubview(btn) actionStack.addArrangedSubview(btn)
cfg.guard.reloadStack(self) cfg.guard.reloadStack(self)
btn.update(style: style) btn.update(style: style)
addTouchUpAction(for: btn) { [weak self] in addTouchUpAction(for: btn) { [weak self] in
action?() handler?()
if btn.tag == UIAlertAction.Style.cancel.rawValue { if btn.tag == UIAlertAction.Style.cancel.rawValue {
self?.pop() self?.pop()
} }
@ -202,7 +204,7 @@ public extension ProHUD.Guard {
/// ///
/// - Parameter index: /// - Parameter index:
@discardableResult func remove(action index: Int...) -> ProHUD.Guard { @discardableResult func remove(action index: Int...) -> Guard {
for (i, idx) in index.enumerated() { for (i, idx) in index.enumerated() {
privRemoveAction(index: idx-i) privRemoveAction(index: idx-i)
} }
@ -211,88 +213,49 @@ public extension ProHUD.Guard {
/// ///
/// - Parameter callback: /// - Parameter callback:
@discardableResult func didDisappear(_ callback: (() -> Void)?) -> ProHUD.Guard { @discardableResult func didDisappear(_ callback: (() -> Void)?) -> Guard {
disappearCallback = callback disappearCallback = callback
return self return self
} }
} }
// MARK:
public extension ProHUD { // MARK:
/// public extension Guard {
/// - Parameter alert:
@discardableResult func push(_ guard: Guard, to viewController: UIViewController? = nil) -> Guard {
`guard`.push(to: viewController)
return `guard`
}
/// ///
/// - Parameter alert: /// - Parameter alert:
/// - Parameter title: /// - Parameter title:
/// - Parameter message: /// - Parameter message:
/// - Parameter icon: /// - Parameter icon:
@discardableResult func push(guard viewController: UIViewController? = nil, title: String? = nil, message: String? = nil, actions: ((Guard) -> Void)? = nil) -> Guard { @discardableResult class func push(to viewController: UIViewController? = nil, actions: ((Guard) -> Void)? = nil) -> Guard {
let g = Guard(title: title, message: message) let g = Guard(actions: actions)
actions?(g)
g.view.layoutIfNeeded()
g.push(to: viewController) g.push(to: viewController)
return g return g
} }
/// ///
/// - Parameter alert: /// - Parameter identifier:
func pop(_ guard: Guard) { class func guards(_ identifier: String?, from viewController: UIViewController? = nil) -> [Guard] {
`guard`.pop() var gg = [Guard]()
} if let vc = viewController ?? cfg.rootViewController {
for child in vc.children {
/// if child.isKind(of: Guard.self) {
/// - Parameter alert: if let g = child as? Guard {
func pop(guard from: UIViewController?, animated: Bool = true) { gg.append(g)
if let vc = from {
for c in vc.children {
if c.isKind(of: Guard.self) {
if let cc = c as? Guard {
cc.pop(animated: animated)
} }
} }
} }
} }
} return gg
}
// MARK:
public extension ProHUD {
///
/// - Parameter alert:
@discardableResult class func push(_ guard: Guard, to viewController: UIViewController? = nil) -> Guard {
return shared.push(`guard`, to: viewController)
}
///
/// - Parameter alert:
/// - Parameter title:
/// - Parameter message:
/// - Parameter icon:
@discardableResult class func push(guard viewController: UIViewController? = nil, title: String? = nil, message: String? = nil, actions: ((Guard) -> Void)? = nil) -> Guard {
return shared.push(guard: viewController, title: title, message: message, actions: actions)
} }
/// ///
/// - Parameter alert: /// - Parameter alert:
class func pop(_ guard: Guard) { class func pop(_ guard: Guard) {
shared.pop(`guard`) `guard`.pop()
}
///
/// - Parameter alert:
class func pop(guard from: UIViewController?, animated: Bool = true) {
shared.pop(guard: from, animated: animated)
} }
@ -302,7 +265,7 @@ public extension ProHUD {
// MARK: - // MARK: -
fileprivate extension ProHUD.Guard { fileprivate extension Guard {
/// ///
/// - Parameter sender: /// - Parameter sender:
@ -329,7 +292,7 @@ fileprivate extension ProHUD.Guard {
/// ///
/// - Parameter index: /// - Parameter index:
@discardableResult func privRemoveAction(index: Int) -> ProHUD.Guard { @discardableResult func privRemoveAction(index: Int) -> Guard {
if index < 0 { if index < 0 {
for view in self.actionStack.arrangedSubviews { for view in self.actionStack.arrangedSubviews {
if let btn = view as? UIButton { if let btn = view as? UIButton {