增加一个更简便更强大的接口

This commit is contained in:
xaoxuu 2020-06-22 14:09:34 +08:00
parent 0d60632592
commit 24469b8d62
6 changed files with 110 additions and 77 deletions

View File

@ -116,24 +116,15 @@ class TestAlertVC: BaseListVC {
loading() loading()
} else if row == 4 { } else if row == 4 {
func loading(_ index: Int = 1) { func loading(_ index: Int = 1) {
Alert.find("loading", last: { (a) in if let _ = Alert.find("loading").last {
Toast.find("loading-tip", last: { (t) in Toast.push("loading-tip") { (t) in
t.update { (vm) in t.update { (vm) in
vm.title = "此时又调用了一次相同的弹窗 x\(index)" vm.title = "此时又调用了一次相同的弹窗 x\(index)"
} }
t.pulse() t.pulse()
}) {
Toast.push(title: "此时又调用了一次相同的弹窗 x\(index)", message: "页面应该是没有任何变化的") { (t) in
t.identifier = "loading-tip"
t.update { (vm) in
vm.scene = .default
} }
} } else {
} Alert.push("loading") { (a) in
}) {
Alert.push() { (a) in
a.identifier = "loading"
a.update { (vm) in a.update { (vm) in
vm.scene = .loading vm.scene = .loading
vm.title = "正在加载" vm.title = "正在加载"

View File

@ -26,7 +26,7 @@ class TestGuardVC: BaseListVC {
tableView.deselectRow(at: indexPath, animated: true) tableView.deselectRow(at: indexPath, animated: true)
let row = indexPath.row let row = indexPath.row
if row == 0 { if row == 0 {
Guard.push() { (vc) in Guard.push("del") { (vc) in
vc.update { (vm) in vc.update { (vm) in
vm.add(action: .destructive, title: "删除") { [weak vc] in vm.add(action: .destructive, title: "删除") { [weak vc] in
Alert.push(scene: .delete) { (vc) in Alert.push(scene: .delete) { (vc) in
@ -44,8 +44,7 @@ class TestGuardVC: BaseListVC {
} }
} else if row == 1 { } else if row == 1 {
// id // id
Guard.find("pro") { Guard.push("pro") { (vc) in
Guard.push() { (vc) in
vc.identifier = "pro" vc.identifier = "pro"
vc.update { (vm) in vc.update { (vm) in
vm.add(title: "升级至专业版") vm.add(title: "升级至专业版")
@ -85,9 +84,8 @@ class TestGuardVC: BaseListVC {
vm.add(action: .cancel, title: "取消", handler: nil) vm.add(action: .cancel, title: "取消", handler: nil)
} }
} }
}
} else if row == 2 { } else if row == 2 {
Guard.push() { (vc) in Guard.push("license") { (vc) in
vc.isFullScreen = true vc.isFullScreen = true
vc.update { (vm) in vc.update { (vm) in
let titleLabel = vm.add(title: "隐私协议") let titleLabel = vm.add(title: "隐私协议")
@ -105,9 +103,7 @@ class TestGuardVC: BaseListVC {
vc?.pop() vc?.pop()
} }
} }
} }
} else if row == 3 { } else if row == 3 {
let ac = UIAlertController(title: "Title", message: "message", preferredStyle: .actionSheet) let ac = UIAlertController(title: "Title", message: "message", preferredStyle: .actionSheet)
let ok = UIAlertAction(title: "OK", style: .default, handler: nil) let ok = UIAlertAction(title: "OK", style: .default, handler: nil)
@ -120,7 +116,6 @@ class TestGuardVC: BaseListVC {
let vc = UIViewController() let vc = UIViewController()
vc.view.backgroundColor = .white vc.view.backgroundColor = .white
vc.title = "ceshi" vc.title = "ceshi"
present(vc, animated: true, completion: nil) present(vc, animated: true, completion: nil)
} }

View File

@ -98,12 +98,12 @@ class TestToastVC: BaseListVC {
} }
} }
} else if row == 7 { } else if row == 7 {
Toast.find("aaa", last: { (t) in if let t = Toast.find("aaa").last {
t.pulse() t.pulse()
t.update() { (vm) in t.update() { (vm) in
vm.title = "已经存在了" vm.title = "已经存在了"
} }
}) { } else {
Toast.push(title: "这是一条id为aaa的横幅", message: "避免重复发布同一条信息") { (t) in Toast.push(title: "这是一条id为aaa的横幅", message: "避免重复发布同一条信息") { (t) in
t.identifier = "aaa" t.identifier = "aaa"
t.update { (vm) in t.update { (vm) in
@ -113,14 +113,14 @@ class TestToastVC: BaseListVC {
} }
} }
} else if row == 8 { } else if row == 8 {
Toast.find("aaa", last: { (t) in Toast.push("aaa") { (t) in
t.update { (vm) in t.update { (vm) in
vm.scene = .success vm.scene = .success
vm.title = "找到了哈哈" vm.title = "找到了哈哈"
vm.message = "根据id查找并修改实例" vm.message = "根据id查找并修改实例"
} }
}) t.pulse()
}
} else if row == 9 { } else if row == 9 {
Toast.push() { (a) in Toast.push() { (a) in
a.update { (vm) in a.update { (vm) in

View File

@ -172,9 +172,26 @@ public extension Alert {
return Alert(scene: scene, title: title, message: message, actions: actions).push() 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: /// - Parameter identifier:
class func find(_ identifier: String?) -> [Alert] { class func find(_ identifier: String) -> [Alert] {
var aa = [Alert]() var aa = [Alert]()
for a in Alert.alerts { for a in Alert.alerts {
if a.identifier == identifier { if a.identifier == identifier {
@ -188,11 +205,9 @@ public extension Alert {
/// - Parameter identifier: /// - Parameter identifier:
/// - Parameter last: /// - Parameter last:
/// - Parameter none: /// - 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 { if let t = find(identifier).last {
last?(t) last(t)
} else {
none?()
} }
} }
@ -204,7 +219,7 @@ public extension Alert {
/// ///
/// - Parameter identifier: /// - Parameter identifier:
class func pop(_ identifier: String?) { class func pop(_ identifier: String) {
for a in find(identifier) { for a in find(identifier) {
a.pop() a.pop()
} }

View File

@ -180,6 +180,23 @@ public extension Guard {
return Guard(actions: actions).push(to: viewController) 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: /// - Parameter identifier:
class func find(_ identifier: String?, from viewController: UIViewController? = nil) -> [Guard] { class func find(_ identifier: String?, from viewController: UIViewController? = nil) -> [Guard] {
@ -206,11 +223,9 @@ public extension Guard {
/// - Parameter identifier: /// - Parameter identifier:
/// - Parameter last: /// - Parameter last:
/// - Parameter none: /// - 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 { if let t = find(identifier, from: viewController).last {
last?(t) last(t)
} else {
none?()
} }
} }

View File

@ -213,7 +213,7 @@ extension Toast: LoadingRotateAnimation {}
// MARK: - // MARK: -
public extension Toast { public extension Toast {
/// ///
/// - Parameter toast: /// - Parameter toast:
/// - Parameter title: /// - Parameter title:
/// - Parameter message: /// - Parameter message:
@ -222,9 +222,26 @@ public extension Toast {
return Toast(scene: scene, title: title, message: message, duration: duration, actions: actions).push() 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: /// - Parameter identifier:
class func find(_ identifier: String?) -> [Toast] { class func find(_ identifier: String) -> [Toast] {
var tt = [Toast]() var tt = [Toast]()
for t in toasts { for t in toasts {
if t.identifier == identifier { if t.identifier == identifier {
@ -237,12 +254,9 @@ public extension Toast {
/// ///
/// - Parameter identifier: /// - Parameter identifier:
/// - Parameter last: /// - Parameter last:
/// - Parameter none: class func find(_ identifier: String, last: @escaping (Toast) -> Void) {
class func find(_ identifier: String?, last: ((Toast) -> Void)? = nil, none: (() -> Void)? = nil) {
if let t = find(identifier).last { if let t = find(identifier).last {
last?(t) last(t)
} else {
none?()
} }
} }
@ -274,7 +288,7 @@ public extension Toast {
/// ///
/// - Parameter identifier: /// - Parameter identifier:
class func pop(_ identifier: String?) { class func pop(_ identifier: String) {
for t in find(identifier) { for t in find(identifier) {
t.pop() t.pop()
} }
@ -290,8 +304,11 @@ fileprivate extension Toast {
/// ///
/// - Parameter sender: /// - Parameter sender:
@objc func privDidTapped(_ sender: UITapGestureRecognizer) { @objc func privDidTapped(_ sender: UITapGestureRecognizer) {
if let cb = vm.tapCallback {
cb()
} else {
pulse() pulse()
vm.tapCallback?() }
} }
/// ///