Explorar o código

Getting working architecture first time

Pavel Yurchenko hai 1 ano
pai
achega
b2db3a5b5f

+ 2 - 2
CoordinatorSUI/Sources/CoordinatorSUI/Router.swift

@@ -16,11 +16,11 @@ public final class Router: ObservableObject {
 
     }
 
-    public func push(_ route: any Hashable) {
+    func push(_ route: any Hashable) {
         path.append(route)
     }
 
-    public func pop() {
+    func pop() {
         path.removeLast()
     }
 }

+ 7 - 0
SUIExamples/Application/ApplicationCoordinator.swift

@@ -9,6 +9,13 @@ import CoordinatorSUI
 
 final class ApplicationCoordinator: BaseCoordinator {
 
+    static var rootRouter = Router()
+
+    override init() {
+        super.init()
+        self.currentRouter = Self.rootRouter
+    }
+
     override func run() {
         runMainFlow()
     }

+ 5 - 15
SUIExamples/Application/SUIExamplesApp.swift

@@ -12,11 +12,7 @@ import SwiftUI
 struct SUIExamplesApp: App {
 
     private let applicationCoordinator = ApplicationCoordinator()
-    @State private var router: Router = Router()
-
-    init() {
-        self.applicationCoordinator.currentRouter = router
-    }
+    @StateObject private var router: Router = ApplicationCoordinator.rootRouter
 
     var body: some Scene {
         WindowGroup {
@@ -24,21 +20,15 @@ struct SUIExamplesApp: App {
                 MainBuilder().build(
                     with: .init(
                         onProducts: {
-                            applicationCoordinator.currentRouter.push(
+                            router.path.append(
                                 Route.products
                             )
                         })
                 )
                 .navigationDestination(
-                    for: Route.self
-                ) { _ in
-                    print("Destination")
-                    return EmptyView()
-                }
-//                .navigationDestination(
-//                    for: Route.self,
-//                    destination: applicationCoordinator.buildDestination
-//                )
+                    for: Route.self,
+                    destination: applicationCoordinator.buildDestination
+                )
             }
         }
     }

+ 0 - 38
SUIExamples/Flows/Route.swift

@@ -1,38 +0,0 @@
-//
-//  Route.swift
-//  SUIExamples
-//
-//  Created by Pavel Yurchenko on 27.11.2024.
-//
-
-import CoordinatorSUI
-import SwiftUI
-
-enum Route: Hashable {
-    case main
-    case products
-    case product(id: Int)
-    case cart
-    case feedback
-}
-
-extension Coordinator {
-
-    @ViewBuilder
-    func buildDestination(_ route: Route) -> some View {
-        switch route {
-        case .main:
-            MainBuilder().build(with: .init(onProducts: { [weak self] in
-                self?.currentRouter.push(Route.products)
-            }))
-        case .products:
-            ProductsBuilder().build(with: .init())
-        case .product:
-            MainBuilder().build(with: .init())
-        case .cart:
-            MainBuilder().build(with: .init())
-        case .feedback:
-            MainBuilder().build(with: .init())
-        }
-    }
-}