JCMineViewController.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // JCMineViewController.swift
  3. // JChat
  4. //
  5. // Created by deng on 2017/2/16.
  6. // Copyright © 2017年 HXHG. All rights reserved.
  7. //
  8. import UIKit
  9. import JMessage
  10. class JCMineViewController: UIViewController {
  11. //MARK: - life cycle
  12. override func viewDidLoad() {
  13. super.viewDidLoad()
  14. _init()
  15. }
  16. deinit {
  17. NotificationCenter.default.removeObserver(self)
  18. JMessage.remove(self, with: nil)
  19. }
  20. fileprivate lazy var tableview: UITableView = {
  21. let tableview = UITableView(frame: self.view.frame, style: .grouped)
  22. tableview.delegate = self
  23. tableview.dataSource = self
  24. tableview.separatorStyle = .none
  25. tableview.register(JCMineInfoCell.self, forCellReuseIdentifier: "JCMineInfoCell")
  26. tableview.register(JCMineAvatorCell.self, forCellReuseIdentifier: "JCMineAvatorCell")
  27. tableview.register(JCButtonCell.self, forCellReuseIdentifier: "JCButtonCell")
  28. return tableview
  29. }()
  30. //MARK: - private func
  31. private func _init() {
  32. view.backgroundColor = .white
  33. view.addSubview(tableview)
  34. JMessage.add(self, with: nil)
  35. NotificationCenter.default.addObserver(self, selector: #selector(_updateUserInfo), name: NSNotification.Name(rawValue: kUpdateUserInfo), object: nil)
  36. }
  37. func _updateUserInfo() {
  38. tableview.reloadData()
  39. }
  40. func updateCurrentUserAvator() {
  41. JMSGUser.myInfo().thumbAvatarData({ (data, id, error) in
  42. if let data = data {
  43. let imageData = NSKeyedArchiver.archivedData(withRootObject: data)
  44. UserDefaults.standard.set(imageData, forKey: kLastUserAvator)
  45. } else {
  46. UserDefaults.standard.removeObject(forKey: kLastUserAvator)
  47. }
  48. })
  49. }
  50. }
  51. extension JCMineViewController: JMessageDelegate {
  52. func onReceive(_ event: JMSGNotificationEvent!) {
  53. switch event.eventType {
  54. case .currentUserInfoChange:
  55. updateCurrentUserAvator()
  56. tableview.reloadData()
  57. default:
  58. break
  59. }
  60. }
  61. }
  62. extension JCMineViewController: UITableViewDelegate, UITableViewDataSource {
  63. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  64. if indexPath.section == 0 {
  65. return 85
  66. }
  67. if indexPath.section == 2 {
  68. return 40
  69. }
  70. return 45
  71. }
  72. func numberOfSections(in tableView: UITableView) -> Int {
  73. return 3
  74. }
  75. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  76. if section == 0 || section == 2 {
  77. return 1
  78. }
  79. return 4
  80. }
  81. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  82. if indexPath.section == 0 {
  83. return tableView.dequeueReusableCell(withIdentifier: "JCMineAvatorCell", for: indexPath)
  84. }
  85. if indexPath.section == 2 {
  86. return tableView.dequeueReusableCell(withIdentifier: "JCButtonCell", for: indexPath)
  87. }
  88. return tableView.dequeueReusableCell(withIdentifier: "JCMineInfoCell", for: indexPath)
  89. }
  90. func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  91. cell.selectionStyle = .none
  92. if indexPath.section == 2 {
  93. guard let cell = cell as? JCButtonCell else {
  94. return
  95. }
  96. cell.delegate = self
  97. return
  98. }
  99. cell.accessoryType = .disclosureIndicator
  100. if indexPath.section == 0 {
  101. guard let cell = cell as? JCMineAvatorCell else {
  102. return
  103. }
  104. let user = JMSGUser.myInfo()
  105. cell.baindDate(user: user)
  106. return
  107. }
  108. guard let cell = cell as? JCMineInfoCell else {
  109. return
  110. }
  111. if indexPath.section == 1 && indexPath.row == 1 {
  112. cell.delegate = self
  113. cell.accessoryType = .none
  114. cell.isShowSwitch = true
  115. }
  116. switch indexPath.row {
  117. case 0:
  118. cell.title = "修改密码"
  119. case 1:
  120. cell.isSwitchOn = JMessage.isSetGlobalNoDisturb()
  121. cell.title = "免打扰"
  122. case 2:
  123. cell.title = "意见反馈"
  124. case 3:
  125. cell.title = "关于JChat"
  126. default:
  127. break
  128. }
  129. }
  130. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  131. if indexPath.section == 0 {
  132. let vc = JCMyInfoViewController()
  133. navigationController?.pushViewController(vc, animated: true)
  134. return
  135. }
  136. switch indexPath.row {
  137. case 0:
  138. navigationController?.pushViewController(JCUpdatePassworkViewController(), animated: true)
  139. case 2:
  140. navigationController?.pushViewController(JCFeedbackViewController(), animated: true)
  141. case 3:
  142. navigationController?.pushViewController(JCJChatInfoViewController(), animated: true)
  143. default:
  144. break
  145. }
  146. }
  147. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  148. return 0.0001
  149. }
  150. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  151. if section == 0 {
  152. return 5
  153. }
  154. return 15
  155. }
  156. }
  157. extension JCMineViewController: UIAlertViewDelegate {
  158. func alertView(_ alertView: UIAlertView, clickedButtonAt buttonIndex: Int) {
  159. switch buttonIndex {
  160. case 1:
  161. JMSGUser.logout({ (result, error) in
  162. JCVerificationInfoDB.shareInstance.queue = nil
  163. UserDefaults.standard.removeObject(forKey: kCurrentUserName)
  164. UserDefaults.standard.removeObject(forKey: kCurrentUserPassword)
  165. // let appDelegate = UIApplication.shared.delegate
  166. // let window = appDelegate?.window!
  167. // window?.rootViewController = JCNavigationController(rootViewController: JCLoginViewController())
  168. })
  169. default:
  170. break
  171. }
  172. }
  173. }
  174. extension JCMineViewController: JCMineInfoCellDelegate {
  175. func mineInfoCell(clickSwitchButton button: UISwitch, indexPath: IndexPath?) {
  176. JMessage.setIsGlobalNoDisturb(button.isOn) { (result, error) in
  177. }
  178. }
  179. }
  180. extension JCMineViewController: JCButtonCellDelegate {
  181. func buttonCell(clickButton button: UIButton) {
  182. let alertView = UIAlertView(title: "", message: "确定要退出登录?", delegate: self, cancelButtonTitle: "取消", otherButtonTitles: "确定")
  183. alertView.show()
  184. }
  185. }