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

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()
} 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 = "正在加载"

View File

@ -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,8 +44,7 @@ class TestGuardVC: BaseListVC {
}
} else if row == 1 {
// id
Guard.find("pro") {
Guard.push() { (vc) in
Guard.push("pro") { (vc) in
vc.identifier = "pro"
vc.update { (vm) in
vm.add(title: "升级至专业版")
@ -85,9 +84,8 @@ class TestGuardVC: BaseListVC {
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)
}

View File

@ -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

View File

@ -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()
}

View File

@ -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)
}
}

View File

@ -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 {
@ -237,12 +254,9 @@ public extension Toast {
///
/// - Parameter identifier:
/// - Parameter last:
/// - Parameter none:
class func find(_ identifier: String?, last: ((Toast) -> Void)? = nil, none: (() -> Void)? = nil) {
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) {
if let cb = vm.tapCallback {
cb()
} else {
pulse()
vm.tapCallback?()
}
}
///