Skip to content

Commit

Permalink
feat: ✨New Issue ViewController, ViewModel, UseCase 생성(Lia316#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghis22130 committed Jun 24, 2021
1 parent 736b9e4 commit da27a45
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation
import Combine

protocol PostNewIssueUseCase {

func execute(title: String, mainComments:String, authorId: Int, assigneeIds: [Int]?, labelIds: [Int]?, milestoneId: Int?, completion: @escaping (Result<IssueDetail, NetworkError>) -> Void )
}

final class DefaultPostNewIssueUseCase: PostNewIssueUseCase {
Expand All @@ -21,17 +21,17 @@ final class DefaultPostNewIssueUseCase: PostNewIssueUseCase {
self.networkManager = networkManager
}

func execute(title: String, mainComments:String, authorId: Int, assigneeIds: [Int]?, labelIds: [Int]?, milestoneId: Int?, completion: @escaping (Result<DetailIssue, NetworkError>) -> Void ) {
func execute(title: String, mainComments:String, authorId: Int, assigneeIds: [Int]?, labelIds: [Int]?, milestoneId: Int?, completion: @escaping (Result<IssueDetail, NetworkError>) -> Void ) {
let newIssue = NewIssue.init(title: title, mainCommentContents: mainComments, authorId: authorId, assigneeIds: assigneeIds, labelIds: labelIds, milestoneId: milestoneId)
networkManager.post(path: "/issues", data: newIssue, result: DetailIssue.self)
networkManager.post(path: "/issues", data: newIssue, result: IssueDetail.self)
.receive(on: DispatchQueue.main)
.sink { error in
switch error {
case .failure(let error): completion(.failure(error))
case .finished: break
}
} receiveValue: { detailIssue in
completion(.success(detailIssue))
} receiveValue: { issueDetail in
completion(.success(issueDetail))
}
.store(in: &cancelBag)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,29 @@
import UIKit
import Combine

final class NewIssueViewController: UIViewController, ViewControllerIdentifierable, UIImagePickerControllerDelegate & UINavigationControllerDelegate {
struct NewIssueViewControllerAction {
let showIssueDetailView: (IssueDetail) -> Void
}

final class NewIssueViewController: UIViewController, ViewControllerIdentifierable {

static func create(_ viewModel: NewIssueViewModel, _ markdownViewController: MarkdownViewController, _ previewViewController: PreviewViewController) -> NewIssueViewController {
static func create(_ viewModel: NewIssueViewModel, _ markdownViewController: MarkdownViewController, _ previewViewController: PreviewViewController, _ action: NewIssueViewControllerAction) -> NewIssueViewController {
guard let vc = storyboard.instantiateViewController(identifier: storyboardID) as? NewIssueViewController else {
return NewIssueViewController()
}
vc.viewModel = viewModel
vc.action = action
vc.markdownViewController = markdownViewController
vc.previewViewController = previewViewController
return vc
}

@IBOutlet private var titleTextField: UIView!
@IBOutlet private var titleTextField: UITextField!
@IBOutlet private weak var containerView: UIView!
@IBOutlet private weak var filteringTableView: UITableView!

private let imagePicker = UIImagePickerController()
private var viewModel: NewIssueViewModel!
private var action: NewIssueViewControllerAction?
private var markdownViewController: MarkdownViewController?
private var previewViewController: PreviewViewController?
private var cancelBag = Set<AnyCancellable>()
Expand All @@ -44,7 +49,6 @@ final class NewIssueViewController: UIViewController, ViewControllerIdentifierab
extension NewIssueViewController {
private func setting() {
setNavigation()
setImagePicker()
setTableView()
}

Expand Down Expand Up @@ -80,10 +84,6 @@ extension NewIssueViewController {
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
}

private func setImagePicker() {
imagePicker.delegate = self
}

private func setTableView() {
filteringTableView.backgroundColor = view.backgroundColor
filteringTableView.setEditing(true, animated: true)
Expand All @@ -103,7 +103,10 @@ extension NewIssueViewController {
}

@objc func saveButtonTouched(_ sender: UIButton) {

guard let title = titleTextField.text, let comments = markdownViewController?.textView.text else { return }
viewModel.saveNewIssue(title, comments) { [weak self] issueDetail in
self?.action?.showIssueDetailView(issueDetail)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,21 @@ final class NewIssueViewModel {
}
}
}

func saveNewIssue(_ title: String, _ comments: String, completion: @escaping (IssueDetail) -> Void ) {
postNewIssueUseCase.execute(title: title,
mainComments: comments,
authorId: 0,
assigneeIds: [0],
labelIds: [0],
milestoneId: 0)
{ result in
switch result {
case .success(let issueDetail):
completion(issueDetail)
case .failure(let error):
self.handleError(error)
}
}
}
}

0 comments on commit da27a45

Please sign in to comment.