JCSearchFriendViewController.swift 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //
  2. // JCSearchFriendViewController.swift
  3. // JChat
  4. //
  5. // Created by deng on 2017/4/27.
  6. // Copyright © 2017年 HXHG. All rights reserved.
  7. //
  8. import UIKit
  9. import JMessage
  10. class JCSearchFriendViewController: UIViewController {
  11. var isSearchUser: Bool = false
  12. override func viewDidLoad() {
  13. super.viewDidLoad()
  14. _init()
  15. }
  16. override func viewWillAppear(_ animated: Bool) {
  17. super.viewWillAppear(animated)
  18. if isActive {
  19. searchController.isActive = true
  20. }
  21. }
  22. override func viewWillDisappear(_ animated: Bool) {
  23. super.viewDidDisappear(animated)
  24. if searchController.isActive {
  25. searchController.isActive = false
  26. isActive = true
  27. }
  28. }
  29. deinit {
  30. NotificationCenter.default.removeObserver(self)
  31. }
  32. private var isActive = false
  33. fileprivate lazy var searchController: JCSearchController = JCSearchController(searchResultsController: nil)
  34. private lazy var searchView: UIView = UIView(frame: CGRect(x: 0, y: self.topOffset, width: self.view.width, height: 31))
  35. fileprivate lazy var bgView: UIView = UIView(frame: CGRect(x: 0, y: self.topOffset + 31 + 5, width: self.view.width, height: self.view.height - 31 - 5))
  36. fileprivate lazy var infoView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.width, height: 65.5))
  37. fileprivate lazy var avatorView: UIImageView = UIImageView(frame: CGRect(x: 15, y: 7.5, width: 50, height: 50))
  38. fileprivate lazy var nameLabel: UILabel = UILabel(frame: CGRect(x: 65 + 10, y: 21, width: 200, height: 22.5))
  39. private let width = UIScreen.main.applicationFrame.size.width
  40. fileprivate lazy var addButton = UIButton()
  41. fileprivate lazy var tipsView: UIView = {
  42. let view = UIView(frame: CGRect(x: 0, y: 0, width: self.view.width, height: 67))
  43. view.backgroundColor = .white
  44. let tips = UILabel()
  45. tips.frame = view.frame
  46. tips.font = UIFont.systemFont(ofSize: 16)
  47. tips.textColor = UIColor(netHex: 0x999999)
  48. tips.textAlignment = .center
  49. tips.text = "未搜索到用户"
  50. view.addSubview(tips)
  51. return view
  52. }()
  53. private lazy var networkErrorView: UIView = {
  54. let tipsView = UIView(frame: CGRect(x: 0, y: self.topOffset + 36, width: self.view.width, height: self.view.height))
  55. var tipsLabel: UILabel = UILabel(frame: CGRect(x: 0, y: self.topOffset, width: tipsView.width, height: 22.5))
  56. tipsLabel.textColor = UIColor(netHex: 0x999999)
  57. tipsLabel.textAlignment = .center
  58. tipsLabel.font = UIFont.systemFont(ofSize: 16)
  59. tipsLabel.text = "无法连接网络"
  60. tipsView.addSubview(tipsLabel)
  61. tipsView.isHidden = true
  62. tipsView.backgroundColor = .white
  63. return tipsView
  64. }()
  65. fileprivate var user: JMSGUser?
  66. private var topOffset: CGFloat {
  67. if #available(iOS 11, *){
  68. if isIPhoneX {
  69. return 22
  70. }
  71. return 0
  72. }else{
  73. if isIPhoneX {
  74. return 22
  75. }
  76. return 0
  77. }
  78. }
  79. private func _init() {
  80. view.backgroundColor = UIColor(netHex: 0xE8EDF3)
  81. automaticallyAdjustsScrollViewInsets = false
  82. if isSearchUser {
  83. self.title = "发起单聊"
  84. } else {
  85. self.title = "添加好友"
  86. addButton.frame = CGRect(x: self.width - 72 - 15, y: 20, width: 72, height: 30)
  87. addButton.setTitle("加好友", for: .normal)
  88. addButton.titleLabel?.font = UIFont.systemFont(ofSize: 14)
  89. let image = UIImage.createImage(color: UIColor(netHex: 0x2dd0cf), size: CGSize(width: 72, height: 25))
  90. addButton.setBackgroundImage(image, for: .normal)
  91. addButton.layer.cornerRadius = 2
  92. addButton.layer.masksToBounds = true
  93. addButton.addTarget(self, action: #selector(_addFriend), for: .touchUpInside)
  94. infoView.backgroundColor = .white
  95. infoView.addSubview(addButton)
  96. }
  97. searchController.searchBar.placeholder = "搜索用户名"
  98. searchController.hidesNavigationBarDuringPresentation = false
  99. searchController.searchControllerDelegate = self
  100. searchController.searchBar.delegate = self
  101. searchView.addSubview(searchController.searchBar)
  102. view.addSubview(searchView)
  103. nameLabel.textColor = UIColor(netHex: 0x2C2C2C)
  104. nameLabel.font = UIFont.systemFont(ofSize: 18)
  105. let line = UILabel(frame: CGRect(x: 15, y: 50 + 15, width: view.width - 30, height: 0.5))
  106. line.backgroundColor = UIColor(netHex: 0xD9D9D9)
  107. tipsView.isHidden = true
  108. infoView.addSubview(avatorView)
  109. infoView.addSubview(nameLabel)
  110. infoView.addSubview(line)
  111. infoView.addSubview(tipsView)
  112. let tapGR = UITapGestureRecognizer(target: self, action: #selector(_tapHandler))
  113. infoView.addGestureRecognizer(tapGR)
  114. bgView.backgroundColor = .white
  115. bgView.addSubview(infoView)
  116. bgView.isHidden = true
  117. view.addSubview(bgView)
  118. view.addSubview(networkErrorView)
  119. if JCNetworkManager.isNotReachable {
  120. networkErrorView.isHidden = false
  121. }
  122. NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged(note:)), name: NSNotification.Name(rawValue: "kNetworkReachabilityChangedNotification"), object: nil)
  123. }
  124. @objc func reachabilityChanged(note: NSNotification) {
  125. if let curReach = note.object as? Reachability {
  126. let status = curReach.currentReachabilityStatus()
  127. switch status {
  128. case NotReachable:
  129. networkErrorView.isHidden = false
  130. default :
  131. networkErrorView.isHidden = true
  132. }
  133. }
  134. }
  135. @objc func _tapHandler(sender:UITapGestureRecognizer) {
  136. if (user?.isEqual(to: JMSGUser.myInfo()))! {
  137. searchController.isActive = false
  138. navigationController?.pushViewController(JCMyInfoViewController(), animated: true)
  139. return
  140. }
  141. let vc = JCUserInfoViewController()
  142. vc.user = user
  143. if !isSearchUser && !(user?.isFriend)! {
  144. vc.isOnAddFriend = true
  145. }
  146. searchController.isActive = false
  147. navigationController?.pushViewController(vc, animated: true)
  148. }
  149. @objc func _addFriend() {
  150. let vc = JCAddFriendViewController()
  151. vc.user = self.user!
  152. searchController.isActive = false
  153. navigationController?.pushViewController(vc, animated: true)
  154. }
  155. }
  156. extension JCSearchFriendViewController: JCSearchControllerDelegate, UISearchBarDelegate {
  157. func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
  158. bgView.isHidden = true
  159. }
  160. func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
  161. searchBar.showsCancelButton = true
  162. for view in (searchBar.subviews.first?.subviews)! {
  163. if view is UIButton {
  164. let cancelButton = view as! UIButton
  165. cancelButton.setTitleColor(UIColor(netHex: 0x999999), for: .normal)
  166. break
  167. }
  168. }
  169. }
  170. func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
  171. guard let name = searchBar.text else {
  172. return
  173. }
  174. if name.isEmpty {
  175. return
  176. }
  177. MBProgressHUD_JChat.showMessage(message: "查找中", toView: view)
  178. JMSGUser.userInfoArray(withUsernameArray: [name]) { (result, error) in
  179. self.bgView.isHidden = false
  180. MBProgressHUD_JChat.hide(forView: self.view, animated: true)
  181. if error == nil {
  182. self.tipsView.isHidden = true
  183. let users = result as! [JMSGUser]
  184. let user = users.first
  185. self.user = user
  186. if (user?.isFriend)! || (user?.isEqual(to: JMSGUser.myInfo()))! {
  187. self.addButton.isHidden = true
  188. } else {
  189. self.addButton.isHidden = false
  190. }
  191. self.nameLabel.text = user?.displayName()
  192. self.avatorView.image = UIImage.loadImage("com_icon_user_50")
  193. user?.thumbAvatarData({ (data, id, error) in
  194. if data != nil {
  195. let image = UIImage(data: data!)
  196. self.avatorView.image = image
  197. }
  198. })
  199. } else {
  200. self.tipsView.isHidden = false
  201. }
  202. }
  203. }
  204. }