Prechádzať zdrojové kódy

Cart module added and navigation to it

Pavel Yurchenko 1 rok pred
rodič
commit
965380800f

+ 24 - 2
SUIExamples/Flows/MainCoordinator.swift

@@ -24,6 +24,8 @@ final class MainCoordinator: BaseCoordinator {
         switch route {
         case .products:
             showProductsModule()
+        case .cart:
+            showCartModule()
         default: EmptyView()
         }
     }
@@ -35,11 +37,31 @@ final class MainCoordinator: BaseCoordinator {
             with: .init(
                 onProducts: { [weak self] in
                     self?.currentRouter.push(Route.products)
-                })
+                },
+                onCart: { [weak self] in
+                    self?.currentRouter.push(Route.cart)
+                }
+            )
         )
     }
 
     private func showProductsModule() -> some View {
-        ProductsBuilder().build(with: .init())
+        ProductsBuilder().build(
+            with: .init(
+                onCart: { [weak self] in
+                    self?.currentRouter.push(Route.cart)
+                }
+            )
+        )
+    }
+
+    private func showCartModule() -> some View {
+        CartBuilder().build(
+            with: .init(
+                onProducts: { [weak self] in
+                    self?.currentRouter.push(Route.products)
+                }
+            )
+        )
     }
 }

+ 22 - 0
SUIExamples/Modules/Cart/CartBuilder.swift

@@ -0,0 +1,22 @@
+//
+//  CartBuilder.swift
+//  SUIExamples
+//
+//  Created by Pavel Yurchenko on 27.11.2024.
+//
+
+import SwiftUI
+
+struct CartBuilder {
+
+    typealias Output = CartVM.Output
+
+    func build(with output: Output) -> some View {
+        CartView()
+            .environment(
+                CartVM(
+                    output: output
+                )
+            )
+    }
+}

+ 22 - 0
SUIExamples/Modules/Cart/CartVM.swift

@@ -0,0 +1,22 @@
+//
+//  CartVM.swift
+//  SUIExamples
+//
+//  Created by Pavel Yurchenko on 27.11.2024.
+//
+
+import SwiftUI
+
+@Observable
+final class CartVM {
+
+    struct Output {
+        var onProducts: Action?
+    }
+
+    let output: Output
+
+    init(output: Output) {
+        self.output = output
+    }
+}

+ 27 - 0
SUIExamples/Modules/Cart/CartView.swift

@@ -0,0 +1,27 @@
+//
+//  CartView.swift
+//  SUIExamples
+//
+//  Created by Pavel Yurchenko on 17.08.2024.
+//
+
+import SwiftUI
+
+struct CartView: View {
+    @Environment(CartVM.self) private var vm
+
+    var body: some View {
+        VStack {
+            Text("Cart")
+            Spacer()
+            Button("Products") {
+                vm.output.onProducts?()
+            }
+        }
+        .padding()
+    }
+}
+
+#Preview {
+    ProductsBuilder().build(with: .init())
+}

+ 1 - 0
SUIExamples/Modules/Products/ProductsVM.swift

@@ -12,6 +12,7 @@ final class ProductsVM {
 
     struct Output {
         var onProduct: IntAction?
+        var onCart: Action?
     }
 
     let output: Output

+ 5 - 0
SUIExamples/Modules/Products/ProductsView.swift

@@ -12,6 +12,8 @@ struct ProductsView: View {
 
     var body: some View {
         VStack {
+            Text("Products")
+            Spacer()
             Button("Product 1") {
                 vm.output.onProduct?(1)
             }
@@ -27,6 +29,9 @@ struct ProductsView: View {
             Button("Product 5") {
                 vm.output.onProduct?(5)
             }
+            Button("Cart") {
+                vm.output.onCart?()
+            }
         }
         .padding()
     }