Files
suixinkan_ios_new/suixinkan/App/Navigation/NavigationRouter.swift
汉秋 26f4d0e671 Migrate from iOS 17 Observation to iOS 16-compatible Combine architecture.
Lower deployment target to iOS 16, replace @Observable with ObservableObject, add navigation and UI compatibility shims, and include login smoke UI tests.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-26 10:16:35 +08:00

139 lines
4.2 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 Combine
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)
case .depositDetail(let orderNumber):
DepositOrderDetailView(orderNumber: orderNumber)
case .depositShootingInfo(let orderNumber, let scenicSpotId, let photogUid):
DepositOrderShootingInfoView(orderNumber: orderNumber, scenicSpotId: scenicSpotId, photogUid: photogUid)
case .historicalShooting(let orderNumber):
HistoricalShootingInfoView(orderNumber: orderNumber)
case .multiTravelTaskUpload(let orderNumber):
MultiTravelTaskUploadView(initialOrderNumber: orderNumber)
case .orderTrailer(let orderNumber, let title):
OrderTrailerView(orderNumber: orderNumber, title: title)
}
}
}
@MainActor
/// NavigationStack Tab
final class RouterPath: ObservableObject {
@Published var path: [AppRoute] = []
/// Tab
func navigate(to route: AppRoute) {
path.append(route)
}
/// Tab
func reset() {
path = []
}
}
@MainActor
/// Tab Tab NavigationStack
final class AppRouter: ObservableObject {
@Published var selectedTab: AppTab = .home
@Published var selectedOrdersEntry: OrdersEntry = .storeOrders
@Published private(set) var pendingOrderScanCode: String?
@Published 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() }
}
}