ProHUD/Source/Toast/ToastConfig.swift

110 lines
3.4 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 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)
2019-08-03 17:48:37 +08:00
// MARK:
///
2019-07-31 17:40:39 +08:00
public var iconSize = CGSize(width: 48, height: 48)
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.titleLabel.superview == nil {
vc.view.addSubview(vc.titleLabel)
2019-08-03 17:48:37 +08:00
}
2019-08-12 15:02:36 +08:00
if vc.bodyLabel.superview == nil {
vc.view.addSubview(vc.bodyLabel)
}
if vc.imageView.superview == nil {
vc.view.addSubview(vc.imageView)
}
//
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
//
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.titleLabel.snp.makeConstraints { (mk) in
mk.top.equalToSuperview().offset(config.padding)
mk.leading.equalTo(vc.imageView.snp.trailing).offset(config.margin)
mk.leading.greaterThanOrEqualToSuperview().offset(config.padding)
mk.trailing.equalToSuperview().offset(-config.padding)
}
vc.bodyLabel.snp.makeConstraints { (mk) in
mk.top.equalTo(vc.titleLabel.snp.bottom).offset(config.margin)
mk.leading.trailing.equalTo(vc.titleLabel)
mk.bottom.lessThanOrEqualToSuperview().offset(-config.padding)
}
vc.view.layoutIfNeeded()
2019-08-12 17:59:40 +08:00
//
vc.vm.updateDuration()
2019-08-12 15:02:36 +08:00
}
}()