138 lines
4.6 KiB
Swift
138 lines
4.6 KiB
Swift
//
|
||
// CloudDriveActionSheetViewController.swift
|
||
// suixinkan
|
||
//
|
||
|
||
import SnapKit
|
||
import UIKit
|
||
|
||
/// 云盘底部操作弹层,用于同步 Android 云盘上传与文件操作菜单。
|
||
final class CloudDriveActionSheetViewController: UIViewController {
|
||
/// 云盘底部弹层操作项。
|
||
struct Item {
|
||
/// 操作文案。
|
||
let title: String
|
||
/// 操作图标。
|
||
let image: UIImage?
|
||
/// 是否展示为危险操作。
|
||
let isDestructive: Bool
|
||
/// 点击后的业务动作。
|
||
let handler: () -> Void
|
||
}
|
||
|
||
private let sheetTitle: String?
|
||
private let items: [Item]
|
||
private let dimView = UIView()
|
||
private let panelView = UIView()
|
||
private let stackView = UIStackView()
|
||
|
||
/// 初始化云盘底部弹层。
|
||
init(title: String? = nil, items: [Item]) {
|
||
self.sheetTitle = title
|
||
self.items = items
|
||
super.init(nibName: nil, bundle: nil)
|
||
modalPresentationStyle = .overFullScreen
|
||
modalTransitionStyle = .crossDissolve
|
||
}
|
||
|
||
@available(*, unavailable)
|
||
required init?(coder: NSCoder) {
|
||
fatalError("init(coder:) has not been implemented")
|
||
}
|
||
|
||
override func viewDidLoad() {
|
||
super.viewDidLoad()
|
||
setupUI()
|
||
setupConstraints()
|
||
}
|
||
|
||
private func setupUI() {
|
||
view.backgroundColor = .clear
|
||
dimView.backgroundColor = UIColor.black.withAlphaComponent(0.35)
|
||
dimView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(cancelTapped)))
|
||
|
||
panelView.backgroundColor = .white
|
||
panelView.layer.cornerRadius = 18
|
||
panelView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
|
||
panelView.clipsToBounds = true
|
||
|
||
stackView.axis = .vertical
|
||
stackView.spacing = 0
|
||
|
||
view.addSubview(dimView)
|
||
view.addSubview(panelView)
|
||
panelView.addSubview(stackView)
|
||
|
||
if let sheetTitle {
|
||
let titleLabel = UILabel()
|
||
titleLabel.text = sheetTitle
|
||
titleLabel.font = .app(.bodyMedium)
|
||
titleLabel.textColor = AppColor.textPrimary
|
||
titleLabel.textAlignment = .center
|
||
titleLabel.snp.makeConstraints { make in
|
||
make.height.equalTo(54)
|
||
}
|
||
stackView.addArrangedSubview(titleLabel)
|
||
}
|
||
|
||
for item in items {
|
||
stackView.addArrangedSubview(makeItemButton(item))
|
||
}
|
||
|
||
let spacer = UIView()
|
||
spacer.backgroundColor = AppColor.pageBackground
|
||
spacer.snp.makeConstraints { make in
|
||
make.height.equalTo(8)
|
||
}
|
||
stackView.addArrangedSubview(spacer)
|
||
|
||
let cancelButton = UIButton(type: .system)
|
||
cancelButton.setTitle("取消", for: .normal)
|
||
cancelButton.setTitleColor(AppColor.textPrimary, for: .normal)
|
||
cancelButton.titleLabel?.font = .app(.body)
|
||
cancelButton.addTarget(self, action: #selector(cancelTapped), for: .touchUpInside)
|
||
cancelButton.snp.makeConstraints { make in
|
||
make.height.equalTo(54)
|
||
}
|
||
stackView.addArrangedSubview(cancelButton)
|
||
}
|
||
|
||
private func setupConstraints() {
|
||
dimView.snp.makeConstraints { make in
|
||
make.edges.equalToSuperview()
|
||
}
|
||
panelView.snp.makeConstraints { make in
|
||
make.leading.trailing.bottom.equalToSuperview()
|
||
}
|
||
stackView.snp.makeConstraints { make in
|
||
make.top.leading.trailing.equalToSuperview()
|
||
make.bottom.equalTo(view.safeAreaLayoutGuide)
|
||
}
|
||
}
|
||
|
||
private func makeItemButton(_ item: Item) -> UIButton {
|
||
let button = UIButton(type: .system)
|
||
button.setTitle(item.title, for: .normal)
|
||
button.setImage(item.image, for: .normal)
|
||
button.tintColor = item.isDestructive ? AppColor.danger : AppColor.textPrimary
|
||
button.setTitleColor(item.isDestructive ? AppColor.danger : AppColor.textPrimary, for: .normal)
|
||
button.titleLabel?.font = .app(.body)
|
||
button.contentHorizontalAlignment = .left
|
||
button.contentEdgeInsets = UIEdgeInsets(top: 0, left: AppSpacing.xl, bottom: 0, right: AppSpacing.xl)
|
||
button.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: AppSpacing.md)
|
||
button.addAction(UIAction { [weak self] _ in
|
||
self?.dismiss(animated: true) {
|
||
item.handler()
|
||
}
|
||
}, for: .touchUpInside)
|
||
button.snp.makeConstraints { make in
|
||
make.height.equalTo(54)
|
||
}
|
||
return button
|
||
}
|
||
|
||
@objc private func cancelTapped() {
|
||
dismiss(animated: true)
|
||
}
|
||
}
|