Refactor home to single collection view and add all-functions customization.
Replace scroll/stack layout with one UICollectionView, fix grid column math and Diffable item IDs so location report and quick actions render, and add the customizable all-functions page with persistence and tests. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
104
suixinkan/UI/Home/Views/AllFunctionMenuCell.swift
Normal file
104
suixinkan/UI/Home/Views/AllFunctionMenuCell.swift
Normal file
@ -0,0 +1,104 @@
|
||||
//
|
||||
// AllFunctionMenuCell.swift
|
||||
// suixinkan
|
||||
//
|
||||
|
||||
import SnapKit
|
||||
import UIKit
|
||||
|
||||
/// 全部功能页菜单卡片,支持右上角添加或移除操作。
|
||||
final class AllFunctionMenuCell: UICollectionViewCell {
|
||||
|
||||
static let reuseIdentifier = "AllFunctionMenuCell"
|
||||
|
||||
enum ActionStyle {
|
||||
case none
|
||||
case add
|
||||
case remove
|
||||
}
|
||||
|
||||
var onActionTap: (() -> Void)?
|
||||
|
||||
private let cardView = UIView()
|
||||
private let iconView = UIImageView()
|
||||
private let titleLabel = UILabel()
|
||||
private let actionButton = UIButton(type: .system)
|
||||
|
||||
override init(frame: CGRect) {
|
||||
super.init(frame: frame)
|
||||
setupUI()
|
||||
}
|
||||
|
||||
@available(*, unavailable)
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
override func prepareForReuse() {
|
||||
super.prepareForReuse()
|
||||
onActionTap = nil
|
||||
}
|
||||
|
||||
func apply(menu: HomeMenuItem, actionStyle: ActionStyle) {
|
||||
iconView.image = UIImage(systemName: menu.iconName)
|
||||
titleLabel.text = menu.title
|
||||
|
||||
switch actionStyle {
|
||||
case .none:
|
||||
actionButton.isHidden = true
|
||||
case .add:
|
||||
actionButton.isHidden = false
|
||||
actionButton.setImage(UIImage(systemName: "plus.circle.fill"), for: .normal)
|
||||
actionButton.tintColor = AppColor.primary
|
||||
case .remove:
|
||||
actionButton.isHidden = false
|
||||
actionButton.setImage(UIImage(systemName: "minus.circle.fill"), for: .normal)
|
||||
actionButton.tintColor = AppColor.danger
|
||||
}
|
||||
}
|
||||
|
||||
private func setupUI() {
|
||||
contentView.backgroundColor = .clear
|
||||
|
||||
cardView.backgroundColor = .white
|
||||
cardView.layer.cornerRadius = AppRadius.md
|
||||
cardView.clipsToBounds = true
|
||||
|
||||
iconView.tintColor = AppColor.primary
|
||||
iconView.contentMode = .scaleAspectFit
|
||||
|
||||
titleLabel.font = .app(.caption)
|
||||
titleLabel.textColor = AppColor.textPrimary
|
||||
titleLabel.textAlignment = .center
|
||||
titleLabel.numberOfLines = 2
|
||||
|
||||
actionButton.addTarget(self, action: #selector(actionTapped), for: .touchUpInside)
|
||||
|
||||
contentView.addSubview(cardView)
|
||||
cardView.addSubview(iconView)
|
||||
cardView.addSubview(titleLabel)
|
||||
cardView.addSubview(actionButton)
|
||||
|
||||
cardView.snp.makeConstraints { make in
|
||||
make.edges.equalToSuperview()
|
||||
}
|
||||
iconView.snp.makeConstraints { make in
|
||||
make.top.equalToSuperview().offset(AppSpacing.sm)
|
||||
make.centerX.equalToSuperview()
|
||||
make.width.height.equalTo(32)
|
||||
}
|
||||
titleLabel.snp.makeConstraints { make in
|
||||
make.top.equalTo(iconView.snp.bottom).offset(AppSpacing.xs)
|
||||
make.leading.trailing.equalToSuperview().inset(AppSpacing.xxs)
|
||||
make.bottom.lessThanOrEqualToSuperview().inset(AppSpacing.xs)
|
||||
}
|
||||
actionButton.snp.makeConstraints { make in
|
||||
make.top.trailing.equalToSuperview().inset(AppSpacing.xxs)
|
||||
make.width.height.equalTo(20)
|
||||
}
|
||||
}
|
||||
|
||||
@objc private func actionTapped() {
|
||||
onActionTap?()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user