mirror of https://github.com/xaoxuu/ProHUD
代码优化
This commit is contained in:
parent
cbd654d80b
commit
c3afd50eac
|
@ -25,7 +25,7 @@ class DemoCapsuleVC: ListVC {
|
||||||
// 设置vm或者handler都会自动push,这里测试传入vm:
|
// 设置vm或者handler都会自动push,这里测试传入vm:
|
||||||
// Capsule(.message("一条简短消息"))
|
// Capsule(.message("一条简短消息"))
|
||||||
// 如果只有一条文字信息,可以直接传字符串:
|
// 如果只有一条文字信息,可以直接传字符串:
|
||||||
Capsule("一条简短消息")
|
Capsule("成功")
|
||||||
}
|
}
|
||||||
section.add(title: "一条稍微长一点的消息") {
|
section.add(title: "一条稍微长一点的消息") {
|
||||||
// 设置vm或者handler都会自动push,这里测试传入handler:
|
// 设置vm或者handler都会自动push,这里测试传入handler:
|
||||||
|
@ -53,6 +53,9 @@ class DemoCapsuleVC: ListVC {
|
||||||
}
|
}
|
||||||
|
|
||||||
list.add(title: "默认布局:图文") { section in
|
list.add(title: "默认布局:图文") { section in
|
||||||
|
section.add(title: "短的消息") {
|
||||||
|
Capsule(.icon(.init(systemName: "checkmark.circle.fill")).title("成功"))
|
||||||
|
}
|
||||||
section.add(title: "下载进度") {
|
section.add(title: "下载进度") {
|
||||||
let capsule = CapsuleTarget()
|
let capsule = CapsuleTarget()
|
||||||
capsule.vm = .message("正在下载").icon(.init(systemName: "arrow.down.circle.fill")).duration(.infinity)
|
capsule.vm = .message("正在下载").icon(.init(systemName: "arrow.down.circle.fill")).duration(.infinity)
|
||||||
|
@ -191,6 +194,14 @@ class DemoCapsuleVC: ListVC {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
list.add(title: "网络图片") { section in
|
||||||
|
section.add(title: "设置iconSize以修改默认约束尺寸") {
|
||||||
|
Capsule(.icon("https://xaoxuu.com/assets/wiki/prohud/icon.png").title("ProHUD").duration(5)) { capsule in
|
||||||
|
capsule.config.iconSize = .init(width: 24, height: 24)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -177,7 +177,7 @@ class DemoToastVC: ListVC {
|
||||||
Toast(.title(title).message(message).icon(.init(named: "avatar")).duration(.infinity)) { toast in
|
Toast(.title(title).message(message).icon(.init(named: "avatar")).duration(.infinity)) { toast in
|
||||||
toast.isRemovable = false
|
toast.isRemovable = false
|
||||||
toast.imageView.layer.masksToBounds = true
|
toast.imageView.layer.masksToBounds = true
|
||||||
toast.imageView.layer.cornerRadius = toast.config.iconSize.width / 2
|
toast.imageView.layer.cornerRadius = 44 / 2
|
||||||
toast.add(action: "拒绝", style: .destructive) { toast in
|
toast.add(action: "拒绝", style: .destructive) { toast in
|
||||||
Alert(.identifier("Dracarys")) { alert in
|
Alert(.identifier("Dracarys")) { alert in
|
||||||
alert.vm = .message("Dracarys")
|
alert.vm = .message("Dracarys")
|
||||||
|
|
|
@ -35,6 +35,11 @@ public class AlertConfiguration: CommonConfiguration {
|
||||||
customBackgroundViewMask = callback
|
customBackgroundViewMask = callback
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 最小宽度
|
||||||
|
public var cardMinWidth = CGFloat(32)
|
||||||
|
/// 最小高度
|
||||||
|
public var cardMinHeight = CGFloat(32)
|
||||||
|
|
||||||
override var animateDurationForBuildOutByDefault: CGFloat {
|
override var animateDurationForBuildOutByDefault: CGFloat {
|
||||||
animateDurationForBuildOut ?? 0.2
|
animateDurationForBuildOut ?? 0.2
|
||||||
}
|
}
|
||||||
|
|
|
@ -150,8 +150,8 @@ extension AlertTarget {
|
||||||
imageView.snp.remakeConstraints { (mk) in
|
imageView.snp.remakeConstraints { (mk) in
|
||||||
mk.top.left.greaterThanOrEqualTo(contentView).inset(cardEdgeInsets.top * 2)
|
mk.top.left.greaterThanOrEqualTo(contentView).inset(cardEdgeInsets.top * 2)
|
||||||
mk.right.bottom.lessThanOrEqualTo(contentView).inset(cardEdgeInsets.right * 2)
|
mk.right.bottom.lessThanOrEqualTo(contentView).inset(cardEdgeInsets.right * 2)
|
||||||
mk.width.equalTo(config.iconSize.width)
|
mk.width.equalTo(config.iconSizeByDefault.width)
|
||||||
mk.height.equalTo(config.iconSize.height)
|
mk.height.equalTo(config.iconSizeByDefault.height)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let rotation = vm?.rotation {
|
if let rotation = vm?.rotation {
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
|
|
||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
public final class AlertProvider: HUDProviderType {
|
open class AlertProvider: HUDProviderType {
|
||||||
|
|
||||||
public typealias ViewModel = AlertViewModel
|
public typealias ViewModel = AlertViewModel
|
||||||
public typealias Target = AlertTarget
|
public typealias Target = AlertTarget
|
||||||
|
|
|
@ -27,18 +27,21 @@ public class CapsuleConfiguration: CommonConfiguration {
|
||||||
/// 默认的持续时间
|
/// 默认的持续时间
|
||||||
public var defaultDuration: TimeInterval = 3
|
public var defaultDuration: TimeInterval = 3
|
||||||
|
|
||||||
override var cardCornerRadiusByDefault: CGFloat {
|
|
||||||
cardCornerRadius ?? 16
|
|
||||||
}
|
|
||||||
|
|
||||||
override var cardEdgeInsetsByDefault: UIEdgeInsets {
|
|
||||||
cardEdgeInsets ?? .init(top: 12, left: 16, bottom: 12, right: 16)
|
|
||||||
}
|
|
||||||
|
|
||||||
override var cardMaxWidthByDefault: CGFloat { cardMaxWidth ?? 320 }
|
override var cardMaxWidthByDefault: CGFloat { cardMaxWidth ?? 320 }
|
||||||
|
|
||||||
override var cardMaxHeightByDefault: CGFloat { cardMaxHeight ?? 120 }
|
override var cardMaxHeightByDefault: CGFloat { cardMaxHeight ?? 120 }
|
||||||
|
|
||||||
|
/// 最小高度
|
||||||
|
public var cardMinHeight = CGFloat(40)
|
||||||
|
|
||||||
|
override var cardCornerRadiusByDefault: CGFloat { cardCornerRadius ?? 16 }
|
||||||
|
|
||||||
|
override var cardEdgeInsetsByDefault: UIEdgeInsets {
|
||||||
|
cardEdgeInsets ?? .init(top: 10, left: 16, bottom: 10, right: 16)
|
||||||
|
}
|
||||||
|
|
||||||
|
override var iconSizeByDefault: CGSize { iconSize ?? .init(width: 20, height: 20) }
|
||||||
|
|
||||||
override var animateDurationForBuildInByDefault: CGFloat {
|
override var animateDurationForBuildInByDefault: CGFloat {
|
||||||
animateDurationForBuildIn ?? 0.64
|
animateDurationForBuildIn ?? 0.64
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,23 +24,21 @@ extension CapsuleTarget: DefaultLayout {
|
||||||
loadContentViewIfNeeded()
|
loadContentViewIfNeeded()
|
||||||
loadContentMaskViewIfNeeded()
|
loadContentMaskViewIfNeeded()
|
||||||
|
|
||||||
// image
|
|
||||||
setupImageView()
|
|
||||||
|
|
||||||
// text
|
// text
|
||||||
textLabel.removeFromSuperview()
|
contentStack.addArrangedSubview(textLabel)
|
||||||
var text = [vm?.title ?? "", vm?.message ?? ""].filter({ $0.count > 0 }).joined(separator: " ")
|
var text = [vm?.title ?? "", vm?.message ?? ""].filter({ $0.count > 0 }).joined(separator: " ")
|
||||||
if text.count > 0 {
|
if text.count > 0 {
|
||||||
contentStack.addArrangedSubview(textLabel)
|
textLabel.snp.remakeConstraints { make in
|
||||||
textLabel.snp.makeConstraints { make in
|
|
||||||
make.width.lessThanOrEqualTo(AppContext.appBounds.width * 0.5)
|
make.width.lessThanOrEqualTo(AppContext.appBounds.width * 0.5)
|
||||||
}
|
}
|
||||||
|
textLabel.snp.contentHuggingVerticalPriority = .infinity
|
||||||
textLabel.text = text
|
textLabel.text = text
|
||||||
textLabel.sizeToFit()
|
textLabel.sizeToFit()
|
||||||
} else {
|
|
||||||
contentStack.removeArrangedSubview(textLabel)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 后设置image, 需要参考text
|
||||||
|
setupImageView()
|
||||||
|
|
||||||
view.layoutIfNeeded()
|
view.layoutIfNeeded()
|
||||||
|
|
||||||
if isViewAppeared {
|
if isViewAppeared {
|
||||||
|
@ -91,6 +89,12 @@ extension CapsuleTarget: DefaultLayout {
|
||||||
contentStack.removeArrangedSubview(imageView)
|
contentStack.removeArrangedSubview(imageView)
|
||||||
} else {
|
} else {
|
||||||
contentStack.insertArrangedSubview(imageView, at: 0)
|
contentStack.insertArrangedSubview(imageView, at: 0)
|
||||||
|
if config.iconSizeByDefault != .zero {
|
||||||
|
imageView.snp.remakeConstraints { make in
|
||||||
|
make.height.equalTo(config.iconSizeByDefault)
|
||||||
|
make.width.equalTo(config.iconSizeByDefault)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
imageView.image = vm?.icon
|
imageView.image = vm?.icon
|
||||||
if let iconURL = vm?.iconURL {
|
if let iconURL = vm?.iconURL {
|
||||||
|
|
|
@ -48,7 +48,9 @@ extension CapsuleTarget {
|
||||||
let cardEdgeInsetsByDefault = config.cardEdgeInsetsByDefault
|
let cardEdgeInsetsByDefault = config.cardEdgeInsetsByDefault
|
||||||
view.layoutIfNeeded()
|
view.layoutIfNeeded()
|
||||||
var size = contentStack.frame.size
|
var size = contentStack.frame.size
|
||||||
size = CGSize(width: min(config.cardMaxWidthByDefault, size.width + cardEdgeInsetsByDefault.left + cardEdgeInsetsByDefault.right), height: min(config.cardMaxHeightByDefault, size.height + cardEdgeInsetsByDefault.top + cardEdgeInsetsByDefault.bottom))
|
let width = min(config.cardMaxWidthByDefault, size.width + cardEdgeInsetsByDefault.left + cardEdgeInsetsByDefault.right)
|
||||||
|
let height = min(config.cardMaxHeightByDefault, size.height + cardEdgeInsetsByDefault.top + cardEdgeInsetsByDefault.bottom)
|
||||||
|
size = CGSize(width: width, height: max(height, config.cardMinHeight))
|
||||||
|
|
||||||
// 应用到frame
|
// 应用到frame
|
||||||
let newFrame: CGRect
|
let newFrame: CGRect
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
|
|
||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
public final class CapsuleProvider: HUDProviderType {
|
open class CapsuleProvider: HUDProviderType {
|
||||||
|
|
||||||
public typealias ViewModel = CapsuleViewModel
|
public typealias ViewModel = CapsuleViewModel
|
||||||
public typealias Target = CapsuleTarget
|
public typealias Target = CapsuleTarget
|
||||||
|
|
|
@ -18,7 +18,7 @@ open class CapsuleTarget: BaseController, HUDTargetType {
|
||||||
let stack = StackView()
|
let stack = StackView()
|
||||||
stack.spacing = 8
|
stack.spacing = 8
|
||||||
stack.distribution = .equalCentering
|
stack.distribution = .equalCentering
|
||||||
stack.alignment = .fill
|
stack.alignment = .center
|
||||||
stack.axis = .horizontal
|
stack.axis = .horizontal
|
||||||
config.customContentStack?(stack)
|
config.customContentStack?(stack)
|
||||||
return stack
|
return stack
|
||||||
|
|
|
@ -101,47 +101,55 @@ open class BaseViewModel: NSObject, HUDViewModelType {
|
||||||
// MARK: - convenience func
|
// MARK: - convenience func
|
||||||
public extension BaseViewModel {
|
public extension BaseViewModel {
|
||||||
|
|
||||||
func identifier(_ identifier: String?) -> Self {
|
@discardableResult func identifier(_ identifier: String?) -> Self {
|
||||||
self.identifier = identifier
|
self.identifier = identifier
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
func lazyIdentifier(file: String = #file, line: Int = #line) -> Self {
|
@discardableResult func lazyIdentifier(file: String = #file, line: Int = #line) -> Self {
|
||||||
self.identifier = (file + "#\(line)")
|
self.identifier = (file + "#\(line)")
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
func icon(_ image: UIImage?) -> Self {
|
@discardableResult func icon(_ image: UIImage?) -> Self {
|
||||||
self.icon = image
|
self.icon = image
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
func icon(_ imageURL: URL?) -> Self {
|
@discardableResult func icon(_ imageURL: URL?) -> Self {
|
||||||
self.iconURL = imageURL
|
self.iconURL = imageURL
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
func title(_ text: String?) -> Self {
|
@discardableResult func icon(_ imageURLStr: String) -> Self {
|
||||||
|
if let url = URL(string: imageURLStr) {
|
||||||
|
return icon(url)
|
||||||
|
} else {
|
||||||
|
return icon(.init(named: imageURLStr))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@discardableResult func title(_ text: String?) -> Self {
|
||||||
self.title = text
|
self.title = text
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
func message(_ text: String?) -> Self {
|
@discardableResult func message(_ text: String?) -> Self {
|
||||||
self.message = text
|
self.message = text
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
func duration(_ seconds: TimeInterval?) -> Self {
|
@discardableResult func duration(_ seconds: TimeInterval?) -> Self {
|
||||||
self.duration = seconds
|
self.duration = seconds
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
func rotation(_ rotation: Rotation?) -> Self {
|
@discardableResult func rotation(_ rotation: Rotation?) -> Self {
|
||||||
self.rotation = rotation
|
self.rotation = rotation
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
func tintColor(_ tintColor: UIColor?) -> Self {
|
@discardableResult func tintColor(_ tintColor: UIColor?) -> Self {
|
||||||
self.tintColor = tintColor
|
self.tintColor = tintColor
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
@ -173,6 +181,16 @@ public extension BaseViewModel {
|
||||||
.message(text)
|
.message(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static func icon(_ imageURLStr: String) -> Self {
|
||||||
|
.init().icon(imageURLStr)
|
||||||
|
}
|
||||||
|
static func icon(_ imageURL: URL) -> Self {
|
||||||
|
.init().icon(imageURL)
|
||||||
|
}
|
||||||
|
static func icon(_ image: UIImage?) -> Self {
|
||||||
|
.init().icon(image)
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: loading
|
// MARK: loading
|
||||||
static var loading: Self {
|
static var loading: Self {
|
||||||
.init()
|
.init()
|
||||||
|
|
|
@ -60,10 +60,6 @@ open class CommonConfiguration: NSObject {
|
||||||
var cardMaxHeightByDefault: CGFloat {
|
var cardMaxHeightByDefault: CGFloat {
|
||||||
cardMaxHeight ?? (AppContext.appBounds.height - 100)
|
cardMaxHeight ?? (AppContext.appBounds.height - 100)
|
||||||
}
|
}
|
||||||
/// 最小宽度
|
|
||||||
public var cardMinWidth = CGFloat(32)
|
|
||||||
/// 最小高度
|
|
||||||
public var cardMinHeight = CGFloat(32)
|
|
||||||
|
|
||||||
/// 卡片内边距
|
/// 卡片内边距
|
||||||
public var cardEdgeInsets: UIEdgeInsets?
|
public var cardEdgeInsets: UIEdgeInsets?
|
||||||
|
@ -102,7 +98,9 @@ open class CommonConfiguration: NSObject {
|
||||||
|
|
||||||
// MARK: 图标样式
|
// MARK: 图标样式
|
||||||
/// 图标尺寸
|
/// 图标尺寸
|
||||||
public var iconSize = CGSize(width: 44, height: 44)
|
public var iconSize: CGSize?
|
||||||
|
|
||||||
|
var iconSizeByDefault: CGSize { iconSize ?? .init(width: 44, height: 44) }
|
||||||
|
|
||||||
// MARK: 文本样式
|
// MARK: 文本样式
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ public final class SheetProvider: HUDProviderType {
|
||||||
|
|
||||||
/// 根据自定义的初始化代码创建一个Target并显示
|
/// 根据自定义的初始化代码创建一个Target并显示
|
||||||
/// - Parameter initializer: 初始化代码(传空值时不会做任何事)
|
/// - Parameter initializer: 初始化代码(传空值时不会做任何事)
|
||||||
@discardableResult public required init(initializer: ((_ target: Target) -> Void)?) {
|
@discardableResult public required init(initializer: ((_ sheet: Target) -> Void)?) {
|
||||||
guard let initializer = initializer else {
|
guard let initializer = initializer else {
|
||||||
// Provider的作用就是push一个target
|
// Provider的作用就是push一个target
|
||||||
// 如果没有任何初始化代码就没有target,就是个无意义的Provider
|
// 如果没有任何初始化代码就没有target,就是个无意义的Provider
|
||||||
|
|
|
@ -33,9 +33,9 @@ extension ToastTarget: DefaultLayout {
|
||||||
infoStack.insertArrangedSubview(imageView, at: 0)
|
infoStack.insertArrangedSubview(imageView, at: 0)
|
||||||
imageView.snp.makeConstraints { make in
|
imageView.snp.makeConstraints { make in
|
||||||
if titleCount > 0 && bodyCount > 0 {
|
if titleCount > 0 && bodyCount > 0 {
|
||||||
make.width.height.equalTo(config.iconSize)
|
make.width.height.equalTo(config.iconSizeByDefault)
|
||||||
} else {
|
} else {
|
||||||
make.width.equalTo(config.iconSize)
|
make.width.equalTo(config.iconSizeByDefault)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
|
|
||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
public final class ToastProvider: HUDProviderType {
|
open class ToastProvider: HUDProviderType {
|
||||||
|
|
||||||
public typealias ViewModel = ToastViewModel
|
public typealias ViewModel = ToastViewModel
|
||||||
public typealias Target = ToastTarget
|
public typealias Target = ToastTarget
|
||||||
|
|
Loading…
Reference in New Issue