代码优化

This commit is contained in:
xaoxuu 2022-09-10 13:52:56 +08:00
parent f028cceee9
commit 4b2d9dac3c
9 changed files with 130 additions and 73 deletions

View File

@ -108,8 +108,8 @@ class AlertVC: ListVC {
}
}
}
list.add(title: "控件动态管理") { section in
section.add(title: "按钮增删") {
list.add(title: "控件管理") { section in
section.add(title: "按钮增删改查") {
Alert(.note) { alert in
alert.vm.message = "可以动态增加、删除按钮"
alert.add(action: "在底部增加按钮", style: .filled(color: .systemGreen)) { alert in
@ -172,7 +172,7 @@ class AlertVC: ListVC {
}
}
}
list.add(title: "实例管理") { section in
list.add(title: "实例管理") { section in
section.add(title: "多层级弹窗") {
func f(i: Int) {
Alert { alert in
@ -186,7 +186,7 @@ class AlertVC: ListVC {
}
f(i: 1)
}
section.add(title: "弹出loading如果有就不重复弹") {
section.add(title: "弹出loading如果已经存在就更新") {
func f(i: Int) {
Alert.lazyPush(identifier: "haha") { alert in
if i < 2 {
@ -209,7 +209,7 @@ class AlertVC: ListVC {
}
}
list.add(title: "放在特定页面") { [weak self] section in
section.add(title: "放在特定页面") {
section.add(title: "放在一个VC中") {
let vc = UIViewController()
vc.title = "页面"
vc.view.backgroundColor = .systemYellow

View File

@ -63,18 +63,10 @@ class SheetVC: ListVC {
}
}
section.add(title: "全屏") {
Sheet { sheet in
sheet.config.isFullScreen = true
sheet.add(title: "ProHUD")
sheet.add(action: "OK")
}
}
}
list.add(title: "事件管理") { section in
section.add(title: "拦截返回事件") {
section.add(title: "拦截点击背景事件") {
Sheet { sheet in
sheet.add(title: "ProHUD")
sheet.add(message: "点击背景将不会dismiss必须在下方做出选择才能关掉")
@ -84,7 +76,7 @@ class SheetVC: ListVC {
} onTappedBackground: { sheet in
print("点击了背景")
Toast.lazyPush(identifier: "alert") { toast in
toast.vm = .note
toast.vm = .error
toast.vm.title = "点击了背景"
toast.vm.message = "点击背景将不会dismiss必须在下方做出选择才能关掉"
toast.vm.duration = 2
@ -206,6 +198,15 @@ class SheetVC: ListVC {
}
}
}
section.add(title: "全屏Sheet") {
Sheet { sheet in
sheet.config.isFullScreen = true
sheet.add(title: "ProHUD")
sheet.add(action: "OK")
}
}
}
}

View File

@ -39,7 +39,7 @@ class ToastVC: ListVC {
toast.update(progress: 1)
}
DispatchQueue.main.asyncAfter(deadline: .now() + 2.5) {
Toast.update(identifier: "loading") { toast in
Toast.find(identifier: "loading") { toast in
toast.vm = .success(2).title("加载成功").message("这条通知2s后消失")
}
}
@ -106,13 +106,35 @@ class ToastVC: ListVC {
}
}
list.add(title: "实例管理") { section in
var i = 0
section.add(title: "多实例共存") {
Toast(.loading.title("多实例共存").message("直接创建的实例,以平铺方式排列").duration(2)).push()
}
section.add(title: "不存在就创建,存在就更新") {
i += 1
Toast.lazyPush(identifier: "loading") { toast in
toast.vm = .loading.title("正在加载\(i)").message("这条消息不会重复显示多条")
}
}
section.add(title: "如果存在就更新,如果不存在就忽略") {
i += 1
Toast.find(identifier: "loading") { toast in
toast.vm = .success.title("加载完成\(i)").message("这条消息不会重复显示多条").duration(2)
}
}
section.add(title: "移除指定实例") {
Toast.find(identifier: "loading")?.pop()
}
}
list.add(title: "自定义视图") { section in
section.add(title: "图片") {
Toast { toast in
toast.config.cardMaxHeight = 200 //
let imgv = UIImageView(image: UIImage(named: "landscape"))
imgv.contentMode = .scaleAspectFill
toast.add(customView: imgv).snp.makeConstraints { make in
toast.add(subview: imgv).snp.makeConstraints { make in
make.edges.equalToSuperview()
}
toast.onTapped { toast in
@ -121,6 +143,43 @@ class ToastVC: ListVC {
}
}
section.add(title: "圆角半径") {
func foo() {
Toast { toast in
toast.vm = .title("圆角半径").message("可以在共享配置中设置,则全局生效")
toast.add(action: "8", style: .gray) { toast in
Toast.Configuration.shared { config in
config.cardCornerRadius = 8
}
toast.pop()
foo()
}
toast.add(action: "16", style: .gray) { toast in
Toast.Configuration.shared { config in
config.cardCornerRadius = 16
}
toast.pop()
foo()
}
toast.add(action: "24", style: .gray) { toast in
Toast.Configuration.shared { config in
config.cardCornerRadius = 24
}
toast.pop()
foo()
}
toast.add(action: "32", style: .gray) { toast in
Toast.Configuration.shared { config in
config.cardCornerRadius = 32
}
toast.pop()
foo()
}
}
}
foo()
}
section.add(title: "其它控件") {
Toast { toast in
toast.config.cardMaxHeight = 200 //
@ -158,7 +217,7 @@ class ToastVC: ListVC {
make.height.equalTo(40)
}
toast.add(customView: stack).snp.makeConstraints { make in
toast.add(subview: stack).snp.makeConstraints { make in
make.edges.equalToSuperview().inset(8)
}
toast.onTapped { toast in

View File

@ -85,29 +85,24 @@ extension Alert {
/// HUD
/// - Parameter callback:
public func update(callback: @escaping (_ alert: Alert) -> Void) {
callback(self)
func update(handler: @escaping (_ alert: Alert) -> Void) {
handler(self)
reloadData()
UIView.animateEaseOut(duration: config.animateDurationForReloadByDefault) {
self.view.layoutIfNeeded()
}
/// HUD
/// - Parameters:
/// - identifier:
/// - callback:
public static func update(identifier: String, callback: @escaping (_ alert: Alert) -> Void) {
guard let vc = AlertWindow.alerts.last(where: { $0.identifier == identifier }) else {
return
}
callback(vc)
vc.reloadData()
}
/// HUD
/// - Parameter identifier:
/// - Returns: HUD
public static func find(identifier: String) -> Alert? {
public static func find(identifier: String, update handler: ((_ alert: Alert) -> Void)? = nil) -> Alert? {
guard let vc = AlertWindow.alerts.last(where: { $0.identifier == identifier }) else {
return nil
}
if let handler = handler {
vc.update(handler: handler)
}
return vc
}

View File

@ -73,29 +73,6 @@ public class Sheet: Controller {
}
extension Sheet {
func translateIn(completion: (() -> Void)?) {
UIView.animateEaseOut(duration: config.animateDurationForBuildInByDefault) {
self._translateIn()
} completion: { done in
completion?()
}
}
func translateOut(completion: (() -> Void)?) {
UIView.animateEaseOut(duration: config.animateDurationForBuildOutByDefault) {
self._translateOut()
} completion: { done in
completion?()
}
}
func _translateIn() {
backgroundView.alpha = 1
contentView.transform = .identity
}
func _translateOut() {
backgroundView.alpha = 0
contentView.transform = .init(translationX: 0, y: view.frame.size.height - contentView.frame.minY + config.margin)
}
@objc func _onTappedBackground(_ sender: UITapGestureRecognizer) {
if let act = onTappedBackground {
act(self)

View File

@ -18,3 +18,33 @@ extension Sheet: HUD {
}
}
extension Sheet {
func translateIn(completion: (() -> Void)?) {
UIView.animateEaseOut(duration: config.animateDurationForBuildInByDefault) {
self._translateIn()
} completion: { done in
completion?()
}
}
func translateOut(completion: (() -> Void)?) {
UIView.animateEaseOut(duration: config.animateDurationForBuildOutByDefault) {
self._translateOut()
} completion: { done in
completion?()
}
}
func _translateIn() {
backgroundView.alpha = 1
contentView.transform = .identity
}
func _translateOut() {
backgroundView.alpha = 0
contentView.transform = .init(translationX: 0, y: view.frame.size.height - contentView.frame.minY + config.margin)
}
}

View File

@ -9,12 +9,12 @@ import UIKit
public extension Toast {
@discardableResult func add(customView: UIView) -> UIView {
@discardableResult func add(subview: UIView) -> UIView {
if contentStack.superview != nil {
contentStack.removeFromSuperview()
}
contentView.addSubview(customView)
return customView
contentView.addSubview(subview)
return subview
}
@discardableResult func add(action: Action) -> Button {

View File

@ -40,29 +40,24 @@ extension Toast {
/// HUD
/// - Parameter callback:
public func update(callback: @escaping (_ toast: Toast) -> Void) {
callback(self)
func update(handler: @escaping (_ toast: Toast) -> Void) {
handler(self)
reloadData()
UIView.animateEaseOut(duration: config.animateDurationForReloadByDefault) {
self.view.layoutIfNeeded()
}
/// HUD
/// - Parameters:
/// - identifier:
/// - callback:
public static func update(identifier: String, callback: @escaping (_ toast: Toast) -> Void) {
guard let vc = ToastWindow.windows.last(where: { $0.toast.identifier == identifier })?.toast else {
return
}
callback(vc)
vc.reloadData()
}
/// HUD
/// - Parameter identifier:
/// - Returns: HUD
public static func find(identifier: String) -> Toast? {
@discardableResult public static func find(identifier: String, update handler: ((_ toast: Toast) -> Void)? = nil) -> Toast? {
guard let vc = ToastWindow.windows.last(where: { $0.toast.identifier == identifier })?.toast else {
return nil
}
if let handler = handler {
vc.update(handler: handler)
}
return vc
}