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:
@ -13,13 +13,9 @@ final class HomeViewController: BaseViewController {
|
||||
private let homeAPI = NetworkServices.shared.homeAPI
|
||||
|
||||
private let scenicHeaderView = HomeScenicHeaderView()
|
||||
private let scrollView = UIScrollView()
|
||||
private let contentStack = UIStackView()
|
||||
private let workStatusCardView = HomeWorkStatusCardView()
|
||||
private let locationReportCardView = HomeLocationReportCardView()
|
||||
private let quickActionsView = HomeQuickActionsView()
|
||||
private let storeCardView = HomeStoreCardView()
|
||||
private let commonAppsGridView = HomeCommonAppsGridView()
|
||||
private var collectionView: UICollectionView!
|
||||
private var dataSource: UICollectionViewDiffableDataSource<HomeCollectionSection, HomeCollectionItem>!
|
||||
private var currentSections: [HomeCollectionSection] = []
|
||||
|
||||
private var hasInitialized = false
|
||||
private var countdownTimer: Timer?
|
||||
@ -32,19 +28,28 @@ final class HomeViewController: BaseViewController {
|
||||
override func setupUI() {
|
||||
view.backgroundColor = AppColor.pageBackground
|
||||
|
||||
contentStack.axis = .vertical
|
||||
contentStack.spacing = AppSpacing.sm
|
||||
collectionView = UICollectionView(
|
||||
frame: .zero,
|
||||
collectionViewLayout: HomeCollectionLayoutBuilder.makeLayout(sections: [.commonApps])
|
||||
)
|
||||
collectionView.backgroundColor = .clear
|
||||
collectionView.alwaysBounceVertical = true
|
||||
collectionView.alwaysBounceHorizontal = false
|
||||
collectionView.showsHorizontalScrollIndicator = false
|
||||
collectionView.isDirectionalLockEnabled = true
|
||||
// 水平边距由 section.contentInsets 控制,避免 contentInset 与布局宽度不一致导致横向滑动。
|
||||
collectionView.contentInset = UIEdgeInsets(
|
||||
top: AppSpacing.sm,
|
||||
left: 0,
|
||||
bottom: AppSpacing.md,
|
||||
right: 0
|
||||
)
|
||||
registerCollectionCells()
|
||||
|
||||
scrollView.showsVerticalScrollIndicator = false
|
||||
view.addSubview(scenicHeaderView)
|
||||
view.addSubview(scrollView)
|
||||
scrollView.addSubview(contentStack)
|
||||
view.addSubview(collectionView)
|
||||
|
||||
contentStack.addArrangedSubview(workStatusCardView)
|
||||
contentStack.addArrangedSubview(locationReportCardView)
|
||||
contentStack.addArrangedSubview(quickActionsView)
|
||||
contentStack.addArrangedSubview(storeCardView)
|
||||
contentStack.addArrangedSubview(commonAppsGridView)
|
||||
dataSource = makeDataSource()
|
||||
}
|
||||
|
||||
override func setupConstraints() {
|
||||
@ -53,14 +58,10 @@ final class HomeViewController: BaseViewController {
|
||||
make.leading.trailing.equalToSuperview()
|
||||
make.height.equalTo(AppSpacing.homeHeaderHeight)
|
||||
}
|
||||
scrollView.snp.makeConstraints { make in
|
||||
collectionView.snp.makeConstraints { make in
|
||||
make.top.equalTo(scenicHeaderView.snp.bottom)
|
||||
make.leading.trailing.bottom.equalToSuperview()
|
||||
}
|
||||
contentStack.snp.makeConstraints { make in
|
||||
make.edges.equalToSuperview().inset(AppSpacing.screenHorizontalInset)
|
||||
make.width.equalTo(scrollView.snp.width).offset(-AppSpacing.screenHorizontalInset * 2)
|
||||
}
|
||||
}
|
||||
|
||||
override func bindActions() {
|
||||
@ -72,27 +73,7 @@ final class HomeViewController: BaseViewController {
|
||||
}
|
||||
|
||||
scenicHeaderView.onTap = { [weak self] in self?.presentScenicSelection() }
|
||||
workStatusCardView.onOnlineTap = { [weak self] in self?.viewModel.showOnlineStatusSwitchDialog() }
|
||||
workStatusCardView.onReminderTap = { [weak self] in self?.presentReminderPicker() }
|
||||
locationReportCardView.onReportTap = { [weak self] in
|
||||
Task { await self?.viewModel.manualReportLocation(api: self?.homeAPI ?? NetworkServices.shared.homeAPI) }
|
||||
}
|
||||
quickActionsView.onCollectPayment = { [weak self] in
|
||||
guard AppStore.shared.currentScenicId > 0 else {
|
||||
self?.showToast("请先选择景区")
|
||||
return
|
||||
}
|
||||
self?.navigationController?.pushViewController(
|
||||
PaymentCollectionDetailsViewController(),
|
||||
animated: true
|
||||
)
|
||||
}
|
||||
quickActionsView.onSubmitTask = { [weak self] in self?.showToast("功能开发中") }
|
||||
quickActionsView.onToggleOnline = { [weak self] in self?.viewModel.showOnlineStatusSwitchDialog() }
|
||||
commonAppsGridView.onMenuSelected = { [weak self] menu in
|
||||
guard let self else { return }
|
||||
HomeRouteHandler.open(uri: menu.uri, from: self, storeItem: self.viewModel.storeItem)
|
||||
}
|
||||
collectionView.delegate = self
|
||||
|
||||
NotificationCenter.default.addObserver(
|
||||
self,
|
||||
@ -129,6 +110,130 @@ final class HomeViewController: BaseViewController {
|
||||
NotificationCenter.default.removeObserver(self)
|
||||
}
|
||||
|
||||
private func registerCollectionCells() {
|
||||
collectionView.register(HomeWorkStatusCell.self, forCellWithReuseIdentifier: HomeWorkStatusCell.reuseIdentifier)
|
||||
collectionView.register(
|
||||
HomeLocationReportCell.self,
|
||||
forCellWithReuseIdentifier: HomeLocationReportCell.reuseIdentifier
|
||||
)
|
||||
collectionView.register(
|
||||
HomeQuickActionsCell.self,
|
||||
forCellWithReuseIdentifier: HomeQuickActionsCell.reuseIdentifier
|
||||
)
|
||||
collectionView.register(HomeStoreCell.self, forCellWithReuseIdentifier: HomeStoreCell.reuseIdentifier)
|
||||
collectionView.register(HomeMenuCell.self, forCellWithReuseIdentifier: HomeMenuCell.reuseIdentifier)
|
||||
collectionView.register(
|
||||
HomeSectionHeaderView.self,
|
||||
forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader,
|
||||
withReuseIdentifier: HomeSectionHeaderView.reuseIdentifier
|
||||
)
|
||||
}
|
||||
|
||||
private func makeDataSource() -> UICollectionViewDiffableDataSource<HomeCollectionSection, HomeCollectionItem> {
|
||||
let dataSource = UICollectionViewDiffableDataSource<HomeCollectionSection, HomeCollectionItem>(
|
||||
collectionView: collectionView
|
||||
) { [weak self] collectionView, indexPath, item in
|
||||
guard let self else { return UICollectionViewCell() }
|
||||
let section = self.currentSections[indexPath.section]
|
||||
switch (section, item) {
|
||||
case (.workStatus, .workStatus):
|
||||
let cell = collectionView.dequeueReusableCell(
|
||||
withReuseIdentifier: HomeWorkStatusCell.reuseIdentifier,
|
||||
for: indexPath
|
||||
) as! HomeWorkStatusCell
|
||||
cell.cardView.onOnlineTap = { [weak self] in
|
||||
self?.viewModel.showOnlineStatusSwitchDialog()
|
||||
}
|
||||
cell.cardView.onReminderTap = { [weak self] in
|
||||
self?.presentReminderPicker()
|
||||
}
|
||||
cell.apply(
|
||||
isOnline: self.viewModel.isOnline,
|
||||
countdownText: self.viewModel.countdownDisplayText,
|
||||
reminderMinutes: self.viewModel.reminderMinutes
|
||||
)
|
||||
return cell
|
||||
|
||||
case (.locationReport, .locationReport):
|
||||
let cell = collectionView.dequeueReusableCell(
|
||||
withReuseIdentifier: HomeLocationReportCell.reuseIdentifier,
|
||||
for: indexPath
|
||||
) as! HomeLocationReportCell
|
||||
cell.cardView.onReportTap = { [weak self] in
|
||||
guard let self else { return }
|
||||
Task { await self.viewModel.manualReportLocation(api: self.homeAPI) }
|
||||
}
|
||||
cell.apply(
|
||||
address: self.viewModel.locationInfoText,
|
||||
isReporting: self.viewModel.isLocationReporting
|
||||
)
|
||||
return cell
|
||||
|
||||
case (.quickActions, .quickActions):
|
||||
let cell = collectionView.dequeueReusableCell(
|
||||
withReuseIdentifier: HomeQuickActionsCell.reuseIdentifier,
|
||||
for: indexPath
|
||||
) as! HomeQuickActionsCell
|
||||
cell.cardView.onCollectPayment = { [weak self] in
|
||||
guard AppStore.shared.currentScenicId > 0 else {
|
||||
self?.showToast("请先选择景区")
|
||||
return
|
||||
}
|
||||
self?.navigationController?.pushViewController(
|
||||
PaymentCollectionDetailsViewController(),
|
||||
animated: true
|
||||
)
|
||||
}
|
||||
cell.cardView.onSubmitTask = { [weak self] in
|
||||
self?.showToast("功能开发中")
|
||||
}
|
||||
cell.cardView.onToggleOnline = { [weak self] in
|
||||
self?.viewModel.showOnlineStatusSwitchDialog()
|
||||
}
|
||||
cell.apply(isOnline: self.viewModel.isOnline)
|
||||
return cell
|
||||
|
||||
case (.store, .store):
|
||||
let cell = collectionView.dequeueReusableCell(
|
||||
withReuseIdentifier: HomeStoreCell.reuseIdentifier,
|
||||
for: indexPath
|
||||
) as! HomeStoreCell
|
||||
if let store = self.viewModel.storeItem {
|
||||
cell.apply(store: store)
|
||||
}
|
||||
return cell
|
||||
|
||||
case (.commonApps, .menu(let menu)):
|
||||
let cell = collectionView.dequeueReusableCell(
|
||||
withReuseIdentifier: HomeMenuCell.reuseIdentifier,
|
||||
for: indexPath
|
||||
) as! HomeMenuCell
|
||||
cell.apply(menu: menu)
|
||||
return cell
|
||||
|
||||
default:
|
||||
return UICollectionViewCell()
|
||||
}
|
||||
}
|
||||
|
||||
dataSource.supplementaryViewProvider = { collectionView, kind, indexPath in
|
||||
guard kind == UICollectionView.elementKindSectionHeader,
|
||||
indexPath.section < self.currentSections.count,
|
||||
self.currentSections[indexPath.section] == .commonApps else {
|
||||
return nil
|
||||
}
|
||||
let header = collectionView.dequeueReusableSupplementaryView(
|
||||
ofKind: kind,
|
||||
withReuseIdentifier: HomeSectionHeaderView.reuseIdentifier,
|
||||
for: indexPath
|
||||
) as! HomeSectionHeaderView
|
||||
header.apply(title: "常用应用")
|
||||
return header
|
||||
}
|
||||
|
||||
return dataSource
|
||||
}
|
||||
|
||||
private func initializeHome() async {
|
||||
showLoading()
|
||||
await viewModel.initialize(api: homeAPI)
|
||||
@ -150,34 +255,49 @@ final class HomeViewController: BaseViewController {
|
||||
@MainActor
|
||||
private func applyViewModel() {
|
||||
scenicHeaderView.apply(scenicName: viewModel.currentScenicName)
|
||||
|
||||
let showWorkBlock = !viewModel.isMinimalTopRole
|
||||
workStatusCardView.isHidden = !showWorkBlock
|
||||
locationReportCardView.isHidden = !showWorkBlock
|
||||
quickActionsView.isHidden = !showWorkBlock
|
||||
|
||||
if showWorkBlock {
|
||||
workStatusCardView.apply(
|
||||
isOnline: viewModel.isOnline,
|
||||
countdownText: viewModel.countdownDisplayText,
|
||||
reminderMinutes: viewModel.reminderMinutes
|
||||
)
|
||||
locationReportCardView.apply(
|
||||
address: viewModel.locationInfoText,
|
||||
isReporting: viewModel.isLocationReporting
|
||||
)
|
||||
quickActionsView.apply(isOnline: viewModel.isOnline)
|
||||
}
|
||||
|
||||
let showStore = viewModel.currentAppRole == .storeAdmin && viewModel.storeItem != nil
|
||||
storeCardView.isHidden = !showStore
|
||||
if showStore, let store = viewModel.storeItem {
|
||||
storeCardView.apply(store: store)
|
||||
let sections = HomeCollectionLayoutBuilder.sectionOrder(
|
||||
showWorkBlock: showWorkBlock,
|
||||
showStore: showStore,
|
||||
storeItem: viewModel.storeItem
|
||||
)
|
||||
|
||||
if sections != currentSections {
|
||||
currentSections = sections
|
||||
collectionView.setCollectionViewLayout(
|
||||
HomeCollectionLayoutBuilder.makeLayout(sections: sections),
|
||||
animated: false
|
||||
)
|
||||
}
|
||||
|
||||
commonAppsGridView.apply(menus: viewModel.commonMenus)
|
||||
let snapshot = HomeCollectionLayoutBuilder.makeSnapshot(
|
||||
showWorkBlock: showWorkBlock,
|
||||
showStore: showStore,
|
||||
storeItem: viewModel.storeItem,
|
||||
commonMenus: viewModel.commonMenus
|
||||
)
|
||||
dataSource.apply(snapshot, animatingDifferences: false)
|
||||
presentDialogsIfNeeded()
|
||||
}
|
||||
|
||||
private func refreshWorkStatusVisibleCell() {
|
||||
guard !viewModel.isMinimalTopRole,
|
||||
let sectionIndex = currentSections.firstIndex(of: .workStatus) else {
|
||||
return
|
||||
}
|
||||
let indexPath = IndexPath(item: 0, section: sectionIndex)
|
||||
guard let cell = collectionView.cellForItem(at: indexPath) as? HomeWorkStatusCell else {
|
||||
return
|
||||
}
|
||||
cell.apply(
|
||||
isOnline: viewModel.isOnline,
|
||||
countdownText: viewModel.countdownDisplayText,
|
||||
reminderMinutes: viewModel.reminderMinutes
|
||||
)
|
||||
}
|
||||
|
||||
private func presentDialogsIfNeeded() {
|
||||
if viewModel.showPermissionDialog {
|
||||
presentDialog(.permission)
|
||||
@ -345,13 +465,7 @@ final class HomeViewController: BaseViewController {
|
||||
countdownTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] _ in
|
||||
guard let self else { return }
|
||||
self.viewModel.triggerLocationTimeoutIfNeeded()
|
||||
if !self.viewModel.isMinimalTopRole {
|
||||
self.workStatusCardView.apply(
|
||||
isOnline: self.viewModel.isOnline,
|
||||
countdownText: self.viewModel.countdownDisplayText,
|
||||
reminderMinutes: self.viewModel.reminderMinutes
|
||||
)
|
||||
}
|
||||
self.refreshWorkStatusVisibleCell()
|
||||
}
|
||||
}
|
||||
|
||||
@ -372,3 +486,25 @@ final class HomeViewController: BaseViewController {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension HomeViewController: UICollectionViewDelegate {
|
||||
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
||||
guard indexPath.section < currentSections.count,
|
||||
currentSections[indexPath.section] == .commonApps,
|
||||
let item = dataSource.itemIdentifier(for: indexPath),
|
||||
case let .menu(menu) = item else {
|
||||
return
|
||||
}
|
||||
|
||||
HomeRouteHandler.open(
|
||||
uri: menu.uri,
|
||||
from: self,
|
||||
storeItem: viewModel.storeItem,
|
||||
onCommonMenusChanged: { [weak self] in
|
||||
guard let self else { return }
|
||||
self.viewModel.rebuildCommonMenus()
|
||||
self.applyViewModel()
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user