SingleChatDetailViewController.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //
  2. // ChatDetailViewController.swift
  3. // deltachat-ios
  4. //
  5. // Created by Bastian van de Wetering on 04.05.19.
  6. // Copyright © 2019 Jonas Reinsch. All rights reserved.
  7. //
  8. import UIKit
  9. class ChatDetailViewController: UIViewController {
  10. weak var coordinator: ChatDetailCoordinator?
  11. fileprivate let chat: MRChat
  12. var chatDetailTable: UITableView = {
  13. let table = UITableView(frame: .zero, style: .grouped)
  14. table.bounces = false
  15. table.register(UITableViewCell.self, forCellReuseIdentifier: "tableCell")
  16. table.register(ActionCell.self, forCellReuseIdentifier: "actionCell")
  17. table.register(ContactCell.self, forCellReuseIdentifier: "contactCell")
  18. return table
  19. }()
  20. init(chatId: Int) {
  21. self.chat = MRChat(id: chatId)
  22. super.init(nibName: nil, bundle: nil)
  23. setupSubviews()
  24. }
  25. override func viewWillAppear(_ animated: Bool) {
  26. chatDetailTable.reloadData() // to display updates
  27. }
  28. required init?(coder _: NSCoder) {
  29. fatalError("init(coder:) has not been implemented")
  30. }
  31. private func setupSubviews() {
  32. view.addSubview(chatDetailTable)
  33. chatDetailTable.translatesAutoresizingMaskIntoConstraints = false
  34. chatDetailTable.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
  35. chatDetailTable.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
  36. chatDetailTable.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
  37. chatDetailTable.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
  38. }
  39. @objc func editButtonPressed() {
  40. // will be overwritten
  41. }
  42. func showNotificationSetup() {
  43. let notificationSetupAlert = UIAlertController(title: "Notifications Setup is not implemented yet", message: "But you get an idea where this is going", preferredStyle: .actionSheet)
  44. let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
  45. notificationSetupAlert.addAction(cancelAction)
  46. present(notificationSetupAlert, animated: true, completion: nil)
  47. }
  48. }
  49. class SingleChatDetailViewController: ChatDetailViewController {
  50. var contact: MRContact? {
  51. if let id = chat.contactIds.first {
  52. return MRContact(id: id)
  53. }
  54. return nil
  55. }
  56. override func viewDidLoad() {
  57. super.viewDidLoad()
  58. title = "Info"
  59. navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Edit", style: .plain, target: self, action: #selector(editButtonPressed))
  60. chatDetailTable.delegate = self
  61. chatDetailTable.dataSource = self
  62. }
  63. @objc override func editButtonPressed() {
  64. if let id = chat.contactIds.first {
  65. coordinator?.showSingleChatEdit(contactId: id)
  66. }
  67. }
  68. }
  69. extension SingleChatDetailViewController: UITableViewDelegate, UITableViewDataSource {
  70. func numberOfSections(in tableView: UITableView) -> Int {
  71. return 2
  72. }
  73. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  74. return 1
  75. }
  76. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  77. if section == 0 {
  78. guard let contact = contact else {
  79. return nil
  80. }
  81. let bg = UIColor(red: 248 / 255, green: 248 / 255, blue: 255 / 255, alpha: 1.0)
  82. let contactCell = ContactCell()
  83. contactCell.backgroundColor = bg
  84. contactCell.nameLabel.text = contact.name
  85. contactCell.emailLabel.text = contact.email
  86. contactCell.darkMode = false
  87. contactCell.selectionStyle = .none
  88. if let img = chat.profileImage {
  89. contactCell.setImage(img)
  90. } else {
  91. contactCell.setBackupImage(name: contact.name, color: contact.color)
  92. }
  93. contactCell.setVerified(isVerified: chat.isVerified)
  94. return contactCell
  95. } else {
  96. return nil
  97. }
  98. }
  99. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  100. let section = indexPath.section
  101. if section == 0 {
  102. let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath)
  103. cell.textLabel?.text = "Notifications"
  104. return cell
  105. } else if section == 1 {
  106. let cell = tableView.dequeueReusableCell(withIdentifier: "actionCell", for: indexPath) as! ActionCell
  107. if let contact = contact {
  108. cell.actionTitle = contact.isBlocked ? "Unblock Contact" : "Block Contact"
  109. cell.actionColor = contact.isBlocked ? SystemColor.blue.uiColor : UIColor.red // SystemColor.red.uiColor
  110. }
  111. return cell
  112. }
  113. return UITableViewCell(frame: .zero)
  114. }
  115. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  116. let section = indexPath.section
  117. if section == 0 {
  118. showNotificationSetup()
  119. } else if section == 1 {
  120. if let contact = contact {
  121. contact.isBlocked ? contact.unblock() : contact.block()
  122. tableView.reloadData()
  123. }
  124. }
  125. }
  126. }
  127. class GroupChatDetailViewController: ChatDetailViewController {
  128. // var currentUserChatId:
  129. var groupMembers: [MRContact] {
  130. let ids = chat.contactIds
  131. return ids.map({MRContact(id: $0)})
  132. }
  133. override func viewDidLoad() {
  134. super.viewDidLoad()
  135. title = "Group Info"
  136. chatDetailTable.delegate = self
  137. chatDetailTable.dataSource = self
  138. }
  139. }
  140. extension GroupChatDetailViewController: UITableViewDelegate, UITableViewDataSource {
  141. func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  142. if section == 1 {
  143. return "Members:"
  144. }
  145. return nil
  146. }
  147. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  148. if section == 0 {
  149. let bg = UIColor(red: 248 / 255, green: 248 / 255, blue: 255 / 255, alpha: 1.0)
  150. let contactCell = ContactCell()
  151. contactCell.backgroundColor = bg
  152. contactCell.nameLabel.text = chat.name
  153. contactCell.emailLabel.text = chat.subtitle
  154. contactCell.darkMode = false
  155. contactCell.selectionStyle = .none
  156. if let img = chat.profileImage {
  157. contactCell.setImage(img)
  158. } else {
  159. contactCell.setBackupImage(name: chat.name, color: chat.color)
  160. }
  161. contactCell.setVerified(isVerified: chat.isVerified)
  162. return contactCell
  163. } else {
  164. return nil
  165. }
  166. }
  167. func numberOfSections(in tableView: UITableView) -> Int {
  168. return 3
  169. }
  170. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  171. if section == 0 {
  172. return 1
  173. } else if section == 1 {
  174. return groupMembers.count
  175. } else if section == 2 {
  176. return 1
  177. } else {
  178. return 0
  179. }
  180. }
  181. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  182. let section = indexPath.section
  183. let row = indexPath.row
  184. if section == 0 {
  185. let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath)
  186. cell.textLabel?.text = "Notifications"
  187. return cell
  188. } else if section == 1 {
  189. let cell = tableView.dequeueReusableCell(withIdentifier: "contactCell", for: indexPath) as! ContactCell
  190. let contact = groupMembers[row]
  191. cell.nameLabel.text = contact.name
  192. cell.emailLabel.text = contact.email
  193. cell.initialsLabel.text = Utils.getInitials(inputName: contact.name)
  194. cell.setColor(contact.color)
  195. return cell
  196. } else if section == 2 {
  197. let cell = tableView.dequeueReusableCell(withIdentifier: "actionCell", for: indexPath) as! ActionCell
  198. cell.actionTitle = "Leave Group"
  199. cell.actionColor = UIColor.red
  200. return cell
  201. }
  202. return UITableViewCell(frame: .zero)
  203. }
  204. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  205. let section = indexPath.section
  206. let row = indexPath.row
  207. if section == 0 {
  208. showNotificationSetup()
  209. } else if section == 1 {
  210. // ignore for now - in Telegram tapping a contactCell leads into ContactDetail
  211. } else if section == 2 {
  212. // leave group
  213. }
  214. }
  215. }