2019-07-31 17:40:39 +08:00
|
|
|
|
//
|
|
|
|
|
// AlertModel.swift
|
|
|
|
|
// ProHUD
|
|
|
|
|
//
|
|
|
|
|
// Created by xaoxuu on 2019/7/29.
|
|
|
|
|
// Copyright © 2019 Titan Studio. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
2019-08-05 20:13:17 +08:00
|
|
|
|
import UIKit
|
|
|
|
|
|
2019-08-08 11:09:22 +08:00
|
|
|
|
public extension Alert {
|
2019-07-31 17:40:39 +08:00
|
|
|
|
enum Scene {
|
|
|
|
|
/// 默认场景
|
|
|
|
|
case `default`
|
|
|
|
|
|
|
|
|
|
/// 加载中场景
|
|
|
|
|
case loading
|
|
|
|
|
|
|
|
|
|
/// 确认场景
|
|
|
|
|
case confirm
|
|
|
|
|
|
|
|
|
|
/// 删除场景
|
|
|
|
|
case delete
|
|
|
|
|
|
|
|
|
|
/// 成功场景
|
|
|
|
|
case success
|
|
|
|
|
|
|
|
|
|
/// 警告场景
|
|
|
|
|
case warning
|
|
|
|
|
|
|
|
|
|
/// 错误场景
|
|
|
|
|
case error
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-12 15:02:36 +08:00
|
|
|
|
class ViewModel {
|
2019-07-31 17:40:39 +08:00
|
|
|
|
|
2019-08-09 18:02:41 +08:00
|
|
|
|
/// ID标识
|
|
|
|
|
public var identifier = String(Date().timeIntervalSince1970)
|
|
|
|
|
|
2019-07-31 17:40:39 +08:00
|
|
|
|
/// 使用场景
|
|
|
|
|
public var scene = Scene.default
|
|
|
|
|
|
|
|
|
|
/// 标题
|
2019-08-09 18:02:41 +08:00
|
|
|
|
public var title: String? {
|
|
|
|
|
didSet {
|
|
|
|
|
vc?.titleLabel?.text = title
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-31 17:40:39 +08:00
|
|
|
|
|
|
|
|
|
/// 正文
|
2019-08-09 18:02:41 +08:00
|
|
|
|
public var message: String? {
|
|
|
|
|
didSet {
|
|
|
|
|
vc?.bodyLabel?.text = message
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-31 17:40:39 +08:00
|
|
|
|
|
|
|
|
|
/// 图标
|
2019-08-09 18:02:41 +08:00
|
|
|
|
public var icon: UIImage? {
|
|
|
|
|
didSet {
|
|
|
|
|
vc?.imageView?.image = icon
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-31 17:40:39 +08:00
|
|
|
|
|
2019-08-09 18:02:41 +08:00
|
|
|
|
/// 持续时间(为空代表根据场景不同的默认配置,为0代表无穷大)
|
|
|
|
|
public var duration: TimeInterval? {
|
|
|
|
|
didSet {
|
|
|
|
|
durationBlock?.cancel()
|
|
|
|
|
if let t = duration, t > 0 {
|
2019-08-12 15:02:36 +08:00
|
|
|
|
durationBlock = DispatchWorkItem(block: { [weak self] in
|
|
|
|
|
self?.vc?.pop()
|
2019-08-09 18:02:41 +08:00
|
|
|
|
})
|
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now()+t, execute: durationBlock!)
|
|
|
|
|
} else {
|
|
|
|
|
durationBlock = nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public weak var vc: Alert?
|
2019-08-05 11:18:25 +08:00
|
|
|
|
|
2019-08-12 15:02:36 +08:00
|
|
|
|
// MARK: 私有
|
|
|
|
|
|
2019-08-05 11:18:25 +08:00
|
|
|
|
/// 持续时间
|
|
|
|
|
internal var durationBlock: DispatchWorkItem?
|
|
|
|
|
|
2019-08-05 19:14:25 +08:00
|
|
|
|
/// 强制退出按钮
|
2019-08-05 11:18:25 +08:00
|
|
|
|
internal var forceQuitTimerBlock: DispatchWorkItem?
|
|
|
|
|
|
|
|
|
|
/// 强制退出代码
|
|
|
|
|
internal var forceQuitCallback: (() -> Void)?
|
|
|
|
|
|
2019-08-09 18:02:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public extension Alert.ViewModel {
|
|
|
|
|
|
|
|
|
|
/// 添加按钮
|
|
|
|
|
/// - Parameter style: 样式
|
|
|
|
|
/// - Parameter text: 标题
|
|
|
|
|
/// - Parameter handler: 事件处理
|
2019-08-12 15:02:36 +08:00
|
|
|
|
@discardableResult func add(action style: UIAlertAction.Style, title: String?, handler: (() -> Void)?) -> UIButton {
|
2019-08-09 18:02:41 +08:00
|
|
|
|
duration = 0
|
2019-08-12 15:02:36 +08:00
|
|
|
|
return vc!.insert(action: nil, style: style, title: title, handler: handler)
|
2019-08-09 18:02:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 插入按钮
|
|
|
|
|
/// - Parameter index: 索引
|
|
|
|
|
/// - Parameter style: 样式
|
|
|
|
|
/// - Parameter title: 标题
|
|
|
|
|
/// - Parameter handler: 事件处理
|
2019-08-12 15:02:36 +08:00
|
|
|
|
@discardableResult func insert(action index: Int, style: UIAlertAction.Style, title: String?, handler: (() -> Void)?) -> UIButton {
|
2019-08-09 18:02:41 +08:00
|
|
|
|
duration = 0
|
2019-08-12 15:02:36 +08:00
|
|
|
|
return vc!.insert(action: index, style: style, title: title, handler: handler)
|
2019-08-09 18:02:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 更新按钮
|
|
|
|
|
/// - Parameter index: 索引
|
|
|
|
|
/// - Parameter style: 样式
|
|
|
|
|
/// - Parameter title: 标题
|
|
|
|
|
/// - Parameter handler: 事件处理
|
2019-08-12 15:02:36 +08:00
|
|
|
|
func update(action index: Int, style: UIAlertAction.Style, title: String?, handler: (() -> Void)?) {
|
|
|
|
|
vc?.update(action: index, style: style, title: title, handler: handler)
|
2019-08-09 18:02:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 移除按钮
|
|
|
|
|
/// - Parameter index: 索引
|
2019-08-12 15:02:36 +08:00
|
|
|
|
func remove(action index: Int...) {
|
2019-08-09 18:02:41 +08:00
|
|
|
|
for (i, idx) in index.enumerated() {
|
2019-08-12 15:02:36 +08:00
|
|
|
|
vc?.remove(action: idx-i)
|
2019-08-05 14:20:52 +08:00
|
|
|
|
}
|
2019-07-31 17:40:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-09 18:02:41 +08:00
|
|
|
|
|
2019-07-31 17:40:39 +08:00
|
|
|
|
}
|