diff --git a/HomeCafeRecipes/HomeCafeRecipes/Domain/Interactor/RecipeDetailInteractor.swift b/HomeCafeRecipes/HomeCafeRecipes/Domain/Interactor/RecipeDetailInteractor.swift index 187863f..ef9c80c 100644 --- a/HomeCafeRecipes/HomeCafeRecipes/Domain/Interactor/RecipeDetailInteractor.swift +++ b/HomeCafeRecipes/HomeCafeRecipes/Domain/Interactor/RecipeDetailInteractor.swift @@ -6,71 +6,42 @@ // import Foundation + import RxSwift protocol RecipeDetailInteractorDelegate: AnyObject { func fetchedRecipe(result: Result) } -protocol InputRecipeDetailInteractor { +protocol RecipeDetailInteractor { func viewDidLoad() } -protocol OutputRecipeDetailInteractor { - var recipe: Observable> { get } -} - -class RecipeDetailInteractor: InputRecipeDetailInteractor, OutputRecipeDetailInteractor { - +class RecipeDetailInteractorImpl: RecipeDetailInteractor { + private let fetchRecipeDetailUseCase: FetchRecipeDetailUseCase private let recipeID: Int private let disposeBag = DisposeBag() - private let recipeDetailSubject = PublishSubject>() weak var delegate: RecipeDetailInteractorDelegate? - - var recipe: Observable> { - return recipeDetailSubject.asObservable() - } - - init( - fetchRecipeDetailUseCase: FetchRecipeDetailUseCase, - recipeID: Int - ) { + + init(fetchRecipeDetailUseCase: FetchRecipeDetailUseCase, recipeID: Int) { self.fetchRecipeDetailUseCase = fetchRecipeDetailUseCase self.recipeID = recipeID } func setDelegate(_ delegate: RecipeDetailInteractorDelegate) { self.delegate = delegate - bindOutputs() - } - - private func bindOutputs() { - recipe - .subscribe(onNext: { [weak self] result in - self?.delegate?.fetchedRecipe(result: result) - }) - .disposed(by: disposeBag) } - + func viewDidLoad() { fetchRecipeDetail() } - + private func fetchRecipeDetail() { fetchRecipeDetailUseCase.execute(recipeID: recipeID) - .subscribe { [weak self] result in - self?.handleResult(result) - } + .subscribe(onSuccess: { [weak self] result in + self?.delegate?.fetchedRecipe(result: result) + }) .disposed(by: disposeBag) } - - private func handleResult(_ result: Result) { - switch result { - case .success(let recipe): - self.recipeDetailSubject.onNext(.success(recipe)) - case .failure(let error): - self.recipeDetailSubject.onNext(.failure(error)) - } - } } diff --git a/HomeCafeRecipes/HomeCafeRecipes/Presentation/Feed/View/RecipeDetailViewController.swift b/HomeCafeRecipes/HomeCafeRecipes/Presentation/Feed/View/RecipeDetailViewController.swift index c6ec16d..a038e83 100644 --- a/HomeCafeRecipes/HomeCafeRecipes/Presentation/Feed/View/RecipeDetailViewController.swift +++ b/HomeCafeRecipes/HomeCafeRecipes/Presentation/Feed/View/RecipeDetailViewController.swift @@ -13,12 +13,12 @@ final class RecipeDetailViewController: UIViewController { private let contentView = RecipeDetailView() private let customNavigationBar = CustomNavigationBar() - private let interactor: RecipeDetailInteractor + private let interactor: RecipeDetailInteractorImpl private let disposeBag = DisposeBag() private var recipeDetailViewModel: RecipeDetailViewModel? private let recipeListMapper = RecipeListMapper() - init(interactor: RecipeDetailInteractor) { + init(interactor: RecipeDetailInteractorImpl) { self.interactor = interactor super.init(nibName: nil, bundle: nil) self.interactor.setDelegate(self) @@ -37,7 +37,7 @@ final class RecipeDetailViewController: UIViewController { interactor.viewDidLoad() contentView.customNavigationBar.backButton.addTarget(self, action: #selector(backButtonTapped), for: .touchUpInside) } - + private func displayError(_ error: Error) { let alert = UIAlertController(title: "해당 레시피를 로드하는데 실패했습니다.", message: error.localizedDescription, preferredStyle: .alert) alert.addAction(UIAlertAction(title: "OK", style: .default)) @@ -60,7 +60,15 @@ extension RecipeDetailViewController: RecipeDetailInteractorDelegate { self.contentView.configure(with: recipeItemViewModel) } case .failure(let error): - self.displayError(error) + DispatchQueue.main.async { + self.displayError(error) + } } } } + +extension RecipeDetailViewController: Drawable { + var viewController: UIViewController? { + return self + } +}