|
|
@@ -0,0 +1,88 @@
|
|
|
+//
|
|
|
+// ModalCartCoordinator.swift
|
|
|
+// CoordinatorSUIExamples
|
|
|
+//
|
|
|
+// Created by Pavel Yurchenko on 05.12.2024.
|
|
|
+//
|
|
|
+
|
|
|
+import CoordinatorSUI
|
|
|
+import SwiftUI
|
|
|
+
|
|
|
+final class ModalCartCoordinator: BaseCoordinator {
|
|
|
+
|
|
|
+ func run() -> some View {
|
|
|
+ showCartModule()
|
|
|
+ }
|
|
|
+
|
|
|
+ @ViewBuilder
|
|
|
+ func buildDestination(_ route: Route) -> some View {
|
|
|
+ switch route {
|
|
|
+ case .product(let id):
|
|
|
+ showProductModule(with: id)
|
|
|
+ case .cart:
|
|
|
+ showCartModule()
|
|
|
+ default:
|
|
|
+ EmptyView()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // MARK: - Private methods
|
|
|
+
|
|
|
+ private func showProductModule(with id: Int) -> some View {
|
|
|
+ ProductBuilder().build(
|
|
|
+ with: .init(
|
|
|
+ id: id,
|
|
|
+ showCart: false,
|
|
|
+ showProducts: false
|
|
|
+ ),
|
|
|
+ output: .init(
|
|
|
+ onAlert: { [weak self] title in
|
|
|
+ self?.alertPresenter.showSimpleAlert(title) {
|
|
|
+ print("On alert OK!")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ )
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ private func showCartModule() -> some View {
|
|
|
+ CartBuilder().build(
|
|
|
+ with: .init(products: [1, 2, 3]),
|
|
|
+ output: .init(
|
|
|
+ onProduct: { [weak self] in
|
|
|
+ self?.currentRouter.push(Route.product(id: $0))
|
|
|
+ }
|
|
|
+ )
|
|
|
+ )
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// MARK: - Navigation View
|
|
|
+
|
|
|
+struct ModalCartNavigationView: View {
|
|
|
+
|
|
|
+ @StateObject private var coordinator: ModalCartCoordinator
|
|
|
+ @StateObject private var router: Router
|
|
|
+ @StateObject private var alertPresenter: AlertPresenter
|
|
|
+
|
|
|
+ init(_ coordinator: ModalCartCoordinator) {
|
|
|
+ _coordinator = StateObject(wrappedValue: coordinator)
|
|
|
+ _router = StateObject(wrappedValue: coordinator.currentRouter)
|
|
|
+ _alertPresenter = StateObject(wrappedValue: coordinator.alertPresenter)
|
|
|
+ }
|
|
|
+
|
|
|
+ var body: some View {
|
|
|
+ NavigationStack(path: $router.path) {
|
|
|
+ coordinator.run()
|
|
|
+ .navigationDestination(
|
|
|
+ for: Route.self,
|
|
|
+ destination: coordinator.buildDestination
|
|
|
+ )
|
|
|
+ }.alert(
|
|
|
+ alertPresenter.title,
|
|
|
+ isPresented: $alertPresenter.isPresenting,
|
|
|
+ actions: alertPresenter.buildButtons
|
|
|
+ )
|
|
|
+ }
|
|
|
+}
|
|
|
+
|