MainCoordinator.swift 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. case .feedback:
  27. showFeedbackModule()
  28. default:
  29. fatalError("This should not happen!")
  30. }
  31. }
  32. // MARK: - Private methods
  33. private func showMainModule() -> some View {
  34. MainBuilder().build(
  35. with: .init(
  36. onProducts: { [weak self] in
  37. self?.currentRouter.push(Route.products)
  38. },
  39. onProduct: { [weak self] id in
  40. self?.currentRouter.push(Route.product(id: id))
  41. },
  42. onCart: { [weak self] in
  43. self?.currentRouter.push(Route.cart)
  44. },
  45. onFeedback: { [weak self] in
  46. self?.currentRouter.push(Route.feedback)
  47. }
  48. )
  49. )
  50. }
  51. private func showProductsModule() -> some View {
  52. ProductsBuilder().build(
  53. with: .init(
  54. onProduct: { [weak self] in
  55. self?.currentRouter.push(Route.product(id: $0))
  56. },
  57. onCart: { [weak self] in
  58. self?.currentRouter.push(Route.cart)
  59. }
  60. )
  61. )
  62. }
  63. private func showCartModule() -> some View {
  64. CartBuilder().build(
  65. with: .init(
  66. onProducts: { [weak self] in
  67. self?.currentRouter.push(Route.products)
  68. }
  69. )
  70. )
  71. }
  72. private func showProductModule(with id: Int) -> some View {
  73. ProductBuilder().build(
  74. with: .init(id: id),
  75. output: .init(
  76. onProducts: { [weak self] in
  77. self?.currentRouter.push(Route.products)
  78. },
  79. onCart: { [weak self] in
  80. self?.currentRouter.push(Route.cart)
  81. }
  82. )
  83. )
  84. }
  85. private func showFeedbackModule() -> some View {
  86. FeedbackBuilder().build(with: .init())
  87. }
  88. }