IHNewRoomManager.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //
  2. // IHNewRoomManager.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2019/12/13.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. import Photos
  10. import AVFoundation
  11. import PKHUD
  12. class IHNewRoomManager: NSObject ,IHViewManagerProtocolDelegate{
  13. var buildId:String? = "0"
  14. lazy var mainView: IHNewRoomView = {
  15. let mainView = Bundle.main.loadNibNamed("IHNewRoomView", owner: nil, options: nil)?.last as! IHNewRoomView
  16. mainView.delegate = self
  17. return mainView
  18. }()
  19. // private var vc = UIViewController()
  20. private weak var vc : UIViewController?
  21. let imagePicker = UIImagePickerController()
  22. func bindController(_ vc: UIViewController) {
  23. self.vc = vc
  24. self.vc?.navigationBarTitle = "新增房间"
  25. createUI()
  26. imagePicker.delegate = self
  27. self.imagePicker.modalPresentationStyle = .fullScreen
  28. if Intermediate.newRoomInfo != nil {
  29. self.mainView.roomInfo = Intermediate.newRoomInfo
  30. }
  31. }
  32. func shouldPopViewController() -> Bool {
  33. if mainView.isBeginEdit == true{
  34. let alert = UIAlertController(title: "", message: "是否保存编辑结果", preferredStyle: .alert)
  35. let save = UIAlertAction(title: "保存", style: .destructive) { (action) in
  36. Intermediate.isSaveRoomInfo = "1"
  37. self.mainView.shouldPop()
  38. }
  39. let cancel = UIAlertAction(title: "取消", style: .default) { (action) in
  40. self.vc?.navigationController?.popViewController(animated: true)
  41. Intermediate.isSaveRoomInfo = "0"
  42. Intermediate.newRoomInfo = nil
  43. self.mainView.roomInfo = nil
  44. }
  45. alert.addAction(cancel)
  46. alert.addAction(save)
  47. self.vc?.present(alert, animated: true, completion: nil)
  48. return false
  49. }else{
  50. return true
  51. }
  52. }
  53. func listenViewWillApper() {
  54. mainView.equipmentArr = Intermediate.EquipmentArr
  55. }
  56. func createUI() {
  57. mainView.frame = self.vc!.view.bounds
  58. self.vc!.view.addSubview(mainView)
  59. let newRoomVC = self.vc as! IHNewRoomVCtr
  60. mainView.floorList = newRoomVC.floorList
  61. self.buildId = newRoomVC.buildId
  62. }
  63. }
  64. extension IHNewRoomManager:IHNewRoomViewDelegate{
  65. func popController(with roomInfo: NewRoomInfo?, error: IHNewRoomViewEditError) {
  66. switch error {
  67. case .success:
  68. Intermediate.newRoomInfo = roomInfo
  69. self.vc?.navigationController?.popViewController(animated: true)
  70. //上传
  71. print(error)
  72. case .lackRoomName:
  73. print(error)
  74. g_showHUD("缺少房间名称")
  75. case .lackSN:
  76. print(error)
  77. g_showHUD("缺少SN号")
  78. case .lackEquipment:
  79. g_showHUD("缺少设备")
  80. print(error)
  81. case .lackImage:
  82. g_showHUD("缺少房间图片")
  83. }
  84. }
  85. func preserveInNewRoom(_ roomInfo: NewRoomInfo?, error: IHNewRoomViewEditError) {
  86. switch error {
  87. case .success,.lackEquipment:
  88. //上传
  89. IHNewRoomService.share.editRoomInfo(buildId: self.buildId!, roomInfo: roomInfo!, roomId: nil) {
  90. Intermediate.newRoomInfo = roomInfo
  91. self.vc?.navigationController?.popViewController(animated: true)
  92. Intermediate.newRoomInfo = nil
  93. self.mainView.roomInfo = nil
  94. NotificationCenter.default.post(name: NSNotification.Name(kNotificationIHAreaServiceNotificationReloadFloorList), object: nil)
  95. }
  96. //上传成功
  97. print(error)
  98. case .lackRoomName:
  99. print(error)
  100. g_showHUD("lack of room name")
  101. case .lackSN:
  102. print(error)
  103. g_showHUD("lack of SN number")
  104. // case .lackEquipment:
  105. // // g_showHUD("lack of euipment")
  106. // print(error)
  107. case .lackImage:
  108. g_showHUD("缺少房间图片")
  109. }
  110. }
  111. func addNewEquipmentInNewRoom() {
  112. let newEquipment = IHNewEquipmentVCtr()
  113. newEquipment.roomStyle = .newRoom
  114. newEquipment.navigationBarTitle = "新增设备"
  115. self.vc?.navigationController?.pushViewController(newEquipment, animated: true)
  116. }
  117. func selectedPhoto() {
  118. let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
  119. alert.addAction(UIAlertAction(title: "相册", style: .default, handler: { (button) in
  120. if self.checkAlbum() == true{
  121. self.presentToImagePickerController(.photoLibrary)
  122. }else{
  123. self.showAlart(title: "提示", message: "相册权限未启用,请进入系统[设置]>[隐私]>[照片]启用相册功能")
  124. }
  125. }))
  126. alert.addAction(UIAlertAction(title: "相机", style: .default, handler: { (button) in
  127. //判断是否支持使用相册
  128. if UIImagePickerController.isSourceTypeAvailable(.camera){
  129. if self.checkCamera() == true {
  130. self.presentToImagePickerController(.camera)
  131. }else{
  132. self.showAlart(title: "提示", message: "相机权限未开启,请进入系统[设置]>[隐私]>[相机]开启相机功能")
  133. return
  134. }
  135. }else{
  136. g_showHUD("该设备不支持相机功能")
  137. }
  138. }))
  139. alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
  140. self.vc?.present(alert, animated: true, completion: nil)
  141. }
  142. func selectedFloorInNewRoom() {
  143. }
  144. func selectedModeInNewRoom() {
  145. }
  146. func preserveInNewRoom() {
  147. }
  148. func presentToImagePickerController(_ type : UIImagePickerController.SourceType) {
  149. self.imagePicker.sourceType = type
  150. self.vc?.present(self.imagePicker, animated: true, completion: nil)
  151. }
  152. func showAlart(title:String,message:String) {
  153. let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
  154. let action = UIAlertAction(title: "I know", style: .default) { (action) in
  155. }
  156. alert.addAction(action)
  157. self.vc?.present(alert, animated: true, completion: nil)
  158. }
  159. func checkAlbum() -> Bool {
  160. let status = PHPhotoLibrary.authorizationStatus()
  161. if status == .denied || status == .restricted {
  162. return false
  163. }
  164. return true
  165. }
  166. func checkCamera() -> Bool {
  167. let status = AVCaptureDevice.authorizationStatus(for: .video)
  168. if status == .restricted || status == .denied {
  169. return false
  170. }
  171. return true
  172. }
  173. }
  174. extension IHNewRoomManager : UIImagePickerControllerDelegate, UINavigationControllerDelegate {
  175. func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
  176. guard let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else { return }
  177. //imageView.image = pickedImage
  178. log.debug("pickedImage - \(pickedImage)")
  179. NotificationCenter.default.post(name: NSNotification.Name(kNotificationIHNewRoomViewGetRoomImage), object: pickedImage)
  180. self.vc?.dismiss(animated: true, completion: nil)
  181. //上传图片
  182. //let imageData = pickedImage.pngData()
  183. HUD.show(.progress)
  184. IHNewEquipmentService.share.uploadImage(file: pickedImage, requestSuccess: { (path) in
  185. log.debug("图片地址 - \(path)")
  186. NotificationCenter.default.post(name: NSNotification.Name(kNotifactionIHNewRoomViewUploadImageFinished), object: path)
  187. HUD.flash(.success, delay: 1)
  188. }) {
  189. log.debug("上传失败")
  190. HUD.flash(.error, delay: 1)
  191. }
  192. }
  193. }