Bläddra i källkod

First implementation with router in coordinator

Pavel Yurchenko 1 år sedan
förälder
incheckning
c7a0e58098

+ 3 - 0
CoordinatorSUI/Package.swift

@@ -5,6 +5,9 @@ import PackageDescription
 
 let package = Package(
     name: "CoordinatorSUI",
+    platforms: [
+        .iOS(.v16)
+    ],
     products: [
         // Products define the executables and libraries a package produces, making them visible to other packages.
         .library(

+ 6 - 2
CoordinatorSUI/Sources/CoordinatorSUI/BaseCoordinator.swift

@@ -5,7 +5,7 @@
 //  Created by Pavel Yurchenko on 28.11.2024.
 //
 
-import Foundation
+import SwiftUI
 
 open class BaseCoordinator: Coordinator {
 
@@ -13,6 +13,8 @@ open class BaseCoordinator: Coordinator {
 
     public var finishFlow: Action?
 
+    public var currentRouter: Router = Router()
+
     // MARK: - Private properties
 
     private var childCoordinators = [Coordinator]()
@@ -30,7 +32,9 @@ open class BaseCoordinator: Coordinator {
         }
     }
 
-    open func start() {}
+    public init() { }
+
+    open func run() {}
 
     deinit {
         print("\(self.self) deinit")

+ 2 - 1
CoordinatorSUI/Sources/CoordinatorSUI/Coordinator.swift

@@ -9,5 +9,6 @@ import Foundation
 
 public protocol Coordinator: AnyObject {
     var finishFlow: Action? { get set }
-    func start()
+    var currentRouter: Router { get }
+    func run()
 }

+ 27 - 0
CoordinatorSUI/Sources/CoordinatorSUI/Router.swift

@@ -0,0 +1,27 @@
+//
+//  File.swift
+//  CoordinatorSUI
+//
+//  Created by Pavel Yurchenko on 28.11.2024.
+//
+
+import SwiftUI
+
+public final class Router: ObservableObject {
+
+    @Published
+    public var path = NavigationPath()
+
+    public init() {
+
+    }
+
+    public func push(_ route: any Hashable) {
+        path.append(route)
+    }
+
+    public func pop() {
+        path.removeLast()
+    }
+}
+

+ 8 - 3
SUIExamples/Application/ApplicationCoordinator.swift

@@ -9,14 +9,19 @@ import CoordinatorSUI
 
 final class ApplicationCoordinator: BaseCoordinator {
 
-    override func start() {
+    override func run() {
         runMainFlow()
     }
 
-    // MARK: - Private properties
+    // MARK: - Private methods
 
     private func runMainFlow() {
-        
+        let coordinator = MainCoordinator(router: self.currentRouter)
+        addChild(coordinator)
+        coordinator.finishFlow = { [weak self] in
+            self?.removeChild(coordinator)
+        }
+        coordinator.run()
     }
 }
 

+ 19 - 7
SUIExamples/Application/SUIExamplesApp.swift

@@ -5,28 +5,40 @@
 //  Created by Pavel Yurchenko on 17.08.2024.
 //
 
+import CoordinatorSUI
 import SwiftUI
 
 @main
 struct SUIExamplesApp: App {
 
-    @StateObject private var router: Router = DI.shared.router
+    private let applicationCoordinator = ApplicationCoordinator()
+    @State private var router: Router = Router()
+
+    init() {
+        self.applicationCoordinator.currentRouter = router
+    }
 
     var body: some Scene {
         WindowGroup {
-            NavigationStack(path: $router.path){
+            NavigationStack(path: $router.path) {
                 MainBuilder().build(
                     with: .init(
                         onProducts: {
-                            router.push(
-                                .products
+                            applicationCoordinator.currentRouter.push(
+                                Route.products
                             )
                         })
                 )
                 .navigationDestination(
-                    for: Route.self,
-                    destination: router.buildDestination
-                )
+                    for: Route.self
+                ) { _ in
+                    print("Destination")
+                    return EmptyView()
+                }
+//                .navigationDestination(
+//                    for: Route.self,
+//                    destination: applicationCoordinator.buildDestination
+//                )
             }
         }
     }

+ 0 - 2
SUIExamples/Core/DI.swift

@@ -9,6 +9,4 @@ import Foundation
 
 final class DI {
     static var shared: DI = .init()
-
-    let router = Router()
 }

+ 21 - 0
SUIExamples/Flows/MainCoordinator.swift

@@ -0,0 +1,21 @@
+//
+//  MainCoordinator.swift
+//  SUIExamples
+//
+//  Created by Pavel Yurchenko on 28.11.2024.
+//
+
+import CoordinatorSUI
+
+final class MainCoordinator: BaseCoordinator {
+
+    init(router: Router) {
+        super.init()
+        self.currentRouter = router
+    }
+
+    override func run() {
+
+    }
+
+}

+ 11 - 14
SUIExamples/Navigator/Router.swift → SUIExamples/Flows/Route.swift

@@ -1,32 +1,29 @@
 //
-//  Router.swift
+//  Route.swift
 //  SUIExamples
 //
 //  Created by Pavel Yurchenko on 27.11.2024.
 //
 
-import Foundation
+import CoordinatorSUI
 import SwiftUI
 
-final class Router: ObservableObject {
-
-    @Published
-    var path = NavigationPath()
-
-    func push(_ route: Route) {
-        path.append(route)
-    }
+enum Route: Hashable {
+    case main
+    case products
+    case product(id: Int)
+    case cart
+    case feedback
+}
 
-    func pop() {
-        path.removeLast()
-    }
+extension Coordinator {
 
     @ViewBuilder
     func buildDestination(_ route: Route) -> some View {
         switch route {
         case .main:
             MainBuilder().build(with: .init(onProducts: { [weak self] in
-                self?.push(.products)
+                self?.currentRouter.push(Route.products)
             }))
         case .products:
             ProductsBuilder().build(with: .init())

+ 0 - 16
SUIExamples/Navigator/Route.swift

@@ -1,16 +0,0 @@
-//
-//  Route.swift
-//  SUIExamples
-//
-//  Created by Pavel Yurchenko on 27.11.2024.
-//
-
-import SwiftUI
-
-enum Route: Hashable {
-    case main
-    case products
-    case product(id: Int)
-    case cart
-    case feedback
-}