Replace CYLTabBarController with native UITabBarController for main tabs.
Use a system scan tab slot with MainScanTabItem interception so scanner presentation no longer depends on CYL PlusButton, and remove the CYLTabBarController pod dependency. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@ -3,26 +3,60 @@
|
||||
// suixinkan
|
||||
//
|
||||
|
||||
import CYLTabBarController
|
||||
import UIKit
|
||||
|
||||
@MainActor
|
||||
/// 主 Tab 容器,使用 CYLTabBarController 展示四个一级入口及中间扫码按钮。
|
||||
final class MainTabBarController: CYLTabBarController {
|
||||
/// 主 Tab 容器,使用系统 TabBar 展示四个一级入口及中间扫码按钮。
|
||||
final class MainTabBarController: UITabBarController {
|
||||
|
||||
private let services: AppServices
|
||||
private let badgeViewModel = MainTabBadgeViewModel()
|
||||
private var tabNavigationControllers: [AppTab: TabNavigationController] = [:]
|
||||
private var isSyncingTabSelection = false
|
||||
|
||||
/// 测试时可注入扫码点击处理,生产环境为空时执行真实扫码展示。
|
||||
var onScanTabSelected: (() -> Void)?
|
||||
|
||||
/// 主 TabBar 固定槽位,扫码槽位只负责触发全局扫码,不参与业务路由。
|
||||
private enum TabSlot: CaseIterable {
|
||||
case home
|
||||
case orders
|
||||
case scan
|
||||
case statistics
|
||||
case profile
|
||||
|
||||
/// 返回槽位对应的业务 Tab,扫码槽位无业务根页面。
|
||||
var appTab: AppTab? {
|
||||
switch self {
|
||||
case .home:
|
||||
return .home
|
||||
case .orders:
|
||||
return .orders
|
||||
case .scan:
|
||||
return nil
|
||||
case .statistics:
|
||||
return .statistics
|
||||
case .profile:
|
||||
return .profile
|
||||
}
|
||||
}
|
||||
|
||||
/// 返回指定业务 Tab 在系统 TabBar 中的槽位索引。
|
||||
static func index(for tab: AppTab) -> Int? {
|
||||
allCases.firstIndex { $0.appTab == tab }
|
||||
}
|
||||
|
||||
/// 返回扫码槽位在系统 TabBar 中的索引。
|
||||
static var scanIndex: Int? {
|
||||
allCases.firstIndex(of: .scan)
|
||||
}
|
||||
}
|
||||
|
||||
/// 初始化实例。
|
||||
init(services: AppServices) {
|
||||
self.services = services
|
||||
super.init(nibName: nil, bundle: nil)
|
||||
MainScanPlusButton.onScanTap = { [weak self] in
|
||||
self?.presentGlobalScanner()
|
||||
}
|
||||
// delegate = self
|
||||
delegate = self
|
||||
}
|
||||
|
||||
@available(*, unavailable)
|
||||
@ -44,13 +78,6 @@ final class MainTabBarController: CYLTabBarController {
|
||||
#endif
|
||||
}
|
||||
|
||||
/// 控制器释放前清理全局扫码按钮回调。
|
||||
deinit {
|
||||
Task { @MainActor in
|
||||
MainScanPlusButton.onScanTap = nil
|
||||
}
|
||||
}
|
||||
|
||||
/// 配置 TabBar 外观,对齐参考工程 CustomMainTabBar 样式。
|
||||
private func configureTabBarAppearance() {
|
||||
tabBar.tintColor = AppDesign.primary
|
||||
@ -76,26 +103,22 @@ final class MainTabBarController: CYLTabBarController {
|
||||
tabBar.scrollEdgeAppearance = appearance
|
||||
}
|
||||
|
||||
/// 为每个 Tab 创建独立导航栈,并按 CYLTabBarController 要求先配置 Tab 属性再设置控制器。
|
||||
/// 为每个业务 Tab 创建独立导航栈,并在中间插入系统扫码槽位。
|
||||
private func configureTabs() {
|
||||
let tabs = AppTab.allCases
|
||||
tabBarItemsAttributes = tabs.map { makeTabBarItemAttributes(for: $0) }
|
||||
viewControllers = tabs.map { tab in
|
||||
let navigationController = TabNavigationController(tab: tab, services: services)
|
||||
tabNavigationControllers[tab] = navigationController
|
||||
return navigationController
|
||||
tabNavigationControllers.removeAll()
|
||||
viewControllers = TabSlot.allCases.map { slot in
|
||||
if let tab = slot.appTab {
|
||||
let navigationController = TabNavigationController(tab: tab, services: services)
|
||||
navigationController.tabBarItem = tab.makeTabBarItem()
|
||||
tabNavigationControllers[tab] = navigationController
|
||||
return navigationController
|
||||
}
|
||||
let scannerPlaceholder = UIViewController()
|
||||
scannerPlaceholder.tabBarItem = MainScanTabItem.makeTabBarItem()
|
||||
return scannerPlaceholder
|
||||
}
|
||||
}
|
||||
|
||||
/// 构建 CYLTabBarController 识别的 TabBarItem 属性字典。
|
||||
private func makeTabBarItemAttributes(for tab: AppTab) -> [String: String] {
|
||||
[
|
||||
CYLTabBarItemTitle: tab.title,
|
||||
CYLTabBarItemImage: tab.unselectedImageName,
|
||||
CYLTabBarItemSelectedImage: tab.selectedImageName
|
||||
]
|
||||
}
|
||||
|
||||
/// 绑定角标 ViewModel 回调。
|
||||
private func bindBadgeViewModel() {
|
||||
badgeViewModel.onChange = { [weak self] in
|
||||
@ -132,6 +155,8 @@ final class MainTabBarController: CYLTabBarController {
|
||||
|
||||
/// 从 TabBar 中间按钮打开全局扫码页,成功后路由至订单核销入口。
|
||||
private func presentGlobalScanner() {
|
||||
guard presentedViewController == nil else { return }
|
||||
|
||||
let scanner = OrderCodeScannerViewController()
|
||||
scanner.onScanResult = { [weak self, weak scanner] result in
|
||||
scanner?.dismiss(animated: true)
|
||||
@ -147,9 +172,25 @@ final class MainTabBarController: CYLTabBarController {
|
||||
navigationController.modalPresentationStyle = .fullScreen
|
||||
present(navigationController, animated: true)
|
||||
}
|
||||
}
|
||||
|
||||
extension MainTabBarController: UITabBarControllerDelegate {
|
||||
/// 用户选择 TabBar 槽位前拦截扫码入口,使关闭扫码页后保留原页面。
|
||||
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
|
||||
guard viewControllers?.firstIndex(of: viewController) == TabSlot.scanIndex else {
|
||||
return true
|
||||
}
|
||||
|
||||
if let onScanTabSelected {
|
||||
onScanTabSelected()
|
||||
} else {
|
||||
presentGlobalScanner()
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/// 用户点击 TabBar 时刷新订单角标。
|
||||
override func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
|
||||
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
|
||||
guard !isSyncingTabSelection,
|
||||
let navigationController = viewController as? TabNavigationController else { return }
|
||||
|
||||
@ -162,7 +203,7 @@ final class MainTabBarController: CYLTabBarController {
|
||||
extension MainTabBarController: AppNavigationHost {
|
||||
/// 选中指定主 Tab。
|
||||
func selectTab(_ tab: AppTab) {
|
||||
guard let index = AppTab.allCases.firstIndex(of: tab) else { return }
|
||||
guard let index = TabSlot.index(for: tab) else { return }
|
||||
guard selectedIndex != index else { return }
|
||||
isSyncingTabSelection = true
|
||||
selectedIndex = index
|
||||
|
||||
Reference in New Issue
Block a user