ProHUD/Source/Alert/AlertModel.swift

116 lines
3.2 KiB
Swift
Raw Normal View History

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 {
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
}