Преглед на файлове

Transfers to product module added

Pavel Yurchenko преди 1 година
родител
ревизия
d9db2a266a

+ 1 - 0
SUIExamples/Application/SUIExamplesApp.swift

@@ -22,6 +22,7 @@ struct SUIExamplesApp: App {
                     for: Route.self,
                     destination: applicationCoordinator.buildDestination
                 )
+                
             }
         }
     }

+ 22 - 0
SUIExamples/Flows/MainCoordinator.swift

@@ -24,6 +24,8 @@ final class MainCoordinator: BaseCoordinator {
         switch route {
         case .products:
             showProductsModule()
+        case .product(let id):
+            showProductModule(with: id)
         case .cart:
             showCartModule()
         default: EmptyView()
@@ -38,6 +40,9 @@ final class MainCoordinator: BaseCoordinator {
                 onProducts: { [weak self] in
                     self?.currentRouter.push(Route.products)
                 },
+                onProduct: { [weak self] id in
+                    self?.currentRouter.push(Route.product(id: id))
+                },
                 onCart: { [weak self] in
                     self?.currentRouter.push(Route.cart)
                 }
@@ -48,6 +53,9 @@ final class MainCoordinator: BaseCoordinator {
     private func showProductsModule() -> some View {
         ProductsBuilder().build(
             with: .init(
+                onProduct: { [weak self] in
+                    self?.currentRouter.push(Route.product(id: $0))
+                },
                 onCart: { [weak self] in
                     self?.currentRouter.push(Route.cart)
                 }
@@ -64,4 +72,18 @@ final class MainCoordinator: BaseCoordinator {
             )
         )
     }
+
+    private func showProductModule(with id: Int) -> some View {
+        ProductBuilder().build(
+            with: .init(id: id),
+            output: .init(
+                onProducts: { [weak self] in
+                    self?.currentRouter.push(Route.products)
+                },
+                onCart: { [weak self] in
+                    self?.currentRouter.push(Route.cart)
+                }
+            )
+        )
+    }
 }

+ 1 - 1
SUIExamples/Modules/Cart/CartView.swift

@@ -23,5 +23,5 @@ struct CartView: View {
 }
 
 #Preview {
-    ProductsBuilder().build(with: .init())
+    CartBuilder().build(with: .init())
 }

+ 24 - 0
SUIExamples/Modules/Product/ProductBuilder.swift

@@ -0,0 +1,24 @@
+//
+//  ProductBuilder.swift
+//  SUIExamples
+//
+//  Created by Pavel Yurchenko on 27.11.2024.
+//
+
+import SwiftUI
+
+struct ProductBuilder {
+
+    typealias Input = ProductVM.Input
+    typealias Output = ProductVM.Output
+
+    func build(with input: Input, output: Output) -> some View {
+        ProductView()
+            .environment(
+                ProductVM(
+                    input: input,
+                    output: output
+                )
+            )
+    }
+}

+ 34 - 0
SUIExamples/Modules/Product/ProductVM.swift

@@ -0,0 +1,34 @@
+//
+//  ProductVM.swift
+//  SUIExamples
+//
+//  Created by Pavel Yurchenko on 27.11.2024.
+//
+
+import SwiftUI
+
+@Observable
+final class ProductVM {
+
+    struct Input {
+        let id: Int
+    }
+
+    struct Output {
+        var onProducts: Action?
+        var onCart: Action?
+    }
+
+    let output: Output
+
+    var productId: Int {
+        input.id
+    }
+
+    private let input: Input
+
+    init(input: Input, output: Output) {
+        self.input = input
+        self.output = output
+    }
+}

+ 32 - 0
SUIExamples/Modules/Product/ProductView.swift

@@ -0,0 +1,32 @@
+//
+//  ProductView.swift
+//  SUIExamples
+//
+//  Created by Pavel Yurchenko on 17.08.2024.
+//
+
+import SwiftUI
+
+struct ProductView: View {
+    @Environment(ProductVM.self) private var vm
+
+    var body: some View {
+        VStack {
+            Text("Product")
+            Spacer()
+            Text("#\(vm.productId)")
+            Spacer()
+            Button("Products") {
+                vm.output.onProducts?()
+            }
+            Button("Cart") {
+                vm.output.onCart?()
+            }
+        }
+        .padding()
+    }
+}
+
+#Preview {
+    ProductBuilder().build(with: .init(id: 25), output: .init())
+}