Skip to content

Commit

Permalink
Merge pull request #116 from boostcampwm-2024/feature/create-book-cover
Browse files Browse the repository at this point in the history
BookCover 생성할 때, 카테고리 설정 로직 구현
  • Loading branch information
Kyxxn authored Dec 2, 2024
2 parents 541c1b0 + bdbb9b0 commit 353c3dd
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ final class SceneDelegate: UIResponder, UIWindowSceneDelegate {

// MARK: - BookCover UseCase
let bookCoverRepository = try DIContainer.shared.resolve(BookCoverRepository.self)
DIContainer.shared.register(
CreateBookCoverUseCase.self,
object: DefaultCreateBookCoverUseCase(repository: bookCoverRepository)
)
DIContainer.shared.register(
FetchAllBookCoverUseCase.self,
object: DefaultFetchAllBookCoverUseCase(repository: bookCoverRepository)
Expand All @@ -209,6 +213,7 @@ final class SceneDelegate: UIResponder, UIWindowSceneDelegate {
DeleteBookCoverUseCase.self,
object: DefaultDeleteBookCoverUseCase(repository: bookCoverRepository)
)

// MARK: - EditBook UseCase
DIContainer.shared.register(
PersistentlyStoreMediaUseCase.self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import MHCore
import Combine

final class CreateBookViewController: UIViewController {
// MARK: - Constant
static let maxTitleLength = 10
// MARK: - Property
private let bookView: MHBookCover = MHBookCover()
// MARK: - UI Components
private let bookCoverView: MHBookCover = MHBookCover()
private let bookTitleTextField: UITextField = {
let textField = UITextField()
textField.font = UIFont.ownglyphBerry(size: 25)
Expand Down Expand Up @@ -43,11 +41,11 @@ final class CreateBookViewController: UIViewController {
}
private let categorySelectionButton: UIButton = {
let button = UIButton()
var attributedTitle = AttributedString(stringLiteral: "없음")
var attributedTitle = AttributedString(stringLiteral: "카테고리를 선택해주세요")
attributedTitle.font = UIFont.ownglyphBerry(size: 25)

button.setAttributedTitle(NSAttributedString(attributedTitle), for: .normal)
button.setTitleColor(.mhTitle, for: .normal)
button.setTitleColor(.systemGray3, for: .normal) // TODO: - 우측에 > 이미지 추가
button.contentHorizontalAlignment = .left

return button
Expand Down Expand Up @@ -76,10 +74,12 @@ final class CreateBookViewController: UIViewController {

return shadowLayer
}()
// TODO: - 뷰모델 개선 필요

// MARK: - Property
@Published
private var viewModel: CreateBookViewModel
private var cancellables: Set<AnyCancellable> = []
private let maxTitleLength = 10

// MARK: - Initializer
init(viewModel: CreateBookViewModel) {
Expand Down Expand Up @@ -124,8 +124,8 @@ final class CreateBookViewController: UIViewController {
}
private func configureConstraints() {
// 책 미리보기
let bookPreviewViewBackground = bookView.embededInDefaultBackground(
with: UIEdgeInsets(top: 70, left: 100, bottom: 70, right: 100)
let bookPreviewViewBackground = bookCoverView.embededInDefaultBackground(
with: UIEdgeInsets(top: 50, left: 80, bottom: 50, right: 80)
)
view.addSubview(bookPreviewViewBackground)
bookPreviewViewBackground.setAnchor(
Expand Down Expand Up @@ -242,8 +242,8 @@ final class CreateBookViewController: UIViewController {
// TitleTextField 변경
let titleAction = UIAction { [weak self] _ in
guard let self else { return }
if self.bookTitleTextField.text?.count ?? 0 > Self.maxTitleLength {
self.bookTitleTextField.text = String(self.bookTitleTextField.text?.prefix(Self.maxTitleLength) ?? "")
if self.bookTitleTextField.text?.count ?? 0 > maxTitleLength {
self.bookTitleTextField.text = String(self.bookTitleTextField.text?.prefix(maxTitleLength) ?? "")
}

self.viewModel.bookTitle = self.bookTitleTextField.text ?? ""
Expand All @@ -261,14 +261,35 @@ final class CreateBookViewController: UIViewController {
imageSelectionButton.addAction(pictureSelectingAction, for: .touchUpInside)

// TODO: - 사진 선택 뷰모델?에 반영

categorySelectionButton.addAction(UIAction { [weak self] _ in
self?.presentCategorySelectionView()
}, for: .touchUpInside)
}
private func presentCategorySelectionView() {
do {
let categoryViewModelFactory = try DIContainer.shared.resolve(BookCategoryViewModelFactory.self)
let categoryViewModel = categoryViewModelFactory.makeForCreateBook()
let categoryViewController = BookCategoryViewController(viewModel: categoryViewModel)
let navigationController = UINavigationController(rootViewController: categoryViewController)
categoryViewController.delegate = self

if let sheet = navigationController.sheetPresentationController {
sheet.detents = [.medium(), .large()]
}

self.present(navigationController, animated: true)
} catch {
MHLogger.error(error)
}
}

// TODO: ViewModel에 @Published 제거해야 함
private func configureViewModelBinding() {
$viewModel
.receive(on: DispatchQueue.main)
.sink { [weak self] viewModel in
guard let self else { return }
self.bookView.configure(
self.bookCoverView.configure(
title: viewModel.bookTitle,
bookCoverImage: viewModel.currentColor.image,
// TODO: - 이미지 선택시 변경
Expand Down Expand Up @@ -345,3 +366,16 @@ extension CreateBookViewController: UITextFieldDelegate {
return true
}
}

extension CreateBookViewController: BookCategoryViewControllerDelegate {
func categoryViewController(
_ categoryViewController: BookCategoryViewController,
didSelectCategory category: String
) {
var attributedTitle = AttributedString(stringLiteral: category)
attributedTitle.font = UIFont.ownglyphBerry(size: 25)

categorySelectionButton.setTitleColor(.mhTitle, for: .normal)
categorySelectionButton.setAttributedTitle(NSAttributedString(attributedTitle), for: .normal)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class BookCategoryViewController: UIViewController {
guard let viewModelFactory = try? DIContainer.shared.resolve(BookCategoryViewModelFactory.self) else {
return nil
}
self.viewModel = viewModelFactory.make()
self.viewModel = viewModelFactory.makeForHome()
super.init(coder: coder)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,24 @@ final class BookCategoryViewModel: ViewModelType {
private let deleteBookCategoryUseCase: DeleteBookCategoryUseCase
private let output = PassthroughSubject<Output, Never>()
private var cancellables = Set<AnyCancellable>()
private(set) var categories = [
BookCategory(order: 0, name: "전체"),
BookCategory(order: 1, name: "즐겨찾기")
]
private(set) var categories = [BookCategory]()
private(set) var currentCategoryName = ""

init(
createBookCategoryUseCase: CreateBookCategoryUseCase,
fetchBookCategoriesUseCase: FetchBookCategoriesUseCase,
updateBookCategoryUseCase: UpdateBookCategoryUseCase,
deleteBookCategoryUseCase: DeleteBookCategoryUseCase
deleteBookCategoryUseCase: DeleteBookCategoryUseCase,
categories: [BookCategory] = [
BookCategory(order: 0, name: "전체"),
BookCategory(order: 1, name: "즐겨찾기")
]
) {
self.createBookCategoryUseCase = createBookCategoryUseCase
self.fetchBookCategoriesUseCase = fetchBookCategoriesUseCase
self.updateBookCategoryUseCase = updateBookCategoryUseCase
self.deleteBookCategoryUseCase = deleteBookCategoryUseCase
self.categories = categories
}

func setup(currentCategory: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,22 @@ public struct BookCategoryViewModelFactory {
self.deleteBookCategoryUseCase = deleteBookCategoryUseCase
}

func make() -> BookCategoryViewModel {
func makeForHome() -> BookCategoryViewModel {
BookCategoryViewModel(
createBookCategoryUseCase: createBookCategoryUseCase,
fetchBookCategoriesUseCase: fetchBookCategoriesUseCase,
updateBookCategoryUseCase: updateBookCategoryUseCase,
deleteBookCategoryUseCase: deleteBookCategoryUseCase
)
}

func makeForCreateBook() -> BookCategoryViewModel {
BookCategoryViewModel(
createBookCategoryUseCase: createBookCategoryUseCase,
fetchBookCategoriesUseCase: fetchBookCategoriesUseCase,
updateBookCategoryUseCase: updateBookCategoryUseCase,
deleteBookCategoryUseCase: deleteBookCategoryUseCase,
categories: []
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public final class HomeViewController: UIViewController {
do {
guard let self else { return }
let categoryViewModelFactory = try DIContainer.shared.resolve(BookCategoryViewModelFactory.self)
let categoryViewModel = categoryViewModelFactory.make()
let categoryViewModel = categoryViewModelFactory.makeForHome()
categoryViewModel.setup(currentCategory: self.currentCategory)
let categoryViewController = BookCategoryViewController(viewModel: categoryViewModel)
categoryViewController.delegate = self
Expand Down

0 comments on commit 353c3dd

Please sign in to comment.