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

Hiding Cart and Products button when running modal products flow

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

+ 8 - 4
CoordinatorSUIExamples/CoordinatorSUIExamples/Flows/ModalProductsCoordinator.swift

@@ -10,8 +10,7 @@ import SwiftUI
 
 final class ModalProductsCoordinator: BaseCoordinator {
 
-    func run() -> some View
-    {
+    func run() -> some View {
         showProductsModule()
     }
 
@@ -29,7 +28,8 @@ final class ModalProductsCoordinator: BaseCoordinator {
 
     private func showProductsModule() -> some View {
         ProductsBuilder().build(
-            with: .init(
+            with: .init(showCart: false),
+            output: .init(
                 onProduct: { [weak self] in
                     self?.currentRouter.push(Route.product(id: $0))
                 }
@@ -39,7 +39,11 @@ final class ModalProductsCoordinator: BaseCoordinator {
 
     private func showProductModule(with id: Int) -> some View {
         ProductBuilder().build(
-            with: .init(id: id),
+            with: .init(
+                id: id,
+                showCart: false,
+                showProducts: false
+            ),
             output: .init()
         )
     }

+ 7 - 2
CoordinatorSUIExamples/CoordinatorSUIExamples/Flows/Tabs/MainCoordinator.swift

@@ -78,7 +78,8 @@ final class MainCoordinator: BaseCoordinator, ObservableObject {
 
     private func showProductsModule() -> some View {
         ProductsBuilder().build(
-            with: .init(
+            with: .init(showCart: true),
+            output: .init(
                 onProduct: { [weak self] in
                     self?.currentRouter.push(Route.product(id: $0))
                 },
@@ -101,7 +102,11 @@ final class MainCoordinator: BaseCoordinator, ObservableObject {
 
     private func showProductModule(with id: Int) -> some View {
         ProductBuilder().build(
-            with: .init(id: id),
+            with: .init(
+                id: id,
+                showCart: true,
+                showProducts: true
+            ),
             output: .init(
                 onProducts: { [weak self] in
                     self?.currentRouter.push(Route.products)

+ 7 - 2
CoordinatorSUIExamples/CoordinatorSUIExamples/Flows/Tabs/ProductsCoordinator.swift

@@ -32,7 +32,8 @@ final class ProductsCoordinator: BaseCoordinator {
 
     private func showProductsModule() -> some View {
         ProductsBuilder().build(
-            with: .init(
+            with: .init(showCart: true),
+            output: .init(
                 onProduct: { [weak self] in
                     self?.currentRouter.push(Route.product(id: $0))
                 },
@@ -55,7 +56,11 @@ final class ProductsCoordinator: BaseCoordinator {
 
     private func showProductModule(with id: Int) -> some View {
         ProductBuilder().build(
-            with: .init(id: id),
+            with: .init(
+                id: id,
+                showCart: true,
+                showProducts: true
+            ),
             output: .init(
                 onProducts: { [weak self] in
                     self?.currentRouter.push(Route.products)

+ 6 - 0
CoordinatorSUIExamples/CoordinatorSUIExamples/Modules/Product/ProductVM.swift

@@ -12,6 +12,8 @@ final class ProductVM {
 
     struct Input {
         let id: Int
+        let showCart: Bool
+        let showProducts: Bool
     }
 
     struct Output {
@@ -21,6 +23,8 @@ final class ProductVM {
 
     let output: Output
     var objectId: Int
+    var showCart: Bool
+    var showProducts: Bool
 
     private let input: Input
 
@@ -29,6 +33,8 @@ final class ProductVM {
         self.output = output
 
         self.objectId = input.id
+        self.showCart = input.showCart
+        self.showProducts = input.showProducts
 
         DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
             self.objectId = input.id + 5

+ 9 - 5
CoordinatorSUIExamples/CoordinatorSUIExamples/Modules/Product/ProductView.swift

@@ -16,11 +16,15 @@ struct ProductView: View {
             Spacer()
             Text("#\(vm.objectId)")
             Spacer()
-            Button("Products") {
-                vm.output.onProducts?()
+            if vm.showProducts {
+                Button("Products") {
+                    vm.output.onProducts?()
+                }
             }
-            Button("Cart") {
-                vm.output.onCart?()
+            if vm.showCart {
+                Button("Cart") {
+                    vm.output.onCart?()
+                }
             }
         }
         .padding()
@@ -28,5 +32,5 @@ struct ProductView: View {
 }
 
 #Preview {
-    ProductBuilder().build(with: .init(id: 25), output: .init())
+    ProductBuilder().build(with: .init(id: 25, showCart: true, showProducts: true), output: .init())
 }

+ 3 - 1
CoordinatorSUIExamples/CoordinatorSUIExamples/Modules/Products/ProductsBuilder.swift

@@ -9,10 +9,12 @@ import SwiftUI
 
 struct ProductsBuilder {
 
+    typealias Input = ProductsVM.Input
     typealias Output = ProductsVM.Output
 
-    func build(with output: Output) -> some View {
+    func build(with input: Input, output: Output) -> some View {
         let vm = ProductsVM(
+            input: input,
             output: output
         )
         return ProductsView().environment(vm)

+ 11 - 1
CoordinatorSUIExamples/CoordinatorSUIExamples/Modules/Products/ProductsVM.swift

@@ -10,14 +10,24 @@ import SwiftUI
 @Observable
 final class ProductsVM {
 
+    struct Input {
+        let showCart: Bool
+    }
+
     struct Output {
         var onProduct: IntAction?
         var onCart: Action?
     }
 
     let output: Output
+    var showCart: Bool
 
-    init(output: Output) {
+    private let input: Input
+
+    init(input: Input, output: Output) {
+        self.input = input
         self.output = output
+
+        self.showCart = input.showCart
     }
 }

+ 5 - 3
CoordinatorSUIExamples/CoordinatorSUIExamples/Modules/Products/ProductsView.swift

@@ -29,8 +29,10 @@ struct ProductsView: View {
             Button("Product 5") {
                 vm.output.onProduct?(5)
             }
-            Button("Cart") {
-                vm.output.onCart?()
+            if vm.showCart {
+                Button("Cart") {
+                    vm.output.onCart?()
+                }
             }
         }
         .padding()
@@ -38,5 +40,5 @@ struct ProductsView: View {
 }
 
 #Preview {
-    ProductsBuilder().build(with: .init())
+    ProductsBuilder().build(with: .init(showCart: true), output: .init())
 }