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()
|
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() {
|
||||||
|
|
|
@ -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) })
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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)?) {
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
|
@ -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) })
|
||||||
|
|
Loading…
Reference in New Issue