From 29a9d889846080ba8c993acedf4f65b9d73154fc Mon Sep 17 00:00:00 2001 From: MINJAE KIM Date: Tue, 17 Oct 2023 12:19:59 +0900 Subject: [PATCH] =?UTF-8?q?[FEAT]=20ChallengeCoordinator=EC=97=90=20Adapto?= =?UTF-8?q?r=20Pattern=20=EC=A0=81=EC=9A=A9=20(#155)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Scenes/Adaptor/ChallengeAdaptor.swift | 22 +++++++++ .../ChallengeViewController.swift | 12 +++-- .../ChallengeViewControllerable.swift | 2 +- .../ChallengeCoordinatorImpl.swift | 48 ++++++++++++++++++- .../ChallengeCoordinator.swift | 41 ++-------------- .../TabbbarCoordinator.swift | 2 +- .../FactoryImpl/ChallengeFactoryImpl.swift | 4 +- .../FactoryInterface/ChallengeFactory.swift | 2 +- .../ChallengeNavigation.swift | 2 +- 9 files changed, 87 insertions(+), 48 deletions(-) diff --git a/LionHeart-iOS/LionHeart-iOS/Scenes/Adaptor/ChallengeAdaptor.swift b/LionHeart-iOS/LionHeart-iOS/Scenes/Adaptor/ChallengeAdaptor.swift index 9f25873d..ed0a7420 100644 --- a/LionHeart-iOS/LionHeart-iOS/Scenes/Adaptor/ChallengeAdaptor.swift +++ b/LionHeart-iOS/LionHeart-iOS/Scenes/Adaptor/ChallengeAdaptor.swift @@ -6,3 +6,25 @@ // import Foundation + + +final class ChallengeAdaptor: ChallengeNavigation { + + let coordinator: ChallengeCoordinator + + init(coordinator: ChallengeCoordinator) { + self.coordinator = coordinator + } + + func navigationRightButtonTapped() { + self.coordinator.showMypageViewController() + } + + func navigationLeftButtonTapped() { + self.coordinator.showBookmarkViewController() + } + + func checkTokenIsExpired() { + self.coordinator.exitApplication() + } +} diff --git a/LionHeart-iOS/LionHeart-iOS/Scenes/Challenge/ViewController/ChallengeViewController.swift b/LionHeart-iOS/LionHeart-iOS/Scenes/Challenge/ViewController/ChallengeViewController.swift index 1838da12..e407c9ba 100644 --- a/LionHeart-iOS/LionHeart-iOS/Scenes/Challenge/ViewController/ChallengeViewController.swift +++ b/LionHeart-iOS/LionHeart-iOS/Scenes/Challenge/ViewController/ChallengeViewController.swift @@ -13,7 +13,8 @@ import Lottie final class ChallengeViewController: UIViewController, ChallengeViewControllerable { - weak var coordinator: ChallengeNavigation? + + var navigator: ChallengeNavigation private var manager: ChallengeManager private let leftSeperateLine = LHUnderLine(lineColor: .background) @@ -33,8 +34,9 @@ final class ChallengeViewController: UIViewController, ChallengeViewControllerab } } - init(manager: ChallengeManager) { + init(manager: ChallengeManager, navigator: ChallengeNavigation) { self.manager = manager + self.navigator = navigator super.init(nibName: nil, bundle: nil) } @@ -173,11 +175,11 @@ private extension ChallengeViewController { func setAddTarget() { navigationBar.rightFirstBarItemAction { - self.coordinator?.navigationLeftButtonTapped() + self.navigator.navigationLeftButtonTapped() } navigationBar.rightSecondBarItemAction { - self.coordinator?.navigationRightButtonTapped() + self.navigator.navigationRightButtonTapped() } } } @@ -232,7 +234,7 @@ extension ChallengeViewController: ViewControllerServiceable { case .fetchImageError: LHToast.show(message: "챌린지 이미지 패치 에러") case .unAuthorizedError: - LHToast.show(message: "챌린지 Auth 에러") + self.navigator.checkTokenIsExpired() case .clientError(_, let message): LHToast.show(message: message) case .serverError: diff --git a/LionHeart-iOS/LionHeart-iOS/Scenes/Coordinator/ControllerableInterface/ChallengeViewControllerable.swift b/LionHeart-iOS/LionHeart-iOS/Scenes/Coordinator/ControllerableInterface/ChallengeViewControllerable.swift index a29f73ce..32ed2574 100644 --- a/LionHeart-iOS/LionHeart-iOS/Scenes/Coordinator/ControllerableInterface/ChallengeViewControllerable.swift +++ b/LionHeart-iOS/LionHeart-iOS/Scenes/Coordinator/ControllerableInterface/ChallengeViewControllerable.swift @@ -8,5 +8,5 @@ import UIKit protocol ChallengeViewControllerable where Self: UIViewController { - var coordinator: ChallengeNavigation? { get set } + var navigator: ChallengeNavigation { get set } } diff --git a/LionHeart-iOS/LionHeart-iOS/Scenes/Coordinator/CoordinatorImpl/ChallengeCoordinatorImpl.swift b/LionHeart-iOS/LionHeart-iOS/Scenes/Coordinator/CoordinatorImpl/ChallengeCoordinatorImpl.swift index 6d5834a8..95a2b5dc 100644 --- a/LionHeart-iOS/LionHeart-iOS/Scenes/Coordinator/CoordinatorImpl/ChallengeCoordinatorImpl.swift +++ b/LionHeart-iOS/LionHeart-iOS/Scenes/Coordinator/CoordinatorImpl/ChallengeCoordinatorImpl.swift @@ -5,4 +5,50 @@ // Created by 김민재 on 10/17/23. // -import Foundation +import UIKit + + +final class ChallengeCoordinatorImpl: ChallengeCoordinator { + + weak var parentCoordinator: Coordinator? + private let factory: ChallengeFactory + var children: [Coordinator] = [] + + var navigationController: UINavigationController + + init(navigationController: UINavigationController, factory: ChallengeFactory) { + self.navigationController = navigationController + self.factory = factory + } + + func start() { + showChallengeViewController() + } + + func showChallengeViewController() { + let adaptor = ChallengeAdaptor(coordinator: self) + let challengeViewController = factory.makeChallengeViewController(adaptor: adaptor) + navigationController.pushViewController(challengeViewController, animated: true) + } + + func showMypageViewController() { + let mypageCoordinator = MypageCoordinator(navigationController: navigationController, + factory: MyPageFactoryImpl()) + mypageCoordinator.start() + children.append(mypageCoordinator) + } + + func showBookmarkViewController() { + let bookmakrFactory = BookmarkFactoryImpl() + let bookmarkCoordinator = BookmarkCoordinatorImpl(navigationController: navigationController, factory: bookmakrFactory) + bookmarkCoordinator.start() + children.append(bookmarkCoordinator) + } + + func exitApplication() { + UIApplication.shared.perform(#selector(NSXPCConnection.suspend)) + DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { + exit(0) + } + } +} diff --git a/LionHeart-iOS/LionHeart-iOS/Scenes/Coordinator/CoordinatorInterface/ChallengeCoordinator.swift b/LionHeart-iOS/LionHeart-iOS/Scenes/Coordinator/CoordinatorInterface/ChallengeCoordinator.swift index 184eb847..e2bc6e5c 100644 --- a/LionHeart-iOS/LionHeart-iOS/Scenes/Coordinator/CoordinatorInterface/ChallengeCoordinator.swift +++ b/LionHeart-iOS/LionHeart-iOS/Scenes/Coordinator/CoordinatorInterface/ChallengeCoordinator.swift @@ -7,41 +7,10 @@ import UIKit -final class ChallengeCoordinator: Coordinator { - weak var parentCoordinator: Coordinator? - private let factory: ChallengeFactory - var children: [Coordinator] = [] - - var navigationController: UINavigationController - - init(navigationController: UINavigationController, factory: ChallengeFactory) { - self.navigationController = navigationController - self.factory = factory - } - - func start() { - showChallengeViewController() - } - - func showChallengeViewController() { - let challengeViewController = factory.makeChallengeViewController() - challengeViewController.coordinator = self - navigationController.pushViewController(challengeViewController, animated: true) - } -} -extension ChallengeCoordinator: ChallengeNavigation { - func navigationRightButtonTapped() { - let mypageCoordinator = MypageCoordinator(navigationController: navigationController, - factory: MyPageFactoryImpl()) - mypageCoordinator.start() - children.append(mypageCoordinator) - } - - func navigationLeftButtonTapped() { - let bookmakrFactory = BookmarkFactoryImpl() - let bookmarkCoordinator = BookmarkCoordinator(navigationController: navigationController, factory: bookmakrFactory) - bookmarkCoordinator.start() - children.append(bookmarkCoordinator) - } +protocol ChallengeCoordinator: Coordinator { + func showChallengeViewController() + func showMypageViewController() + func showBookmarkViewController() + func exitApplication() } diff --git a/LionHeart-iOS/LionHeart-iOS/Scenes/Coordinator/CoordinatorInterface/TabbbarCoordinator.swift b/LionHeart-iOS/LionHeart-iOS/Scenes/Coordinator/CoordinatorInterface/TabbbarCoordinator.swift index 4f4ba53e..00b2c761 100644 --- a/LionHeart-iOS/LionHeart-iOS/Scenes/Coordinator/CoordinatorInterface/TabbbarCoordinator.swift +++ b/LionHeart-iOS/LionHeart-iOS/Scenes/Coordinator/CoordinatorInterface/TabbbarCoordinator.swift @@ -44,7 +44,7 @@ final class TabbarCoordinator: Coordinator { curriculumCoordinator.parentCoordinator = parentCoordinator let challengeNavigationController = UINavigationController() - let challengeCoordinator = ChallengeCoordinator(navigationController: challengeNavigationController, factory: ChallengeFactoryImpl()) + let challengeCoordinator = ChallengeCoordinatorImpl(navigationController: challengeNavigationController, factory: ChallengeFactoryImpl()) challengeCoordinator.parentCoordinator = parentCoordinator challengeNavigationController.tabBarItem = UITabBarItem(title: "챌린지", image: .assetImage(.challenge), tag: 3) diff --git a/LionHeart-iOS/LionHeart-iOS/Scenes/Factory/FactoryImpl/ChallengeFactoryImpl.swift b/LionHeart-iOS/LionHeart-iOS/Scenes/Factory/FactoryImpl/ChallengeFactoryImpl.swift index 706e46b7..ce75aa21 100644 --- a/LionHeart-iOS/LionHeart-iOS/Scenes/Factory/FactoryImpl/ChallengeFactoryImpl.swift +++ b/LionHeart-iOS/LionHeart-iOS/Scenes/Factory/FactoryImpl/ChallengeFactoryImpl.swift @@ -8,7 +8,7 @@ import Foundation struct ChallengeFactoryImpl: ChallengeFactory { - func makeChallengeViewController() -> ChallengeViewControllerable { - return ChallengeViewController(manager: ChallengeManagerImpl(challengeService: ChallengeServiceImpl(apiService: APIService()))) + func makeChallengeViewController(adaptor: ChallengeAdaptor) -> ChallengeViewControllerable { + return ChallengeViewController(manager: ChallengeManagerImpl(challengeService: ChallengeServiceImpl(apiService: APIService())), navigator: adaptor) } } diff --git a/LionHeart-iOS/LionHeart-iOS/Scenes/Factory/FactoryInterface/ChallengeFactory.swift b/LionHeart-iOS/LionHeart-iOS/Scenes/Factory/FactoryInterface/ChallengeFactory.swift index 7bfbfe9f..e61aa71f 100644 --- a/LionHeart-iOS/LionHeart-iOS/Scenes/Factory/FactoryInterface/ChallengeFactory.swift +++ b/LionHeart-iOS/LionHeart-iOS/Scenes/Factory/FactoryInterface/ChallengeFactory.swift @@ -8,5 +8,5 @@ import Foundation protocol ChallengeFactory { - func makeChallengeViewController() -> ChallengeViewControllerable + func makeChallengeViewController(adaptor: ChallengeAdaptor) -> ChallengeViewControllerable } diff --git a/LionHeart-iOS/LionHeart-iOS/Scenes/NavigationInterface/ChallengeNavigation.swift b/LionHeart-iOS/LionHeart-iOS/Scenes/NavigationInterface/ChallengeNavigation.swift index e73e3feb..2a29bcd8 100644 --- a/LionHeart-iOS/LionHeart-iOS/Scenes/NavigationInterface/ChallengeNavigation.swift +++ b/LionHeart-iOS/LionHeart-iOS/Scenes/NavigationInterface/ChallengeNavigation.swift @@ -7,4 +7,4 @@ import Foundation -protocol ChallengeNavigation: BarNavigation {} +protocol ChallengeNavigation: BarNavigation, ExpireNavigation {}