IHAssociateGatewayView.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // IHAssociateGatewayView.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/8/25.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. import JXSegmentedView
  10. import PKHUD
  11. protocol IHAssociateGatewayViewDelete : NSObjectProtocol {
  12. // func emptyViewSearchDataSegment(_ index:Int)
  13. // func deviceConnectGateway(_ deviceId:String)
  14. // func deviceDisconteGateway(_ deviceId:String)
  15. // func controlightOn(_ isTurnON:Bool,lampId:String)
  16. //添加设备
  17. func deviceConnectGateway(_ deviceUUId:String)
  18. //删除设备
  19. func deviceDisconteGateway(_ deviceAddress:String)
  20. //搜索相应设备
  21. func emptyViewSearchDataSegment(_ index:Int)
  22. //开关灯(有485/cw)
  23. func controlightOn(_ isTurnON : Bool ,_ device :(address : String ,type : String))
  24. }
  25. class IHAssociateGatewayView: UIView {
  26. weak var delegate:IHAssociateGatewayViewDelete?
  27. var currentIndex:Int = 0
  28. // var associatedList:[GatewayLamp]?{
  29. // didSet{
  30. // if let associatedList = self.associatedList{
  31. // if(associatedList.count > 0){
  32. // tableView.ly_emptyView?.isHidden = true
  33. // self.tableView.reloadData()
  34. // }
  35. // }
  36. // }
  37. // }
  38. //
  39. // var notAssociatedList:[GatewayLamp]?{
  40. // didSet{
  41. // if let notAssociatedList = self.notAssociatedList{
  42. // if(notAssociatedList.count > 0){
  43. // tableView.ly_emptyView?.isHidden = true
  44. // self.tableView.reloadData()
  45. // }
  46. // }
  47. //
  48. // }
  49. // }
  50. var associatedDeviceList : [(address : String ,type : String)]?{
  51. didSet{
  52. if let associatedDeviceList = associatedDeviceList{
  53. if(associatedDeviceList.count > 0){
  54. tableView.ly_emptyView?.isHidden = true
  55. self.tableView.reloadData()
  56. }
  57. }
  58. }
  59. }
  60. var notAssociatedDeviceList : [String]?{
  61. didSet{
  62. if let notAssociatedDeviceList = notAssociatedDeviceList{
  63. if(notAssociatedDeviceList.count > 0){
  64. tableView.ly_emptyView?.isHidden = true
  65. self.tableView.reloadData()
  66. }
  67. }
  68. }
  69. }
  70. var emptyView:IHEmptyGatewayView?
  71. lazy var tableView: UITableView = {
  72. let tableView = UITableView(frame: .zero, style: .plain)
  73. tableView.delegate = self
  74. tableView.dataSource = self
  75. tableView.allowsSelection = true
  76. tableView.separatorStyle = .none
  77. tableView.register(UINib(nibName: "IHAssociatedGatewayCell", bundle: nil), forCellReuseIdentifier: "associated")
  78. tableView.register(UINib(nibName: "IHNotAssociatedGatewayCell", bundle: nil), forCellReuseIdentifier: "notassociated")
  79. return tableView
  80. }()
  81. override init(frame: CGRect) {
  82. super.init(frame: frame)
  83. createUI()
  84. }
  85. required init?(coder: NSCoder) {
  86. fatalError("init(coder:) has not been implemented")
  87. }
  88. override func layoutSubviews() {
  89. super.layoutSubviews()
  90. // tableView.frame = self.bounds
  91. if KNavBarHeight == 64 {
  92. tableView.frame = CGRect.init(x: 0, y: 0, width: KSCREENWIDTH, height: self.frame.size.height - 230)
  93. }else{
  94. tableView.frame = self.bounds
  95. }
  96. emptyView?.frame = self.bounds
  97. }
  98. func createUI() {
  99. addSubview(tableView)
  100. emptyView = Bundle.main.loadNibNamed("IHEmptyGatewayView", owner: nil, options: nil)?.last as? IHEmptyGatewayView
  101. emptyView?.searchCallback = {[unowned self] in
  102. //搜索事件
  103. HUD.flash(.progress)
  104. if let delegate = self.delegate {
  105. delegate.emptyViewSearchDataSegment(self.currentIndex)
  106. }
  107. }
  108. // 空数据展示界面
  109. let emptyV :HDEmptyView = HDEmptyView.emptyViewWithCustomView(customView: emptyView!) as! HDEmptyView
  110. tableView.ly_emptyView = emptyV
  111. tableView.ly_showEmptyView()
  112. }
  113. }
  114. extension IHAssociateGatewayView :JXSegmentedListContainerViewListDelegate{
  115. func listView() -> UIView {
  116. return self
  117. }
  118. }
  119. extension IHAssociateGatewayView:UITableViewDelegate,UITableViewDataSource{
  120. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  121. if currentIndex == 0 {
  122. return self.associatedDeviceList?.count ?? 0
  123. }else{
  124. return self.notAssociatedDeviceList?.count ?? 0
  125. }
  126. }
  127. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  128. return 99
  129. }
  130. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  131. if currentIndex == 0 {
  132. let cell = tableView.dequeueReusableCell(withIdentifier: "associated") as! IHAssociatedGatewayCell
  133. let gateDevice = self.associatedDeviceList![indexPath.row]
  134. cell.gateDevice = gateDevice
  135. cell.selectionStyle = .none
  136. cell.disconnectCallback = {[unowned self] in
  137. HUD.flash(.progress)
  138. self.delegate?.deviceDisconteGateway(gateDevice.address)
  139. }
  140. cell.turnonCallback = {[unowned self](isTurnOn) in
  141. if gateDevice.type != "40" && gateDevice.type != "12" {
  142. HUD.flash(.progress)
  143. self.delegate?.controlightOn(isTurnOn,gateDevice)
  144. }
  145. // if let delegate = self.delegate {
  146. // delegate.controlightOn(isTurnOn, lampId: gateDevice.id!)
  147. // }
  148. }
  149. return cell
  150. }else{
  151. let cell = tableView.dequeueReusableCell(withIdentifier: "notassociated") as! IHNotAssociatedGatewayCell
  152. cell.selectionStyle = .none
  153. let gateDevice = self.notAssociatedDeviceList![indexPath.row]
  154. cell.gateDevice = gateDevice
  155. cell.connectCallback = {[unowned self] in
  156. HUD.flash(.progress)
  157. self.delegate?.deviceConnectGateway(gateDevice)
  158. // if let delegate = self.delegate {
  159. // delegate.deviceConnectGateway(gateDevice.deviceId!)
  160. // }
  161. }
  162. return cell
  163. }
  164. }
  165. }