MainCoordinator.swift 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // MainCoordinator.swift
  3. // SUIExamples
  4. //
  5. // Created by Pavel Yurchenko on 28.11.2024.
  6. //
  7. import CoordinatorSUI
  8. import SwiftUI
  9. final class MainCoordinator: BaseCoordinator {
  10. init(router: Router) {
  11. super.init()
  12. self.currentRouter = router
  13. }
  14. func run() -> some View {
  15. showMainModule()
  16. }
  17. @ViewBuilder
  18. func buildDestination(_ route: Route) -> some View {
  19. switch route {
  20. case .products:
  21. showProductsModule()
  22. case .product(let id):
  23. showProductModule(with: id)
  24. case .cart:
  25. showCartModule()
  26. default: EmptyView()
  27. }
  28. }
  29. // MARK: - Private methods
  30. private func showMainModule() -> some View {
  31. MainBuilder().build(
  32. with: .init(
  33. onProducts: { [weak self] in
  34. self?.currentRouter.push(Route.products)
  35. },
  36. onProduct: { [weak self] id in
  37. self?.currentRouter.push(Route.product(id: id))
  38. },
  39. onCart: { [weak self] in
  40. self?.currentRouter.push(Route.cart)
  41. }
  42. )
  43. )
  44. }
  45. private func showProductsModule() -> some View {
  46. ProductsBuilder().build(
  47. with: .init(
  48. onProduct: { [weak self] in
  49. self?.currentRouter.push(Route.product(id: $0))
  50. },
  51. onCart: { [weak self] in
  52. self?.currentRouter.push(Route.cart)
  53. }
  54. )
  55. )
  56. }
  57. private func showCartModule() -> some View {
  58. CartBuilder().build(
  59. with: .init(
  60. onProducts: { [weak self] in
  61. self?.currentRouter.push(Route.products)
  62. }
  63. )
  64. )
  65. }
  66. private func showProductModule(with id: Int) -> some View {
  67. ProductBuilder().build(
  68. with: .init(id: id),
  69. output: .init(
  70. onProducts: { [weak self] in
  71. self?.currentRouter.push(Route.products)
  72. },
  73. onCart: { [weak self] in
  74. self?.currentRouter.push(Route.cart)
  75. }
  76. )
  77. )
  78. }
  79. }