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:
2026-07-07 09:57:41 +08:00
parent d8329d19fd
commit 715393e78c
14 changed files with 1222 additions and 209 deletions

View 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?()
}
}

View File

@ -0,0 +1,193 @@
//
// HomeCollectionCells.swift
// suixinkan
//
import SnapKit
import UIKit
/// 线 cell
final class HomeWorkStatusCell: UICollectionViewCell {
static let reuseIdentifier = "HomeWorkStatusCell"
let cardView = HomeWorkStatusCardView()
override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(cardView)
cardView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func apply(isOnline: Bool, countdownText: String, reminderMinutes: Int) {
cardView.apply(isOnline: isOnline, countdownText: countdownText, reminderMinutes: reminderMinutes)
}
}
/// cell
final class HomeLocationReportCell: UICollectionViewCell {
static let reuseIdentifier = "HomeLocationReportCell"
let cardView = HomeLocationReportCardView()
override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(cardView)
cardView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func apply(address: String, isReporting: Bool) {
cardView.apply(address: address, isReporting: isReporting)
}
}
/// cell
final class HomeQuickActionsCell: UICollectionViewCell {
static let reuseIdentifier = "HomeQuickActionsCell"
let cardView = HomeQuickActionsView()
override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(cardView)
cardView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func apply(isOnline: Bool) {
cardView.apply(isOnline: isOnline)
}
}
/// cell
final class HomeStoreCell: UICollectionViewCell {
static let reuseIdentifier = "HomeStoreCell"
let cardView = HomeStoreCardView()
override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(cardView)
cardView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func apply(store: StoreItem) {
cardView.apply(store: store)
}
}
/// cell
final class HomeMenuCell: UICollectionViewCell {
static let reuseIdentifier = "HomeMenuCell"
private let cardView = UIView()
private let iconView = UIImageView()
private let titleLabel = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
setupUI()
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func apply(menu: HomeMenuItem) {
iconView.image = UIImage(systemName: menu.iconName)
titleLabel.text = menu.title
}
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(.body)
titleLabel.textColor = AppColor.textSecondary
titleLabel.textAlignment = .center
titleLabel.numberOfLines = 2
contentView.addSubview(cardView)
cardView.addSubview(iconView)
cardView.addSubview(titleLabel)
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(24)
}
titleLabel.snp.makeConstraints { make in
make.top.equalTo(iconView.snp.bottom).offset(AppSpacing.sm)
make.leading.trailing.equalToSuperview().inset(AppSpacing.xxs)
make.bottom.lessThanOrEqualToSuperview().inset(AppSpacing.xs)
}
}
}
/// supplementary view
final class HomeSectionHeaderView: UICollectionReusableView {
static let reuseIdentifier = "HomeSectionHeaderView"
private let titleLabel = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
titleLabel.font = .app(.title)
titleLabel.textColor = AppColor.textPrimary
addSubview(titleLabel)
titleLabel.snp.makeConstraints { make in
make.leading.trailing.bottom.equalToSuperview()
}
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func apply(title: String) {
titleLabel.text = title
}
}

View File

@ -1,132 +0,0 @@
//
// HomeCommonAppsGridView.swift
// suixinkan
//
import SnapKit
import UIKit
///
final class HomeCommonAppsGridView: UIView {
var onMenuSelected: ((HomeMenuItem) -> Void)?
private enum Section { case main }
private enum Item: Hashable { case menu(HomeMenuItem) }
private var menus: [HomeMenuItem] = []
private let titleLabel = UILabel()
private lazy var collectionView: UICollectionView = {
let itemSize = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(0.25),
heightDimension: .absolute(88)
)
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let groupSize = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1),
heightDimension: .absolute(88)
)
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
let section = NSCollectionLayoutSection(group: group)
let layout = UICollectionViewCompositionalLayout(section: section)
return UICollectionView(frame: .zero, collectionViewLayout: layout)
}()
private lazy var dataSource = UICollectionViewDiffableDataSource<Section, Item>(collectionView: collectionView) {
collectionView, indexPath, item in
let cell = collectionView.dequeueReusableCell(
withReuseIdentifier: HomeMenuCell.reuseIdentifier,
for: indexPath
) as! HomeMenuCell
if case let .menu(menu) = item {
cell.apply(menu: menu)
}
return cell
}
override init(frame: CGRect) {
super.init(frame: frame)
titleLabel.text = "常用应用"
titleLabel.font = .app(.title)
titleLabel.textColor = AppColor.textPrimary
collectionView.backgroundColor = .clear
collectionView.delegate = self
collectionView.register(HomeMenuCell.self, forCellWithReuseIdentifier: HomeMenuCell.reuseIdentifier)
addSubview(titleLabel)
addSubview(collectionView)
titleLabel.snp.makeConstraints { make in
make.top.leading.trailing.equalToSuperview()
}
collectionView.snp.makeConstraints { make in
make.top.equalTo(titleLabel.snp.bottom).offset(AppSpacing.sm)
make.leading.trailing.bottom.equalToSuperview()
make.height.equalTo(88)
}
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func apply(menus: [HomeMenuItem]) {
self.menus = menus + [.moreFunctions]
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
snapshot.appendSections([.main])
snapshot.appendItems(self.menus.map { Item.menu($0) })
dataSource.apply(snapshot, animatingDifferences: false)
}
}
extension HomeCommonAppsGridView: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard indexPath.item < menus.count else { return }
onMenuSelected?(menus[indexPath.item])
}
}
/// cell
final class HomeMenuCell: UICollectionViewCell {
static let reuseIdentifier = "HomeMenuCell"
private let iconView = UIImageView()
private let titleLabel = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
iconView.tintColor = AppColor.primary
iconView.contentMode = .scaleAspectFit
titleLabel.font = .app(.caption)
titleLabel.textColor = AppColor.textPrimary
titleLabel.textAlignment = .center
titleLabel.numberOfLines = 2
contentView.addSubview(iconView)
contentView.addSubview(titleLabel)
iconView.snp.makeConstraints { make in
make.top.equalToSuperview().offset(AppSpacing.xs)
make.centerX.equalToSuperview()
make.width.height.equalTo(28)
}
titleLabel.snp.makeConstraints { make in
make.top.equalTo(iconView.snp.bottom).offset(AppSpacing.xs)
make.leading.trailing.equalToSuperview().inset(AppSpacing.xxs)
}
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func apply(menu: HomeMenuItem) {
iconView.image = UIImage(systemName: menu.iconName)
titleLabel.text = menu.title
}
}