Skip to content

Commit

Permalink
fix: CreateBookViewModel를 기존의 Stucrt로 변환
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyxxn committed Dec 2, 2024
1 parent 1572890 commit 40d81ea
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,6 @@ final class SceneDelegate: UIResponder, UIWindowSceneDelegate {
)
)

// MARK: - BookCover ViewModel
let createBookCoverUseCase = try DIContainer.shared.resolve(CreateBookCoverUseCase.self)
DIContainer.shared.register(
CreateBookViewModelFactory.self,
object: CreateBookViewModelFactory(
createBookCoverUseCase: createBookCoverUseCase
)
)

// MARK: Category ViewModel
let createBookCategoryUseCase = try DIContainer.shared.resolve(CreateBookCategoryUseCase.self)
let fetchBookCategoriesUseCase = try DIContainer.shared.resolve(FetchBookCategoriesUseCase.self)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ final class CreateBookViewController: UIViewController {
// MARK: - Property
@Published
private var viewModel: CreateBookViewModel
private let input = PassthroughSubject<CreateBookViewModel.Input, Never>()
private var cancellables: Set<AnyCancellable> = []
private let maxTitleLength = 10

Expand All @@ -89,10 +88,7 @@ final class CreateBookViewController: UIViewController {
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
guard let createBookViewModelFactory = try? DIContainer.shared.resolve(CreateBookViewModelFactory.self) else {
return nil
}
viewModel = createBookViewModelFactory.make(houseName: "")
viewModel = CreateBookViewModel()

super.init(coder: coder)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
import Combine
import Photos
import MHCore
import MHDomain
import MHFoundation
import Photos

final class CreateBookViewModel: ViewModelType {
enum Input {
case createBook
case updateTextField(text: String)
}

enum Output {
case createdBook
case updatedBookCover
}

struct CreateBookViewModel {
var bookTitle: String = ""
var bookCategory: String = ""
var previousColorNumber: Int = -1
Expand All @@ -30,50 +18,5 @@ final class CreateBookViewModel: ViewModelType {
default: .blue
}
}
private let createBookCoverUseCase: CreateBookCoverUseCase
private let output = PassthroughSubject<Output, Never>()
private var cancellables = Set<AnyCancellable>()
let houseName: String

init(
createBookCoverUseCase: CreateBookCoverUseCase,
houseName: String
) {
self.createBookCoverUseCase = createBookCoverUseCase
self.houseName = houseName
}

func transform(input: AnyPublisher<Input, Never>) -> AnyPublisher<Output, Never> {
input.sink { [weak self] event in
switch event {
case .createBook:
Task { await self?.createBookInCoreData() }
case .updateTextField(let text):
self?.vaildateBookTitle(text: text)
}
}.store(in: &cancellables)

return output.eraseToAnyPublisher()
}

private func createBookInCoreData() async {
let bookCover = BookCover(
order: 0,
title: bookTitle,
imageURL: nil,
color: currentColor,
category: bookCategory,
favorite: false
)
do {
try await createBookCoverUseCase.execute(with: bookCover)
output.send(.createdBook)
} catch {
MHLogger.error("Create BookCover 실패: \(error.localizedDescription)")
}
}

private func vaildateBookTitle(text: String) {
bookTitle = text
}
let houseName: String = UserDefaults.standard.string(forKey: Constant.houseNameUserDefaultKey) ?? ""
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,8 @@ public final class HomeViewController: UIViewController {
}

private func moveMakingBookViewController() {
do {
let createBookViewModelFactory = try DIContainer.shared.resolve(CreateBookViewModelFactory.self)
let createBookViewModel = createBookViewModelFactory.make(houseName: viewModel.houseName)
let bookCreationViewController = CreateBookViewController(viewModel: createBookViewModel)
navigationController?.pushViewController(bookCreationViewController, animated: true)
} catch {
MHLogger.error(error.localizedDescription)
}
let bookCreationViewController = CreateBookViewController(viewModel: CreateBookViewModel())
navigationController?.pushViewController(bookCreationViewController, animated: true)
}

private func configureConstraints() {
Expand Down

0 comments on commit 40d81ea

Please sign in to comment.