ProHUD/Source/Alert/AlertModel.swift

116 lines
3.2 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// AlertModel.swift
// ProHUD
//
// Created by xaoxuu on 2019/7/29.
// Copyright © 2019 Titan Studio. All rights reserved.
//
import UIKit
public extension Alert {
class ViewModel {
/// 使
public var scene = ProHUD.Scene.default
///
public var title: String? {
didSet {
vc?.titleLabel?.text = title
}
}
///
public var message: String? {
didSet {
vc?.bodyLabel?.text = message
}
}
///
public var icon: UIImage? {
didSet {
vc?.imageView.image = icon
}
}
/// 0
public var duration: TimeInterval? {
didSet {
updateDuration()
}
}
public weak var vc: Alert?
// MARK:
///
internal var durationBlock: DispatchWorkItem?
/// 退
internal var hideTimerBlock: DispatchWorkItem?
/// 退
internal var forceQuitCallback: (() -> Void)?
internal func updateDuration() {
durationBlock?.cancel()
if let t = duration ?? scene.alertDuration, t > 0 {
durationBlock = DispatchWorkItem(block: { [weak self] in
if let vc = self?.vc {
if vc.buttonEvents.count == 0 {
vc.pop()
}
}
})
DispatchQueue.main.asyncAfter(deadline: .now()+t, execute: durationBlock!)
} else {
durationBlock = nil
}
}
}
}
public extension Alert.ViewModel {
///
/// - Parameter style:
/// - Parameter text:
/// - Parameter handler:
@discardableResult func add(action style: UIAlertAction.Style, title: String?, handler: (() -> Void)?) -> UIButton {
return vc!.insert(action: nil, style: style, title: title, handler: handler)
}
///
/// - Parameter index:
/// - Parameter style:
/// - Parameter title:
/// - Parameter handler:
@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)
}
///
/// - Parameter index:
/// - Parameter style:
/// - Parameter title:
/// - Parameter handler:
func update(action index: Int, style: UIAlertAction.Style, title: String?, handler: (() -> Void)?) {
vc?.update(action: index, style: style, title: title, handler: handler)
}
///
/// - Parameter index:
func remove(action index: Int...) {
for (i, idx) in index.enumerated() {
vc?.remove(action: idx-i)
}
}
}