ProHUD/Source/Toast/ToastConfig.swift

122 lines
3.8 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.

//
// cfg.toast.swift
// ProHUD
//
// Created by xaoxuu on 2019/7/31.
// Copyright © 2019 Titan Studio. All rights reserved.
//
import UIKit
import SnapKit
public extension UIWindow.Level {
static let proToast = UIWindow.Level.alert + 1000
}
public extension ProHUD.Configuration {
struct Toast {
// MARK:
/// iPad
public var maxWidth = CGFloat(556)
///
public var cornerRadius = CGFloat(12)
///
public var margin = CGFloat(8)
///
public var padding = CGFloat(16)
///
public var lineSpace = CGFloat(4)
// MARK:
///
public var iconSize = CGSize(width: 44, height: 44)
// MARK:
///
public var titleFont = UIFont.boldSystemFont(ofSize: 18)
///
public var titleMaxLines = Int(5)
///
public var bodyFont = UIFont.systemFont(ofSize: 16)
///
public var bodyMaxLines = Int(20)
///
/// - Parameter callback:
public mutating func reloadData(_ callback: @escaping (ProHUD.Toast) -> Void) {
privReloadData = callback
}
}
}
// MARK: -
internal extension ProHUD.Configuration.Toast {
var reloadData: (ProHUD.Toast) -> Void {
return privReloadData
}
}
// MARK: -
fileprivate var privReloadData: (ProHUD.Toast) -> Void = {
return { (vc) in
debug(vc, "reloadData")
let config = cfg.toast
let scene = vc.vm.scene
if vc.imageView.superview == nil {
vc.view.addSubview(vc.imageView)
}
if vc.textStack.superview == nil {
vc.view.addSubview(vc.textStack)
vc.textStack.addArrangedSubview(vc.titleLabel)
vc.textStack.addArrangedSubview(vc.bodyLabel)
}
//
vc.imageView.image = vc.vm.icon ?? vc.vm.scene.image
vc.imageView.layer.removeAllAnimations()
vc.titleLabel.textColor = cfg.primaryLabelColor
vc.titleLabel.text = vc.vm.title ?? vc.vm.scene.title
vc.bodyLabel.textColor = cfg.secondaryLabelColor
vc.bodyLabel.text = vc.vm.message ?? vc.vm.scene.message
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()
}
//
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)
}
vc.textStack.snp.makeConstraints { (mk) in
mk.top.equalToSuperview().offset(config.padding)
mk.leading.equalTo(vc.imageView.snp.trailing).offset(config.padding - 4)
mk.leading.greaterThanOrEqualToSuperview().offset(config.padding)
mk.trailing.equalToSuperview().offset(-config.padding)
mk.bottom.lessThanOrEqualToSuperview().offset(-config.padding)
}
vc.view.layoutIfNeeded()
//
vc.vm.updateDuration()
}
}()