增加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()
if let vm = vm {
self.vm = vm
}
callback?(self)
handler?(self)
DispatchQueue.main.async {
if callback != nil {
if handler != nil {
self.setDefaultAxis()
self.push()
}
}
}
@discardableResult public convenience init(callback: ((_ alert: Alert) -> Void)?) {
self.init(nil, callback: callback)
@discardableResult public convenience init(handler: ((_ alert: Alert) -> Void)?) {
self.init(nil, handler: handler)
}
public override func viewDidLoad() {

View File

@ -64,28 +64,28 @@ extension Alert: HUD {
}
extension Alert {
public extension Alert {
/// HUD
/// - Parameters:
/// - identifier:
/// - callback:
public static func lazyPush(identifier: String? = nil, file: String = #file, line: Int = #line, callback: @escaping (_ alert: Alert) -> Void) {
/// - handler:
static func lazyPush(identifier: String? = nil, file: String = #file, line: Int = #line, handler: @escaping (_ alert: Alert) -> Void) {
let id = identifier ?? (file + "#\(line)")
if let vc = AlertWindow.alerts.last(where: { $0.identifier == id }) {
callback(vc)
handler(vc)
vc.reloadData()
} else {
Alert { hud in
hud.identifier = id
callback(hud)
}.push()
Alert { alert in
alert.identifier = id
handler(alert)
}
}
}
/// HUD
/// - Parameter callback:
public func update(handler: @escaping (_ alert: Alert) -> Void) {
func update(handler: @escaping (_ alert: Alert) -> Void) {
handler(self)
reloadData()
UIView.animateEaseOut(duration: config.animateDurationForReloadByDefault) {
@ -96,7 +96,7 @@ extension Alert {
/// HUD
/// - Parameter identifier:
/// - 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 })
if let 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()
onTappedBackground = action
callback(self)
handler(self)
DispatchQueue.main.async {
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 {
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()
if let vm = vm {
self.vm = vm
}
callback?(self)
handler?(self)
DispatchQueue.main.async {
if callback != nil {
if handler != nil {
ToastWindow.push(toast: self)
}
}
}
@discardableResult public convenience init(callback: ((_ toast: Toast) -> Void)?) {
self.init(nil, callback: callback)
@discardableResult public convenience init(handler: ((_ toast: Toast) -> Void)?) {
self.init(nil, handler: handler)
}
required public init?(coder: NSCoder) {

View File

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