Skip to content

Commit

Permalink
[FEAT] ChallengeCoordinator에 Adaptor Pattern 적용 (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
ffalswo2 committed Oct 17, 2023
1 parent d642175 commit 29a9d88
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 48 deletions.
22 changes: 22 additions & 0 deletions LionHeart-iOS/LionHeart-iOS/Scenes/Adaptor/ChallengeAdaptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}

Expand Down Expand Up @@ -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()
}
}
}
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
import UIKit

protocol ChallengeViewControllerable where Self: UIViewController {
var coordinator: ChallengeNavigation? { get set }
var navigator: ChallengeNavigation { get set }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
import Foundation

protocol ChallengeFactory {
func makeChallengeViewController() -> ChallengeViewControllerable
func makeChallengeViewController(adaptor: ChallengeAdaptor) -> ChallengeViewControllerable
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@

import Foundation

protocol ChallengeNavigation: BarNavigation {}
protocol ChallengeNavigation: BarNavigation, ExpireNavigation {}

0 comments on commit 29a9d88

Please sign in to comment.