Files
suixinkan_ios_new/suixinkan/App/Navigation/NavigationRouter.swift
汉秋 1d1eb46ed8 Add custom main tab bar with global scan-to-verify flow.
Introduce configurable custom TabBar with order badge, central scanner routing to verification orders, and update the migration checklist for recently completed modules.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-23 10:52:45 +08:00

131 lines
3.5 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.

//
// NavigationRouter.swift
// suixinkan
//
// Created by Codex on 2026/6/18.
//
import Observation
import SwiftUI
/// Tab
enum AppRoute: Hashable {
case placeholder(title: String)
case home(HomeRoute)
case profile(ProfileRoute)
case orders(OrdersRoute)
/// NavigationStack push TabBar
var hidesTabBarWhenPushed: Bool {
true
}
///
@ViewBuilder
var destinationView: some View {
switch self {
case .placeholder(let title):
PlaceholderDetailView(title: title)
case .home(let route):
route.destinationView
case .profile(let route):
route.destinationView
case .orders(let route):
route.destinationView
}
}
}
extension OrdersRoute {
///
@ViewBuilder
var destinationView: some View {
switch self {
case .storeDetail(let item):
StoreOrderDetailView(item: item)
case .writeOffDetail(let item):
WriteOffOrderDetailView(item: item)
}
}
}
@MainActor
@Observable
/// NavigationStack Tab
final class RouterPath {
var path: [AppRoute] = []
/// Tab
func navigate(to route: AppRoute) {
path.append(route)
}
/// Tab
func reset() {
path = []
}
}
@MainActor
@Observable
/// Tab Tab NavigationStack
final class AppRouter {
var selectedTab: AppTab = .home
var selectedOrdersEntry: OrdersEntry = .storeOrders
private(set) var pendingOrderScanCode: String?
private var routers: [AppTab: RouterPath] = [:]
/// Tab
func router(for tab: AppTab) -> RouterPath {
if let router = routers[tab] {
return router
}
let router = RouterPath()
routers[tab] = router
return router
}
/// SwiftUI NavigationStack 使
func binding(for tab: AppTab) -> Binding<[AppRoute]> {
let router = router(for: tab)
return Binding(
get: { router.path },
set: { router.path = $0 }
)
}
/// Tab
func select(_ tab: AppTab) {
selectedTab = tab
}
/// Tab
func selectOrders(entry: OrdersEntry) {
selectedOrdersEntry = entry
selectedTab = .orders
}
///
func routeToOrderVerification(scannedCode: String) {
pendingOrderScanCode = scannedCode
selectOrders(entry: .verificationOrders)
}
///
func consumePendingOrderScanCode() -> String? {
let code = pendingOrderScanCode
pendingOrderScanCode = nil
return code
}
/// Tab Tab
func reset() {
selectedTab = .home
selectedOrdersEntry = .storeOrders
pendingOrderScanCode = nil
routers.values.forEach { $0.reset() }
}
}