Skip to content

Commit

Permalink
Fix: RecipeDetailInteractor의 Ouput 제거 및 네이밍 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
GeonH0 committed Jul 14, 2024
1 parent f37a122 commit 06a6f89
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,71 +6,42 @@
//

import Foundation

import RxSwift

protocol RecipeDetailInteractorDelegate: AnyObject {
func fetchedRecipe(result: Result<Recipe, Error>)
}

protocol InputRecipeDetailInteractor {
protocol RecipeDetailInteractor {
func viewDidLoad()
}

protocol OutputRecipeDetailInteractor {
var recipe: Observable<Result<Recipe, Error>> { get }
}

class RecipeDetailInteractor: InputRecipeDetailInteractor, OutputRecipeDetailInteractor {

class RecipeDetailInteractorImpl: RecipeDetailInteractor {

private let fetchRecipeDetailUseCase: FetchRecipeDetailUseCase
private let recipeID: Int
private let disposeBag = DisposeBag()
private let recipeDetailSubject = PublishSubject<Result<Recipe, Error>>()
weak var delegate: RecipeDetailInteractorDelegate?

var recipe: Observable<Result<Recipe, Error>> {
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<Recipe, Error>) {
switch result {
case .success(let recipe):
self.recipeDetailSubject.onNext(.success(recipe))
case .failure(let error):
self.recipeDetailSubject.onNext(.failure(error))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
Expand All @@ -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
}
}

0 comments on commit 06a6f89

Please sign in to comment.