代码优化

This commit is contained in:
xaoxuu 2023-04-27 21:06:48 +08:00
parent 398a9f8472
commit a41d03ca00
22 changed files with 105 additions and 59 deletions

View File

@ -71,14 +71,17 @@ class AlertVC: ListVC {
list.add(title: "文字 + 按钮") { section in
section.add(title: "只有一小段文字 + 无背景色按钮") {
Alert { alert in
alert.config.cardMinWidth = 270
alert.config.cardEdgeInsets = .init(top: 0, left: 0, bottom: 16, right: 0)
alert.config.textEdgeInsets = .init(top: 30, left: 32, bottom: 30, right: 32)
alert.config.cardCornerRadius = 12
alert.vm.title = "你正在使用移动网络观看"
alert.config.customTextLabel { label in
label.font = .systemFont(ofSize: 15)
}
alert.config.customButton { button in
button.titleLabel?.font = .systemFont(ofSize: 15)
}
alert.vm.title = "你正在使用移动网络观看"
} .onViewDidLoad { vc in
guard let alert = vc as? Alert else {
return
@ -97,14 +100,17 @@ class AlertVC: ListVC {
}
section.add(title: "只有一段文字 + 无背景色按钮") {
Alert { alert in
alert.config.cardMinWidth = 270
alert.config.cardEdgeInsets = .init(top: 0, left: 0, bottom: 16, right: 0)
alert.config.textEdgeInsets = .init(top: 30, left: 32, bottom: 30, right: 32)
alert.config.cardCornerRadius = 12
alert.vm.title = "为了维护社区氛围,上麦用户需进行主播认证"
alert.config.customBodyLabel { label in
alert.config.customTextLabel { label in
label.font = .systemFont(ofSize: 15)
}
alert.config.customButton { button in
button.titleLabel?.font = .systemFont(ofSize: 15)
}
alert.vm.message = "为了维护社区氛围,上麦用户需进行主播认证"
} .onViewDidLoad { vc in
guard let alert = vc as? Alert else {
return
@ -315,7 +321,7 @@ class AlertVC: ListVC {
slider.maximumValue = 100
slider.value = 50
alert.add(action: slider)
alert.add(actionSpacing: 124)
alert.add(actionSpacing: 24)
alert.add(action: "取消", style: .gray)
}
}

View File

@ -81,7 +81,6 @@ class SheetVC: ListVC {
toast.vm.message = "点击背景将不会dismiss必须在下方做出选择才能关掉"
toast.vm.duration = 2
}
}
}
}
@ -112,13 +111,13 @@ class SheetVC: ListVC {
let s2 = UISlider()
s2.minimumValue = 0
s2.maximumValue = 40
s2.value = Float(sheet.config.edgeInset)
s2.value = Float(sheet.config.screenEdgeInset)
sheet.add(subview: s2).snp.makeConstraints { make in
make.height.equalTo(50)
}
if #available(iOS 14.0, *) {
s2.addAction(.init(handler: { [unowned s2] act in
sheet.config.edgeInset = CGFloat(s2.value)
sheet.config.screenEdgeInset = CGFloat(s2.value)
sheet.reloadData()
}), for: .valueChanged)
} else {

View File

@ -155,7 +155,9 @@ class ToastVC: ListVC {
list.add(title: "实例管理") { section in
var i = 0
section.add(title: "多实例共存") {
Toast(.loading.title("多实例共存").message("直接创建的实例,以平铺方式排列").duration(2)).push()
Toast(.loading.title("多实例共存").message("直接创建的实例,以平铺方式排列").duration(2)) { toast in
toast.config.cardEdgeInsets = .init(top: 32, left: 20, bottom: 32, right: 20)
}
}
section.add(title: "不存在就创建,存在就更新") {
i += 1
@ -244,12 +246,12 @@ class ToastVC: ListVC {
lb.textAlignment = .center
stack.addArrangedSubview(lb)
let btn1 = ProHUD.Button(config: toast.config, action: .init(style: .gray, title: "取消"))
let btn1 = ProHUD.ToastButton(config: toast.config, action: .init(style: .gray, title: "取消"))
btn1.onTouchUpInside { action in
print("点击了取消")
testAlert()
}
let btn2 = ProHUD.Button(config: toast.config, action: .init(style: .tinted, title: "确定"))
let btn2 = ProHUD.ToastButton(config: toast.config, action: .init(style: .tinted, title: "确定"))
btn2.onTouchUp { action in
print("点击了确定")
testAlert()

View File

@ -20,16 +20,18 @@ public class Alert: ProHUD.Controller {
/// icontextStackactionStack)
public lazy var contentStack: StackView = {
let stack = StackView(axis: .vertical)
stack.spacing = config.margin
stack.spacing = 24
stack.alignment = .center
config.customContentStack?(stack)
return stack
}()
///
public lazy var textStack: StackView = {
let stack = StackView(axis: .vertical)
stack.spacing = config.margin
stack.spacing = 8
stack.alignment = .center
config.customTextStack?(stack)
return stack
}()
@ -66,7 +68,8 @@ public class Alert: ProHUD.Controller {
let stack = StackView()
stack.alignment = .fill
stack.distribution = .fillEqually
stack.spacing = config.margin
stack.spacing = 8
config.customActionStack?(stack)
return stack
}()

View File

@ -15,6 +15,7 @@ public class AlertButton: Button {
public override func update(config: ProHUD.Configuration, action: Action) {
titleLabel?.font = .boldSystemFont(ofSize: 15)
layer.cornerRadiusWithContinuous = 8
super.update(config: config, action: action)
}

View File

@ -11,6 +11,7 @@ public extension Alert {
class Configuration: ProHUD.Configuration {
///
public var actionAxis: NSLayoutConstraint.Axis?

View File

@ -84,17 +84,19 @@ extension Alert: DefaultLayout {
contentView.snp.remakeConstraints { make in
make.center.equalToSuperview()
if customView == nil {
make.width.greaterThanOrEqualTo(config.cardMinWidth).priority(.low)
make.width.greaterThanOrEqualTo(config.cardMinWidth)
make.width.lessThanOrEqualTo(config.cardMaxWidthByDefault)
make.height.greaterThanOrEqualTo(config.cardMinHeight).priority(.low)
make.height.greaterThanOrEqualTo(config.cardMinHeight)
make.height.lessThanOrEqualTo(config.cardMaxHeightByDefault)
}
}
if contentStack.superview == nil {
contentView.addSubview(contentStack)
contentStack.spacing = config.margin + config.padding
contentStack.snp.remakeConstraints { make in
make.edges.equalTo(contentView).inset(config.padding)
make.top.equalToSuperview().inset(config.cardEdgeInsets.top)
make.left.equalToSuperview().inset(config.cardEdgeInsets.left)
make.bottom.equalToSuperview().inset(config.cardEdgeInsets.bottom)
make.right.equalToSuperview().inset(config.cardEdgeInsets.right)
}
}
// card background
@ -144,8 +146,8 @@ extension Alert {
if imageView.superview == nil {
contentStack.insertArrangedSubview(imageView, at: 0)
imageView.snp.remakeConstraints { (mk) in
mk.top.left.greaterThanOrEqualTo(contentView).inset(config.padding*2.25)
mk.right.bottom.lessThanOrEqualTo(contentView).inset(config.padding*2.25)
mk.top.left.greaterThanOrEqualTo(contentView).inset(config.cardEdgeInsets.top * 2)
mk.right.bottom.lessThanOrEqualTo(contentView).inset(config.cardEdgeInsets.right * 2)
mk.width.equalTo(config.iconSize.width)
mk.height.equalTo(config.iconSize.height)
}
@ -172,15 +174,10 @@ extension Alert {
contentStack.insertArrangedSubview(textStack, at: 0)
}
textStack.snp.remakeConstraints { (mk) in
mk.top.greaterThanOrEqualTo(contentView).inset(config.padding*1.875)
mk.bottom.lessThanOrEqualTo(contentView).inset(config.padding*1.875)
if UIScreen.main.bounds.width < 414 {
mk.left.greaterThanOrEqualTo(contentView).inset(config.padding*2)
mk.right.lessThanOrEqualTo(contentView).inset(config.padding*2)
} else {
mk.left.greaterThanOrEqualTo(contentView).inset(config.padding*3)
mk.right.lessThanOrEqualTo(contentView).inset(config.padding*3)
}
mk.left.greaterThanOrEqualToSuperview().inset(config.textEdgeInsets.left)
mk.right.lessThanOrEqualToSuperview().inset(config.textEdgeInsets.right)
mk.top.greaterThanOrEqualTo(contentView).inset(config.cardEdgeInsets.top + config.textEdgeInsets.top)
mk.bottom.lessThanOrEqualTo(contentView).inset(config.cardEdgeInsets.bottom + config.textEdgeInsets.bottom)
}
}
if titleCount > 0 {

View File

@ -73,7 +73,7 @@ public extension Alert {
static func lazyPush(identifier: String? = nil, file: String = #file, line: Int = #line, handler: @escaping (_ alert: Alert) -> Void, onExists: ((_ alert: Alert) -> Void)? = nil) {
let id = identifier ?? (file + "#\(line)")
if let vc = AlertWindow.alerts.last(where: { $0.identifier == id }) {
onExists?(vc)
vc.update(handler: onExists ?? handler)
} else {
Alert { alert in
alert.identifier = id

View File

@ -90,18 +90,40 @@ public class Configuration: NSObject {
///
public var cardMinHeight = CGFloat(32)
///
public var cardEdgeInsets: UIEdgeInsets = {
.init(top: 16, left: 16, bottom: 16, right: 16)
}()
///
public var textEdgeInsets: UIEdgeInsets = {
.init(top: 16, left: 16, bottom: 16, right: 16)
}()
///
public var cardCornerRadius: CGFloat?
var cardCornerRadiusByDefault: CGFloat { cardCornerRadius ?? 16 }
///
public var margin = CGFloat(8)
///
public var padding = CGFloat(16)
///
public var tintColor: UIColor?
var customContentStack: ((_ stack: StackView) -> Void)?
public func customContentStack(handler: @escaping (_ stack: StackView) -> Void) {
customContentStack = handler
}
var customTextStack: ((_ stack: StackView) -> Void)?
public func customTextStack(handler: @escaping (_ stack: StackView) -> Void) {
customTextStack = handler
}
var customActionStack: ((_ stack: StackView) -> Void)?
public func customActionStack(handler: @escaping (_ stack: StackView) -> Void) {
customActionStack = handler
}
// MARK:
///
public var iconSize = CGSize(width: 44, height: 44)
@ -144,10 +166,6 @@ public class Configuration: NSObject {
customButton = handler
}
///
public var buttonCornerRadius: CGFloat?
var buttonCornerRadiusByDefault: CGFloat { buttonCornerRadius ?? 8 }
// MARK:
public var animateDurationForBuildIn: TimeInterval?

View File

@ -31,13 +31,13 @@ open class Button: UIButton {
public convenience init(config: ProHUD.Configuration, action: Action) {
self.init(frame: .zero)
layer.cornerRadiusWithContinuous = 8
self.update(config: config, action: action)
}
///
/// - Parameter style:
open func update(config: ProHUD.Configuration, action: Action) {
layer.cornerRadiusWithContinuous = config.buttonCornerRadiusByDefault
self.action = action
setTitle(action.title, for: .normal)
switch action.style {

View File

@ -27,8 +27,9 @@ public class Sheet: Controller {
/// icontextStackactionStack)
public lazy var contentStack: StackView = {
let stack = StackView()
stack.spacing = config.margin
stack.spacing = 8
stack.alignment = .fill
config.customContentStack?(stack)
return stack
}()

View File

@ -15,6 +15,7 @@ public class SheetButton: Button {
public override func update(config: ProHUD.Configuration, action: Action) {
titleLabel?.font = .boldSystemFont(ofSize: 18)
layer.cornerRadiusWithContinuous = 12
super.update(config: config, action: action)
}

View File

@ -12,7 +12,7 @@ public extension Sheet {
class Configuration: ProHUD.Configuration {
///
public var edgeInset: CGFloat = 4
public var screenEdgeInset: CGFloat = 4
///
public var isFullScreen = false
@ -45,8 +45,6 @@ public extension Sheet {
override var cardMaxHeightByDefault: CGFloat { cardMaxHeight ?? (UIScreen.main.bounds.height - 50) }
override var buttonCornerRadiusByDefault: CGFloat { buttonCornerRadius ?? 12 }
override var animateDurationForBuildInByDefault: CGFloat {
animateDurationForBuildIn ?? 0.5
}

View File

@ -153,10 +153,10 @@ public extension Sheet {
if #available(iOS 11.0, *) {
let count = contentStack.arrangedSubviews.count
if count > 0 {
contentStack.setCustomSpacing(config.margin * 1.5, after: contentStack.arrangedSubviews[count-1])
contentStack.setCustomSpacing(12, after: contentStack.arrangedSubviews[count-1])
}
if count > 1 {
contentStack.setCustomSpacing(config.margin * 2, after: contentStack.arrangedSubviews[count-2])
contentStack.setCustomSpacing(16, after: contentStack.arrangedSubviews[count-2])
}
} else {
// Fallback on earlier versions

View File

@ -44,7 +44,7 @@ extension Sheet: DefaultLayout {
loadContentMaskViewIfNeeded()
// layout
let maxWidth = config.cardMaxWidthByDefault
var width = UIScreen.main.bounds.width - config.edgeInset * 2
var width = UIScreen.main.bounds.width - config.screenEdgeInset * 2
if width > maxWidth {
// landscape iPhone or iPad
width = maxWidth
@ -56,7 +56,7 @@ extension Sheet: DefaultLayout {
make.centerX.equalToSuperview()
if UIDevice.current.userInterfaceIdiom == .phone {
if width < maxWidth {
make.bottom.equalToSuperview().inset(config.edgeInset)
make.bottom.equalToSuperview().inset(config.screenEdgeInset)
} else {
make.bottom.equalToSuperview().inset(screenSafeAreaInsets.bottom)
}
@ -78,16 +78,20 @@ extension Sheet: DefaultLayout {
if contentStack.superview == nil {
contentView.addSubview(contentStack)
contentStack.snp.remakeConstraints { make in
let safeArea: UIEdgeInsets
if config.isFullScreen {
make.top.equalToSuperview().offset(screenSafeAreaInsets.top)
safeArea = screenSafeAreaInsets
} else {
make.top.equalToSuperview().offset(config.padding * 2)
safeArea = .zero
}
make.bottom.equalToSuperview().inset(config.padding * 2)
make.top.equalToSuperview().offset(safeArea.top + config.cardEdgeInsets.top)
make.bottom.equalToSuperview().inset(safeArea.bottom + config.cardEdgeInsets.bottom)
if isPortrait {
make.left.right.equalToSuperview().inset(config.padding)
make.left.equalToSuperview().inset(safeArea.left + config.cardEdgeInsets.left)
make.right.equalToSuperview().inset(safeArea.right + config.cardEdgeInsets.right)
} else {
make.left.right.equalToSuperview().inset(config.padding * 2)
make.left.equalToSuperview().inset(safeArea.left + config.cardEdgeInsets.left * 2)
make.right.equalToSuperview().inset(safeArea.right + config.cardEdgeInsets.right * 2)
}
}
}

View File

@ -28,7 +28,7 @@ public extension Sheet {
static func lazyPush(identifier: String? = nil, file: String = #file, line: Int = #line, handler: @escaping (_ sheet: Sheet) -> Void, onExists: ((_ sheet: Sheet) -> Void)? = nil) {
let id = identifier ?? (file + "#\(line)")
if let vc = find(identifier: id).last {
onExists?(vc)
vc.update(handler: onExists ?? handler)
} else {
Sheet { sheet in
sheet.identifier = id
@ -86,7 +86,7 @@ extension Sheet {
func _translateOut() {
backgroundView.alpha = 0
contentView.transform = .init(translationX: 0, y: view.frame.size.height - contentView.frame.minY + config.margin)
contentView.transform = .init(translationX: 0, y: view.frame.size.height - contentView.frame.minY + config.screenEdgeInset)
}
}

View File

@ -22,15 +22,17 @@ public class Toast: Controller {
/// icontextStackactionStack)
public lazy var contentStack: StackView = {
let stack = StackView(axis: .vertical)
stack.spacing = config.margin * 2
stack.spacing = 16
config.customContentStack?(stack)
return stack
}()
/// image+text
public lazy var infoStack: StackView = {
let stack = StackView(axis: .horizontal)
stack.spacing = config.margin
stack.spacing = 8
stack.alignment = .top
config.customInfoStack?(stack)
return stack
}()
@ -39,13 +41,15 @@ public class Toast: Controller {
let stack = StackView(axis: .vertical)
stack.spacing = config.lineSpace
stack.distribution = .equalSpacing
config.customTextStack?(stack)
return stack
}()
///
public lazy var actionStack: StackView = {
let stack = StackView(axis: .horizontal)
stack.spacing = config.margin
stack.spacing = 8
config.customActionStack?(stack)
return stack
}()

View File

@ -15,6 +15,7 @@ public class ToastButton: Button {
public override func update(config: ProHUD.Configuration, action: Action) {
titleLabel?.font = .boldSystemFont(ofSize: 15)
layer.cornerRadiusWithContinuous = 8
super.update(config: config, action: action)
}

View File

@ -11,6 +11,13 @@ public extension Toast {
class Configuration: ProHUD.Configuration {
///
public var margin = CGFloat(8)
var customInfoStack: ((_ stack: StackView) -> Void)?
public func customInfoStack(handler: @escaping (_ stack: StackView) -> Void) {
customInfoStack = handler
}
///
public var lineSpace = CGFloat(4)

View File

@ -96,8 +96,11 @@ extension Toast: DefaultLayout {
// stacks
if contentStack.superview != contentView {
contentView.addSubview(contentStack)
contentStack.snp.remakeConstraints { (mk) in
mk.edges.equalToSuperview().inset(config.padding)
contentStack.snp.remakeConstraints { make in
make.top.equalToSuperview().inset(config.cardEdgeInsets.top)
make.left.equalToSuperview().inset(config.cardEdgeInsets.left)
make.bottom.equalToSuperview().inset(config.cardEdgeInsets.bottom)
make.right.equalToSuperview().inset(config.cardEdgeInsets.right)
}
}
contentStack.insertArrangedSubview(infoStack, at: 0)

View File

@ -28,7 +28,7 @@ public extension Toast {
static func lazyPush(identifier: String? = nil, file: String = #file, line: Int = #line, handler: @escaping (_ toast: Toast) -> Void, onExists: ((_ toast: Toast) -> Void)? = nil) {
let id = identifier ?? (file + "#\(line)")
if let vc = find(identifier: id).last {
onExists?(vc)
vc.update(handler: onExists ?? handler)
} else {
Toast { toast in
toast.identifier = id

View File

@ -50,7 +50,7 @@ class ToastWindow: Window {
}
let config = toast.config
// frame
let width = CGFloat.minimum(UIScreen.main.bounds.width - 2*config.margin, config.cardMaxWidthByDefault)
let width = CGFloat.minimum(UIScreen.main.bounds.width - config.cardEdgeInsets.left - config.cardEdgeInsets.right, config.cardMaxWidthByDefault)
toast.view.frame.size = CGSize(width: width, height: config.cardMaxHeightByDefault)
toast.titleLabel.sizeToFit()
toast.bodyLabel.sizeToFit()
@ -164,7 +164,7 @@ fileprivate extension Toast {
height = CGFloat.maximum(v.frame.maxY, height)
}
//
height += 2 * config.padding
height += config.cardEdgeInsets.top + config.cardEdgeInsets.bottom
return height
}
}