Files
suixinkan_uikit/suixinkan/UI/CloudDrive/CloudDriveActionSheetViewController.swift

138 lines
4.6 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// 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)
}
}