代码优化

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

View File

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

View File

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

View File

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

View File

@ -7,7 +7,7 @@
import UIKit
public final class AlertProvider: HUDProviderType {
open class AlertProvider: HUDProviderType {
public typealias ViewModel = AlertViewModel
public typealias Target = AlertTarget

View File

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

View File

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

View File

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

View File

@ -7,7 +7,7 @@
import UIKit
public final class CapsuleProvider: HUDProviderType {
open class CapsuleProvider: HUDProviderType {
public typealias ViewModel = CapsuleViewModel
public typealias Target = CapsuleTarget

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -7,7 +7,7 @@
import UIKit
public final class ToastProvider: HUDProviderType {
open class ToastProvider: HUDProviderType {
public typealias ViewModel = ToastViewModel
public typealias Target = ToastTarget