ResidentsViewController.swift
4.74 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
//
// ResidentsViewController.swift
// InterQR-Internship
//
// Created by Дмитрий Тимофеев on 14.06.2022.
//
import UIKit
class ResidentsViewController: UIViewController {
var mainView = ResidentsView()
var myData: [ResidentModel] = []
let networkManager = AuthService()
override func loadView() {
view = mainView
}
override func viewDidLoad() {
initViewController()
initDevice()
}
func initViewController() {
mainView.tableView.delegate = self
mainView.tableView.dataSource = self
mainView.tableView.register(ResidentTableViewCell.self, forCellReuseIdentifier: ResidentTableViewCell.id)
mainView.backButton.addTarget(self, action: #selector(didShowChooseApartVC), for: .touchUpInside)
mainView.addResidentsButton.addTarget(self, action: #selector(willShowAddResidentVC), for: .touchUpInside)
}
func modelFilling() -> InitRequestModel? {
guard let deviceUUID = UIDevice.current.identifierForVendor?.uuidString else {
return nil
}
guard let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String else {
return nil
}
return InitRequestModel(deviceUUID: deviceUUID,
manufacturer: .Text.apple,
model: UIDevice.current.model,
platform: UIDevice.current.systemName,
osVersion: UIDevice.current.systemVersion,
appVersion: appVersion)
}
private func initDevice() {
guard let model = modelFilling() else {
return
}
AuthService.share.initRequest(model: model) { result in
print("1️⃣✅\(result.message)✅")
} fail: { error in
print("1️⃣\(error)")
}
}
@objc private func didShowChooseApartVC() {
navigationController?.popViewController(animated: true)
}
@objc private func willShowAddResidentVC() {
let addResidentsVC = AddResidentsViewController()
addResidentsVC.modalPresentationStyle = .overCurrentContext
addResidentsVC.modalTransitionStyle = .crossDissolve
addResidentsVC.delegate = self
present(addResidentsVC, animated: true)
}
}
//MARK: - TableView delegate & data source
extension ResidentsViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
myData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: ResidentTableViewCell.id, for: indexPath) as? ResidentTableViewCell else { return UITableViewCell() }
cell.serialNumberLabel.text = "\(indexPath.row + 1)"
let data = myData[indexPath.row]
cell.model = data
cell.delegate = self
return cell
}
}
//MARK: - ResidentSavable delegate
extension ResidentsViewController: ResidentSavable {
func save(_ data: ResidentModel) {
self.myData.append(data)
mainView.tableView.reloadData()
}
}
//MARK: residentcell delegate
extension ResidentsViewController: ResidentTableViewCellDelegate {
func didButtonsTapped(cell: ResidentTableViewCell) {
if !cell.managerButton.isSelected {
cell.managerButton.isSelected = true
} else {
cell.managerButton.isSelected = false
}
}
func didRemoveButtonTapped(cell: ResidentTableViewCell) {
guard let model = cell.model else {
return
}
myData.removeAll(where: {$0.name == model.name})
mainView.tableView.reloadData()
}
// func didButtonsTapped(_ button: UIButton) {
// if !button.isSelected {
// button.isSelected = true
// } else {
// button.isSelected = false
// }
// }
func didManagerButtonTapped(cell: ResidentTableViewCell) {
if !cell.managerButton.isSelected {
cell.managerButton.isSelected = true
} else {
cell.managerButton.isSelected = false
}
}
func didHiddenButtonTapped(cell: ResidentTableViewCell) {
if !cell.hiddenButton.isSelected {
cell.hiddenButton.isSelected = true
} else {
cell.hiddenButton.isSelected = false
}
}
func didHeardButtonTapped(cell: ResidentTableViewCell) {
if !cell.heartButton.isSelected {
cell.heartButton.isSelected = true
} else {
cell.heartButton.isSelected = false
}
}
}