-
Notifications
You must be signed in to change notification settings - Fork 0
/
NewTaskViewController.swift
73 lines (64 loc) · 2.72 KB
/
NewTaskViewController.swift
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
import UIKit
final class NewTaskViewController: TypedViewController<TaskTableView> {
var animated = false
private var group = {
let group = TaskGroup()
group.uiColor = .random(in: .dark)
return group
}()
private var isUpdateMode = false
init(group: TaskGroup? = nil, animated: Bool = false) {
self.animated = animated
self.group = group ?? {
let group = TaskGroup()
group.uiColor = .random(in: .dark)
return group
}()
isUpdateMode = group != nil
super.init()
}
private var isToastOpened = false
override func viewDidLoad() {
super.viewDidLoad()
typedView.group = group
typedView.editable = true
setupNavigation()
}
private func setupNavigation() {
navigationItem.hidesBackButton = isUpdateMode
navigationItem.leftBarButtonItem = isUpdateMode ? .none : .init(image: .init(systemName: "arrow.left"), style: .plain, target: self, action: #selector(backButtonTapped))
navigationItem.rightBarButtonItem = .init(title: "Done", style: .done, target: self, action: #selector(doneButtonTapped))
navigationItem.leftBarButtonItem?.tintColor = .label
navigationItem.rightBarButtonItem?.tintColor = .label
}
@objc private func backButtonTapped() {
navigationController?.popViewController(animated: animated)
}
@objc private func doneButtonTapped() {
guard group.image != nil else { showToast(message: "Choose an image that represents your tasks."); return }
guard !group.name.isEmpty else { showToast(message: "Fill out the name of the tasks"); return }
guard !group.tasks.isEmpty else { showToast(message: "Please create at least one task."); return }
if isUpdateMode {
EventBus.shared.emit(UpdateTaskGroup(payload: .init(group: group, completion: { [weak self] in
guard let self else { return }
self.navigationController?.popViewController(animated: self.animated)
})))
} else {
EventBus.shared.emit(CreateNewTaskGroup(payload: .init(group: group, completion: { [weak self] in
guard let self else { return }
self.navigationController?.popViewController(animated: self.animated)
})))
}
}
private func showToast(message: String) {
if isToastOpened { return }
isToastOpened = true
let toast = ToastView()
toast.show(
view: view,
message: message,
position: .init(x: view.center.x, y: view.safeAreaInsets.top),
color: group.uiColor ?? .black
) { [weak self] in self?.isToastOpened = false }
}
}