代码优化

This commit is contained in:
xaoxuu 2023-08-20 00:37:08 +08:00
parent ccaad1d13d
commit a37cb52b79
14 changed files with 146 additions and 53 deletions

View File

@ -110,7 +110,7 @@ class DemoAlertVC: ListVC {
alert.config.customButton { button in alert.config.customButton { button in
button.titleLabel?.font = .systemFont(ofSize: 15) button.titleLabel?.font = .systemFont(ofSize: 15)
} }
alert.vm?.message = "为了维护社区氛围,上麦用户需进行主播认证" alert.title = "为了维护社区氛围,上麦用户需进行主播认证"
alert.onViewDidLoad { vc in alert.onViewDidLoad { vc in
guard let alert = vc as? AlertTarget else { guard let alert = vc as? AlertTarget else {
return return
@ -140,7 +140,7 @@ class DemoAlertVC: ListVC {
alert.config.customButton { button in alert.config.customButton { button in
button.titleLabel?.font = .systemFont(ofSize: 15) button.titleLabel?.font = .systemFont(ofSize: 15)
} }
alert.vm?.message = "本次消费需要你支付999软妹豆确认支付吗" alert.title = "本次消费需要你支付999软妹豆确认支付吗"
alert.config.customActionStack { stack in alert.config.customActionStack { stack in
stack.spacing = 0 stack.spacing = 0
stack.axis = .vertical // stack.axis = .vertical //
@ -193,8 +193,7 @@ class DemoAlertVC: ListVC {
} }
section.add(title: "标题 + 正文 + 按钮") { section.add(title: "标题 + 正文 + 按钮") {
Alert { alert in Alert { alert in
alert.vm?.title = "标题" alert.vm = .title("标题").message("这是一段正文,长度超出最大宽度时会自动换行")
alert.vm?.message = "这是一段正文,长度超出最大宽度时会自动换行"
alert.add(action: "取消", style: .gray) alert.add(action: "取消", style: .gray)
alert.add(action: "删除", style: .destructive) { alert in alert.add(action: "删除", style: .destructive) { alert in
// pop // pop
@ -301,8 +300,7 @@ class DemoAlertVC: ListVC {
section.add(title: "多层级弹窗") { section.add(title: "多层级弹窗") {
func f(i: Int) { func f(i: Int) {
Alert { alert in Alert { alert in
alert.vm?.title = "\(i)次弹" alert.vm = .title("\(i)次弹").message("每次都是一个新的实例覆盖在上一个弹窗上面,而背景不会叠加变深。")
alert.vm?.message = "每次都是一个新的实例覆盖在上一个弹窗上面,而背景不会叠加变深。"
alert.add(action: "取消", style: .gray) alert.add(action: "取消", style: .gray)
alert.add(action: "增加一个") { alert in alert.add(action: "增加一个") { alert in
f(i: i + 1) f(i: i + 1)
@ -338,7 +336,7 @@ class DemoAlertVC: ListVC {
let vc = UIViewController() let vc = UIViewController()
vc.title = "页面" vc.title = "页面"
vc.view.backgroundColor = .systemYellow vc.view.backgroundColor = .systemYellow
let alert = Alert(.loading.title("正在加载").message("这个弹窗被放在指定容器中")).target let alert = AlertTarget(.loading.title("正在加载").message("这个弹窗被放在指定容器中"))
alert.add(action: "返回上一页") { alert in alert.add(action: "返回上一页") { alert in
vc.dismiss(animated: true) vc.dismiss(animated: true)
} }

View File

@ -35,9 +35,8 @@ class DemoCapsuleVC: ListVC {
} }
} }
section.add(title: "延迟显示") { section.add(title: "延迟显示") {
// push // Targetpush
let obj = Capsule().target let obj = CapsuleTarget(.message("状态胶囊控件,用于状态显示,一个主程序窗口只有一个状态胶囊实例。"))
obj.vm = .message("状态胶囊控件,用于状态显示,一个主程序窗口只有一个状态胶囊实例。")
// ... push // ... push
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
obj.push() obj.push()
@ -55,7 +54,7 @@ class DemoCapsuleVC: ListVC {
list.add(title: "默认布局:图文") { section in list.add(title: "默认布局:图文") { section in
section.add(title: "下载进度") { section.add(title: "下载进度") {
let capsule = Capsule().target 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)
capsule.update(progress: 0) capsule.update(progress: 0)
capsule.push() capsule.push()

View File

@ -79,7 +79,7 @@ class DemoToastVC: ListVC {
section.add(title: "图标 + 标题 + 正文") { section.add(title: "图标 + 标题 + 正文") {
let s1 = "笑容正在加载" let s1 = "笑容正在加载"
let s2 = "这通常不会太久" let s2 = "这通常不会太久"
let toast = Toast(.loading.title(s1).message(s2)).target let toast = ToastTarget(.loading.title(s1).message(s2))
toast.push() toast.push()
toast.update(progress: 0) toast.update(progress: 0)
updateProgress(in: 4) { percent in updateProgress(in: 4) { percent in
@ -186,9 +186,7 @@ class DemoToastVC: ListVC {
} }
section.add(title: "不存在就创建,存在就更新") { section.add(title: "不存在就创建,存在就更新") {
i += 1 i += 1
Toast.lazyPush(identifier: "loading") { toast in Toast(.identifier("loading").title("正在加载\(i)").message("这条消息不会重复显示多条"))
toast.vm = .loading.title("正在加载\(i)").message("这条消息不会重复显示多条")
}
} }
section.add(title: "如果存在就更新,如果不存在就忽略") { section.add(title: "如果存在就更新,如果不存在就忽略") {
i += 1 i += 1

View File

@ -21,9 +21,17 @@ open class AlertProvider: HUDProvider<AlertViewModel, AlertTarget> {
/// - vm: /// - vm:
/// - initializer: /// - initializer:
@discardableResult public convenience init(_ vm: ViewModel, initializer: ((_ alert: Target) -> Void)?) { @discardableResult public convenience init(_ vm: ViewModel, initializer: ((_ alert: Target) -> Void)?) {
self.init { alert in if let id = vm.identifier, id.count > 0 {
alert.vm = vm Self.lazyPush(identifier: id) { target in
initializer?(alert) target.vm = vm
initializer?(target)
}
self.init(initializer: nil)
} else {
self.init { target in
target.vm = vm
initializer?(target)
}
} }
} }
/// ViewModelTarget /// ViewModelTarget

View File

@ -9,6 +9,8 @@ import UIKit
open class AlertTarget: BaseController, HUDTargetType { open class AlertTarget: BaseController, HUDTargetType {
public typealias ViewModel = AlertViewModel
public lazy var config: AlertConfiguration = { public lazy var config: AlertConfiguration = {
var cfg = AlertConfiguration() var cfg = AlertConfiguration()
AlertConfiguration.customGlobalConfig?(cfg) AlertConfiguration.customGlobalConfig?(cfg)
@ -74,7 +76,11 @@ open class AlertTarget: BaseController, HUDTargetType {
}() }()
/// ///
@objc public var vm: AlertViewModel? @objc public var vm: AlertViewModel? {
didSet {
vm?.vc = self
}
}
public override var title: String? { public override var title: String? {
didSet { didSet {
@ -94,6 +100,11 @@ open class AlertTarget: BaseController, HUDTargetType {
fatalError("init(coder:) has not been implemented") fatalError("init(coder:) has not been implemented")
} }
public convenience init(_ vm: ViewModel) {
self.init()
self.vm = vm
}
public override func viewDidLoad() { public override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
reloadData(animated: false) reloadData(animated: false)

View File

@ -21,9 +21,17 @@ open class CapsuleProvider: HUDProvider<CapsuleViewModel, CapsuleTarget> {
/// - vm: /// - vm:
/// - initializer: /// - initializer:
@discardableResult public convenience init(_ vm: ViewModel, initializer: ((_ capsule: Target) -> Void)?) { @discardableResult public convenience init(_ vm: ViewModel, initializer: ((_ capsule: Target) -> Void)?) {
self.init { capsule in if let id = vm.identifier, id.count > 0 {
capsule.vm = vm Self.lazyPush(identifier: id) { target in
initializer?(capsule) target.vm = vm
initializer?(target)
}
self.init(initializer: nil)
} else {
self.init { target in
target.vm = vm
initializer?(target)
}
} }
} }

View File

@ -9,7 +9,9 @@ import UIKit
open class CapsuleTarget: BaseController, HUDTargetType { open class CapsuleTarget: BaseController, HUDTargetType {
public lazy var config: CapsuleConfiguration = { public typealias ViewModel = CapsuleViewModel
@objc public lazy var config: CapsuleConfiguration = {
var cfg = CapsuleConfiguration() var cfg = CapsuleConfiguration()
CapsuleConfiguration.customGlobalConfig?(cfg) CapsuleConfiguration.customGlobalConfig?(cfg)
return cfg return cfg
@ -46,7 +48,11 @@ open class CapsuleTarget: BaseController, HUDTargetType {
return lb return lb
}() }()
public var vm: CapsuleViewModel? @objc public var vm: CapsuleViewModel? {
didSet {
vm?.vc = self
}
}
public override var title: String? { public override var title: String? {
didSet { didSet {
@ -68,6 +74,11 @@ open class CapsuleTarget: BaseController, HUDTargetType {
fatalError("init(coder:) has not been implemented") fatalError("init(coder:) has not been implemented")
} }
public convenience init(_ vm: ViewModel) {
self.init()
self.vm = vm
}
public override func viewDidLoad() { public override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
view.layer.shadowRadius = 8 view.layer.shadowRadius = 8
@ -84,7 +95,7 @@ open class CapsuleTarget: BaseController, HUDTargetType {
} }
public func onTapped(action: @escaping (_ capsule: CapsuleTarget) -> Void) { @objc public func onTapped(action: @escaping (_ capsule: CapsuleTarget) -> Void) {
self.tapActionCallback = action self.tapActionCallback = action
} }

View File

@ -12,6 +12,26 @@ public protocol HUDViewModelType {}
/// ///
open class BaseViewModel: NSObject, HUDViewModelType { open class BaseViewModel: NSObject, HUDViewModelType {
/// targetid
private var tmpStoredIdentifier: String?
/// target
@objc open var identifier: String? {
set {
tmpStoredIdentifier = newValue
if let id = tmpStoredIdentifier, id.count > 0 {
vc?.identifier = id
}
}
get {
if let vc = vc {
return vc.identifier
} else {
return tmpStoredIdentifier
}
}
}
/// ///
@objc open var icon: UIImage? @objc open var icon: UIImage?
@objc var iconURL: URL? @objc var iconURL: URL?
@ -34,7 +54,15 @@ open class BaseViewModel: NSObject, HUDViewModelType {
} }
} }
@objc public required override init() { weak var vc: BaseController? {
didSet {
if let id = tmpStoredIdentifier {
vc?.identifier = id
}
}
}
@objc required public override init() {
} }
@ -70,6 +98,11 @@ open class BaseViewModel: NSObject, HUDViewModelType {
// MARK: - convenience func // MARK: - convenience func
public extension BaseViewModel { public extension BaseViewModel {
func identifier(_ identifier: String?) -> Self {
self.identifier = identifier
return self
}
func icon(_ image: UIImage?) -> Self { func icon(_ image: UIImage?) -> Self {
self.icon = image self.icon = image
return self return self
@ -110,6 +143,11 @@ public extension BaseViewModel {
// MARK: - example scenes // MARK: - example scenes
public extension BaseViewModel { public extension BaseViewModel {
static func identifier(_ text: String?) -> Self {
.init()
.identifier(text)
}
// MARK: plain // MARK: plain
static func title(_ text: String?) -> Self { static func title(_ text: String?) -> Self {
.init() .init()

View File

@ -18,28 +18,23 @@ public protocol HUDProviderType {
} }
open class HUDProvider<ViewModel: HUDViewModelType, Target: HUDTargetType>: HUDProviderType { open class HUDProvider<ViewModel: HUDViewModelType, Target: HUDTargetType>: NSObject, HUDProviderType {
/// HUD
public var target: Target
/// Target /// Target
/// - Parameter initializer: /// - Parameter initializer:
@discardableResult public required init(initializer: ((_ target: Target) -> Void)?) { @discardableResult public required init(initializer: ((_ target: Target) -> Void)?) {
guard let initializer = initializer else {
// Providerpushtarget
// targetProvider
// lazyPushProvider
// self.init(initializer: nil)
return
}
var t = Target() var t = Target()
initializer?(t) initializer(t)
self.target = t
if (t.vm == nil && initializer == nil) == false {
DispatchQueue.main.async { DispatchQueue.main.async {
t.push() t.push()
} }
} }
}
/// target.push()
@discardableResult public convenience init() {
self.init(initializer: nil)
}
} }

View File

@ -34,7 +34,7 @@ public class ProgressView: UIView {
let bgLayer = CAShapeLayer() let bgLayer = CAShapeLayer()
bgLayer.fillColor = UIColor.clear.cgColor bgLayer.fillColor = UIColor.clear.cgColor
bgLayer.path = path.cgPath bgLayer.path = path.cgPath
bgLayer.strokeColor = UIColor.white.cgColor bgLayer.strokeColor = UIColor.clear.cgColor
bgLayer.lineWidth = lineWidth bgLayer.lineWidth = lineWidth
bgLayer.lineCap = .round bgLayer.lineCap = .round
bgLayer.strokeStart = 0 bgLayer.strokeStart = 0

View File

@ -41,7 +41,11 @@ open class SheetTarget: BaseController, HUDTargetType {
} }
} }
public var vm: SheetViewModel? = nil public var vm: SheetViewModel? = nil {
didSet {
vm?.vc = self
}
}
required public override init() { required public override init() {
super.init() super.init()

View File

@ -26,11 +26,17 @@ extension ToastTarget: DefaultLayout {
} }
return return
} }
let titleCount = vm?.title?.count ?? 0
let bodyCount = vm?.message?.count ?? 0
if vm?.icon != nil || vm?.iconURL != nil { if vm?.icon != nil || vm?.iconURL != nil {
if imageView.superview == nil { if imageView.superview == nil {
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 {
make.width.height.equalTo(config.iconSize) make.width.height.equalTo(config.iconSize)
} else {
make.width.equalTo(config.iconSize)
}
} }
} }
} else { } else {
@ -42,8 +48,6 @@ extension ToastTarget: DefaultLayout {
if textStack.superview == nil { if textStack.superview == nil {
infoStack.addArrangedSubview(textStack) infoStack.addArrangedSubview(textStack)
} }
let titleCount = vm?.title?.count ?? 0
let bodyCount = vm?.message?.count ?? 0
if titleCount > 0 { if titleCount > 0 {
textStack.insertArrangedSubview(titleLabel, at: 0) textStack.insertArrangedSubview(titleLabel, at: 0)
if bodyCount > 0 { if bodyCount > 0 {

View File

@ -21,9 +21,17 @@ open class ToastProvider: HUDProvider<ToastViewModel, ToastTarget> {
/// - vm: /// - vm:
/// - initializer: /// - initializer:
@discardableResult public convenience init(_ vm: ViewModel, initializer: ((_ toast: Target) -> Void)?) { @discardableResult public convenience init(_ vm: ViewModel, initializer: ((_ toast: Target) -> Void)?) {
self.init { toast in if let id = vm.identifier, id.count > 0 {
toast.vm = vm Self.lazyPush(identifier: id) { target in
initializer?(toast) target.vm = vm
initializer?(target)
}
self.init(initializer: nil)
} else {
self.init { target in
target.vm = vm
initializer?(target)
}
} }
} }

View File

@ -9,6 +9,8 @@ import UIKit
open class ToastTarget: BaseController, HUDTargetType { open class ToastTarget: BaseController, HUDTargetType {
public typealias ViewModel = ToastViewModel
weak var window: ToastWindow? weak var window: ToastWindow?
public lazy var config: ToastConfiguration = { public lazy var config: ToastConfiguration = {
@ -84,7 +86,11 @@ open class ToastTarget: BaseController, HUDTargetType {
public var isRemovable = true public var isRemovable = true
/// ///
@objc public var vm: ToastViewModel? @objc public var vm: ToastViewModel? {
didSet {
vm?.vc = self
}
}
private var tapActionCallback: ((_ toast: ToastTarget) -> Void)? private var tapActionCallback: ((_ toast: ToastTarget) -> Void)?
@ -103,7 +109,12 @@ open class ToastTarget: BaseController, HUDTargetType {
} }
required public init?(coder: NSCoder) { required public init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented") super.init(coder: coder)
}
public convenience init(_ vm: ViewModel) {
self.init()
self.vm = vm
} }
public override func viewDidLoad() { public override func viewDidLoad() {