Files
suixinkan_ios_new/suixinkan/App/Navigation/NavigationRouter.swift

116 lines
2.9 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 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
}
/// Tab Tab
func reset() {
selectedTab = .home
selectedOrdersEntry = .storeOrders
routers.values.forEach { $0.reset() }
}
}