Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

책 읽기 페이지에서 책 수정 기능 구현 #131

Merged
merged 4 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,29 @@ final class BookViewController: UIViewController {
setup()
configureNavigationBar()
configureConstraints()
input.send(.viewDidLoad)
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

input.send(.loadBook)
}

// MARK: - Binding
private func bind() {
let output = viewModel.transform(input: input.eraseToAnyPublisher())

output.receive(on: DispatchQueue.main)
output
.receive(on: DispatchQueue.main)
.sink { [weak self] event in
switch event {
case .setBookTitle(let bookTitle):
self?.title = bookTitle
case .loadFirstPage(let page):
guard let page else { return }
self?.configureFirstPageViewController(firstPage: page)
case .moveToEdit(let bookID):
self?.presentEditBookView(bookID: bookID)
}
}
.store(in: &cancellables)
Expand Down Expand Up @@ -94,8 +102,8 @@ final class BookViewController: UIViewController {
title: "수정",
normal: normalAttributes,
selected: selectedAttributes
) {
// TODO: - 추후 책 속지 수정 페이지로 넘어가는 로직 필요
) { [weak self] in
self?.input.send(.editBook)
}
}

Expand All @@ -119,6 +127,18 @@ final class BookViewController: UIViewController {

return ReadPageViewController(viewModel: readPageViewModel)
}

// MARK: - PresentEditBookView
private func presentEditBookView(bookID: UUID) {
do {
let editBookViewModelFactory = try DIContainer.shared.resolve(EditBookViewModelFactory.self)
let editBookViewModel = editBookViewModelFactory.make(bookID: bookID)
let editBookViewController = EditBookViewController(viewModel: editBookViewModel, mode: .modify)
navigationController?.pushViewController(editBookViewController, animated: true)
} catch {
MHLogger.error(error)
}
}
}

// MARK: - UIPageViewControllerDelegate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import Combine

