JCFriendSettingViewController.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // JCFriendSettingViewController.swift
  3. // JChat
  4. //
  5. // Created by deng on 2017/5/10.
  6. // Copyright © 2017年 HXHG. All rights reserved.
  7. //
  8. import UIKit
  9. import JMessage
  10. class JCFriendSettingViewController: UIViewController {
  11. var user: JMSGUser!
  12. override func viewDidLoad() {
  13. super.viewDidLoad()
  14. _init()
  15. }
  16. deinit {
  17. NotificationCenter.default.removeObserver(self)
  18. }
  19. fileprivate lazy var tableview: UITableView = {
  20. var tableview = UITableView(frame: CGRect(x: 0, y: 64, width: self.view.width, height: self.view.height - 64), style: .grouped)
  21. tableview.delegate = self
  22. tableview.dataSource = self
  23. tableview.register(JCMineInfoCell.self, forCellReuseIdentifier: "JCMineInfoCell")
  24. tableview.register(JCButtonCell.self, forCellReuseIdentifier: "JCButtonCell")
  25. tableview.separatorStyle = .none
  26. tableview.backgroundColor = UIColor(netHex: 0xe8edf3)
  27. return tableview
  28. }()
  29. //MARK: - private func
  30. private func _init() {
  31. self.title = "设置"
  32. automaticallyAdjustsScrollViewInsets = false
  33. view.addSubview(tableview)
  34. NotificationCenter.default.addObserver(self, selector: #selector(_updateFriendInfo), name: NSNotification.Name(rawValue: kUpdateFriendInfo), object: nil)
  35. }
  36. func _updateFriendInfo() {
  37. tableview.reloadData()
  38. }
  39. }
  40. //MARK: - UITableViewDataSource & UITableViewDelegate
  41. extension JCFriendSettingViewController: UITableViewDataSource, UITableViewDelegate {
  42. func numberOfSections(in tableView: UITableView) -> Int {
  43. if user.isFriend {
  44. return 2
  45. }
  46. return 1
  47. }
  48. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  49. if section == 0 {
  50. if user.isFriend {
  51. return 3
  52. }
  53. return 2
  54. }
  55. return 1
  56. }
  57. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  58. if indexPath.section == 0 {
  59. return 45
  60. }
  61. return 40
  62. }
  63. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  64. return 20
  65. }
  66. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  67. return 0.0001
  68. }
  69. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  70. if indexPath.section == 1 {
  71. return tableView.dequeueReusableCell(withIdentifier: "JCButtonCell", for: indexPath)
  72. }
  73. return tableView.dequeueReusableCell(withIdentifier: "JCMineInfoCell", for: indexPath)
  74. }
  75. func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  76. cell.selectionStyle = .none
  77. if indexPath.section == 1 {
  78. guard let cell = cell as? JCButtonCell else {
  79. return
  80. }
  81. cell.delegate = self
  82. cell.buttonColor = UIColor(netHex: 0xEB424D)
  83. cell.buttonTitle = "删除好友"
  84. }
  85. if indexPath.section == 0 {
  86. guard let cell = cell as? JCMineInfoCell else {
  87. return
  88. }
  89. if user.isFriend {
  90. switch indexPath.row {
  91. case 0:
  92. cell.title = "备注名"
  93. cell.accessoryType = .disclosureIndicator
  94. cell.detail = user.noteName ?? ""
  95. case 1:
  96. cell.title = "发送名片"
  97. cell.accessoryType = .disclosureIndicator
  98. case 2:
  99. cell.isSwitchOn = user.isInBlacklist
  100. cell.delegate = self
  101. cell.accessoryType = .none
  102. cell.isShowSwitch = true
  103. cell.title = "加入黑名单"
  104. default:
  105. break
  106. }
  107. } else {
  108. switch indexPath.row {
  109. case 0:
  110. cell.title = "发送名片"
  111. cell.accessoryType = .disclosureIndicator
  112. case 1:
  113. cell.isSwitchOn = user.isInBlacklist
  114. cell.delegate = self
  115. cell.accessoryType = .none
  116. cell.isShowSwitch = true
  117. cell.title = "加入黑名单"
  118. default:
  119. break
  120. }
  121. }
  122. }
  123. }
  124. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  125. if indexPath.section == 0 {
  126. switch indexPath.row {
  127. case 0:
  128. if user.isFriend {
  129. let vc = JCNoteNameViewController()
  130. vc.user = user
  131. navigationController?.pushViewController(vc, animated: true)
  132. } else {
  133. let vc = JCForwardViewController()
  134. vc.fromUser = user
  135. let nav = JCNavigationController(rootViewController: vc)
  136. present(nav, animated: true)
  137. }
  138. case 1:
  139. if user.isFriend {
  140. let vc = JCForwardViewController()
  141. vc.fromUser = user
  142. let nav = JCNavigationController(rootViewController: vc)
  143. present(nav, animated: true)
  144. }
  145. default:
  146. break
  147. }
  148. }
  149. }
  150. }
  151. extension JCFriendSettingViewController: JCButtonCellDelegate {
  152. func buttonCell(clickButton button: UIButton) {
  153. let alertView = UIAlertView(title: "删除好友", message: "是否确认删除该好友?", delegate: self, cancelButtonTitle: "取消", otherButtonTitles: "删除")
  154. alertView.show()
  155. }
  156. }
  157. extension JCFriendSettingViewController: UIAlertViewDelegate {
  158. func alertView(_ alertView: UIAlertView, clickedButtonAt buttonIndex: Int) {
  159. if buttonIndex == 1 {
  160. JMSGFriendManager.removeFriend(withUsername: user.username, appKey: user.appKey, completionHandler: { (result, error) in
  161. if error == nil {
  162. if JMSGConversation.singleConversation(withUsername: self.user.username) != nil {
  163. JMSGConversation.deleteSingleConversation(withUsername: self.user.username)
  164. NotificationCenter.default.post(name: Notification.Name(rawValue: kUpdateConversation), object: nil)
  165. }
  166. NotificationCenter.default.post(name: Notification.Name(rawValue: kUpdateFriendList), object: nil)
  167. self.navigationController?.popToRootViewController(animated: true)
  168. } else {
  169. MBProgressHUD_JChat.show(text: "\(String.errorAlert(error! as NSError))", view: self.view)
  170. }
  171. })
  172. }
  173. }
  174. }
  175. extension JCFriendSettingViewController: JCMineInfoCellDelegate {
  176. func mineInfoCell(clickSwitchButton button: UISwitch, indexPath: IndexPath?) {
  177. MBProgressHUD_JChat.showMessage(message: "修改中", toView: view)
  178. if button.isOn {
  179. JMSGUser.addUsers(toBlacklist: [user.username]) { (result, error) in
  180. MBProgressHUD_JChat.hide(forView: self.view, animated: true)
  181. if error == nil {
  182. MBProgressHUD_JChat.show(text: "修改成功", view: self.view)
  183. } else {
  184. button.isOn = !button.isOn
  185. MBProgressHUD_JChat.show(text: "\(String.errorAlert(error! as NSError))", view: self.view)
  186. }
  187. }
  188. } else {
  189. JMSGUser.delUsers(fromBlacklist: [user.username]) { (result, error) in
  190. MBProgressHUD_JChat.hide(forView: self.view, animated: true)
  191. if error == nil {
  192. MBProgressHUD_JChat.show(text: "修改成功", view: self.view)
  193. } else {
  194. button.isOn = !button.isOn
  195. MBProgressHUD_JChat.show(text: "\(String.errorAlert(error! as NSError))", view: self.view)
  196. }
  197. }
  198. }
  199. }
  200. }