Moon-Duck-Org / MoonDuckFE-iOS

B-Side 405 ํฌํ…๋ฐ์ด - Moon Duck iOS
0 stars 0 forks source link

[REFACTOR} Coordinator ๊ตฌ์กฐ ๊ฐœ์„  #19

Open SuniDev opened 4 months ago

SuniDev commented 4 months ago

๐Ÿ’ก ์š”์•ฝ

[์ฐธ๊ณ ]

AuthCoordinator์—์„œ MainCoordinator๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์€ ์‚ฌ์šฉ์ž ์ธ์ฆ์ด ์™„๋ฃŒ๋œ ํ›„ ๋ฉ”์ธ ์•ฑ ํ๋ฆ„์œผ๋กœ ์ „ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด ์ž‘์—…์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‹จ๊ณ„๋ฅผ ํ†ตํ•ด ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

  1. ํ˜„์žฌ AuthCoordinator๋ฅผ ์ข…๋ฃŒํ•˜๊ณ ,
  2. ์ƒˆ๋กœ์šด MainCoordinator๋ฅผ ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ด๋ฅผ ์œ„ํ•ด์„œ๋Š” AuthCoordinator๊ฐ€ MainCoordinator๋กœ์˜ ์ „ํ™˜์„ ๊ด€๋ฆฌํ•˜๋Š” ์ƒ์œ„ Coordinator ๋˜๋Š” AppDelegate/SceneDelegate์™€ ๊ฐ™์€ ์ƒ์œ„ ๊ณ„์ธต์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ๊ฐ€์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

Step-by-Step Guide

1. Coordinator ํ”„๋กœํ† ์ฝœ ์ˆ˜์ • (์˜ต์…˜)

์ƒ์œ„ Coordinator์™€์˜ ์ƒํ˜ธ์ž‘์šฉ์„ ์œ„ํ•ด ๊ธฐ๋ณธ Coordinator ํ”„๋กœํ† ์ฝœ์— ๋ถ€๋ชจ Coordinator์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

protocol Coordinator {
    var navigationController: UINavigationController { get set }
    var parentCoordinator: Coordinator? { get set }
    func start()
}

2. AuthCoordinator ๊ตฌํ˜„

AuthCoordinator๋Š” ์ธ์ฆ์ด ์™„๋ฃŒ๋˜๋ฉด MainCoordinator๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

class AuthCoordinator: Coordinator {
    var navigationController: UINavigationController
    var parentCoordinator: Coordinator?

    init(navigationController: UINavigationController) {
        self.navigationController = navigationController
    }

    func start() {
        let loginViewController = LoginViewController()
        loginViewController.coordinator = self
        navigationController.pushViewController(loginViewController, animated: true)
    }

    func didFinishAuthentication() {
        parentCoordinator?.childDidFinish(self)
        let mainCoordinator = MainCoordinator(navigationController: navigationController)
        mainCoordinator.parentCoordinator = parentCoordinator
        parentCoordinator?.childDidStart(mainCoordinator)
        mainCoordinator.start()
    }
}

3. MainCoordinator ๊ตฌํ˜„

MainCoordinator๋Š” ๊ธฐ๋ณธ ๋„ค๋น„๊ฒŒ์ด์…˜ ํ๋ฆ„์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

class MainCoordinator: Coordinator {
    var navigationController: UINavigationController
    var parentCoordinator: Coordinator?

    init(navigationController: UINavigationController) {
        self.navigationController = navigationController
    }

    func start() {
        let viewController = MainViewController()
        viewController.coordinator = self
        navigationController.pushViewController(viewController, animated: true)
    }

    func showDetail() {
        let detailViewController = DetailViewController()
        navigationController.pushViewController(detailViewController, animated: true)
    }
}

4. RootCoordinator ๊ตฌํ˜„ (์ƒ์œ„ Coordinator)

์ƒ์œ„ Coordinator๋Š” ์•ฑ์˜ ์ „๋ฐ˜์ ์ธ ํ๋ฆ„์„ ๊ด€๋ฆฌํ•˜๊ณ , ์ž์‹ Coordinator๋ฅผ ์‹œ์ž‘ํ•˜๊ฑฐ๋‚˜ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.

class RootCoordinator: Coordinator {
    var navigationController: UINavigationController
    var childCoordinator: Coordinator?

    init(navigationController: UINavigationController) {
        self.navigationController = navigationController
    }

    func start() {
        showAuthentication()
    }

    func showAuthentication() {
        let authCoordinator = AuthCoordinator(navigationController: navigationController)
        authCoordinator.parentCoordinator = self
        childCoordinator = authCoordinator
        authCoordinator.start()
    }

    func showMain() {
        let mainCoordinator = MainCoordinator(navigationController: navigationController)
        mainCoordinator.parentCoordinator = self
        childCoordinator = mainCoordinator
        mainCoordinator.start()
    }

    func childDidFinish(_ child: Coordinator?) {
        if child === childCoordinator {
            childCoordinator = nil
        }
    }

    func childDidStart(_ child: Coordinator) {
        childCoordinator = child
    }
}

5. AppDelegate ๋˜๋Š” SceneDelegate์—์„œ RootCoordinator ์‚ฌ์šฉ

AppDelegate ๋˜๋Š” SceneDelegate์—์„œ RootCoordinator๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

import UIKit

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?
    var rootCoordinator: RootCoordinator?

    func scene(
        _ scene: UIScene,
        willConnectTo session: UISceneSession,
        options connectionOptions: UIScene.ConnectionOptions
    ) {
        guard let windowScene = (scene as? UIWindowScene) else { return }

        let navController = UINavigationController()
        window = UIWindow(windowScene: windowScene)
        window?.rootViewController = navController
        window?.makeKeyAndVisible()

        rootCoordinator = RootCoordinator(navigationController: navController)
        rootCoordinator?.start()
    }
}

์š”์•ฝ

์ด ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด AuthCoordinator์—์„œ MainCoordinator๋กœ ์ „ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋ฉด ์•ฑ์˜ ๋„ค๋น„๊ฒŒ์ด์…˜ ํ๋ฆ„์„ ๋ช…ํ™•ํ•˜๊ฒŒ ๊ด€๋ฆฌํ•˜๊ณ , ์ฝ”๋“œ์˜ ์œ ์ง€๋ณด์ˆ˜์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

SuniDev commented 3 months ago

๋‚ด๋น„๊ฒŒ์ด์…˜ ๋กœ์ง์„ ViewController์—์„œ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์€ ์ฝ”๋“œ์˜ ์œ ์ง€๋ณด์ˆ˜์„ฑ, ํ…Œ์ŠคํŠธ ์šฉ์ด์„ฑ, ํ™•์žฅ์„ฑ ๋“ฑ์„ ํฌ๊ฒŒ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ์Œ.