public final class BookViewModel: ViewModelType {
enum Input {
case viewDidLoad
case loadBook
case loadPreviousPage
case loadNextPage
case editBook
}

enum Output {
case setBookTitle(with: String?)
case loadFirstPage(page: Page?)
case moveToEdit(bookID: UUID)
}

private let fetchBookUseCase: FetchBookUseCase
Expand All @@ -32,11 +34,10 @@ public final class BookViewModel: ViewModelType {
self.identifier = identifier
}

@MainActor
func transform(input: AnyPublisher<Input, Never>) -> AnyPublisher<Output, Never> {
input.sink { [weak self] event in
switch event {
case .viewDidLoad:
case .loadBook:
Task { try await self?.fetchBook() }
case .loadPreviousPage:
if self?.nowPageIndex ?? 0 > 0 {
Expand All @@ -46,6 +47,9 @@ public final class BookViewModel: ViewModelType {
if self?.nowPageIndex ?? 0 < (self?.book?.pages.count ?? 0) - 1 {
self?.nowPageIndex += 1
}
case .editBook:
guard let self else { return }
self.output.send(.moveToEdit(bookID: self.identifier))
}
}
.store(in: &cancellables)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,15 @@ extension BookCoverViewController {
self?.modifyInput.send(.saveBookCover)
}
}

private func configureAddSubviews() {
view.addSubview(bookPreviewViewBackground)
view.addSubview(bookTitleTextFieldBackground)
view.addSubview(bookColorSelectionBackground)
view.addSubview(categorySelectionButtonBackground)
view.addSubview(imageSelectionButtonBackground)
}

private func configureConstraints() {
bookPreviewViewBackground.setAnchor(
top: view.safeAreaLayoutGuide.topAnchor,
Expand Down Expand Up @@ -352,6 +354,7 @@ extension BookCoverViewController {
height: 63
)
}

private func configureAction() {
bookColorButtons.enumerated().forEach { index, button in
let colorButtonAction = UIAction { [weak self] _ in
Expand Down Expand Up @@ -393,6 +396,7 @@ extension BookCoverViewController {
}
categorySelectionButton.addAction(selectCategoryAction, for: .touchUpInside)
}

private func configuredColorButtons() -> UIView {
let firstLineColorButtonStackView = UIStackView()
firstLineColorButtonStackView.axis = .horizontal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import Combine

// TODO: - 페이지 없애는 기능 추가
final class EditBookViewController: UIViewController {
enum Mode {
case create
case modify
}
// MARK: - Constant
static let buttonBottomConstant: CGFloat = -20

Expand Down Expand Up @@ -76,16 +80,24 @@ final class EditBookViewController: UIViewController {
private let viewModel: EditBookViewModel
private let input = PassthroughSubject<EditBookViewModel.Input, Never>()
private var cancellables = Set<AnyCancellable>()
private let mode: Mode

// MARK: - Initializer
init(viewModel: EditBookViewModel) {
init(
viewModel: EditBookViewModel,
mode: Mode = .create
) {
self.viewModel = viewModel
self.mode = mode

super.init(nibName: nil, bundle: nil)
}

required init?(coder: NSCoder) {
guard let viewModel = try? DIContainer.shared.resolve(EditBookViewModelFactory.self) else { return nil }
self.viewModel = viewModel.make(bookID: .init())
self.mode = .create

super.init(coder: coder)
}

Expand Down Expand Up @@ -135,19 +147,28 @@ final class EditBookViewController: UIViewController {
alert.addAction(UIAlertAction(title: "취소", style: .cancel))
alert.addAction(UIAlertAction(title: "확인", style: .default) { _ in
self?.input.send(.didCancelButtonTapped)
self?.navigationController?.popViewController(animated: true)
})
self?.present(alert, animated: true)
}

// 네비게이션 오른쪽 아이템
navigationItem.rightBarButtonItem = UIBarButtonItem(
title: "기록 마치기",
normal: normalAttributes,
selected: selectedAttributes
) { [weak self] in
self?.input.send(.didSaveButtonTapped)
self?.navigationController?.popToRootViewController(animated: true)
switch mode {
case .create:
navigationItem.rightBarButtonItem = UIBarButtonItem(
title: "기록 마치기",
normal: normalAttributes,
selected: selectedAttributes
) { [weak self] in
self?.input.send(.didSaveButtonTapped)
}
case .modify:
navigationItem.rightBarButtonItem = UIBarButtonItem(
title: "수정 마치기",
normal: normalAttributes,
selected: selectedAttributes
) { [weak self] in
self?.input.send(.didSaveButtonTapped)
}
}
}
private func configureAddSubView() {
Expand Down Expand Up @@ -203,12 +224,24 @@ final class EditBookViewController: UIViewController {
}
private func configureBinding() {
let output = viewModel.transform(input: input.eraseToAnyPublisher())
output.receive(on: DispatchQueue.main)

output
.receive(on: DispatchQueue.main)
.sink { [weak self] event in
switch event {
case let .updateViewController(title):
case .updateViewController(let title):
self?.navigationItem.title = title
self?.editPageTableView.reloadData()
case .saveDone:
guard let self else { return }
switch self.mode {
case .create:
self.navigationController?.popToRootViewController(animated: true)
case .modify:
self.navigationController?.popViewController(animated: true)
}
case .revokeDone:
self?.navigationController?.popViewController(animated: true)
case .error(message: let message):
MHLogger.error(message) // TODO: - Alert 띄우기
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ final class EditBookViewModel: ViewModelType {
}
enum Output {
case updateViewController(title: String)
case saveDone
case revokeDone
case error(message: String)
}

Expand Down Expand Up @@ -135,6 +137,7 @@ final class EditBookViewModel: ViewModelType {
do {
try await updateBookUseCase.execute(id: bookID, book: book)
try await storeMediaUseCase.execute(to: bookID, mediaList: mediaList)
output.send(.saveDone)
} catch {
output.send(.error(message: "책을 저장하는데 실패했습니다."))
MHLogger.error(error.localizedDescription + #function)
Expand All @@ -144,6 +147,7 @@ final class EditBookViewModel: ViewModelType {
private func revokeMediaAll() async {
do {
try await storeMediaUseCase.execute(to: bookID, mediaList: nil)
output.send(.revokeDone)
} catch {
output.send(.error(message: "저장 취소하는데 실패했습니다."))
MHLogger.error(error.localizedDescription + #function)
Expand All @@ -154,6 +158,7 @@ final class EditBookViewModel: ViewModelType {
func numberOfPages() -> Int {
return editPageViewModels.count
}

func editPageViewModel(at index: Int) -> EditPageViewModel {
let editPageViewModel = editPageViewModels[index]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ final class ReadPageViewController: UIViewController {
private func bind() {
let output = viewModel.transform(input: input.eraseToAnyPublisher())

output.sink { [weak self] event in
output
.receive(on: DispatchQueue.main)
.sink { [weak self] event in
switch event {
case .loadPage(let page):
guard let page else { return }
Expand All @@ -68,8 +70,7 @@ final class ReadPageViewController: UIViewController {
// TODO: Alert 띄우기 ?
MHLogger.error(message)
}
}
.store(in: &cancellables)
}.store(in: &cancellables)
}

// MARK: - Setup & Configure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public final class ReadPageViewModel: ViewModelType {
return output.eraseToAnyPublisher()
}

@MainActor
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋습니다

private func loadMediaForData(media: MediaDescription) async {
do {
let mediaData: Data = try await fetchMediaUseCase.execute(media: media, in: bookID)
Expand Down
Loading