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-08-12 15:02:36 +08:00
|
|
|
|
class ViewModel {
|
2019-07-31 17:40:39 +08:00
|
|
|
|
|
|
|
|
|
/// 使用场景
|
2019-08-13 11:32:27 +08:00
|
|
|
|
public var scene = ProHUD.Scene.default
|
2019-07-31 17:40:39 +08:00
|
|
|
|
|
|
|
|
|
/// 标题
|
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 {
|
2020-06-19 19:13:33 +08:00
|
|
|
|
vc?.imageView.image = icon
|
2019-08-09 18:02:41 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-31 17:40:39 +08:00
|
|
|
|
|
2019-08-09 18:02:41 +08:00
|
|
|
|
/// 持续时间(为空代表根据场景不同的默认配置,为0代表无穷大)
|
|
|
|
|
public var duration: TimeInterval? {
|
|
|
|
|
didSet {
|
2019-08-12 17:59:40 +08:00
|
|
|
|
updateDuration()
|
2019-08-09 18:02:41 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
/// 持续时间
|
2021-07-20 16:04:39 +08:00
|
|
|
|
var durationBlock: DispatchWorkItem?
|
2019-08-05 11:18:25 +08:00
|
|
|
|
|
2019-08-05 19:14:25 +08:00
|
|
|
|
/// 强制退出按钮
|
2021-07-20 16:04:39 +08:00
|
|
|
|
var hideTimerBlock: DispatchWorkItem?
|
2019-08-05 11:18:25 +08:00
|
|
|
|
|
|
|
|
|
/// 强制退出代码
|
2021-07-20 16:04:39 +08:00
|
|
|
|
var forceQuitCallback: (() -> Void)?
|
2019-08-05 11:18:25 +08:00
|
|
|
|
|
2021-07-20 16:04:39 +08:00
|
|
|
|
func updateDuration() {
|
2019-08-12 17:59:40 +08:00
|
|
|
|
durationBlock?.cancel()
|
2019-08-13 11:32:27 +08:00
|
|
|
|
if let t = duration ?? scene.alertDuration, t > 0 {
|
2019-08-12 17:59:40 +08:00
|
|
|
|
durationBlock = DispatchWorkItem(block: { [weak self] in
|
2019-08-13 14:42:08 +08:00
|
|
|
|
if let vc = self?.vc {
|
|
|
|
|
if vc.buttonEvents.count == 0 {
|
|
|
|
|
vc.pop()
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-12 17:59:40 +08:00
|
|
|
|
})
|
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now()+t, execute: durationBlock!)
|
|
|
|
|
} else {
|
|
|
|
|
durationBlock = nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
|
|
|
|
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 {
|
|
|
|
|
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
|
|
|
|
}
|