From 06a6f896495eae6c7e50707cafc4df7e26e975d8 Mon Sep 17 00:00:00 2001 From: GeonH0 Date: Mon, 15 Jul 2024 00:56:58 +0900 Subject: [PATCH] =?UTF-8?q?Fix:=20RecipeDetailInteractor=EC=9D=98=20Ouput?= =?UTF-8?q?=20=EC=A0=9C=EA=B1=B0=20=EB=B0=8F=20=EB=84=A4=EC=9D=B4=EB=B0=8D?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Interactor/RecipeDetailInteractor.swift | 51 ++++--------------- .../View/RecipeDetailViewController.swift | 16 ++++-- 2 files changed, 23 insertions(+), 44 deletions(-) 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 + } +}