ProfileViewController.swift 5.3 KB

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