ProHUD/Source/Guard/GuardConfig.swift

143 lines
4.7 KiB
Swift
Raw Normal View History

2019-07-31 17:40:39 +08:00
//
2019-08-03 17:48:37 +08:00
// cfg.guard.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
2019-08-03 16:22:50 +08:00
import Inspire
2019-07-31 17:40:39 +08:00
public extension ProHUD.Configuration {
struct Guard {
2019-08-03 16:22:50 +08:00
// MARK:
2019-07-31 17:40:39 +08:00
/// iPad
2019-08-03 16:22:50 +08:00
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:
2019-07-31 17:40:39 +08:00
///
2019-08-03 16:22:50 +08:00
public var titleFont = UIFont.boldSystemFont(ofSize: 22)
2019-08-08 09:25:28 +08:00
///
public var subTitleFont = UIFont.boldSystemFont(ofSize: 20)
2019-07-31 17:40:39 +08:00
///
2019-08-03 16:22:50 +08:00
public var bodyFont = UIFont.systemFont(ofSize: 18)
2019-07-31 17:40:39 +08:00
2019-08-03 16:22:50 +08:00
// MARK:
///
public var buttonFont = UIFont.boldSystemFont(ofSize: 18)
2019-08-03 18:03:20 +08:00
2019-08-03 16:22:50 +08:00
///
public var buttonCornerRadius = CGFloat(12)
2019-07-31 17:40:39 +08:00
///
/// - Parameter callback:
2019-08-03 17:48:37 +08:00
public mutating func reloadData(_ callback: @escaping (ProHUD.Guard) -> Void) {
privReloadData = callback
2019-07-31 17:40:39 +08:00
}
}
}
2019-08-03 16:22:50 +08:00
2019-08-12 15:02:36 +08:00
// MARK: -
2019-08-03 17:48:37 +08:00
internal extension ProHUD.Configuration.Guard {
2019-08-12 15:02:36 +08:00
2019-08-03 17:48:37 +08:00
var reloadData: (ProHUD.Guard) -> Void {
return privReloadData
2019-08-03 16:22:50 +08:00
}
2019-08-12 15:02:36 +08:00
2019-08-08 09:25:28 +08:00
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()
}
}
}
2019-08-03 16:22:50 +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.Guard) -> Void = {
return { (vc) in
debug(vc, "reloadData")
let config = cfg.guard
2019-08-09 18:02:41 +08:00
// 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)
2019-08-03 17:48:37 +08:00
//
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
2019-08-12 17:59:40 +08:00
if isPortrait && vc.isFullScreen {
mk.top.equalToSuperview()
}
2019-08-03 17:48:37 +08:00
mk.centerX.equalToSuperview()
if UIDevice.current.userInterfaceIdiom == .phone {
2019-08-12 15:02:36 +08:00
if width < config.cardMaxWidth {
2019-08-03 17:48:37 +08:00
mk.bottom.equalToSuperview()
2019-08-12 15:02:36 +08:00
} else {
mk.bottom.equalToSuperview().offset(-Inspire.shared.screen.safeAreaInsets.bottom)
2019-08-03 17:48:37 +08:00
}
} else if UIDevice.current.userInterfaceIdiom == .pad {
mk.centerY.equalToSuperview()
}
mk.width.equalTo(width)
}
// stack
vc.contentStack.snp.makeConstraints { (mk) in
2019-08-12 17:59:40 +08:00
if isPortrait && vc.isFullScreen {
mk.top.equalToSuperview().offset(Inspire.shared.screen.safeAreaInsets.top)
} else {
mk.top.equalToSuperview().offset(config.padding)
}
2019-08-03 17:48:37 +08:00
mk.centerX.equalToSuperview()
2019-08-12 15:02:36 +08:00
if width < config.cardMaxWidth {
let bottom = Inspire.shared.screen.safeAreaInsets.bottom
if bottom == 0 {
mk.bottom.equalToSuperview().offset(-config.padding)
} else {
mk.bottom.equalToSuperview().offset(-config.padding/2 - bottom)
}
2019-08-03 17:48:37 +08:00
} else {
2019-08-12 15:02:36 +08:00
mk.bottom.equalToSuperview().offset(-config.padding)
2019-08-03 17:48:37 +08:00
}
if isPortrait {
mk.width.equalToSuperview().offset(-config.padding * 2)
} else {
mk.width.equalToSuperview().offset(-config.padding * 4)
}
}
2019-08-12 15:02:36 +08:00
config.reloadStack(vc)
2019-08-03 17:48:37 +08:00
}
}()