-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddTaskViewController.swift
More file actions
89 lines (74 loc) · 2.55 KB
/
Copy pathAddTaskViewController.swift
File metadata and controls
89 lines (74 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//
// AddTaskViewController.swift
// hack_iOS
//
// Created by 山根大生 on 2021/03/09.
//
import UIKit
import RxSwift
import RxCocoa
class AddTaskViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var inputnameTextField: UITextField! {
didSet {
inputnameTextField.placeholder = "タスクのなまえ"
inputnameTextField.delegate = self
}
}
@IBOutlet weak var inputDescriptionTextView: UITextView! {
didSet {
inputDescriptionTextView.isScrollEnabled = false
}
}
@IBOutlet weak var addButton: UIButton! {
didSet {
addButton.setTitle("Add", for: .normal)
addButton.addTarget(self, action: #selector(tapAddButton), for: .touchUpInside)
}
}
private let taskRepository: TaskRepository
private let disposeBag = DisposeBag()
init(taskRepository: TaskRepository = .init()) {
self.taskRepository = taskRepository
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
@objc private func tapAddButton() {
addTask()
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
inputnameTextField.resignFirstResponder()
return true
}
private func addTask() {
guard let name = inputnameTextField.text else { return }
let `description` = inputDescriptionTextView.text
taskRepository.post(name: name, description: description)
.subscribe(on: SerialDispatchQueueScheduler(qos: .background))
.observe(on: MainScheduler.instance)
.subscribe(onSuccess: { [weak self] in
self?.navigationController?.popViewController(animated: true)
},
onFailure: { error in
guard let error = error as? APIError else { return }
switch error {
case .decode(let error):
print(error)
case .network(let error):
print(error)
case .unknown(let error):
print(error)
case .noResponse:
print("No Response")
}
})
.disposed(by: disposeBag)
}
}