JCUserInfoViewController.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //
  2. // JCUserInfoViewController.swift
  3. // JChat
  4. //
  5. // Created by deng on 2017/3/22.
  6. // Copyright © 2017年 HXHG. All rights reserved.
  7. //
  8. import UIKit
  9. import JMessage
  10. class JCUserInfoViewController: UIViewController {
  11. var user: JMSGUser!
  12. var isOnConversation = false
  13. var isOnAddFriend = false
  14. override func viewDidLoad() {
  15. super.viewDidLoad()
  16. _init()
  17. }
  18. deinit {
  19. NotificationCenter.default.removeObserver(self)
  20. }
  21. fileprivate lazy var tableview: UITableView = {
  22. var tableview = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.width, height: self.view.height), style: .grouped)
  23. tableview.delegate = self
  24. tableview.dataSource = self
  25. tableview.register(JCUserAvatorCell.self, forCellReuseIdentifier: "JCUserAvatorCell")
  26. tableview.register(JCUserInfoCell.self, forCellReuseIdentifier: "JCUserInfoCell")
  27. tableview.register(JCButtonCell.self, forCellReuseIdentifier: "JCButtonCell")
  28. tableview.register(JCDoubleButtonCell.self, forCellReuseIdentifier: "JCDoubleButtonCell")
  29. tableview.separatorStyle = .none
  30. tableview.backgroundColor = UIColor(netHex: 0xe8edf3)
  31. return tableview
  32. }()
  33. private lazy var moreButton = UIButton(frame: CGRect(x: 0, y: 0, width: 36, height: 36))
  34. //MARK: - private func
  35. private func _init() {
  36. self.title = "详细信息"
  37. automaticallyAdjustsScrollViewInsets = false
  38. view.addSubview(tableview)
  39. _setupNavigation()
  40. NotificationCenter.default.addObserver(self, selector: #selector(_updateUserInfo), name: NSNotification.Name(rawValue: kUpdateFriendInfo), object: nil)
  41. NotificationCenter.default.addObserver(self, selector: #selector(_updateUserInfo), name: NSNotification.Name(rawValue: kUpdateUserInfo), object: nil)
  42. }
  43. private func _setupNavigation() {
  44. moreButton.addTarget(self, action: #selector(_clickNavRightButton), for: .touchUpInside)
  45. moreButton.setImage(UIImage.loadImage("com_icon_more"), for: .normal)
  46. let item = UIBarButtonItem(customView: moreButton)
  47. navigationItem.rightBarButtonItem = item
  48. }
  49. @objc func _updateUserInfo() {
  50. tableview.reloadData()
  51. }
  52. @objc func _clickNavRightButton() {
  53. let vc = JCFriendSettingViewController()
  54. vc.user = self.user
  55. navigationController?.pushViewController(vc, animated: true)
  56. }
  57. }
  58. //MARK: - UITableViewDataSource & UITableViewDelegate
  59. extension JCUserInfoViewController: UITableViewDataSource, UITableViewDelegate {
  60. func numberOfSections(in tableView: UITableView) -> Int {
  61. return 2
  62. }
  63. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  64. if section == 1 {
  65. return 1
  66. }
  67. return 6
  68. }
  69. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  70. if indexPath.section == 0 && indexPath.row == 0 {
  71. return 175
  72. }
  73. if indexPath.section == 1 {
  74. return 40
  75. }
  76. return 45
  77. }
  78. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  79. if section == 0 {
  80. return 15
  81. }
  82. return 0.001
  83. }
  84. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  85. return 0.0001
  86. }
  87. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  88. if indexPath.section == 0 && indexPath.row == 0 {
  89. return tableView.dequeueReusableCell(withIdentifier: "JCUserAvatorCell", for: indexPath)
  90. }
  91. if indexPath.section == 1 {
  92. if user.isFriend || isOnAddFriend {
  93. return tableView.dequeueReusableCell(withIdentifier: "JCButtonCell", for: indexPath)
  94. } else {
  95. return tableView.dequeueReusableCell(withIdentifier: "JCDoubleButtonCell", for: indexPath)
  96. }
  97. }
  98. return tableView.dequeueReusableCell(withIdentifier: "JCUserInfoCell", for: indexPath)
  99. }
  100. func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  101. cell.selectionStyle = .none
  102. if indexPath.section == 0 && indexPath.row == 0 {
  103. guard let cell = cell as? JCUserAvatorCell else {
  104. return
  105. }
  106. cell.delegate = self
  107. cell.bindData(user: user)
  108. }
  109. if indexPath.section == 1 {
  110. if user.isFriend || isOnAddFriend {
  111. guard let cell = cell as? JCButtonCell else {
  112. return
  113. }
  114. cell.delegate = self
  115. if isOnAddFriend {
  116. cell.buttonTitle = "添加好友"
  117. } else {
  118. cell.buttonTitle = "发送消息"
  119. }
  120. } else {
  121. guard let cell = cell as? JCDoubleButtonCell else {
  122. return
  123. }
  124. cell.delegate = self
  125. }
  126. }
  127. if indexPath.section == 0 {
  128. guard let cell = cell as? JCUserInfoCell else {
  129. return
  130. }
  131. switch indexPath.row {
  132. case 1:
  133. cell.title = "昵称"
  134. cell.detail = user.nickname ?? ""
  135. cell.icon = UIImage.loadImage("com_icon_nickname")
  136. case 2:
  137. cell.title = "用户名"
  138. cell.detail = user.username
  139. cell.icon = UIImage.loadImage("com_icon_username")
  140. case 3:
  141. cell.title = "性别"
  142. cell.icon = UIImage.loadImage("com_icon_gender")
  143. switch user.gender {
  144. case .male:
  145. cell.detail = "男"
  146. case .female:
  147. cell.detail = "女"
  148. case .unknown:
  149. cell.detail = "保密"
  150. }
  151. case 4:
  152. cell.title = "生日"
  153. cell.icon = UIImage.loadImage("com_icon_birthday")
  154. cell.detail = user.birthday
  155. case 5:
  156. cell.title = "地区"
  157. cell.icon = UIImage.loadImage("com_icon_region")
  158. cell.detail = user.region
  159. default:
  160. break
  161. }
  162. }
  163. }
  164. }
  165. extension JCUserInfoViewController: JCButtonCellDelegate {
  166. func buttonCell(clickButton button: UIButton) {
  167. if isOnAddFriend {
  168. let vc = JCAddFriendViewController()
  169. vc.user = user
  170. navigationController?.pushViewController(vc, animated: true)
  171. return
  172. }
  173. if isOnConversation {
  174. for vc in (navigationController?.viewControllers)! {
  175. if vc is JCChatViewController {
  176. navigationController?.popToViewController(vc, animated: true)
  177. }
  178. }
  179. return
  180. }
  181. JMSGConversation.createSingleConversation(withUsername: (user?.username)!, appKey: (user?.appKey)!) { (result, error) in
  182. if error == nil {
  183. let conv = result as! JMSGConversation
  184. let vc = JCChatViewController(conversation: conv)
  185. NotificationCenter.default.post(name: NSNotification.Name(rawValue: kUpdateConversation), object: nil, userInfo: nil)
  186. self.navigationController?.pushViewController(vc, animated: true)
  187. }
  188. }
  189. }
  190. }
  191. extension JCUserInfoViewController: JCDoubleButtonCellDelegate {
  192. func doubleButtonCell(clickLeftButton button: UIButton) {
  193. let vc = JCAddFriendViewController()
  194. vc.user = user
  195. navigationController?.pushViewController(vc, animated: true)
  196. }
  197. func doubleButtonCell(clickRightButton button: UIButton) {
  198. JMSGConversation.createSingleConversation(withUsername: (user?.username)!, appKey: (user?.appKey)!) { (result, error) in
  199. if error == nil {
  200. let conv = result as! JMSGConversation
  201. let vc = JCChatViewController(conversation: conv)
  202. NotificationCenter.default.post(name: NSNotification.Name(rawValue: kUpdateConversation), object: nil, userInfo: nil)
  203. self.navigationController?.pushViewController(vc, animated: true)
  204. }
  205. }
  206. }
  207. }
  208. extension JCUserInfoViewController: JCUserAvatorCellDelegate {
  209. func tapAvator(_ image: UIImage?) {
  210. guard let image = image else {
  211. return
  212. }
  213. let browserImageVC = JCImageBrowserViewController()
  214. browserImageVC.imageArr = [image]
  215. browserImageVC.imgCurrentIndex = 0
  216. present(browserImageVC, animated: true) {
  217. }
  218. }
  219. }