From 082e5b9fee0d8a6c6dcf95cad18894aac218adc5 Mon Sep 17 00:00:00 2001 From: Zoe Date: Sat, 16 Nov 2024 03:45:33 +0900 Subject: [PATCH] =?UTF-8?q?[Feat/#104]=20=EC=B5=9C=EC=83=81=EC=9C=84=20Coo?= =?UTF-8?q?rdinator?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Sources/Coordinator/AppCoordinator.swift | 64 +++++++++++++++++++ .../Sources/Coordinator/BaseCoordinator.swift | 38 +++++++++++ 2 files changed, 102 insertions(+) create mode 100644 HMH_Tuist_iOS/Projects/App/Sources/Coordinator/AppCoordinator.swift create mode 100644 HMH_Tuist_iOS/Projects/App/Sources/Coordinator/BaseCoordinator.swift diff --git a/HMH_Tuist_iOS/Projects/App/Sources/Coordinator/AppCoordinator.swift b/HMH_Tuist_iOS/Projects/App/Sources/Coordinator/AppCoordinator.swift new file mode 100644 index 00000000..8b87d981 --- /dev/null +++ b/HMH_Tuist_iOS/Projects/App/Sources/Coordinator/AppCoordinator.swift @@ -0,0 +1,64 @@ +// +// AppCoordinator.swift +// Coordinator +// +// Created by 이지희 on 11/7/24. +// Copyright © 2024 HMH-iOS. All rights reserved. +// + +import SwiftUI + +import Core +import LoginFeature +import OnboardingFeature +import MyPageFeature + + +final class AppCoordinator: ObservableObject, CoordinatorType { + @Published var currentView: AnyView = AnyView(SplashView(coordinator: AppCoordinator(navigationPath: .init()))) + @Published var appState: AppState = .login + + var navigationPath: NavigationPath + var parentCoordinator: (any CoordinatorType)? + + init(navigationPath: NavigationPath) { + self.navigationPath = navigationPath + } + + func start() -> AnyView { + // 초기 상태에 따라 적절한 뷰를 설정합니다. + showSplashScreen() + return currentView + } + + private func showSplashScreen() { + currentView = AnyView(SplashView(coordinator: self)) // 코디네이터 주입 + + /// 스플래쉬에서 토큰 검사 과정 (혹은 홈뷰 API 호출) 로 로그인 필요 여부 확인 + } + + func transitionToNextView() { + switch UserManager.shared.appState { + case .onboarding: + currentView = AnyView(OnboardingContentView()) + case .onboardingComplete: + currentView = AnyView(OnboardingCompleteView()) + case .servicePrepare: + currentView = AnyView(ServicePrepareView()) + case .home: + startTabBar() + case .login: + startLogin() + } + } + + func startLogin() { + let authCoordinator = AuthCoordinator(parentCoordinator: self, navigationPath: navigationPath) + currentView = authCoordinator.start() + } + + func startTabBar() { + let tabBarCoordinator = TabBarCoordinator(parentCoordinator: self, navigationPath: self.navigationPath) + currentView = tabBarCoordinator.start() + } +} diff --git a/HMH_Tuist_iOS/Projects/App/Sources/Coordinator/BaseCoordinator.swift b/HMH_Tuist_iOS/Projects/App/Sources/Coordinator/BaseCoordinator.swift new file mode 100644 index 00000000..1b457c7e --- /dev/null +++ b/HMH_Tuist_iOS/Projects/App/Sources/Coordinator/BaseCoordinator.swift @@ -0,0 +1,38 @@ +// +// BaseCoordinator.swift +// Coordinator +// +// Created by 이지희 on 11/7/24. +// Copyright © 2024 HMH-iOS. All rights reserved. +// +import SwiftUI +import Combine + +import Core + +// MARK: - BaseCoordinator 프로토콜 +public protocol CoordinatorType: AnyObject { + + var parentCoordinator: (any CoordinatorType)? { get set } + var navigationPath: NavigationPath { get set } + + func start() -> AnyView + + func push(to view: any Hashable) + func pop() + func popToRoot() +} + +extension CoordinatorType { + func push(to view: any Hashable) { + navigationPath.append(view) + } + + func pop() { + navigationPath.removeLast() + } + + func popToRoot() { + navigationPath.removeLast(navigationPath.count) + } +}