@@ -8,87 +8,233 @@
88
99import UIKit
1010
11−
1211/// Class for displaying chat info
1312///
1413class ChatInfoViewController: UITableViewController {
1514
15+ enum PersonStatus{
16+ case admin
17+ case ordinary
18+ }
19+
20+ weak var delegate: UpdatableGroup?
21+
22+ var groupChat: Model.Group? {
23+ didSet{
24+ if let groupChat = groupChat {
25+ usersArray = groupChat.users.map { $0.key }
26+ }
27+ }
28+ }
29+
30+ var usersArray: [Int] = [] {
31+ didSet {
32+ tableView.reloadData()
33+ }
34+ }
35+
36+ var users: [Int: Model.Users] = [:]
37+
38+ func canIEditThisChat() -> PersonStatus
39+ {
40+ let myId = UserDefaults.standard.integer(forKey: UserDefaultsKeys.id.rawValue)
41+
42+ if let groupChatUserPermission = groupChat?.users[myId]?.isAdmin {
43+ return groupChatUserPermission ? .admin : .ordinary
44+ }
45+
46+ return .ordinary
47+ }
48+
49+ var myPermissions: PersonStatus = .ordinary
50+
51+
1652
1753 override func viewDidLoad() {
1854 super.viewDidLoad()
1955
20− // Uncomment the following line to preserve selection between presentations
21− // self.clearsSelectionOnViewWillAppear = false
22−
23− // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
24− // self.navigationItem.rightBarButtonItem = self.editButtonItem
56+ tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
57+ myPermissions = canIEditThisChat()
58+ tableView.reloadData()
59+
60+ switch myPermissions {
61+ case .admin:
62+ navigationItem.rightBarButtonItem = self.editButtonItem
63+ default:
64+ return
65+ }
2566 }
2667
2768 // MARK: - Table view data source
2869
2970 override func numberOfSections(in tableView: UITableView) -> Int {
30− // #warning Incomplete implementation, return the number of sections
31− return 0
71+ return 3
3272 }
3373
3474 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
35− // #warning Incomplete implementation, return the number of rows
36− return 0
75+ switch section {
76+ case 0:
77+ return 1
78+ case 1:
79+ return 1
80+ default:
81+ return usersArray.count + (myPermissions == .ordinary ? 0 : 1)
82+ }
3783 }
3884
39− /*
40− override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
41− let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
42−
43− // Configure the cell...
85+
86+ override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
87+ {
88+ let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
89+
90+ if indexPath.row == 0 {
91+ switch indexPath.section {
92+ case 0:
93+ cell.textLabel?.text = groupChat!.name
94+ return cell
95+ case 1:
96+ cell.textLabel?.text = "Leave chat"
97+ cell.textLabel?.textColor = #colorLiteral(red: 1, green: 0.1491314173, blue: 0, alpha: 1)
98+ return cell
99+ default:
100+ switch myPermissions{
101+ case .admin:
102+ cell.textLabel?.text = "Add participants"
103+ cell.accessoryType = .disclosureIndicator
104+ case .ordinary:
105+ cell.textLabel?.text = name(for: users[usersArray[indexPath.row]])
106+ }
107+ return cell
108+ }
109+ }
110+
111+ switch myPermissions{
112+ case .admin:
113+ cell.textLabel?.text = name(for: users[usersArray[indexPath.row-1]])
114+ case .ordinary:
115+ cell.textLabel?.text = name(for: users[usersArray[indexPath.row]])
116+ }
44117
45118 return cell
46119 }
47− */
48−
49− /*
50− // Override to support conditional editing of the table view.
51− override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
52− // Return false if you do not want the specified item to be editable.
53− return true
120+
121+ private func name(for user: Model.Users?) -> String {
122+ return "👤 \(user!.fullName())"
54123 }
55− */
56−
57− /*
58− // Override to support editing the table view.
59− override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
60− if editingStyle == .delete {
61− // Delete the row from the data source
62− tableView.deleteRows(at: [indexPath], with: .fade)
63− } else if editingStyle == .insert {
64− // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
65− }
66− }
67− */
68−
69− /*
70− // Override to support rearranging the table view.
71− override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
72−
124+
125+ override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
126+ {
127+ switch section {
128+ case 0:
129+ return "Title"
130+ case 1:
131+ return "Opportunities"
132+ default:
133+ return "Participants"
134+ }
73135 }
74− */
75−
76− /*
77− // Override to support conditional rearranging of the table view.
78− override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
79− // Return false if you do not want the item to be re-orderable.
136+
137+
138+ func editName(for cell: UITableViewCell){
139+ let alert = UIAlertController(title: "Вы уверены?", message: "Название:", preferredStyle: .alert)
140+
141+ let action1 = UIAlertAction(title: "Oтмена", style: .cancel, handler: nil)
142+
143+ alert.addTextField { [weak self] (tf) in
144+ tf.textColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
145+ tf.text = self?.groupChat?.name
146+ }
147+
148+ let action2 = UIAlertAction(title: "OK", style: .default) { [unowned self] _ in
149+ let name = alert.textFields?.first?.text
150+ cell.textLabel?.text = name
151+ self.groupChat?.name = name!
152+ Model.updateGroupChat(with: self.groupChat!)
153+ self.delegate?.updateGroup(with: self.groupChat!)
154+ }
155+
156+ alert.addAction(action1)
157+ alert.addAction(action2)
158+
159+ present(alert, animated: true, completion: nil)
160+ }
161+ override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
162+ {
163+ guard let groupChat = groupChat else {
164+ return
165+ }
166+
167+ guard let cell = tableView.cellForRow(at: indexPath) else {
168+ return
169+ }
170+
171+ if indexPath.row == 0 {
172+ switch indexPath.section{
173+ case 0:
174+ switch myPermissions{
175+ case .admin:
176+ editName(for: cell)
177+ default:
178+ break
179+ }
180+ case 1:
181+ Model.leaveGroupChat(id: groupChat.id) {
182+ [weak self] in
183+ self?.navigationController?.popViewController(animated: true)
184+ }
185+ default:
186+ showUserChoiceVC()
187+ }
188+ }
189+ }
190+
191+ func showUserChoiceVC()
192+ {
193+ let vc = storyboard?.instantiateViewController(withIdentifier: peopleToWriteVC) as! PeopleToWriteViewController
194+
195+ vc.whatToDoWithSelection = { [weak self, weak vc] mapa in
196+
197+ mapa.forEach { self?.users[$0.key] = Model.Channels.fullPeopleDict[$0.key] }
198+ mapa.forEach { self?.groupChat?.users[$0.key] = $0.value }
199+ vc?.navigationController?.popViewController(animated: true)
200+ }
201+
202+ navigationController?.pushViewController(vc, animated: true)
203+ }
204+
205+ override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)
206+ {
207+ if editingStyle == UITableViewCell.EditingStyle.delete {
208+ groupChat?.users[usersArray[indexPath.row-1]] = nil
209+ }
210+
211+ tableView.reloadData()
212+ }
213+
214+ override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
215+ if indexPath.row == 0 {
216+ return false
217+ }
218+
80219 return true
81220 }
82− */
83−
84− /*
85− // MARK: - Navigation
86−
87− // In a storyboard-based application, you will often want to do a little preparation before navigation
88− override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
89− // Get the new view controller using segue.destination.
90− // Pass the selected object to the new view controller.
221+
222+ /// update chatInfo
223+ override func viewWillDisappear(_ animated: Bool) {
224+
225+ defer {
226+ super.viewWillDisappear(animated)
227+ }
228+
229+ guard let _ = self.navigationController?.viewControllers.lastIndex(of: self) else {
230+ switch myPermissions {
231+ case .ordinary:
232+ return
233+ case .admin:
234+ Model.updateGroupChat(with: groupChat!)
235+ delegate?.updateGroup(with: groupChat!)
236+ }
237+ return
238+ }
91239 }
92− */
93−
94240}