ProHUD/Source/Toast/ToastModel.swift

73 lines
1.7 KiB
Swift
Raw Normal View History

2019-07-31 17:40:39 +08:00
//
// ToastModel.swift
// ProHUD
//
// Created by xaoxuu on 2019/7/31.
// 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 Toast {
2019-08-12 15:02:36 +08:00
class ViewModel {
2019-07-31 21:08:25 +08:00
/// 使
2019-08-13 11:32:27 +08:00
public var scene = ProHUD.Scene.default
2019-07-31 21:08:25 +08:00
///
2019-08-12 15:02:36 +08:00
public var title: String? {
didSet {
vc?.titleLabel.text = title
}
}
2019-07-31 21:08:25 +08:00
///
2019-08-12 15:02:36 +08:00
public var message: String? {
didSet {
vc?.bodyLabel.text = message
}
}
2019-07-31 21:08:25 +08:00
///
2019-08-12 15:02:36 +08:00
public var icon: UIImage? {
didSet {
vc?.imageView.image = icon
}
}
2019-07-31 21:08:25 +08:00
2019-08-05 11:18:25 +08:00
///
2019-08-12 15:02:36 +08:00
public var duration: TimeInterval? {
didSet {
2019-08-12 17:59:40 +08:00
updateDuration()
2019-08-12 15:02:36 +08:00
}
}
2019-08-05 11:18:25 +08:00
2019-08-12 15:02:36 +08:00
public weak var vc: Toast?
2019-08-05 11:18:25 +08:00
2019-08-12 15:02:36 +08:00
// MARK:
///
internal var durationBlock: DispatchWorkItem?
2019-08-05 11:18:25 +08:00
///
internal var tapCallback: (() -> Void)?
2019-08-12 17:59:40 +08:00
internal func updateDuration() {
durationBlock?.cancel()
2019-08-13 11:32:27 +08:00
if let t = duration ?? scene.toastDuration, 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-07-31 21:08:25 +08:00
}
}