diff --git a/PHDemo/PHDemo/DemoCapsuleVC.swift b/PHDemo/PHDemo/DemoCapsuleVC.swift index c941877..a84c79d 100644 --- a/PHDemo/PHDemo/DemoCapsuleVC.swift +++ b/PHDemo/PHDemo/DemoCapsuleVC.swift @@ -25,7 +25,7 @@ class DemoCapsuleVC: ListVC { // 设置vm或者handler都会自动push,这里测试传入vm: // Capsule(.message("一条简短消息")) // 如果只有一条文字信息,可以直接传字符串: - Capsule("一条简短消息") + Capsule("成功") } section.add(title: "一条稍微长一点的消息") { // 设置vm或者handler都会自动push,这里测试传入handler: @@ -53,6 +53,9 @@ class DemoCapsuleVC: ListVC { } list.add(title: "默认布局:图文") { section in + section.add(title: "短的消息") { + Capsule(.icon(.init(systemName: "checkmark.circle.fill")).title("成功")) + } section.add(title: "下载进度") { let capsule = CapsuleTarget() 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) + } + } + } + } } diff --git a/PHDemo/PHDemo/DemoToastVC.swift b/PHDemo/PHDemo/DemoToastVC.swift index 748c7dc..0c7157a 100644 --- a/PHDemo/PHDemo/DemoToastVC.swift +++ b/PHDemo/PHDemo/DemoToastVC.swift @@ -177,7 +177,7 @@ class DemoToastVC: ListVC { Toast(.title(title).message(message).icon(.init(named: "avatar")).duration(.infinity)) { toast in toast.isRemovable = false 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 Alert(.identifier("Dracarys")) { alert in alert.vm = .message("Dracarys") diff --git a/Sources/ProHUD/Alert/AlertConfiguration.swift b/Sources/ProHUD/Alert/AlertConfiguration.swift index 5987fb7..6b6309c 100644 --- a/Sources/ProHUD/Alert/AlertConfiguration.swift +++ b/Sources/ProHUD/Alert/AlertConfiguration.swift @@ -35,6 +35,11 @@ public class AlertConfiguration: CommonConfiguration { customBackgroundViewMask = callback } + /// 最小宽度 + public var cardMinWidth = CGFloat(32) + /// 最小高度 + public var cardMinHeight = CGFloat(32) + override var animateDurationForBuildOutByDefault: CGFloat { animateDurationForBuildOut ?? 0.2 } diff --git a/Sources/ProHUD/Alert/AlertDefaultLayout.swift b/Sources/ProHUD/Alert/AlertDefaultLayout.swift index a1be0a6..cb38768 100644 --- a/Sources/ProHUD/Alert/AlertDefaultLayout.swift +++ b/Sources/ProHUD/Alert/AlertDefaultLayout.swift @@ -150,8 +150,8 @@ extension AlertTarget { imageView.snp.remakeConstraints { (mk) in mk.top.left.greaterThanOrEqualTo(contentView).inset(cardEdgeInsets.top * 2) mk.right.bottom.lessThanOrEqualTo(contentView).inset(cardEdgeInsets.right * 2) - mk.width.equalTo(config.iconSize.width) - mk.height.equalTo(config.iconSize.height) + mk.width.equalTo(config.iconSizeByDefault.width) + mk.height.equalTo(config.iconSizeByDefault.height) } } if let rotation = vm?.rotation { diff --git a/Sources/ProHUD/Alert/AlertProvider.swift b/Sources/ProHUD/Alert/AlertProvider.swift index 1b1f3a8..04ca4fb 100644 --- a/Sources/ProHUD/Alert/AlertProvider.swift +++ b/Sources/ProHUD/Alert/AlertProvider.swift @@ -7,7 +7,7 @@ import UIKit -public final class AlertProvider: HUDProviderType { +open class AlertProvider: HUDProviderType { public typealias ViewModel = AlertViewModel public typealias Target = AlertTarget diff --git a/Sources/ProHUD/Capsule/CapsuleConfiguration.swift b/Sources/ProHUD/Capsule/CapsuleConfiguration.swift index 22b855f..8b4e162 100644 --- a/Sources/ProHUD/Capsule/CapsuleConfiguration.swift +++ b/Sources/ProHUD/Capsule/CapsuleConfiguration.swift @@ -27,18 +27,21 @@ public class CapsuleConfiguration: CommonConfiguration { /// 默认的持续时间 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 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 { animateDurationForBuildIn ?? 0.64 } diff --git a/Sources/ProHUD/Capsule/CapsuleDefaultLayout.swift b/Sources/ProHUD/Capsule/CapsuleDefaultLayout.swift index 86fd8b8..ac77829 100644 --- a/Sources/ProHUD/Capsule/CapsuleDefaultLayout.swift +++ b/Sources/ProHUD/Capsule/CapsuleDefaultLayout.swift @@ -24,23 +24,21 @@ extension CapsuleTarget: DefaultLayout { loadContentViewIfNeeded() loadContentMaskViewIfNeeded() - // image - setupImageView() - // text - textLabel.removeFromSuperview() + contentStack.addArrangedSubview(textLabel) var text = [vm?.title ?? "", vm?.message ?? ""].filter({ $0.count > 0 }).joined(separator: " ") if text.count > 0 { - contentStack.addArrangedSubview(textLabel) - textLabel.snp.makeConstraints { make in + textLabel.snp.remakeConstraints { make in make.width.lessThanOrEqualTo(AppContext.appBounds.width * 0.5) } + textLabel.snp.contentHuggingVerticalPriority = .infinity textLabel.text = text textLabel.sizeToFit() - } else { - contentStack.removeArrangedSubview(textLabel) } + // 后设置image, 需要参考text + setupImageView() + view.layoutIfNeeded() if isViewAppeared { @@ -91,6 +89,12 @@ extension CapsuleTarget: DefaultLayout { contentStack.removeArrangedSubview(imageView) } else { 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 if let iconURL = vm?.iconURL { diff --git a/Sources/ProHUD/Capsule/CapsuleManager.swift b/Sources/ProHUD/Capsule/CapsuleManager.swift index 0da93a1..37fabce 100644 --- a/Sources/ProHUD/Capsule/CapsuleManager.swift +++ b/Sources/ProHUD/Capsule/CapsuleManager.swift @@ -48,7 +48,9 @@ extension CapsuleTarget { let cardEdgeInsetsByDefault = config.cardEdgeInsetsByDefault view.layoutIfNeeded() 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 let newFrame: CGRect diff --git a/Sources/ProHUD/Capsule/CapsuleProvider.swift b/Sources/ProHUD/Capsule/CapsuleProvider.swift index 5047676..10ae9f3 100644 --- a/Sources/ProHUD/Capsule/CapsuleProvider.swift +++ b/Sources/ProHUD/Capsule/CapsuleProvider.swift @@ -7,7 +7,7 @@ import UIKit -public final class CapsuleProvider: HUDProviderType { +open class CapsuleProvider: HUDProviderType { public typealias ViewModel = CapsuleViewModel public typealias Target = CapsuleTarget diff --git a/Sources/ProHUD/Capsule/CapsuleTarget.swift b/Sources/ProHUD/Capsule/CapsuleTarget.swift index 5030a85..a8934bf 100644 --- a/Sources/ProHUD/Capsule/CapsuleTarget.swift +++ b/Sources/ProHUD/Capsule/CapsuleTarget.swift @@ -18,7 +18,7 @@ open class CapsuleTarget: BaseController, HUDTargetType { let stack = StackView() stack.spacing = 8 stack.distribution = .equalCentering - stack.alignment = .fill + stack.alignment = .center stack.axis = .horizontal config.customContentStack?(stack) return stack diff --git a/Sources/ProHUD/Core/Models/BaseViewModel.swift b/Sources/ProHUD/Core/Models/BaseViewModel.swift index 3ef2dc9..a4fd4d8 100644 --- a/Sources/ProHUD/Core/Models/BaseViewModel.swift +++ b/Sources/ProHUD/Core/Models/BaseViewModel.swift @@ -101,47 +101,55 @@ open class BaseViewModel: NSObject, HUDViewModelType { // MARK: - convenience func public extension BaseViewModel { - func identifier(_ identifier: String?) -> Self { + @discardableResult func identifier(_ identifier: String?) -> Self { self.identifier = identifier 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)") return self } - func icon(_ image: UIImage?) -> Self { + @discardableResult func icon(_ image: UIImage?) -> Self { self.icon = image return self } - func icon(_ imageURL: URL?) -> Self { + @discardableResult func icon(_ imageURL: URL?) -> Self { self.iconURL = imageURL 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 return self } - func message(_ text: String?) -> Self { + @discardableResult func message(_ text: String?) -> Self { self.message = text return self } - func duration(_ seconds: TimeInterval?) -> Self { + @discardableResult func duration(_ seconds: TimeInterval?) -> Self { self.duration = seconds return self } - func rotation(_ rotation: Rotation?) -> Self { + @discardableResult func rotation(_ rotation: Rotation?) -> Self { self.rotation = rotation return self } - func tintColor(_ tintColor: UIColor?) -> Self { + @discardableResult func tintColor(_ tintColor: UIColor?) -> Self { self.tintColor = tintColor return self } @@ -173,6 +181,16 @@ public extension BaseViewModel { .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 static var loading: Self { .init() diff --git a/Sources/ProHUD/Core/Models/Configuration.swift b/Sources/ProHUD/Core/Models/Configuration.swift index 0bdec18..5c8e19e 100644 --- a/Sources/ProHUD/Core/Models/Configuration.swift +++ b/Sources/ProHUD/Core/Models/Configuration.swift @@ -60,10 +60,6 @@ open class CommonConfiguration: NSObject { var cardMaxHeightByDefault: CGFloat { cardMaxHeight ?? (AppContext.appBounds.height - 100) } - /// 最小宽度 - public var cardMinWidth = CGFloat(32) - /// 最小高度 - public var cardMinHeight = CGFloat(32) /// 卡片内边距 public var cardEdgeInsets: UIEdgeInsets? @@ -102,7 +98,9 @@ open class CommonConfiguration: NSObject { // MARK: 图标样式 /// 图标尺寸 - public var iconSize = CGSize(width: 44, height: 44) + public var iconSize: CGSize? + + var iconSizeByDefault: CGSize { iconSize ?? .init(width: 44, height: 44) } // MARK: 文本样式 diff --git a/Sources/ProHUD/Sheet/SheetProvider.swift b/Sources/ProHUD/Sheet/SheetProvider.swift index e201d8c..55befac 100644 --- a/Sources/ProHUD/Sheet/SheetProvider.swift +++ b/Sources/ProHUD/Sheet/SheetProvider.swift @@ -14,7 +14,7 @@ public final class SheetProvider: HUDProviderType { /// 根据自定义的初始化代码创建一个Target并显示 /// - Parameter initializer: 初始化代码(传空值时不会做任何事) - @discardableResult public required init(initializer: ((_ target: Target) -> Void)?) { + @discardableResult public required init(initializer: ((_ sheet: Target) -> Void)?) { guard let initializer = initializer else { // Provider的作用就是push一个target // 如果没有任何初始化代码就没有target,就是个无意义的Provider diff --git a/Sources/ProHUD/Toast/ToastDefaultLayout.swift b/Sources/ProHUD/Toast/ToastDefaultLayout.swift index 2abacaf..b721493 100644 --- a/Sources/ProHUD/Toast/ToastDefaultLayout.swift +++ b/Sources/ProHUD/Toast/ToastDefaultLayout.swift @@ -33,9 +33,9 @@ extension ToastTarget: DefaultLayout { infoStack.insertArrangedSubview(imageView, at: 0) imageView.snp.makeConstraints { make in if titleCount > 0 && bodyCount > 0 { - make.width.height.equalTo(config.iconSize) + make.width.height.equalTo(config.iconSizeByDefault) } else { - make.width.equalTo(config.iconSize) + make.width.equalTo(config.iconSizeByDefault) } } } diff --git a/Sources/ProHUD/Toast/ToastProvider.swift b/Sources/ProHUD/Toast/ToastProvider.swift index 9dcdea5..0a3398c 100644 --- a/Sources/ProHUD/Toast/ToastProvider.swift +++ b/Sources/ProHUD/Toast/ToastProvider.swift @@ -7,7 +7,7 @@ import UIKit -public final class ToastProvider: HUDProviderType { +open class ToastProvider: HUDProviderType { public typealias ViewModel = ToastViewModel public typealias Target = ToastTarget