Skip to content

Commit

Permalink
[GWL-40] Root Coordinator 세팅 (#48)
Browse files Browse the repository at this point in the history
* fix: tuist generate가 되지않는 현상 해결

Feature프로젝트에 Sources, Resources, Tests 폴더가 형성되지 않아 발생하는 버그를 수정하였습니다.

* chore: APP단 Coordinator관련 파일 추가 및 폴더링

* feat: AppCoordinator 구성

* feat: TabBarCoordinator 구현

예제

* build: Coordinator 필수 구성요소 모듈화

* chrow: 리뷰 적용

앱코디네이터에서 탭바코디네이터 자식 추가, 탭바 코디네이터 selectedImage 작성
  • Loading branch information
JongPyoAhn authored Nov 16, 2023
1 parent 1dda640 commit 58fa5d9
Show file tree
Hide file tree
Showing 24 changed files with 449 additions and 1 deletion.
1 change: 1 addition & 0 deletions iOS/Projects/App/Project.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ let project = Project.makeModule(
product: .app,
dependencies: [
ProjectTargetDependency.Record,
ProjectTargetDependency.Coordinator,
],
resources: ["Resources/**"],
infoPlist: .extendingDefault(
Expand Down
28 changes: 28 additions & 0 deletions iOS/Projects/App/Sources/AScene/Coordinator/ACoordinator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// ACoordinator.swift
// WeTri
//
// Created by 안종표 on 2023/11/15.
// Copyright © 2023 kr.codesquad.boostcamp8. All rights reserved.
//

import Coordinator
import UIKit

final class ACoordinator: ACoordinating {
var navigationController: UINavigationController
var childCoordinators: [Coordinating] = []
weak var finishDelegate: CoordinatorFinishDelegate?
var flow: CoordinatorFlow = .tabBar

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

func start() {
let viewController = AViewController()
navigationController.pushViewController(viewController, animated: false)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// ACoordinating.swift
// WeTri
//
// Created by 안종표 on 2023/11/15.
// Copyright © 2023 kr.codesquad.boostcamp8. All rights reserved.
//

import Coordinator
import Foundation

protocol ACoordinating: Coordinating {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// AViewController.swift
// WeTri
//
// Created by 안종표 on 2023/11/15.
// Copyright © 2023 kr.codesquad.boostcamp8. All rights reserved.
//

import UIKit

class AViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()

view.backgroundColor = .blue
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ final class SceneDelegate: UIResponder, UIWindowSceneDelegate {

func scene(_ scene: UIScene, willConnectTo _: UISceneSession, options _: UIScene.ConnectionOptions) {
guard let windowScene = scene as? UIWindowScene else { return }
let navigationController = UINavigationController()
window = UIWindow(windowScene: windowScene)
window?.rootViewController = ViewController()
window?.rootViewController = navigationController
let coordinator = AppCoordinator(navigationController: navigationController)
coordinator.start()
window?.makeKeyAndVisible()
}
}
28 changes: 28 additions & 0 deletions iOS/Projects/App/Sources/BScene/Coordinator/BCoordinator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// BCoordinator.swift
// WeTri
//
// Created by 안종표 on 2023/11/15.
// Copyright © 2023 kr.codesquad.boostcamp8. All rights reserved.
//

import Coordinator
import UIKit

final class BCoordinator: BCoordinating {
var navigationController: UINavigationController
var childCoordinators: [Coordinating] = []
weak var finishDelegate: CoordinatorFinishDelegate?
var flow: CoordinatorFlow = .tabBar

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

func start() {
let viewController = BViewController()
navigationController.pushViewController(viewController, animated: false)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// BCoordinating.swift
// WeTri
//
// Created by 안종표 on 2023/11/15.
// Copyright © 2023 kr.codesquad.boostcamp8. All rights reserved.
//

import Coordinator
import Foundation

protocol BCoordinating: Coordinating {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// BViewController.swift
// WeTri
//
// Created by 안종표 on 2023/11/15.
// Copyright © 2023 kr.codesquad.boostcamp8. All rights reserved.
//

import UIKit

class BViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()

view.backgroundColor = .green
}
}
28 changes: 28 additions & 0 deletions iOS/Projects/App/Sources/CScene/Coordinator/CCoordinator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// CCoordinator.swift
// WeTri
//
// Created by 안종표 on 2023/11/15.
// Copyright © 2023 kr.codesquad.boostcamp8. All rights reserved.
//

import Coordinator
import UIKit

final class CCoordinator: CCoordinating {
var navigationController: UINavigationController
var childCoordinators: [Coordinating] = []
weak var finishDelegate: CoordinatorFinishDelegate?
var flow: CoordinatorFlow = .tabBar

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

func start() {
let viewController = CViewController()
navigationController.pushViewController(viewController, animated: false)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// CCoordinating.swift
// WeTri
//
// Created by 안종표 on 2023/11/15.
// Copyright © 2023 kr.codesquad.boostcamp8. All rights reserved.
//

import Coordinator
import Foundation

protocol CCoordinating: Coordinating {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// CViewController.swift
// WeTri
//
// Created by 안종표 on 2023/11/15.
// Copyright © 2023 kr.codesquad.boostcamp8. All rights reserved.
//

import UIKit

class CViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// AppCoordinator.swift
// WeTri
//
// Created by 안종표 on 2023/11/15.
// Copyright © 2023 kr.codesquad.boostcamp8. All rights reserved.
//

import Coordinator
import UIKit

final class AppCoordinator: AppCoordinating {
var navigationController: UINavigationController
var childCoordinators: [Coordinating] = []
weak var finishDelegate: CoordinatorFinishDelegate?
var flow: CoordinatorFlow = .tabBar

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

func start() {
// (LoginFlow와 TabBarFlow 분기 처리) (todo)
let tabBarCoordinator = TabBarCoordinator(navigationController: navigationController)
childCoordinators.append(tabBarCoordinator)
tabBarCoordinator.start()
}

func startLoginFlow() {
// (LoginViewController 추가되면 로직 추가) (todo)
}

func startTabBarFlow() {
// (TabBarController 추가되면 로직 추가) (todo)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// AppCoordinating.swift
// WeTri
//
// Created by 안종표 on 2023/11/15.
// Copyright © 2023 kr.codesquad.boostcamp8. All rights reserved.
//

import Coordinator
import Foundation

protocol AppCoordinating: Coordinating {
func startLoginFlow()
func startTabBarFlow()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// TabBarCoordinating.swift
// WeTri
//
// Created by 안종표 on 2023/11/15.
// Copyright © 2023 kr.codesquad.boostcamp8. All rights reserved.
//

import Coordinator
import UIKit

protocol TabBarCoordinating: Coordinating {
var tabBarController: UITabBarController { get }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
//
// TabBarCoordinator.swift
// WeTri
//
// Created by 안종표 on 2023/11/15.
// Copyright © 2023 kr.codesquad.boostcamp8. All rights reserved.
//

import Coordinator
import UIKit

// MARK: - TabBarCoordinator

final class TabBarCoordinator: TabBarCoordinating {
var navigationController: UINavigationController
var childCoordinators: [Coordinating] = []
weak var finishDelegate: CoordinatorFinishDelegate?
var flow: CoordinatorFlow = .tabBar
var tabBarController: UITabBarController

init(
navigationController: UINavigationController,
tabBarController: UITabBarController = UITabBarController()
) {
self.navigationController = navigationController
self.tabBarController = tabBarController
}

func start() {
let tabBarViewControllers = TabBarPage.allCases.map {
return makePageNavigationController(page: $0)
}
let tabBarController = makeTabBarController(tabBarViewControllers: tabBarViewControllers)
navigationController.pushViewController(tabBarController, animated: false)
}

private func startTabBarCoordinator(page: TabBarPage, pageNavigationViewController: UINavigationController) {
switch page {
case .home:
let homeCoordinator = ACoordinator(navigationController: pageNavigationViewController)
childCoordinators.append(homeCoordinator)
homeCoordinator.finishDelegate = self
homeCoordinator.start()
case .record:
let recordCoordinator = BCoordinator(navigationController: pageNavigationViewController)
childCoordinators.append(recordCoordinator)
recordCoordinator.finishDelegate = self
recordCoordinator.start()
case .profile:
let profileCoordinator = CCoordinator(navigationController: pageNavigationViewController)
childCoordinators.append(profileCoordinator)
profileCoordinator.finishDelegate = self
profileCoordinator.start()
}
}

private func makePageNavigationController(page: TabBarPage) -> UINavigationController {
let navigationController = UINavigationController()
let tabBarItem = UITabBarItem(title: page.title, image: page.image, selectedImage: page.selectedImage)
navigationController.tabBarItem = tabBarItem
startTabBarCoordinator(page: page, pageNavigationViewController: navigationController)
return navigationController
}

private func makeTabBarController(
tabBarViewControllers: [UIViewController]
) -> UITabBarController {
tabBarController.setViewControllers(tabBarViewControllers, animated: false)
return tabBarController
}
}

// MARK: CoordinatorFinishDelegate

extension TabBarCoordinator: CoordinatorFinishDelegate {
func flowDidFinished(childCoordinator: Coordinating) {
childCoordinators = childCoordinators.filter {
return $0.flow != childCoordinator.flow
}
}
}

// MARK: - TabBarPage

private enum TabBarPage: CaseIterable {
case home
case record
case profile
}

private extension TabBarPage {
var title: String {
switch self {
case .home:
return ""
case .record:
return "기록"
case .profile:
return "프로필"
}
}

var image: UIImage? {
switch self {
case .home:
return UIImage(systemName: "house")
case .record:
return UIImage(systemName: "star")
case .profile:
return UIImage(systemName: "person")
}
}

var selectedImage: UIImage? {
switch self {
case .home:
return UIImage(systemName: "house.fill")
case .record:
return UIImage(systemName: "star.fill")
case .profile:
return UIImage(systemName: "person.fill")
}
}
}
Loading

0 comments on commit 58fa5d9

Please sign in to comment.