AddResidentsViewController.swift
6.27 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
// AddResidentViewController.swift
// InterQR-Internship
//
// Created by Дмитрий Тимофеев on 14.06.2022.
//
import UIKit
import ContactsUI
class AddResidentsViewController: UIViewController {
var savedResidents: [ResidentModel] = []
weak var delegate: ResidentSavable?
var contact = ContactPickerViewController()
var isKeyboardAppear = false
var mainView = AddResidentsView()
var closeTap: UITapGestureRecognizer?
override func loadView() {
view = mainView
}
override func viewDidLoad() {
super.viewDidLoad()
initViewController()
}
func initViewController() {
self.closeTap = UITapGestureRecognizer(target: self, action: #selector(didTapCloseButton))
if let closeTap = self.closeTap {
mainView.transparentView.addGestureRecognizer(closeTap)
}
mainView.closeButton.addTarget(self, action: #selector(didTapCloseButton), for: .touchUpInside)
mainView.managerButton.addTarget(self, action: #selector(didTapOnChangeableButtons(_:)), for: .touchUpInside)
mainView.hiddenButton.addTarget(self, action: #selector(didTapOnChangeableButtons(_:)), for: .touchUpInside)
mainView.heartButton.addTarget(self, action: #selector(didTapOnChangeableButtons(_:)), for: .touchUpInside)
mainView.addResidentButton.addTarget(self, action: #selector(didTapResidentButton), for: .touchUpInside)
mainView.contactListButton.addTarget(self, action: #selector(didTapContactListButton), for: .touchUpInside)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}
deinit {
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
}
func reloadView() {
mainView.mobileNumberTextField.reloadInputViews()
}
@objc private func didTapCloseButton() {
dismiss(animated: true, completion: nil)
}
@objc private func didTapResidentButton() {
print("✅")
didReceiveData()
dismiss(animated: true, completion: nil)
}
func didReceiveData() {
guard let name = mainView.nameTextField.text,
let mobileNumber = mainView.mobileNumberTextField.text
else {
return
}
let model = [ResidentModel(name: name,
mobileNumber: mobileNumber,
manager: mainView.managerButton.isSelected,
hidden: mainView.hiddenButton.isSelected,
heart: mainView.heartButton.isSelected)]
savedResidents.append(contentsOf: model)
delegate?.save(savedResidents)
// ???????????????????????????????
mainView.nameTextField.text = "" //???
mainView.mobileNumberTextField.text = "" //???
mainView.managerButton.isSelected = false //???
mainView.hiddenButton.isSelected = false //???
mainView.heartButton.isSelected = false //???
// ???????????????????????????????
}
@objc private func didTapOnChangeableButtons(_ sender: SquereCheckbox) {
switch sender {
case mainView.managerButton :
if mainView.managerButton.isSelected == true {
mainView.managerButton.isSelected = false
} else {
mainView.managerButton.isSelected = true
}
case mainView.hiddenButton :
if mainView.hiddenButton.isSelected == true {
mainView.hiddenButton.isSelected = false
} else {
mainView.hiddenButton.isSelected = true
}
case mainView.heartButton :
if mainView.heartButton.isSelected == true {
mainView.heartButton.isSelected = false
} else {
mainView.heartButton.isSelected = true
}
default:
return
}
}
@objc func didTapContactListButton() {
presentContactPickerVC()
}
func presentContactPickerVC() {
let viewController = ContactPickerViewController()
viewController.delegate = self
present(viewController, animated: true)
}
}
//MARK: - Keyboard observer
extension AddResidentsViewController {
@objc private func keyboardWillShow(notification: NSNotification) {
let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double
if !isKeyboardAppear {
UIView.animate(withDuration: duration ?? 0) {
self.mainView.containerView.snp.updateConstraints { make in
make.centerY.equalTo(self.mainView.transparentView.snp.centerY).offset(-150)
}
self.mainView.layoutIfNeeded()
}
isKeyboardAppear = true
}
}
@objc private func keyboardWillHide(notification: NSNotification) {
let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double
if isKeyboardAppear {
UIView.animate(withDuration: duration ?? 0) {
self.mainView.containerView.snp.updateConstraints { make in
make.centerY.equalTo(self.mainView.transparentView.snp.centerY)
}
self.mainView.layoutIfNeeded()
}
isKeyboardAppear = false
}
}
}
//MARK: - Contact picker delegate
extension AddResidentsViewController: CNContactPickerDelegate {
func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
let userPhoneNumbers:[CNLabeledValue<CNPhoneNumber>] = contact.phoneNumbers
let firstPhoneNumber:CNPhoneNumber = userPhoneNumbers[0].value
mainView.mobileNumberTextField.text = firstPhoneNumber.stringValue
}
}