Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router를 정의했습니다. #17

Merged
merged 9 commits into from
Jul 23, 2024
93 changes: 93 additions & 0 deletions HomeCafeRecipes/HomeCafeRecipes/Router/Router.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//
// Router.swift
// HomeCafeRecipes
//
// Created by 김건호 on 7/9/24.
//

import UIKit

public typealias NavigationBackClosure = () -> Void

public protocol Drawable {
var viewController: UIViewController? { get }
}

public protocol RouterProtocol {
func push (
_ drawable: Drawable,
from viewController: UIViewController,
isAnimated: Bool,
onNavigateBack closure: NavigationBackClosure?
)
}

class Router: NSObject, RouterProtocol {
private var closures: [String: NavigationBackClosure] = [:]

func push(
_ drawable: Drawable,
from viewController: UIViewController,
isAnimated: Bool,
onNavigateBack closure: NavigationBackClosure?
) {
guard let targetViewController = drawable.viewController else {
return
}

if let closure = closure {
closures.updateValue(closure, forKey: targetViewController.description)
}
viewController.navigationController?.pushViewController(targetViewController, animated: isAnimated)
}

private func executeClosure(_ viewController: UIViewController) {
guard let closure = closures.removeValue(forKey: viewController.description) else { return }
closure()
}
}

extension Router {
func createRecipeListDependencies() -> RecipeListViewController {
let baseNetworkService = BaseNetworkService()
let recipeFetchService = RecipeFetchServiceImpl(networkService: baseNetworkService)
let feedListRepository = FeedListRepositoryImpl(networkService: recipeFetchService)
let searchFeedRepository = SearchFeedRepositoryImpl(networkService: recipeFetchService)
let fetchFeedListUseCase = FetchFeedListUseCaseImpl(repository: feedListRepository)
let searchFeedListUseCase = SearchFeedListUseCaseImpl(repository: searchFeedRepository)
let recipeListInteractor = RecipeListInteractorImpl(
fetchFeedListUseCase: fetchFeedListUseCase,
searchFeedListUseCase: searchFeedListUseCase
)
let recipeListRouter = RecipeListRouterImpl(router: self)
let recipeListVC = RecipeListViewController(
interactor: recipeListInteractor,
router: recipeListRouter
)
recipeListInteractor.delegate = recipeListVC
return recipeListVC

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let baseNetworkService = BaseNetworkService()
let recipeFetchService = RecipeFetchServiceImpl(networkService: baseNetworkService)
let feedListRepository = FeedListRepositoryImpl(networkService: recipeFetchService)
let searchFeedRepository = SearchFeedRepositoryImpl(networkService: recipeFetchService)
let fetchFeedListUseCase = FetchFeedListUseCaseImpl(repository: feedListRepository)
let searchFeedListUseCase = SearchFeedListUseCaseImpl(repository: searchFeedRepository)
let recipeListInteractor = RecipeListInteractorImpl(
fetchFeedListUseCase: fetchFeedListUseCase,
searchFeedListUseCase: searchFeedListUseCase
)
let recipeListRouter = RecipeListRouterImpl(router: self)
let recipeListVC = RecipeListViewController(
interactor: recipeListInteractor,
router: recipeListRouter
)
recipeListInteractor.delegate = recipeListVC
return recipeListVC
let recipeListInteractor = RecipeListInteractorImpl(
fetchFeedListUseCase: FetchFeedListUseCaseImpl(
repository: FeedListRepositoryImpl(
networkService: RecipeFetchServiceImpl(
networkService: BaseNetworkService()
)
)
),
searchFeedListUseCase: SearchFeedListUseCaseImpl(
repository: SearchFeedRepositoryImpl(
networkService: RecipeFetchServiceImpl(
networkService: BaseNetworkService()
)
)
)
)
let recipeListRouter = RecipeListRouterImpl(router: self)
let recipeListVC = RecipeListViewController(
interactor: recipeListInteractor,
router: recipeListRouter
)
recipeListInteractor.delegate = recipeListVC
return recipeListVC

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

굳이 상수로 갖지 않고 바로 주입해도 될 것 같아요.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 코드 참고해서 다른 디펜던시들도 수정부탁드려요~

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[e4b3115] 수정했습니다

}

func createAddRecipeDependencies(recipeType: RecipeType) -> AddRecipeViewController {
let baseNetworkService = BaseNetworkService()
let recipePostService = RecipePostServiceImpl(networkService: baseNetworkService)
let saveRepository = AddRecipeRepositoryImpl(recipePostService: recipePostService)
let saveRecipeUseCase = SaveRecipeUseCaseImpl(repository: saveRepository)
let addRecipeInteractor = AddRecipeInteractorImpl(saveRecipeUseCase: saveRecipeUseCase)
let addRecipeVC = AddRecipeViewController(recipeType: recipeType, addRecipeInteractor: addRecipeInteractor)
return addRecipeVC
}

func createRecipeDetailDependencies(recipeID: Int) -> RecipeDetailViewController {
let baseNetworkService = BaseNetworkService()
let recipeDetailRepository = RecipeDetailRepositoryImpl(networkService: baseNetworkService)
let fetchRecipeDetailUseCase = FetchRecipeDetailUseCaseImpl(repository: recipeDetailRepository)
let detailInteractor = RecipeDetailInteractorImpl(
fetchRecipeDetailUseCase: fetchRecipeDetailUseCase,
recipeID: recipeID
)
let detailVC = RecipeDetailViewController(interactor: detailInteractor)
detailInteractor.delegate = detailVC
return detailVC
}
}