From 5fb9e6efc3cf9d5dc893f60f79ed4acf5449c950 Mon Sep 17 00:00:00 2001 From: xaoxuu Date: Thu, 17 Aug 2023 16:41:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=AE=BE=E7=BD=AEtintColor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PHDemo/PHDemo/CapsuleVC.swift | 14 ++++++- PHDemo/PHDemo/Extensions.swift | 40 ++++++++++++------- Sources/ProHUD/Alert/Alert.swift | 2 + Sources/ProHUD/Capsule/Capsule.swift | 8 ++-- .../ProHUD/Capsule/CapsuleDefaultLayout.swift | 2 +- .../{ViewModel.swift => BaseViewModel.swift} | 17 +++++--- Sources/ProHUD/Core/Utils/AppContext.swift | 4 +- Sources/ProHUD/Toast/Toast.swift | 2 + 8 files changed, 60 insertions(+), 29 deletions(-) rename Sources/ProHUD/Core/Models/{ViewModel.swift => BaseViewModel.swift} (92%) diff --git a/PHDemo/PHDemo/CapsuleVC.swift b/PHDemo/PHDemo/CapsuleVC.swift index 585b9ce..d4f68d8 100644 --- a/PHDemo/PHDemo/CapsuleVC.swift +++ b/PHDemo/PHDemo/CapsuleVC.swift @@ -43,7 +43,7 @@ class CapsuleVC: ListVC { Capsule(.info("一条简短的消息")).push() } section.add(title: "一条稍微长一点的消息") { - Capsule(.info("一条稍微长一点的消息")).push() + Capsule(.systemError.title("500").message("一条稍微长一点的消息")).push() } section.add(title: "(默认)状态胶囊控件,用于状态显示,一个主程序窗口只有一个状态胶囊实例。") { Capsule(.info("状态胶囊控件,用于状态显示,一个主程序窗口只有一个状态胶囊实例。")).push() @@ -165,7 +165,7 @@ class CapsuleVC: ListVC { } -extension Capsule.CapsuleViewModel { +extension Capsule.ViewModel { static func info(_ text: String?) -> Self { .init() @@ -184,4 +184,14 @@ extension Capsule.CapsuleViewModel { .duration(.infinity) } + static var systemError: Self { + .init() + .icon(.init(systemName: "xmark.circle.fill")) + .tintColor(.systemRed) + } + static func systemError(_ text: String?) -> Self { + .systemError + .message(text) + } + } diff --git a/PHDemo/PHDemo/Extensions.swift b/PHDemo/PHDemo/Extensions.swift index 290454b..0769765 100644 --- a/PHDemo/PHDemo/Extensions.swift +++ b/PHDemo/PHDemo/Extensions.swift @@ -8,7 +8,7 @@ import UIKit import ProHUD -public extension ViewModel { +public extension BaseViewModel { // static var plain: ViewModel { // ViewModel() // } @@ -16,27 +16,37 @@ public extension ViewModel { // ViewModel(icon: UIImage(named: "prohud.note")) // } // MARK: note - static var note: ViewModel { - .init(icon: UIImage(named: "demo.note")) + static var note: Self { + .init() + .icon(.init(named: "demo.note")) } - static func note(_ seconds: TimeInterval) -> ViewModel { - .init(icon: UIImage(named: "demo.note"), duration: seconds) + static func note(_ seconds: TimeInterval) -> Self { + .init() + .icon(.init(named: "demo.note")) + .duration(seconds) } - static var msg: ViewModel { - ViewModel(icon: UIImage(named: "demo.message")) + static var msg: Self { + .init() + .icon(.init(named: "demo.message")) } - static func msg(_ seconds: TimeInterval) -> ViewModel { - ViewModel(icon: UIImage(named: "demo.message"), duration: seconds) + static func msg(_ seconds: TimeInterval) -> Self { + .init() + .icon(.init(named: "demo.message")) + .duration(seconds) } - static var delete: ViewModel { - ViewModel(icon: UIImage(named: "demo.trash")) + static var delete: Self { + .init() + .icon(.init(named: "demo.trash")) } // MARK: confirm - static var confirm: ViewModel { - .init(icon: UIImage(named: "demo.questionmark")) + static var confirm: Self { + .init() + .icon(.init(named: "demo.questionmark")) } - static func confirm(_ seconds: TimeInterval) -> ViewModel { - .init(icon: UIImage(named: "demo.questionmark"), duration: seconds) + static func confirm(_ seconds: TimeInterval) -> Self { + .init() + .icon(.init(named: "demo.questionmark")) + .duration(seconds) } // static func loading(_ seconds: TimeInterval) -> ViewModel { diff --git a/Sources/ProHUD/Alert/Alert.swift b/Sources/ProHUD/Alert/Alert.swift index 0468878..b923a42 100644 --- a/Sources/ProHUD/Alert/Alert.swift +++ b/Sources/ProHUD/Alert/Alert.swift @@ -73,6 +73,8 @@ open class Alert: ProHUD.Controller { return stack }() + open class ViewModel: BaseViewModel {} + /// 视图模型 public var vm = ViewModel() diff --git a/Sources/ProHUD/Capsule/Capsule.swift b/Sources/ProHUD/Capsule/Capsule.swift index 7a7cdbc..059d91e 100644 --- a/Sources/ProHUD/Capsule/Capsule.swift +++ b/Sources/ProHUD/Capsule/Capsule.swift @@ -44,7 +44,7 @@ open class Capsule: Controller { return lb }() - open class CapsuleViewModel: ViewModel { + open class ViewModel: BaseViewModel { public enum Position { case top @@ -72,11 +72,11 @@ open class Capsule: Controller { } /// 视图模型 - public var vm = CapsuleViewModel() + public var vm = ViewModel() private var tapActionCallback: ((_ capsule: Capsule) -> Void)? - @discardableResult public init(_ vm: CapsuleViewModel, handler: ((_ capsule: Capsule) -> Void)? = nil) { + @discardableResult public init(_ vm: ViewModel, handler: ((_ capsule: Capsule) -> Void)? = nil) { super.init() self.vm = vm handler?(self) @@ -97,7 +97,7 @@ open class Capsule: Controller { public override func viewDidLoad() { super.viewDidLoad() - view.tintColor = config.tintColor + view.tintColor = vm.tintColor ?? config.tintColor view.layer.shadowRadius = 8 view.layer.shadowOffset = .init(width: 0, height: 5) view.layer.shadowOpacity = 0.1 diff --git a/Sources/ProHUD/Capsule/CapsuleDefaultLayout.swift b/Sources/ProHUD/Capsule/CapsuleDefaultLayout.swift index dfdb763..dd2c4f6 100644 --- a/Sources/ProHUD/Capsule/CapsuleDefaultLayout.swift +++ b/Sources/ProHUD/Capsule/CapsuleDefaultLayout.swift @@ -33,7 +33,7 @@ extension Capsule: DefaultLayout { // text textLabel.removeFromSuperview() - let text = (vm.title ?? "") + (vm.message ?? "") + var text = [vm.title ?? "", vm.message ?? ""].filter({ $0.count > 0 }).joined(separator: " ") if text.count > 0 { contentStack.addArrangedSubview(textLabel) textLabel.snp.makeConstraints { make in diff --git a/Sources/ProHUD/Core/Models/ViewModel.swift b/Sources/ProHUD/Core/Models/BaseViewModel.swift similarity index 92% rename from Sources/ProHUD/Core/Models/ViewModel.swift rename to Sources/ProHUD/Core/Models/BaseViewModel.swift index 0da5b83..a7a3bde 100644 --- a/Sources/ProHUD/Core/Models/ViewModel.swift +++ b/Sources/ProHUD/Core/Models/BaseViewModel.swift @@ -1,6 +1,6 @@ // -// ViewModel.swift -// +// BaseViewModel.swift +// // // Created by xaoxuu on 2022/8/29. // @@ -8,7 +8,7 @@ import UIKit /// 数据模型 -open class ViewModel: NSObject { +open class BaseViewModel: NSObject { /// 图标 open var icon: UIImage? @@ -23,6 +23,8 @@ open class ViewModel: NSObject { /// 消息正文 open var message: String? + open var tintColor: UIColor? + /// 持续时间(为空代表根据场景不同的默认配置,为0代表无穷大) open var duration: TimeInterval? { didSet { @@ -64,7 +66,7 @@ open class ViewModel: NSObject { } // MARK: - convenience func -public extension ViewModel { +public extension BaseViewModel { func icon(_ image: UIImage?) -> Self { self.icon = image @@ -96,10 +98,15 @@ public extension ViewModel { return self } + func tintColor(_ tintColor: UIColor?) -> Self { + self.tintColor = tintColor + return self + } + } // MARK: - example scenes -public extension ViewModel { +public extension BaseViewModel { // MARK: plain static func title(_ text: String?) -> Self { diff --git a/Sources/ProHUD/Core/Utils/AppContext.swift b/Sources/ProHUD/Core/Utils/AppContext.swift index cb14581..0071857 100644 --- a/Sources/ProHUD/Core/Utils/AppContext.swift +++ b/Sources/ProHUD/Core/Utils/AppContext.swift @@ -36,7 +36,7 @@ public struct AppContext { static var toastWindows: [UIWindowScene: [ToastWindow]] = [:] static var alertWindow: [UIWindowScene: AlertWindow] = [:] static var sheetWindows: [UIWindowScene: [SheetWindow]] = [:] - static var capsuleWindows: [UIWindowScene: [Capsule.CapsuleViewModel.Position: CapsuleWindow]] = [:] + static var capsuleWindows: [UIWindowScene: [Capsule.ViewModel.Position: CapsuleWindow]] = [:] static var current: AppContext? { guard let windowScene = windowScene else { return nil } @@ -120,7 +120,7 @@ extension AppContext { var toastWindows: [ToastWindow] { Self.toastWindows[windowScene] ?? [] } - var capsuleWindows: [Capsule.CapsuleViewModel.Position: CapsuleWindow] { + var capsuleWindows: [Capsule.ViewModel.Position: CapsuleWindow] { Self.capsuleWindows[windowScene] ?? [:] } } diff --git a/Sources/ProHUD/Toast/Toast.swift b/Sources/ProHUD/Toast/Toast.swift index bcb838c..5fe3617 100644 --- a/Sources/ProHUD/Toast/Toast.swift +++ b/Sources/ProHUD/Toast/Toast.swift @@ -83,6 +83,8 @@ open class Toast: Controller { /// 是否可以通过手势移除(向上滑出屏幕) public var isRemovable = true + open class ViewModel: BaseViewModel {} + /// 视图模型 public var vm = ViewModel()