From 2f291f1e8e6aeae0d18e81ecc2e0dbdcc2ec328a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=95=ED=9A=A8=EC=A4=80?= Date: Sun, 1 Dec 2024 21:10:17 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20SceneDelegate=20=EB=A1=9C=EC=A7=81?= =?UTF-8?q?=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Source/App/SceneDelegate.swift | 68 +++++++++++++------ 1 file changed, 49 insertions(+), 19 deletions(-) diff --git a/MemorialHouse/MHApplication/MHApplication/Source/App/SceneDelegate.swift b/MemorialHouse/MHApplication/MHApplication/Source/App/SceneDelegate.swift index 880209e1..f87c56e3 100644 --- a/MemorialHouse/MHApplication/MHApplication/Source/App/SceneDelegate.swift +++ b/MemorialHouse/MHApplication/MHApplication/Source/App/SceneDelegate.swift @@ -17,28 +17,58 @@ final class SceneDelegate: UIResponder, UIWindowSceneDelegate { window = UIWindow(windowScene: windowScene) registerDependency() - guard let createMemorialHouseNameUseCase = try? DIContainer.shared.resolve(CreateMemorialHouseNameUseCase.self) else { - return + let initialViewController = createInitialViewController() + window?.rootViewController = UINavigationController(rootViewController: initialViewController) + window?.makeKeyAndVisible() + } + + private func createInitialViewController() -> UIViewController { + if isUserRegistered() { + return createHomeViewController() + } else { + return createRegisterViewController() } - let registerViewModelFactory = RegisterViewModelFactory(createMemorialHouseNameUseCase: createMemorialHouseNameUseCase) - let registerViewModel = registerViewModelFactory.make() - var initialViewController: UIViewController = RegisterViewController(viewModel: registerViewModel) - if UserDefaults.standard.object(forKey: Constant.houseNameUserDefaultKey) != nil { - do { - let homeViewModelFactory = try DIContainer.shared.resolve(HomeViewModelFactory.self) - let homeViewModel = homeViewModelFactory.make() - initialViewController = HomeViewController(viewModel: homeViewModel) - } catch { - MHLogger.error(error.localizedDescription) - } + } + + private func isUserRegistered() -> Bool { + return UserDefaults.standard.object(forKey: Constant.houseNameUserDefaultKey) != nil + } + + private func createHomeViewController() -> UIViewController { + do { + let homeViewModelFactory = try DIContainer.shared.resolve(HomeViewModelFactory.self) + let homeViewModel = homeViewModelFactory.make() + return HomeViewController(viewModel: homeViewModel) + } catch { + MHLogger.error("HomeViewModelFactory 해제 실패: \(error.localizedDescription)") + return createErrorViewController() } - - let navigationController = UINavigationController(rootViewController: initialViewController) - window?.rootViewController = navigationController - window?.makeKeyAndVisible() } - func registerDependency() { + private func createRegisterViewController() -> UIViewController { + do { + let registerViewModelFactory = try DIContainer.shared.resolve(RegisterViewModelFactory.self) + let registerViewModel = registerViewModelFactory.make() + return RegisterViewController(viewModel: registerViewModel) + } catch { + MHLogger.error("CreateMemorialHouseNameUseCase 해제 실패: \(error.localizedDescription)") + return createErrorViewController() + } + } + + private func createErrorViewController() -> UIViewController { + let errorViewController = UIViewController() + errorViewController.view.backgroundColor = .systemRed + let label = UILabel() + label.text = "오류가 발생했습니다." + label.textColor = .white + label.textAlignment = .center + label.frame = errorViewController.view.bounds + errorViewController.view.addSubview(label) + return errorViewController + } + + private func registerDependency() { do { try registerStorageDepedency() try registerRepositoryDependency() @@ -53,7 +83,7 @@ final class SceneDelegate: UIResponder, UIWindowSceneDelegate { private func registerStorageDepedency() throws { DIContainer.shared.register(CoreDataStorage.self, object: CoreDataStorage()) - + let coreDataStorage = try DIContainer.shared.resolve(CoreDataStorage.self) DIContainer.shared.register( BookCategoryStorage.self,