Skip to content

Commit

Permalink
♻️ Tree 구조로 변경하여 Coordinator 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
yoondj98 committed Nov 30, 2023
1 parent 9605250 commit ea53e79
Show file tree
Hide file tree
Showing 15 changed files with 169 additions and 203 deletions.
4 changes: 2 additions & 2 deletions iOS/MusicSpot/MusicSpot.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@
08CBF8712B18468E007D3797 /* AppCoordinator.swift */,
08CBF8732B18468E007D3797 /* HomeMapCoordinator.swift */,
08CBF8752B18468E007D3797 /* SpotCoordinator.swift */,
08CBF8722B18468E007D3797 /* RewindCoordinator.swift */,
08CBF8742B18468E007D3797 /* SettingCoordinator.swift */,
08CBF8762B18468E007D3797 /* SearchMusicCoordinator.swift */,
08CBF8702B18468E007D3797 /* SaveJourneyCoordinator.swift */,
08CBF8742B18468E007D3797 /* SettingCoordinator.swift */,
08CBF8722B18468E007D3797 /* RewindCoordinator.swift */,
);
path = MSCoordinator;
sourceTree = "<group>";
Expand Down
122 changes: 11 additions & 111 deletions iOS/MusicSpot/MusicSpot/MSCoordinator/AppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

import UIKit

protocol AppCoordinatorDelegate {
func popToHomeMap(from coordinator: Coordinator)
func popToSearchMusic(from coordinator: Coordinator)
}

