ProHUD/Source/Toast/ToastConfig.swift

124 lines
3.9 KiB
Swift
Raw Normal View History

2019-07-31 17:40:39 +08:00
//
2019-08-03 17:48:37 +08:00
// cfg.toast.swift
2019-07-31 17:40:39 +08:00
// ProHUD
//
// Created by xaoxuu on 2019/7/31.
// Copyright © 2019 Titan Studio. All rights reserved.
//
2020-06-10 13:08:34 +08:00
import UIKit
2019-07-31 17:40:39 +08:00
import SnapKit
public extension UIWindow.Level {
static let proToast = UIWindow.Level.alert + 1000
}
2019-07-31 17:40:39 +08:00
public extension ProHUD.Configuration {
struct Toast {
2019-08-03 17:48:37 +08:00
// MARK:
2019-07-31 17:40:39 +08:00
/// iPad
2019-08-02 13:55:23 +08:00
public var maxWidth = CGFloat(556)
2019-07-31 17:40:39 +08:00
///
2019-08-01 17:02:17 +08:00
public var cornerRadius = CGFloat(12)
2019-08-03 17:48:37 +08:00
///
2019-07-31 17:40:39 +08:00
public var margin = CGFloat(8)
2019-08-03 17:48:37 +08:00
///
2019-07-31 17:40:39 +08:00
public var padding = CGFloat(16)
2020-06-19 13:50:17 +08:00
///
public var lineSpace = CGFloat(4)
2019-08-03 17:48:37 +08:00
// MARK:
///
public var iconSize = CGSize(width: 44, height: 44)
2019-07-31 17:40:39 +08:00
2019-08-03 17:48:37 +08:00
// MARK:
///
public var titleFont = UIFont.boldSystemFont(ofSize: 18)
///
public var titleMaxLines = Int(5)
2019-07-31 17:40:39 +08:00
2019-08-03 17:48:37 +08:00
///
public var bodyFont = UIFont.systemFont(ofSize: 16)
///
public var bodyMaxLines = Int(20)
2019-08-01 17:02:17 +08:00
2019-07-31 17:40:39 +08:00
///
/// - Parameter callback:
2019-08-01 17:02:17 +08:00
public mutating func reloadData(_ callback: @escaping (ProHUD.Toast) -> Void) {
2019-08-03 17:48:37 +08:00
privReloadData = callback
2019-07-31 17:40:39 +08:00
}
2019-08-12 15:02:36 +08:00
2019-07-31 17:40:39 +08:00
}
}
2019-07-31 21:08:25 +08:00
2019-08-12 15:02:36 +08:00
// MARK: -
2019-08-03 17:48:37 +08:00
internal extension ProHUD.Configuration.Toast {
2019-08-12 15:02:36 +08:00
2019-08-03 17:48:37 +08:00
var reloadData: (ProHUD.Toast) -> Void {
return privReloadData
2019-07-31 21:08:25 +08:00
}
2019-08-12 15:02:36 +08:00
2019-08-12 17:59:40 +08:00
2019-07-31 21:08:25 +08:00
}
2019-08-03 17:48:37 +08:00
2019-08-12 15:02:36 +08:00
// MARK: -
2019-08-03 17:48:37 +08:00
fileprivate var privReloadData: (ProHUD.Toast) -> Void = {
return { (vc) in
debug(vc, "reloadData")
let config = cfg.toast
2019-08-12 15:02:36 +08:00
let scene = vc.vm.scene
if vc.imageView.superview == nil {
vc.view.addSubview(vc.imageView)
}
2020-06-19 13:50:17 +08:00
if vc.textStack.superview == nil {
vc.view.addSubview(vc.textStack)
vc.textStack.addArrangedSubview(vc.titleLabel)
vc.textStack.addArrangedSubview(vc.bodyLabel)
}
2019-08-12 15:02:36 +08:00
//
2019-08-13 11:32:27 +08:00
vc.imageView.image = vc.vm.icon ?? vc.vm.scene.image
2019-08-12 17:59:40 +08:00
vc.imageView.layer.removeAllAnimations()
2019-08-03 17:48:37 +08:00
vc.titleLabel.textColor = cfg.primaryLabelColor
2019-08-13 11:32:27 +08:00
vc.titleLabel.text = vc.vm.title ?? vc.vm.scene.title
2019-08-03 17:48:37 +08:00
vc.bodyLabel.textColor = cfg.secondaryLabelColor
2019-08-13 11:32:27 +08:00
vc.bodyLabel.text = vc.vm.message ?? vc.vm.scene.message
2019-08-03 17:48:37 +08:00
2020-06-19 13:50:17 +08:00
if let count = vc.titleLabel.text?.count, count > 0 {
vc.textStack.insertArrangedSubview(vc.titleLabel, at: 0)
} else {
vc.titleLabel.removeFromSuperview()
}
if let count = vc.bodyLabel.text?.count, count > 0 {
vc.textStack.addArrangedSubview(vc.bodyLabel)
} else {
vc.bodyLabel.removeFromSuperview()
}
2019-08-03 17:48:37 +08:00
//
vc.imageView.snp.makeConstraints { (mk) in
mk.top.equalToSuperview().offset(config.padding)
mk.leading.equalToSuperview().offset(config.padding)
mk.bottom.lessThanOrEqualToSuperview().offset(-config.padding)
mk.width.height.equalTo(config.iconSize)
}
2020-06-19 13:50:17 +08:00
vc.textStack.snp.makeConstraints { (mk) in
2019-08-03 17:48:37 +08:00
mk.top.equalToSuperview().offset(config.padding)
2020-06-19 13:50:17 +08:00
mk.leading.equalTo(vc.imageView.snp.trailing).offset(config.padding - 4)
2019-08-03 17:48:37 +08:00
mk.leading.greaterThanOrEqualToSuperview().offset(config.padding)
mk.trailing.equalToSuperview().offset(-config.padding)
mk.bottom.lessThanOrEqualToSuperview().offset(-config.padding)
}
vc.view.layoutIfNeeded()
2019-08-12 17:59:40 +08:00
//
vc.vm.updateDuration()
2020-06-22 16:34:21 +08:00
vc.progressView?.removeFromSuperview()
2019-08-12 15:02:36 +08:00
}
}()