NewGroupViewController.swift 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //
  2. // NewGroupViewController.swift
  3. // deltachat-ios
  4. //
  5. // Created by Alla Reinsch on 17.07.18.
  6. // Copyright © 2018 Jonas Reinsch. All rights reserved.
  7. //
  8. import UIKit
  9. class NewGroupViewController: UITableViewController {
  10. let contactCellReuseIdentifier = "xyz"
  11. var contactIds: [Int] = Utils.getContactIds()
  12. var contactIdsForGroup: Set<Int> = [] {
  13. didSet {
  14. let c = contactIdsForGroup.count
  15. navigationItem.prompt = "\(c) members and me"
  16. }
  17. }
  18. @objc func didPressGroupCreationNextButton() {
  19. navigationController?.pushViewController(GroupNameController(contactIdsForGroup: contactIdsForGroup), animated: true)
  20. }
  21. override func viewDidLoad() {
  22. super.viewDidLoad()
  23. title = "New Group"
  24. navigationItem.prompt = "0 members and me"
  25. tableView.register(ContactCell.self, forCellReuseIdentifier: contactCellReuseIdentifier)
  26. navigationController?.navigationBar.prefersLargeTitles = false
  27. let groupCreationNextButton = UIBarButtonItem(title: "Next", style: .done, target: self, action: #selector(didPressGroupCreationNextButton))
  28. navigationItem.rightBarButtonItem = groupCreationNextButton
  29. }
  30. override func didReceiveMemoryWarning() {
  31. super.didReceiveMemoryWarning()
  32. }
  33. override func numberOfSections(in _: UITableView) -> Int {
  34. return 1
  35. }
  36. override func tableView(_: UITableView, numberOfRowsInSection _: Int) -> Int {
  37. return contactIds.count
  38. }
  39. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  40. guard let cell: ContactCell = tableView.dequeueReusableCell(withIdentifier: contactCellReuseIdentifier, for: indexPath) as? ContactCell else {
  41. fatalError("shouldn't happen")
  42. }
  43. let row = indexPath.row
  44. let contactRow = row
  45. let contact = MRContact(id: contactIds[contactRow])
  46. cell.nameLabel.text = contact.name
  47. cell.emailLabel.text = contact.email
  48. cell.initialsLabel.text = Utils.getInitials(inputName: contact.name)
  49. cell.setColor(contact.color)
  50. return cell
  51. }
  52. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  53. let row = indexPath.row
  54. if let cell = tableView.cellForRow(at: indexPath) {
  55. tableView.deselectRow(at: indexPath, animated: true)
  56. let contactId = contactIds[row]
  57. if contactIdsForGroup.contains(contactId) {
  58. contactIdsForGroup.remove(contactId)
  59. cell.accessoryType = .none
  60. } else {
  61. contactIdsForGroup.insert(contactId)
  62. cell.accessoryType = .checkmark
  63. }
  64. }
  65. }
  66. }