diff --git a/Example-Old/Example/TestAlertVC.swift b/Example-Old/Example/TestAlertVC.swift index 6ee94f9..69be621 100644 --- a/Example-Old/Example/TestAlertVC.swift +++ b/Example-Old/Example/TestAlertVC.swift @@ -116,24 +116,15 @@ class TestAlertVC: BaseListVC { loading() } else if row == 4 { func loading(_ index: Int = 1) { - Alert.find("loading", last: { (a) in - Toast.find("loading-tip", last: { (t) in + if let _ = Alert.find("loading").last { + Toast.push("loading-tip") { (t) in t.update { (vm) in vm.title = "此时又调用了一次相同的弹窗 x\(index)" } t.pulse() - }) { - Toast.push(title: "此时又调用了一次相同的弹窗 x\(index)", message: "页面应该是没有任何变化的") { (t) in - t.identifier = "loading-tip" - t.update { (vm) in - vm.scene = .default - - } - } } - }) { - Alert.push() { (a) in - a.identifier = "loading" + } else { + Alert.push("loading") { (a) in a.update { (vm) in vm.scene = .loading vm.title = "正在加载" diff --git a/Example-Old/Example/TestGuardVC.swift b/Example-Old/Example/TestGuardVC.swift index 0dc131a..ef86510 100644 --- a/Example-Old/Example/TestGuardVC.swift +++ b/Example-Old/Example/TestGuardVC.swift @@ -26,7 +26,7 @@ class TestGuardVC: BaseListVC { tableView.deselectRow(at: indexPath, animated: true) let row = indexPath.row if row == 0 { - Guard.push() { (vc) in + Guard.push("del") { (vc) in vc.update { (vm) in vm.add(action: .destructive, title: "删除") { [weak vc] in Alert.push(scene: .delete) { (vc) in @@ -44,50 +44,48 @@ class TestGuardVC: BaseListVC { } } else if row == 1 { // 可以通过id来避免重复 - Guard.find("pro") { - Guard.push() { (vc) in - vc.identifier = "pro" - vc.update { (vm) in - vm.add(title: "升级至专业版") - vm.add(subTitle: "解锁功能") - vm.add(message: "功能1功能2...") - vm.add(subTitle: "价格") - vm.add(message: "只需一次性付费$2999即可永久享用。") - vm.add(message: "只需一次性付费$2999即可永久享用。") - vm.add(action: .destructive, title: "购买") { [weak vc] in - Alert.push(scene: .buy) { (vc) in - vc.identifier = "confirm" - vc.update { (vm) in - vm.add(action: .destructive, title: "购买") { [weak vc] in + Guard.push("pro") { (vc) in + vc.identifier = "pro" + vc.update { (vm) in + vm.add(title: "升级至专业版") + vm.add(subTitle: "解锁功能") + vm.add(message: "功能1功能2...") + vm.add(subTitle: "价格") + vm.add(message: "只需一次性付费$2999即可永久享用。") + vm.add(message: "只需一次性付费$2999即可永久享用。") + vm.add(action: .destructive, title: "购买") { [weak vc] in + Alert.push(scene: .buy) { (vc) in + vc.identifier = "confirm" + vc.update { (vm) in + vm.add(action: .destructive, title: "购买") { [weak vc] in + vc?.update({ (vm) in + vm.scene = .loading + vm.title = "正在付款" + vm.message = "请稍等片刻" + vm.remove(action: 0, 1) + }) + vc?.startRotate() + DispatchQueue.main.asyncAfter(deadline: .now()+1) { vc?.update({ (vm) in - vm.scene = .loading - vm.title = "正在付款" - vm.message = "请稍等片刻" - vm.remove(action: 0, 1) + vm.scene = .success + vm.title = "购买成功" + vm.message = "感谢您的支持" + vm.add(action: .default, title: "我知道了") { + vc?.pop() + } }) - vc?.startRotate() - DispatchQueue.main.asyncAfter(deadline: .now()+1) { - vc?.update({ (vm) in - vm.scene = .success - vm.title = "购买成功" - vm.message = "感谢您的支持" - vm.add(action: .default, title: "我知道了") { - vc?.pop() - } - }) - } } - vm.add(action: .cancel, title: "取消", handler: nil) } + vm.add(action: .cancel, title: "取消", handler: nil) } - vc?.pop() } - vm.add(action: .cancel, title: "取消", handler: nil) + vc?.pop() } + vm.add(action: .cancel, title: "取消", handler: nil) } } } else if row == 2 { - Guard.push() { (vc) in + Guard.push("license") { (vc) in vc.isFullScreen = true vc.update { (vm) in let titleLabel = vm.add(title: "隐私协议") @@ -105,9 +103,7 @@ class TestGuardVC: BaseListVC { vc?.pop() } } - } - } else if row == 3 { let ac = UIAlertController(title: "Title", message: "message", preferredStyle: .actionSheet) let ok = UIAlertAction(title: "OK", style: .default, handler: nil) @@ -120,7 +116,6 @@ class TestGuardVC: BaseListVC { let vc = UIViewController() vc.view.backgroundColor = .white vc.title = "ceshi" - present(vc, animated: true, completion: nil) } diff --git a/Example-Old/Example/TestToastVC.swift b/Example-Old/Example/TestToastVC.swift index ed2187b..123e0bd 100644 --- a/Example-Old/Example/TestToastVC.swift +++ b/Example-Old/Example/TestToastVC.swift @@ -98,12 +98,12 @@ class TestToastVC: BaseListVC { } } } else if row == 7 { - Toast.find("aaa", last: { (t) in + if let t = Toast.find("aaa").last { t.pulse() t.update() { (vm) in vm.title = "已经存在了" } - }) { + } else { Toast.push(title: "这是一条id为aaa的横幅", message: "避免重复发布同一条信息") { (t) in t.identifier = "aaa" t.update { (vm) in @@ -113,14 +113,14 @@ class TestToastVC: BaseListVC { } } } else if row == 8 { - Toast.find("aaa", last: { (t) in + Toast.push("aaa") { (t) in t.update { (vm) in vm.scene = .success vm.title = "找到了哈哈" vm.message = "根据id查找并修改实例" } - }) - + t.pulse() + } } else if row == 9 { Toast.push() { (a) in a.update { (vm) in diff --git a/Source/Alert/AlertController.swift b/Source/Alert/AlertController.swift index fff2b90..466dbcf 100644 --- a/Source/Alert/AlertController.swift +++ b/Source/Alert/AlertController.swift @@ -172,9 +172,26 @@ public extension Alert { return Alert(scene: scene, title: title, message: message, actions: actions).push() } + /// 创建实例并推入屏幕 + /// - Parameters: + /// - identifier: 唯一标识 + /// - toast: 实例对象 + /// - Returns: 回调 + @discardableResult class func push(_ identifier: String, instance: @escaping (Alert) -> Void) -> Alert { + if let a = find(identifier).last { + instance(a) + return a + } else { + return Alert() { (aa) in + aa.identifier = identifier + instance(aa) + }.push() + } + } + /// 查找指定的实例 /// - Parameter identifier: 指定实例的标识 - class func find(_ identifier: String?) -> [Alert] { + class func find(_ identifier: String) -> [Alert] { var aa = [Alert]() for a in Alert.alerts { if a.identifier == identifier { @@ -188,11 +205,9 @@ public extension Alert { /// - Parameter identifier: 标识 /// - Parameter last: 已经存在(获取最后一个) /// - Parameter none: 不存在 - class func find(_ identifier: String?, last: ((Alert) -> Void)? = nil, none: (() -> Void)? = nil) { + class func find(_ identifier: String, last: @escaping (Alert) -> Void) { if let t = find(identifier).last { - last?(t) - } else { - none?() + last(t) } } @@ -204,7 +219,7 @@ public extension Alert { /// 弹出屏幕 /// - Parameter identifier: 指定实例的标识 - class func pop(_ identifier: String?) { + class func pop(_ identifier: String) { for a in find(identifier) { a.pop() } diff --git a/Source/Guard/GuardController.swift b/Source/Guard/GuardController.swift index 59f0e11..d050a7e 100644 --- a/Source/Guard/GuardController.swift +++ b/Source/Guard/GuardController.swift @@ -180,6 +180,23 @@ public extension Guard { return Guard(actions: actions).push(to: viewController) } + /// 创建实例并推入屏幕 + /// - Parameters: + /// - identifier: 唯一标识 + /// - toast: 实例对象 + /// - Returns: 回调 + @discardableResult class func push(_ identifier: String, to viewController: UIViewController? = nil, _ instance: @escaping (Guard) -> Void) -> Guard { + if let g = find(identifier).last { + instance(g) + return g + } else { + return Guard() { (gg) in + gg.identifier = identifier + instance(gg) + }.push(to: viewController) + } + } + /// 查找指定的实例 /// - Parameter identifier: 指定实例的标识 class func find(_ identifier: String?, from viewController: UIViewController? = nil) -> [Guard] { @@ -206,11 +223,9 @@ public extension Guard { /// - Parameter identifier: 标识 /// - Parameter last: 已经存在(获取最后一个) /// - Parameter none: 不存在 - class func find(_ identifier: String?, from viewController: UIViewController? = nil, last: ((Guard) -> Void)? = nil, none: (() -> Void)? = nil) { + class func find(_ identifier: String?, from viewController: UIViewController? = nil, last: @escaping (Guard) -> Void) { if let t = find(identifier, from: viewController).last { - last?(t) - } else { - none?() + last(t) } } diff --git a/Source/Toast/ToastController.swift b/Source/Toast/ToastController.swift index 62e0d16..68de5a3 100644 --- a/Source/Toast/ToastController.swift +++ b/Source/Toast/ToastController.swift @@ -213,7 +213,7 @@ extension Toast: LoadingRotateAnimation {} // MARK: - 实例管理器 public extension Toast { - /// 推入屏幕 + /// 创建实例并推入屏幕 /// - Parameter toast: 场景 /// - Parameter title: 标题 /// - Parameter message: 内容 @@ -222,9 +222,26 @@ public extension Toast { return Toast(scene: scene, title: title, message: message, duration: duration, actions: actions).push() } + /// 创建实例并推入屏幕 + /// - Parameters: + /// - identifier: 唯一标识 + /// - toast: 实例对象 + /// - Returns: 回调 + @discardableResult class func push(_ identifier: String, instance: @escaping (Toast) -> Void) -> Toast { + if let t = find(identifier).last { + instance(t) + return t + } else { + return Toast() { (tt) in + tt.identifier = identifier + instance(tt) + }.push() + } + } + /// 查找指定的实例 /// - Parameter identifier: 标识 - class func find(_ identifier: String?) -> [Toast] { + class func find(_ identifier: String) -> [Toast] { var tt = [Toast]() for t in toasts { if t.identifier == identifier { @@ -236,13 +253,10 @@ public extension Toast { /// 查找指定的实例 /// - Parameter identifier: 标识 - /// - Parameter last: 已经存在(获取最后一个) - /// - Parameter none: 不存在 - class func find(_ identifier: String?, last: ((Toast) -> Void)? = nil, none: (() -> Void)? = nil) { + /// - Parameter last: 已经存在(获取最后一个) + class func find(_ identifier: String, last: @escaping (Toast) -> Void) { if let t = find(identifier).last { - last?(t) - } else { - none?() + last(t) } } @@ -274,7 +288,7 @@ public extension Toast { /// 弹出屏幕 /// - Parameter identifier: 指定实例的标识 - class func pop(_ identifier: String?) { + class func pop(_ identifier: String) { for t in find(identifier) { t.pop() } @@ -290,8 +304,11 @@ fileprivate extension Toast { /// 点击事件 /// - Parameter sender: 手势 @objc func privDidTapped(_ sender: UITapGestureRecognizer) { - pulse() - vm.tapCallback?() + if let cb = vm.tapCallback { + cb() + } else { + pulse() + } } /// 拖拽事件