代码优化

This commit is contained in:
xaoxuu 2023-08-22 13:01:16 +08:00
parent cbd654d80b
commit c3afd50eac
15 changed files with 83 additions and 42 deletions

View File

@ -25,7 +25,7 @@ class DemoCapsuleVC: ListVC {
// vmhandlerpushvm // vmhandlerpushvm
// Capsule(.message("")) // Capsule(.message(""))
// //
Capsule("一条简短消息") Capsule("成功")
} }
section.add(title: "一条稍微长一点的消息") { section.add(title: "一条稍微长一点的消息") {
// vmhandlerpushhandler // vmhandlerpushhandler
@ -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)
}
}
}
} }
} }

View File

@ -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")

View File

@ -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
} }

View File

@ -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 {

View File

@ -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

View File

@ -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
} }

View File

@ -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 {

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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()

View File

@ -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:

View File

@ -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 {
// Providerpushtarget // Providerpushtarget
// targetProvider // targetProvider

View File

@ -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)
} }
} }
} }

View File

@ -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