增加Sheet管理

This commit is contained in:
xaoxuu 2022-09-19 16:21:49 +08:00
parent c456398701
commit 11d1aeb04d
6 changed files with 77 additions and 34 deletions

View File

@ -79,22 +79,22 @@ public class Alert: ProHUD.Controller {
} }
} }
@discardableResult public init(_ vm: ViewModel?, callback: ((_ alert: Alert) -> Void)? = nil) { @discardableResult public init(_ vm: ViewModel?, handler: ((_ alert: Alert) -> Void)? = nil) {
super.init() super.init()
if let vm = vm { if let vm = vm {
self.vm = vm self.vm = vm
} }
callback?(self) handler?(self)
DispatchQueue.main.async { DispatchQueue.main.async {
if callback != nil { if handler != nil {
self.setDefaultAxis() self.setDefaultAxis()
self.push() self.push()
} }
} }
} }
@discardableResult public convenience init(callback: ((_ alert: Alert) -> Void)?) { @discardableResult public convenience init(handler: ((_ alert: Alert) -> Void)?) {
self.init(nil, callback: callback) self.init(nil, handler: handler)
} }
public override func viewDidLoad() { public override func viewDidLoad() {

View File

@ -64,28 +64,28 @@ extension Alert: HUD {
} }
extension Alert { public extension Alert {
/// HUD /// HUD
/// - Parameters: /// - Parameters:
/// - identifier: /// - identifier:
/// - callback: /// - handler:
public static func lazyPush(identifier: String? = nil, file: String = #file, line: Int = #line, callback: @escaping (_ alert: Alert) -> Void) { static func lazyPush(identifier: String? = nil, file: String = #file, line: Int = #line, handler: @escaping (_ alert: Alert) -> Void) {
let id = identifier ?? (file + "#\(line)") let id = identifier ?? (file + "#\(line)")
if let vc = AlertWindow.alerts.last(where: { $0.identifier == id }) { if let vc = AlertWindow.alerts.last(where: { $0.identifier == id }) {
callback(vc) handler(vc)
vc.reloadData() vc.reloadData()
} else { } else {
Alert { hud in Alert { alert in
hud.identifier = id alert.identifier = id
callback(hud) handler(alert)
}.push() }
} }
} }
/// HUD /// HUD
/// - Parameter callback: /// - Parameter callback:
public func update(handler: @escaping (_ alert: Alert) -> Void) { func update(handler: @escaping (_ alert: Alert) -> Void) {
handler(self) handler(self)
reloadData() reloadData()
UIView.animateEaseOut(duration: config.animateDurationForReloadByDefault) { UIView.animateEaseOut(duration: config.animateDurationForReloadByDefault) {
@ -96,7 +96,7 @@ extension Alert {
/// HUD /// HUD
/// - Parameter identifier: /// - Parameter identifier:
/// - Returns: HUD /// - Returns: HUD
@discardableResult public static func find(identifier: String, update handler: ((_ alert: Alert) -> Void)? = nil) -> [Alert] { @discardableResult static func find(identifier: String, update handler: ((_ alert: Alert) -> Void)? = nil) -> [Alert] {
let arr = AlertWindow.alerts.filter({ $0.identifier == identifier }) let arr = AlertWindow.alerts.filter({ $0.identifier == identifier })
if let handler = handler { if let handler = handler {
arr.forEach({ $0.update(handler: handler) }) arr.forEach({ $0.update(handler: handler) })

View File

@ -40,11 +40,11 @@ public class Sheet: Controller {
} }
} }
@discardableResult public init(callback: @escaping (_ sheet: Sheet) -> Void, onTappedBackground action: ((_ sheet: Sheet) -> Void)? = nil) { @discardableResult public init(handler: @escaping (_ sheet: Sheet) -> Void, onTappedBackground action: ((_ sheet: Sheet) -> Void)? = nil) {
super.init() super.init()
onTappedBackground = action onTappedBackground = action
callback(self) handler(self)
DispatchQueue.main.async { DispatchQueue.main.async {
SheetWindow.push(sheet: self) SheetWindow.push(sheet: self)

View File

@ -19,6 +19,49 @@ extension Sheet: HUD {
} }
public extension Sheet {
/// HUD
/// - Parameters:
/// - identifier:
/// - handler:
static func lazyPush(identifier: String? = nil, file: String = #file, line: Int = #line, handler: @escaping (_ sheet: Sheet) -> Void) {
let id = identifier ?? (file + "#\(line)")
if let vc = find(identifier: id).last {
handler(vc)
vc.reloadData()
} else {
Sheet { sheet in
sheet.identifier = id
handler(sheet)
}
}
}
/// HUD
/// - Parameter handler:
func update(handler: @escaping (_ sheet: Sheet) -> Void) {
handler(self)
reloadData()
UIView.animateEaseOut(duration: config.animateDurationForReloadByDefault) {
self.view.layoutIfNeeded()
}
}
/// HUD
/// - Parameter identifier:
/// - Returns: HUD
@discardableResult static func find(identifier: String, update handler: ((_ sheet: Sheet) -> Void)? = nil) -> [Sheet] {
let arr = SheetWindow.windows.compactMap({ $0.sheet }).filter({ $0.identifier == identifier })
if let handler = handler {
arr.forEach({ $0.update(handler: handler) })
}
return arr
}
}
extension Sheet { extension Sheet {
func translateIn(completion: (() -> Void)?) { func translateIn(completion: (() -> Void)?) {

View File

@ -92,21 +92,21 @@ public class Toast: Controller {
} }
@discardableResult public init(_ vm: ViewModel?, callback: ((_ toast: Toast) -> Void)? = nil) { @discardableResult public init(_ vm: ViewModel?, handler: ((_ toast: Toast) -> Void)? = nil) {
super.init() super.init()
if let vm = vm { if let vm = vm {
self.vm = vm self.vm = vm
} }
callback?(self) handler?(self)
DispatchQueue.main.async { DispatchQueue.main.async {
if callback != nil { if handler != nil {
ToastWindow.push(toast: self) ToastWindow.push(toast: self)
} }
} }
} }
@discardableResult public convenience init(callback: ((_ toast: Toast) -> Void)?) { @discardableResult public convenience init(handler: ((_ toast: Toast) -> Void)?) {
self.init(nil, callback: callback) self.init(nil, handler: handler)
} }
required public init?(coder: NSCoder) { required public init?(coder: NSCoder) {

View File

@ -19,28 +19,28 @@ extension Toast: HUD {
} }
extension Toast { public extension Toast {
/// HUD /// HUD
/// - Parameters: /// - Parameters:
/// - identifier: /// - identifier:
/// - callback: /// - handler:
public static func lazyPush(identifier: String? = nil, file: String = #file, line: Int = #line, callback: @escaping (_ toast: Toast) -> Void) { static func lazyPush(identifier: String? = nil, file: String = #file, line: Int = #line, handler: @escaping (_ toast: Toast) -> Void) {
let id = identifier ?? (file + "#\(line)") let id = identifier ?? (file + "#\(line)")
if let vc = ToastWindow.windows.last(where: { $0.toast.identifier == id })?.toast { if let vc = find(identifier: id).last {
callback(vc) handler(vc)
vc.reloadData() vc.reloadData()
} else { } else {
Toast { hud in Toast { toast in
hud.identifier = id toast.identifier = id
callback(hud) handler(toast)
}.push() }
} }
} }
/// HUD /// HUD
/// - Parameter callback: /// - Parameter handler:
public func update(handler: @escaping (_ toast: Toast) -> Void) { func update(handler: @escaping (_ toast: Toast) -> Void) {
handler(self) handler(self)
reloadData() reloadData()
UIView.animateEaseOut(duration: config.animateDurationForReloadByDefault) { UIView.animateEaseOut(duration: config.animateDurationForReloadByDefault) {
@ -51,7 +51,7 @@ extension Toast {
/// HUD /// HUD
/// - Parameter identifier: /// - Parameter identifier:
/// - Returns: HUD /// - Returns: HUD
@discardableResult public static func find(identifier: String, update handler: ((_ toast: Toast) -> Void)? = nil) -> [Toast] { @discardableResult static func find(identifier: String, update handler: ((_ toast: Toast) -> Void)? = nil) -> [Toast] {
let arr = ToastWindow.windows.compactMap({ $0.toast }).filter({ $0.identifier == identifier }) let arr = ToastWindow.windows.compactMap({ $0.toast }).filter({ $0.identifier == identifier })
if let handler = handler { if let handler = handler {
arr.forEach({ $0.update(handler: handler) }) arr.forEach({ $0.update(handler: handler) })