ProHUD/Source/Guard/GuardConfig.swift

142 lines
4.7 KiB
Swift
Raw Permalink 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.guard.swift
// ProHUD
//
// Created by xaoxuu on 2019/7/31.
// Copyright © 2019 Titan Studio. All rights reserved.
//
import UIKit
import SnapKit
public extension ProHUD.Configuration {
struct Guard {
// MARK:
/// iPad
public var cardMaxWidth = CGFloat(460)
/// 0
public var cardCornerRadius = CGFloat(16)
///
public var margin = CGFloat(8)
///
public var padding = CGFloat(16)
///
public var tintColor: UIColor?
// MARK:
///
public var titleFont = UIFont.boldSystemFont(ofSize: 22)
///
public var subTitleFont = UIFont.boldSystemFont(ofSize: 20)
///
public var bodyFont = UIFont.systemFont(ofSize: 18)
// MARK:
///
public var buttonFont = UIFont.boldSystemFont(ofSize: 18)
///
public var buttonCornerRadius = CGFloat(12)
///
/// - Parameter callback:
public mutating func reloadData(_ callback: @escaping (ProHUD.Guard) -> Void) {
privReloadData = callback
}
}
}
// MARK: -
extension ProHUD.Configuration.Guard {
var reloadData: (ProHUD.Guard) -> Void {
return privReloadData
}
var reloadStack: (ProHUD.Guard) -> Void {
return { (vc) in
if vc.textStack.arrangedSubviews.count > 0 {
vc.contentStack.addArrangedSubview(vc.textStack)
} else {
vc.textStack.removeFromSuperview()
}
if vc.actionStack.arrangedSubviews.count > 0 {
vc.contentStack.addArrangedSubview(vc.actionStack)
} else {
vc.actionStack.removeFromSuperview()
}
}
}
}
// MARK: -
fileprivate var privReloadData: (ProHUD.Guard) -> Void = {
return { (vc) in
debug(vc, "reloadData")
let config = cfg.guard
// background
vc.view.tintColor = config.tintColor
vc.view.backgroundColor = UIColor(white: 0, alpha: 0)
vc.view.addSubview(vc.contentView)
vc.contentView.contentView.addSubview(vc.contentStack)
//
var width = UIScreen.main.bounds.width
if width > config.cardMaxWidth {
// iPad
width = config.cardMaxWidth
vc.contentView.layer.masksToBounds = true
vc.contentView.layer.cornerRadius = config.cardCornerRadius
} else {
vc.contentView.layer.shadowRadius = 4
vc.contentView.layer.shadowOffset = .init(width: 0, height: 2)
vc.contentView.layer.shadowOpacity = 0.12
}
vc.contentView.snp.makeConstraints { (mk) in
if isPortrait && vc.isFullScreen {
mk.top.equalToSuperview()
}
mk.centerX.equalToSuperview()
if UIDevice.current.userInterfaceIdiom == .phone {
if width < config.cardMaxWidth {
mk.bottom.equalToSuperview()
} else {
mk.bottom.equalToSuperview().offset(-ProHUD.safeAreaInsets.bottom)
}
} else if UIDevice.current.userInterfaceIdiom == .pad {
mk.centerY.equalToSuperview()
}
mk.width.equalTo(width)
}
// stack
vc.contentStack.snp.makeConstraints { (mk) in
if isPortrait && vc.isFullScreen {
mk.top.equalToSuperview().offset(ProHUD.safeAreaInsets.top)
} else {
mk.top.equalToSuperview().offset(config.padding)
}
mk.centerX.equalToSuperview()
if width < config.cardMaxWidth {
let bottom = ProHUD.safeAreaInsets.bottom
if bottom == 0 {
mk.bottom.equalToSuperview().offset(-config.padding)
} else {
mk.bottom.equalToSuperview().offset(-config.padding/2 - bottom)
}
} else {
mk.bottom.equalToSuperview().offset(-config.padding)
}
if isPortrait {
mk.width.equalToSuperview().offset(-config.padding * 2)
} else {
mk.width.equalToSuperview().offset(-config.padding * 4)
}
}
config.reloadStack(vc)
}
}()