ProHUD/Source/Toast/ToastModel.swift

73 lines
1.7 KiB
Swift

//
// ToastModel.swift
// ProHUD
//
// Created by xaoxuu on 2019/7/31.
// Copyright © 2019 Titan Studio. All rights reserved.
//
import UIKit
public extension Toast {
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
}
}
///
public var duration: TimeInterval? {
didSet {
updateDuration()
}
}
public weak var vc: Toast?
// MARK:
///
var durationBlock: DispatchWorkItem?
///
var tapCallback: (() -> Void)?
func updateDuration() {
durationBlock?.cancel()
if let t = duration ?? scene.toastDuration, 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
}
}
}
}