ProHUD/ProHUD/Alert/AlertModel.swift

137 lines
3.5 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-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
}