JCMyInfoViewController.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. //
  2. // JCUserInfoViewController.swift
  3. // JChat
  4. //
  5. // Created by deng on 2017/3/16.
  6. // Copyright © 2017年 HXHG. All rights reserved.
  7. //
  8. import UIKit
  9. import YHPopupView
  10. class JCMyInfoViewController: UIViewController {
  11. override func viewDidLoad() {
  12. super.viewDidLoad()
  13. _init()
  14. }
  15. deinit {
  16. NotificationCenter.default.removeObserver(self)
  17. }
  18. fileprivate lazy var tableview: UITableView = {
  19. var tableview = UITableView(frame: CGRect(x: 0, y: 64, width: self.view.width, height: self.view.height - 64), style: .grouped)
  20. tableview.delegate = self
  21. tableview.dataSource = self
  22. tableview.register(JCMyAvatorCell.self, forCellReuseIdentifier: "JCMyAvatorCell")
  23. tableview.register(JCMyInfoCell.self, forCellReuseIdentifier: "JCMyInfoCell")
  24. tableview.separatorStyle = .none
  25. tableview.backgroundColor = UIColor(netHex: 0xe8edf3)
  26. return tableview
  27. }()
  28. fileprivate lazy var imagePicker: UIImagePickerController = {
  29. var picker = UIImagePickerController()
  30. picker.sourceType = .camera
  31. picker.cameraCaptureMode = .photo
  32. picker.allowsEditing = true
  33. picker.delegate = self
  34. return picker
  35. }()
  36. fileprivate lazy var user: JMSGUser = JMSGUser.myInfo()
  37. fileprivate lazy var datePickerPopupView: YHPopupView = {
  38. let popupView = YHPopupView(frame: CGRect(x: 0, y: self.view.height - 256, width: self.view.width, height: 256))
  39. var datePickerView = JCDatePickerViwe(frame: CGRect(x: 0, y: 0, width: self.view.width, height: 256))
  40. datePickerView.delegate = self
  41. popupView?.clickBlankSpaceDismiss = true
  42. popupView?.addSubview(datePickerView)
  43. return popupView!
  44. }()
  45. fileprivate lazy var areaPickerPopupView: YHPopupView = {
  46. let popupView = YHPopupView(frame: CGRect(x: 0, y: self.view.height - 256, width: self.view.width, height: 256))
  47. var areaPickerView = JCAreaPickerView(frame: CGRect(x: 0, y: 0, width: (popupView?.width)!, height: 256))
  48. areaPickerView.delegate = self
  49. popupView?.clickBlankSpaceDismiss = true
  50. popupView?.addSubview(areaPickerView)
  51. return popupView!
  52. }()
  53. //MARK: - private func
  54. private func _init() {
  55. self.title = "个人信息"
  56. automaticallyAdjustsScrollViewInsets = false
  57. view.addSubview(tableview)
  58. NotificationCenter.default.addObserver(self, selector: #selector(_updateUserInfo), name: NSNotification.Name(rawValue: kUpdateUserInfo), object: nil)
  59. }
  60. @objc func _updateUserInfo() {
  61. user = JMSGUser.myInfo()
  62. tableview.reloadData()
  63. }
  64. }
  65. //MARK: - UITableViewDataSource & UITableViewDelegate
  66. extension JCMyInfoViewController: UITableViewDataSource, UITableViewDelegate {
  67. func numberOfSections(in tableView: UITableView) -> Int {
  68. return 2
  69. }
  70. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  71. if section == 0 || section == 2 {
  72. return 1
  73. }
  74. return 6
  75. }
  76. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  77. if indexPath.section == 0 {
  78. return 153
  79. }
  80. return 45
  81. }
  82. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  83. if section == 1 {
  84. return 15
  85. }
  86. return 5
  87. }
  88. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  89. return 0.0001
  90. }
  91. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  92. if indexPath.section == 0 {
  93. return tableView.dequeueReusableCell(withIdentifier: "JCMyAvatorCell", for: indexPath)
  94. }
  95. return tableView.dequeueReusableCell(withIdentifier: "JCMyInfoCell", for: indexPath)
  96. }
  97. func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  98. cell.selectionStyle = .none
  99. if indexPath.section == 0 {
  100. guard let cell = cell as? JCMyAvatorCell else {
  101. return
  102. }
  103. cell.bindData(user: user)
  104. }
  105. if indexPath.section == 1 {
  106. guard let cell = cell as? JCMyInfoCell else {
  107. return
  108. }
  109. cell.accessoryType = .disclosureIndicator
  110. switch indexPath.row {
  111. case 0:
  112. cell.title = "昵称"
  113. cell.detail = user.nickname ?? ""
  114. cell.icon = UIImage.loadImage("com_icon_nickname")
  115. case 1:
  116. cell.title = "二维码"
  117. cell.detail = "我的二维码"
  118. cell.icon = UIImage.loadImage("com_icon_qrcode")
  119. case 2:
  120. cell.title = "性别"
  121. cell.icon = UIImage.loadImage("com_icon_gender")
  122. switch user.gender {
  123. case .male:
  124. cell.detail = "男"
  125. case .female:
  126. cell.detail = "女"
  127. case .unknown:
  128. cell.detail = "保密"
  129. }
  130. case 3:
  131. cell.title = "生日"
  132. cell.icon = UIImage.loadImage("com_icon_birthday")
  133. cell.detail = user.birthday
  134. case 4:
  135. cell.title = "地区"
  136. cell.icon = UIImage.loadImage("com_icon_region")
  137. cell.detail = user.region
  138. case 5:
  139. cell.title = "个性签名"
  140. cell.icon = UIImage.loadImage("com_icon_signature")
  141. cell.detail = user.signature
  142. default:
  143. break
  144. }
  145. }
  146. }
  147. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  148. if indexPath.section == 0 {
  149. let actionSheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: nil, otherButtonTitles: " 从相册中选择", "拍照")
  150. actionSheet.tag = 1001
  151. actionSheet.show(in: view)
  152. }
  153. if indexPath.section == 1 {
  154. switch indexPath.row {
  155. case 0:
  156. let vc = JCNicknameViewController()
  157. vc.nickName = user.nickname ?? ""
  158. navigationController?.pushViewController(vc, animated: true)
  159. case 1:
  160. navigationController?.pushViewController(MyQRCodeViewController(), animated: true)
  161. case 2:
  162. let actionSheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: nil, otherButtonTitles: "男", "女", "保密")
  163. actionSheet.tag = 1002
  164. actionSheet.show(in: view)
  165. case 3:
  166. presentPopupView(datePickerPopupView)
  167. case 4:
  168. presentPopupView(areaPickerPopupView)
  169. case 5:
  170. let vc = JCSignatureViewController()
  171. vc.signature = user.signature ?? ""
  172. navigationController?.pushViewController(vc, animated: true)
  173. default:
  174. break
  175. }
  176. }
  177. }
  178. }
  179. extension JCMyInfoViewController: UIActionSheetDelegate {
  180. func actionSheet(_ actionSheet: UIActionSheet, clickedButtonAt buttonIndex: Int) {
  181. let user = JMSGUser.myInfo()
  182. if actionSheet.tag == 1001 {
  183. switch buttonIndex {
  184. case 1:
  185. let picker = UIImagePickerController()
  186. picker.delegate = self
  187. picker.sourceType = .photoLibrary
  188. let temp_mediaType = UIImagePickerController.availableMediaTypes(for: picker.sourceType)
  189. picker.mediaTypes = temp_mediaType!
  190. picker.allowsEditing = true
  191. picker.modalTransitionStyle = .coverVertical
  192. present(picker, animated: true, completion: nil)
  193. case 2:
  194. present(imagePicker, animated: true, completion: nil)
  195. default:
  196. break
  197. }
  198. }
  199. if actionSheet.tag == 1002 {
  200. var index = buttonIndex
  201. if index == 0 {
  202. return
  203. }
  204. if index == 3 {
  205. index = 0
  206. }
  207. if index == Int((user.gender.rawValue)) {
  208. return
  209. }
  210. MBProgressHUD_JChat.showMessage(message: "修改中", toView: view)
  211. JMSGUser.updateMyInfo(withParameter: NSNumber(value: index), userFieldType: .fieldsGender, completionHandler: { (resultObject, error) -> Void in
  212. DispatchQueue.main.async {
  213. MBProgressHUD_JChat.hide(forView: self.view, animated: true)
  214. if error == nil {
  215. MBProgressHUD_JChat.show(text: "修改成功", view: self.view)
  216. self.tableview.reloadData()
  217. } else {
  218. MBProgressHUD_JChat.show(text: "\(String.errorAlert(error! as NSError))", view: self.view)
  219. }
  220. }
  221. })
  222. }
  223. }
  224. }
  225. extension JCMyInfoViewController: UINavigationControllerDelegate, UIImagePickerControllerDelegate {
  226. // MARK: - UIImagePickerControllerDelegate
  227. func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
  228. picker.dismiss(animated: true, completion: nil)
  229. }
  230. func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
  231. var image = info[UIImagePickerController.InfoKey.editedImage] as! UIImage?
  232. image = image?.fixOrientation()
  233. if image != nil {
  234. MBProgressHUD_JChat.showMessage(message: "正在上传", toView: view)
  235. guard let imageData = image?.jpegData(compressionQuality: 0.8) else {
  236. return
  237. }
  238. JMSGUser.updateMyInfo(withParameter: imageData, userFieldType: .fieldsAvatar) { (resultObject, error) -> Void in
  239. DispatchQueue.main.async(execute: { () -> Void in
  240. MBProgressHUD_JChat.hide(forView: self.view, animated: true)
  241. if error == nil {
  242. MBProgressHUD_JChat.show(text: "上传成功", view: self.view)
  243. NotificationCenter.default.post(name: Notification.Name(rawValue: kUpdateUserInfo), object: nil)
  244. self.tableview.reloadData()
  245. let avatorData = NSKeyedArchiver.archivedData(withRootObject: imageData)
  246. UserDefaults.standard.set(avatorData, forKey: kLastUserAvator)
  247. } else {
  248. MBProgressHUD_JChat.show(text: "上传失败", view: self.view)
  249. }
  250. })
  251. }
  252. }
  253. picker.dismiss(animated: true, completion: nil)
  254. }
  255. }
  256. extension JCMyInfoViewController: JCDatePickerViweDelegate {
  257. func datePicker(cancel cancelButton: UIButton, date: Date) {
  258. dismissPopupView()
  259. }
  260. func datePicker(finish finishButton: UIButton, date: Date) {
  261. dismissPopupView()
  262. MBProgressHUD_JChat.showMessage(message: "修改中", toView: view)
  263. JMSGUser.updateMyInfo(withParameter: NSNumber(value: date.timeIntervalSince1970), userFieldType: .fieldsBirthday, completionHandler: { (resultObject, error) -> Void in
  264. DispatchQueue.main.async {
  265. MBProgressHUD_JChat.hide(forView: self.view, animated: true)
  266. if error == nil {
  267. MBProgressHUD_JChat.show(text: "修改成功", view: self.view)
  268. self._updateUserInfo()
  269. } else {
  270. MBProgressHUD_JChat.show(text: "修改失败", view: self.view)
  271. }
  272. }
  273. })
  274. }
  275. }
  276. extension JCMyInfoViewController: JCAreaPickerViewDelegate {
  277. func areaPickerView(_ areaPickerView: JCAreaPickerView, didSelect button: UIButton, selectLocate locate: JCLocation) {
  278. dismissPopupView()
  279. let region = locate.province + locate.city + locate.area
  280. JMSGUser.updateMyInfo(withParameter: region, userFieldType: .fieldsRegion, completionHandler: { (resultObject, error) -> Void in
  281. DispatchQueue.main.async {
  282. MBProgressHUD_JChat.hide(forView: self.view, animated: true)
  283. if error == nil {
  284. MBProgressHUD_JChat.show(text: "修改成功", view: self.view)
  285. self._updateUserInfo()
  286. } else {
  287. MBProgressHUD_JChat.show(text: "修改失败", view: self.view)
  288. }
  289. }
  290. })
  291. }
  292. func areaPickerView(_ areaPickerView: JCAreaPickerView, cancleSelect button: UIButton) {
  293. dismissPopupView()
  294. }
  295. }