ProfileViewController.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import UIKit
  2. class ProfileViewController: UITableViewController {
  3. weak var coordinator: ProfileCoordinator?
  4. var contact: MRContact? {
  5. // This is nil if we do not have an account setup yet
  6. if !MRConfig.configured {
  7. return nil
  8. }
  9. return MRContact(id: Int(DC_CONTACT_ID_SELF))
  10. }
  11. var fingerprint: String? {
  12. if !MRConfig.configured {
  13. return nil
  14. }
  15. if let cString = dc_get_securejoin_qr(mailboxPointer, 0) {
  16. return String(cString: cString)
  17. }
  18. return nil
  19. }
  20. init() {
  21. super.init(style: .plain)
  22. }
  23. required init?(coder _: NSCoder) {
  24. fatalError("init(coder:) has not been implemented")
  25. }
  26. override func viewDidLoad() {
  27. super.viewDidLoad()
  28. title = "My Profile"
  29. }
  30. override func viewWillAppear(_: Bool) {
  31. navigationController?.navigationBar.prefersLargeTitles = false
  32. tableView.reloadData()
  33. }
  34. func displayNewChat(contactId: Int) {
  35. let chatId = dc_create_chat_by_contact_id(mailboxPointer, UInt32(contactId))
  36. let chatVC = ChatViewController(chatId: Int(chatId))
  37. chatVC.hidesBottomBarWhenPushed = true
  38. navigationController?.pushViewController(chatVC, animated: true)
  39. }
  40. override func didReceiveMemoryWarning() {
  41. super.didReceiveMemoryWarning()
  42. // Dispose of any resources that can be recreated.
  43. }
  44. // MARK: - Table view data source
  45. override func numberOfSections(in _: UITableView) -> Int {
  46. return 2
  47. }
  48. override func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
  49. if section == 0 {
  50. return 2
  51. }
  52. return 0
  53. }
  54. override func tableView(_: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  55. let row = indexPath.row
  56. let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
  57. if indexPath.section == 0 {
  58. if row == 0 {
  59. if let fingerprint = self.fingerprint {
  60. cell.textLabel?.text = "Fingerprint: \(fingerprint)"
  61. cell.textLabel?.textAlignment = .center
  62. }
  63. }
  64. if row == 1 {
  65. if let fingerprint = self.fingerprint {
  66. let width: CGFloat = 130
  67. let frame = CGRect(origin: .zero, size: .init(width: width, height: width))
  68. let imageView = QRCodeView(frame: frame)
  69. imageView.generateCode(
  70. fingerprint,
  71. foregroundColor: .darkText,
  72. backgroundColor: .white
  73. )
  74. imageView.translatesAutoresizingMaskIntoConstraints = false
  75. // imageView.center = cell.center
  76. cell.addSubview(imageView)
  77. imageView.centerXAnchor.constraint(equalTo: cell.centerXAnchor).isActive = true
  78. imageView.centerYAnchor.constraint(equalTo: cell.centerYAnchor).isActive = true
  79. imageView.widthAnchor.constraint(equalToConstant: width).isActive = true
  80. imageView.heightAnchor.constraint(equalToConstant: width).isActive = true
  81. }
  82. }
  83. }
  84. if indexPath.section == 1 {
  85. if row == 0 {}
  86. }
  87. return cell
  88. }
  89. override func tableView(_: UITableView, didSelectRowAt _: IndexPath) {}
  90. override func tableView(_: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  91. if section == 0 {
  92. return 80
  93. }
  94. return 20
  95. }
  96. override func tableView(_: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  97. if indexPath.row == 1 {
  98. return 150
  99. }
  100. return 46
  101. }
  102. override func tableView(_: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  103. let bg = UIColor(red: 248 / 255, green: 248 / 255, blue: 255 / 255, alpha: 1.0)
  104. if section == 0 {
  105. let contactCell = ContactCell()
  106. if let contact = self.contact {
  107. let name = MRConfig.displayname ?? contact.name
  108. contactCell.backgroundColor = bg
  109. contactCell.nameLabel.text = name
  110. contactCell.emailLabel.text = contact.email
  111. contactCell.darkMode = false
  112. contactCell.selectionStyle = .none
  113. if let img = contact.profileImage {
  114. contactCell.setImage(img)
  115. } else {
  116. contactCell.setBackupImage(name: name, color: contact.color)
  117. }
  118. contactCell.setVerified(isVerified: contact.isVerified)
  119. } else {
  120. contactCell.nameLabel.text = "No Account set up"
  121. }
  122. return contactCell
  123. }
  124. let vw = UIView()
  125. vw.backgroundColor = bg
  126. return vw
  127. }
  128. }