final class AppCoordinator: Coordinator {

// MARK: - Properties
Expand All @@ -25,129 +30,24 @@ final class AppCoordinator: Coordinator {

func start() {
let homeMapCoordinator = HomeMapCoordinator(navigationController: navigationController)
homeMapCoordinator.delegate = self
self.childCoordinators.append(homeMapCoordinator)
homeMapCoordinator.start()
}
}

extension AppCoordinator {

/// 현재 ViewController 아래에 깔린 ViewController 내에서 HomeMapViewController를 찾는 함수
private func findHomeMapViewController() {
guard let homeMapViewController = navigationController.viewControllers.first(where: { $0 is HomeMapViewController }) else {
navigationController.setViewControllers([HomeMapViewController()], animated: true)
return
}

navigationController.popToViewController(homeMapViewController, animated: true)
}
}

// MARK: - HomeMapCoordinatorDelegate

extension AppCoordinator: HomeMapCoordinatorDelegate {
extension AppCoordinator: AppCoordinatorDelegate {

/// From: HomeMap, To: Spot
func pushToSpot(coordinator: HomeMapCoordinator) {
let spotCoordinator = SpotCoordinator(navigationController: navigationController)
spotCoordinator.delegate = self

self.childCoordinators.append(spotCoordinator)
spotCoordinator.start()
}

/// From: HomeMap, To: Rewind
func pushToRewind(coordinator: HomeMapCoordinator) {
let rewindCoordinator = RewindCoordinator(navigationController: navigationController)
rewindCoordinator.delegate = self

self.childCoordinators.append(rewindCoordinator)
rewindCoordinator.start()
func popToHomeMap(from coordinator: Coordinator) {
self.childCoordinators.removeAll()
self.start()
}

/// From: HomeMap, To: Setting
func pushToSetting(coordinator: HomeMapCoordinator) {
let settingCoordinator = SettingCoordinator(navigationController: navigationController)
settingCoordinator.delegate = self

self.childCoordinators.append(settingCoordinator)
settingCoordinator.start()
}
}

// MARK: - SpotCoordinatorDelegate

extension AppCoordinator: SpotCoordinatorDelegate {

/// From: Spot, To: HomeMap
func popToHomeMap(coordinator: SpotCoordinator) {
removeChildCoordinator(child: coordinator)
navigationController.popViewController(animated: true)
}

/// From: Spot, To: SearchMusic
func pushToSearchMusic(coordinator: SpotCoordinator) {
func popToSearchMusic(from coordinator: Coordinator) {
self.childCoordinators.removeAll()
let searchMusicCoordinator = SearchMusicCoordinator(navigationController: navigationController)
searchMusicCoordinator.delegate = self
self.childCoordinators.append(searchMusicCoordinator)
searchMusicCoordinator.start()
}
}

// MARK: - SearchMusicCoordinatorDelegate

extension AppCoordinator: SearchMusicCoordinatorDelegate {

/// From: SearchMusic, To: HomeMap
func popToHomeMap(coordinator: SearchMusicCoordinator) {
findHomeMapViewController()
}

/// From: SearchMusic, To: SaveJourney
func pushToSaveJourney(coordinator: SearchMusicCoordinator) {
let saveJourneyCoordinator = SaveJourneyCoordinator(navigationController: navigationController)
saveJourneyCoordinator.delegate = self
self.childCoordinators.append(saveJourneyCoordinator)
saveJourneyCoordinator.start()
}
}

// MARK: - RewindCoordinatorDelegate

extension AppCoordinator: RewindCoordinatorDelegate {

/// From: Rewind, To: HomeMap
func popToHomeMap(coordinator: RewindCoordinator) {
removeChildCoordinator(child: coordinator)
navigationController.popViewController(animated: true)
}
}

// MARK: - SettingCoordinatorDelegate

extension AppCoordinator: SettingCoordinatorDelegate {

/// From: Setting, To: HomeMap
func popToHomeMap(coordinator: SettingCoordinator) {
navigationController.popViewController(animated: true)
}
}

// MARK: - SaveJourneyCoordinatorDelegate

extension AppCoordinator: SaveJourneyCoordinatorDelegate {

/// From: SaveJourney, To: HomeMap
func popToHomeMap(coordinator: SaveJourneyCoordinator) {
findHomeMapViewController()
}

/// From: SaveJourney, To: SearchMusic
func popToSearchMusic(coordinator: SaveJourneyCoordinator) {
let searchMusicCoordinator = SearchMusicCoordinator(navigationController: navigationController)
searchMusicCoordinator.delegate = self
self.childCoordinators.append(searchMusicCoordinator)
searchMusicCoordinator.start()
}
}
7 changes: 1 addition & 6 deletions iOS/MusicSpot/MusicSpot/MSCoordinator/Coordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,5 @@ import UIKit
protocol Coordinator: AnyObject {
var navigationController: UINavigationController { get }
var childCoordinators: [Coordinator] { get set }
}

extension Coordinator {
func removeChildCoordinator(child: Coordinator) {
childCoordinators.removeAll { $0 === child }
}

}
34 changes: 23 additions & 11 deletions iOS/MusicSpot/MusicSpot/MSCoordinator/HomeMapCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@

import UIKit

protocol HomeMapCoordinatorDelegate: AnyObject {
func pushToSpot(coordinator: HomeMapCoordinator)
func pushToRewind(coordinator: HomeMapCoordinator)
func pushToSetting(coordinator: HomeMapCoordinator)
}

final class HomeMapCoordinator: Coordinator, HomeMapViewControllerDelegate {

// MARK: - Properties
Expand All @@ -21,7 +15,7 @@ final class HomeMapCoordinator: Coordinator, HomeMapViewControllerDelegate {

var childCoordinators: [Coordinator] = []

var delegate: HomeMapCoordinatorDelegate?
var delegate: AppCoordinatorDelegate?

// MARK: - Initializer

Expand All @@ -34,18 +28,36 @@ final class HomeMapCoordinator: Coordinator, HomeMapViewControllerDelegate {
func start() {
let homeMapViewController = HomeMapViewController()
homeMapViewController.delegate = self
navigationController.pushViewController(homeMapViewController, animated: true)
self.navigationController.pushViewController(homeMapViewController, animated: true)
}

func goSpot() {
delegate?.pushToSpot(coordinator: self)
let spotCoordinator = SpotCoordinator(navigationController: navigationController)
self.childCoordinators.append(spotCoordinator)
spotCoordinator.start()
}

func goRewind() {
delegate?.pushToRewind(coordinator: self)
let rewindCoordinator = RewindCoordinator(navigationController: navigationController)
self.childCoordinators.append(rewindCoordinator)
rewindCoordinator.start()
}

func goSetting() {
delegate?.pushToSetting(coordinator: self)
let settingCoordinator = SettingCoordinator(navigationController: navigationController)
self.childCoordinators.append(settingCoordinator)
settingCoordinator.start()
}
}

extension HomeMapCoordinator: AppCoordinatorDelegate {

func popToHomeMap(from coordinator: Coordinator) {
self.start()
}

func popToSearchMusic(from coordinator: Coordinator) {
self.childCoordinators.removeAll()
self.delegate?.popToSearchMusic(from: self)
}
}
25 changes: 18 additions & 7 deletions iOS/MusicSpot/MusicSpot/MSCoordinator/RewindCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@

import UIKit

protocol RewindCoordinatorDelegate: AnyObject {
func popToHomeMap(coordinator: RewindCoordinator)
}

final class RewindCoordinator: Coordinator, RewindViewControllerDelegate {

// MARK: - Properties
Expand All @@ -19,7 +15,7 @@ final class RewindCoordinator: Coordinator, RewindViewControllerDelegate {

var childCoordinators: [Coordinator] = []

var delegate: RewindCoordinatorDelegate?
var delegate: AppCoordinatorDelegate?

// MARK: - Initializer

Expand All @@ -32,10 +28,25 @@ final class RewindCoordinator: Coordinator, RewindViewControllerDelegate {
func start() {
let rewindViewController = RewindViewController()
rewindViewController.delegate = self
navigationController.pushViewController(rewindViewController, animated: true)
self.navigationController.pushViewController(rewindViewController, animated: true)
}

func goHomeMap() {
delegate?.popToHomeMap(coordinator: self)
self.delegate?.popToHomeMap(from: self)
}

}

extension RewindCoordinator: AppCoordinatorDelegate {

func popToHomeMap(from coordinator: Coordinator) {
self.childCoordinators.removeAll()
self.delegate?.popToHomeMap(from: self)
}

func popToSearchMusic(from coordinator: Coordinator) {
self.childCoordinators.removeAll()
self.delegate?.popToSearchMusic(from: self)
}

}
28 changes: 19 additions & 9 deletions iOS/MusicSpot/MusicSpot/MSCoordinator/SaveJourneyCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@

import UIKit

protocol SaveJourneyCoordinatorDelegate: AnyObject {
func popToHomeMap(coordinator: SaveJourneyCoordinator)
func popToSearchMusic(coordinator: SaveJourneyCoordinator)
}

final class SaveJourneyCoordinator: Coordinator, SaveJourneyViewControllerDelegate {

// MARK: - Properties
Expand All @@ -20,7 +15,7 @@ final class SaveJourneyCoordinator: Coordinator, SaveJourneyViewControllerDelega

var childCoordinators: [Coordinator] = []

var delegate: SaveJourneyCoordinatorDelegate?
var delegate: AppCoordinatorDelegate?

// MARK: - Initializer

Expand All @@ -33,14 +28,29 @@ final class SaveJourneyCoordinator: Coordinator, SaveJourneyViewControllerDelega
func start() {
let saveJourneyViewController = SaveJourneyViewController()
saveJourneyViewController.delegate = self
navigationController.pushViewController(saveJourneyViewController, animated: true)
self.navigationController.pushViewController(saveJourneyViewController, animated: true)
}

func goHomeMap() {
delegate?.popToHomeMap(coordinator: self)
self.delegate?.popToHomeMap(from: self)
}

func goSearchMusic() {
delegate?.popToSearchMusic(coordinator: self)
self.delegate?.popToSearchMusic(from: self)
}

}

extension SaveJourneyCoordinator: AppCoordinatorDelegate {

func popToHomeMap(from coordinator: Coordinator) {
self.childCoordinators.removeAll()
self.delegate?.popToHomeMap(from: self)
}

func popToSearchMusic(from coordinator: Coordinator) {
self.childCoordinators.removeAll()
self.delegate?.popToSearchMusic(from: self)
}

}
29 changes: 20 additions & 9 deletions iOS/MusicSpot/MusicSpot/MSCoordinator/SearchMusicCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@

import UIKit

protocol SearchMusicCoordinatorDelegate: AnyObject {
func popToHomeMap(coordinator: SearchMusicCoordinator)
func pushToSaveJourney(coordinator: SearchMusicCoordinator)
}

final class SearchMusicCoordinator: Coordinator, SearchMusicViewControllerDelegate {

// MARK: - Properties
Expand All @@ -20,7 +15,7 @@ final class SearchMusicCoordinator: Coordinator, SearchMusicViewControllerDelega

var childCoordinators: [Coordinator] = []

var delegate: SearchMusicCoordinatorDelegate?
var delegate: AppCoordinatorDelegate?

// MARK: - Initializer

Expand All @@ -33,14 +28,30 @@ final class SearchMusicCoordinator: Coordinator, SearchMusicViewControllerDelega
func start() {
let searchMusicViewController = SearchMusicViewController()
searchMusicViewController.delegate = self
navigationController.pushViewController(searchMusicViewController, animated: true)
self.navigationController.pushViewController(searchMusicViewController, animated: true)
}

func goHomeMap() {
delegate?.popToHomeMap(coordinator: self)
self.delegate?.popToHomeMap(from: self)
}

func goSaveJourney() {
delegate?.pushToSaveJourney(coordinator: self)
let saveJourneyCoordinator = SaveJourneyCoordinator(navigationController: navigationController)
self.childCoordinators.append(saveJourneyCoordinator)
saveJourneyCoordinator.start()
}

}

extension SearchMusicCoordinator: AppCoordinatorDelegate {

func popToHomeMap(from coordinator: Coordinator) {
self.childCoordinators.removeAll()
self.delegate?.popToHomeMap(from: self)
}

func popToSearchMusic(from coordinator: Coordinator) {
self.start()
}

}
Loading

0 comments on commit ea53e79

Please sign in to comment.