BeeReel/BeeReel/Lib/Alert/BRAlert.swift
2025-07-24 19:21:38 +08:00

184 lines
5.8 KiB
Swift

//
// BRAlert.swift
// BeeReel
//
// Created by 鸿 on 2025/7/24.
//
import UIKit
class BRAlert: BRBaseAlert {
var normalButtonTitle: String? {
didSet {
normalButton.setNeedsUpdateConfiguration()
}
}
var highlightButtonTitle: String? {
didSet {
highlightButton.setNeedsUpdateConfiguration()
}
}
var highlightButtonImage: UIImage? {
didSet {
highlightButton.setNeedsUpdateConfiguration()
}
}
var clickNormalButton: (() -> Void)?
var clickHighlightButton: (() -> Void)?
private lazy var bgView: UIView = {
let view = UIImageView(image: UIImage(named: "alert_bg_image"))
view.isUserInteractionEnabled = true
return view
}()
private lazy var normalButton: UIButton = {
var config = UIButton.Configuration.plain()
let button = UIButton(configuration: config)
button.layer.cornerRadius = 24
button.layer.masksToBounds = true
button.layer.borderColor = UIColor.colorC2C2C2().cgColor
button.layer.borderWidth = 1
button.configurationUpdateHandler = { [weak self] button in
guard let self = self else { return }
button.configuration?.attributedTitle = AttributedString.br_createAttributedString(string: normalButtonTitle ?? "", color: .colorC2C2C2(), font: .fontRegular(ofSize: 14))
}
button.addTarget(self, action: #selector(handleNormalButton), for: .touchUpInside)
return button
}()
private lazy var highlightButton: UIButton = {
var config = UIButton.Configuration.plain()
config.background.image = UIImage(named: "选中光效")
config.background.backgroundColor = .color1C1C1C()
config.imagePadding = 4
let button = UIButton(configuration: config)
button.layer.cornerRadius = 24
button.layer.masksToBounds = true
button.configurationUpdateHandler = { [weak self] button in
guard let self = self else { return }
button.configuration?.image = highlightButtonImage
button.configuration?.attributedTitle = AttributedString.br_createAttributedString(string: highlightButtonTitle ?? "", color: .colorFFFFFF(), font: .fontMedium(ofSize: 14))
}
button.addTarget(self, action: #selector(handleHighlightButton), for: .touchUpInside)
return button
}()
init(title: String, detail: String?, image: UIImage?, normalButtonText: String?, highlightButtonText: String?) {
super.init(frame: .zero)
let titleLabel = UILabel()
titleLabel.font = .fontBold(ofSize: 18)
titleLabel.textColor = .color000000()
titleLabel.text = title
let titleIconImageView = UIImageView(image: UIImage(named: "Vector 22"))
let stackView = UIStackView()
stackView.axis = .vertical
if let image = image {
let imageView = UIImageView(image: image)
stackView.addArrangedSubview(imageView)
}
if let detail = detail {
let label = UILabel()
label.text = detail
label.font = .fontRegular(ofSize: 15)
label.textColor = .color1C1C1C()
label.textAlignment = .center
label.numberOfLines = 0
stackView.addArrangedSubview(label)
}
let buttonStackView = UIStackView()
buttonStackView.axis = .horizontal
buttonStackView.spacing = 19
buttonStackView.distribution = .fillEqually
if let normalButtonText = normalButtonText {
self.normalButtonTitle = normalButtonText
buttonStackView.addArrangedSubview(normalButton)
}
if let highlightButtonText = highlightButtonText {
self.highlightButtonTitle = highlightButtonText
buttonStackView.addArrangedSubview(highlightButton)
}
contentView.addSubview(bgView)
bgView.addSubview(titleIconImageView)
bgView.addSubview(titleLabel)
bgView.addSubview(stackView)
bgView.addSubview(buttonStackView)
bgView.snp.makeConstraints { make in
make.edges.equalToSuperview()
make.width.equalTo(305)
}
titleIconImageView.snp.makeConstraints { make in
make.centerX.equalToSuperview()
make.top.equalToSuperview().offset(42)
}
titleLabel.snp.makeConstraints { make in
make.centerX.equalToSuperview()
make.top.equalToSuperview().offset(30)
}
stackView.snp.makeConstraints { make in
make.centerX.equalToSuperview()
make.left.equalToSuperview().offset(30)
make.top.equalToSuperview().offset(73)
make.bottom.equalToSuperview().offset(-111)
}
buttonStackView.snp.makeConstraints { make in
make.bottom.equalToSuperview().offset(-30)
make.height.equalTo(48)
make.centerX.equalToSuperview()
if buttonStackView.arrangedSubviews.count == 1 {
make.left.equalToSuperview().offset(28)
} else {
make.left.equalToSuperview().offset(15)
}
}
}
@MainActor required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc private func handleNormalButton() {
self.dismiss()
self.clickNormalButton?()
}
@objc private func handleHighlightButton() {
self.dismiss()
self.clickHighlightButton?()
}
}