123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- //
- // IHNewRoomManager.swift
- // Inhealth
- //
- // Created by weclouds on 2019/12/13.
- // Copyright © 2019 weclouds. All rights reserved.
- //
- import UIKit
- import Photos
- import AVFoundation
- import PKHUD
- class IHNewRoomManager: NSObject ,IHViewManagerProtocolDelegate{
- var buildId:String? = "0"
- lazy var mainView: IHNewRoomView = {
- let mainView = Bundle.main.loadNibNamed("IHNewRoomView", owner: nil, options: nil)?.last as! IHNewRoomView
- mainView.delegate = self
- return mainView
- }()
-
- // private var vc = UIViewController()
- private weak var vc : UIViewController?
-
- let imagePicker = UIImagePickerController()
- func bindController(_ vc: UIViewController) {
- self.vc = vc
- self.vc?.navigationBarTitle = "新增房间"
- createUI()
- imagePicker.delegate = self
- self.imagePicker.modalPresentationStyle = .fullScreen
- if Intermediate.newRoomInfo != nil {
- self.mainView.roomInfo = Intermediate.newRoomInfo
- }
- }
-
- func shouldPopViewController() -> Bool {
- if mainView.isBeginEdit == true{
- let alert = UIAlertController(title: "", message: "是否保存编辑结果", preferredStyle: .alert)
- let save = UIAlertAction(title: "保存", style: .destructive) { (action) in
- Intermediate.isSaveRoomInfo = "1"
- self.mainView.shouldPop()
- }
- let cancel = UIAlertAction(title: "取消", style: .default) { (action) in
- self.vc?.navigationController?.popViewController(animated: true)
- Intermediate.isSaveRoomInfo = "0"
- Intermediate.newRoomInfo = nil
- self.mainView.roomInfo = nil
- }
- alert.addAction(cancel)
- alert.addAction(save)
-
- self.vc?.present(alert, animated: true, completion: nil)
- return false
- }else{
- return true
- }
- }
- func listenViewWillApper() {
- mainView.equipmentArr = Intermediate.EquipmentArr
-
- }
- func createUI() {
-
- mainView.frame = self.vc!.view.bounds
- self.vc!.view.addSubview(mainView)
- let newRoomVC = self.vc as! IHNewRoomVCtr
- mainView.floorList = newRoomVC.floorList
- self.buildId = newRoomVC.buildId
- }
- }
- extension IHNewRoomManager:IHNewRoomViewDelegate{
-
-
- func popController(with roomInfo: NewRoomInfo?, error: IHNewRoomViewEditError) {
- switch error {
- case .success:
- Intermediate.newRoomInfo = roomInfo
- self.vc?.navigationController?.popViewController(animated: true)
- //上传
- print(error)
- case .lackRoomName:
- print(error)
- g_showHUD("缺少房间名称")
- case .lackSN:
- print(error)
- g_showHUD("缺少SN号")
- case .lackEquipment:
- g_showHUD("缺少设备")
- print(error)
- case .lackImage:
- g_showHUD("缺少房间图片")
- }
- }
-
- func preserveInNewRoom(_ roomInfo: NewRoomInfo?, error: IHNewRoomViewEditError) {
-
- switch error {
- case .success,.lackEquipment:
-
- //上传
- IHNewRoomService.share.editRoomInfo(buildId: self.buildId!, roomInfo: roomInfo!, roomId: nil) {
-
- Intermediate.newRoomInfo = roomInfo
- self.vc?.navigationController?.popViewController(animated: true)
- Intermediate.newRoomInfo = nil
- self.mainView.roomInfo = nil
- NotificationCenter.default.post(name: NSNotification.Name(kNotificationIHAreaServiceNotificationReloadFloorList), object: nil)
- }
- //上传成功
- print(error)
- case .lackRoomName:
- print(error)
- g_showHUD("lack of room name")
- case .lackSN:
- print(error)
- g_showHUD("lack of SN number")
- // case .lackEquipment:
- // // g_showHUD("lack of euipment")
- // print(error)
- case .lackImage:
- g_showHUD("缺少房间图片")
- }
- }
-
-
- func addNewEquipmentInNewRoom() {
- let newEquipment = IHNewEquipmentVCtr()
- newEquipment.roomStyle = .newRoom
- newEquipment.navigationBarTitle = "新增设备"
- self.vc?.navigationController?.pushViewController(newEquipment, animated: true)
- }
- func selectedPhoto() {
- let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
-
- alert.addAction(UIAlertAction(title: "相册", style: .default, handler: { (button) in
- if self.checkAlbum() == true{
- self.presentToImagePickerController(.photoLibrary)
- }else{
- self.showAlart(title: "提示", message: "相册权限未启用,请进入系统[设置]>[隐私]>[照片]启用相册功能")
- }
- }))
-
- alert.addAction(UIAlertAction(title: "相机", style: .default, handler: { (button) in
- //判断是否支持使用相册
- if UIImagePickerController.isSourceTypeAvailable(.camera){
- if self.checkCamera() == true {
- self.presentToImagePickerController(.camera)
- }else{
- self.showAlart(title: "提示", message: "相机权限未开启,请进入系统[设置]>[隐私]>[相机]开启相机功能")
- return
- }
- }else{
- g_showHUD("该设备不支持相机功能")
- }
- }))
-
- alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
-
- self.vc?.present(alert, animated: true, completion: nil)
- }
- func selectedFloorInNewRoom() {
- }
-
- func selectedModeInNewRoom() {
-
- }
-
- func preserveInNewRoom() {
-
- }
-
-
- func presentToImagePickerController(_ type : UIImagePickerController.SourceType) {
- self.imagePicker.sourceType = type
- self.vc?.present(self.imagePicker, animated: true, completion: nil)
- }
-
- func showAlart(title:String,message:String) {
- let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
- let action = UIAlertAction(title: "I know", style: .default) { (action) in
-
- }
- alert.addAction(action)
- self.vc?.present(alert, animated: true, completion: nil)
-
- }
- func checkAlbum() -> Bool {
- let status = PHPhotoLibrary.authorizationStatus()
- if status == .denied || status == .restricted {
- return false
- }
- return true
- }
- func checkCamera() -> Bool {
- let status = AVCaptureDevice.authorizationStatus(for: .video)
- if status == .restricted || status == .denied {
-
- return false
- }
- return true
- }
-
-
- }
- extension IHNewRoomManager : UIImagePickerControllerDelegate, UINavigationControllerDelegate {
- func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
- guard let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else { return }
- //imageView.image = pickedImage
- log.debug("pickedImage - \(pickedImage)")
- NotificationCenter.default.post(name: NSNotification.Name(kNotificationIHNewRoomViewGetRoomImage), object: pickedImage)
- self.vc?.dismiss(animated: true, completion: nil)
- //上传图片
- //let imageData = pickedImage.pngData()
- HUD.show(.progress)
- IHNewEquipmentService.share.uploadImage(file: pickedImage, requestSuccess: { (path) in
- log.debug("图片地址 - \(path)")
-
- NotificationCenter.default.post(name: NSNotification.Name(kNotifactionIHNewRoomViewUploadImageFinished), object: path)
- HUD.flash(.success, delay: 1)
- }) {
- log.debug("上传失败")
- HUD.flash(.error, delay: 1)
- }
-
- }
-
- }
|