mirror of https://github.com/xaoxuu/ProHUD
增加Sheet管理
This commit is contained in:
parent
c456398701
commit
11d1aeb04d
|
@ -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() {
|
||||
|
|
|
@ -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) })
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)?) {
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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) })
|
||||
|
|
Loading…
Reference in New Issue