IHRoomLightView.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // IHRoomLightView.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2019/12/16.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. protocol IHRoomLightViewDelegate : NSObjectProtocol {
  10. func rl_switchDataType(_ index: Int)
  11. func rl_controlSwitch(_ status: String)
  12. func rl_adjustBrightness(_ light: Int)
  13. func rl_adjustColorTemperature(_ color: Int)
  14. func rl_adjustColor(_ red: Int, green: Int, blue: Int)
  15. }
  16. class IHRoomLightView: UIView {
  17. weak var delegate :IHRoomLightViewDelegate?
  18. var reportData: ReportData?{
  19. didSet{
  20. if self.reportData != nil {
  21. // let indexP = IndexPath(row: 0, section: 0)
  22. // self.tableView.reloadRows(at: [indexP], with: .fade)
  23. self.tableView.reloadData()
  24. }
  25. }
  26. }
  27. var lightData : LightInfodata?{
  28. didSet{
  29. self.tableView.reloadData()
  30. }
  31. }
  32. lazy var tableView: UITableView = {
  33. let tableView = UITableView(frame: CGRect.zero, style: .plain)
  34. tableView.dataSource = self
  35. tableView.delegate = self
  36. tableView.separatorStyle = .none
  37. tableView.register(UINib(nibName: "IHRoomLightBarChartCell", bundle: nil), forCellReuseIdentifier: "barCell")
  38. tableView.register(UINib(nibName: "IHRoomLightColorCell", bundle: nil), forCellReuseIdentifier: "colorCell")
  39. return tableView
  40. }()
  41. override init(frame: CGRect) {
  42. super.init(frame: frame)
  43. addSubview(tableView)
  44. backgroundColor = .white
  45. }
  46. required init?(coder: NSCoder) {
  47. fatalError("init(coder:) has not been implemented")
  48. }
  49. private func reloadData() {
  50. let indexP = IndexPath(row: 1, section: 0)
  51. self.tableView.reloadRows(at: [indexP], with: .fade)
  52. }
  53. override func layoutSubviews() {
  54. super.layoutSubviews()
  55. tableView.frame = self.bounds
  56. }
  57. }
  58. extension IHRoomLightView: UITableViewDelegate,UITableViewDataSource{
  59. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  60. return self.lightData == nil ? 0 : 2
  61. }
  62. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  63. if indexPath.row == 0 {
  64. if self.lightData?.alarmStatus == "1" {
  65. return 350
  66. }else{
  67. return 350 - 60
  68. }
  69. }else{
  70. if self.lightData?.alarmStatus == "1" {
  71. return KSCREENHEIGHT - 350
  72. }else{
  73. return KSCREENHEIGHT - 350 + 60
  74. }
  75. // return 375
  76. }
  77. }
  78. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  79. if indexPath.row == 0 {
  80. let cell = tableView.dequeueReusableCell(withIdentifier: "barCell") as! IHRoomLightBarChartCell
  81. cell.lightData = lightData
  82. cell.reportData = self.reportData
  83. cell.delegate = self
  84. cell.selectionStyle = .none
  85. return cell
  86. }else{
  87. let cell = tableView.dequeueReusableCell(withIdentifier: "colorCell") as! IHRoomLightColorCell
  88. cell.lightId = self.lightData?.deviceId
  89. cell.brightness = self.lightData?.light
  90. cell.delegate = self
  91. cell.color = self.lightData!.color
  92. cell.switchStatus = self.lightData?.status
  93. cell.model = self.lightData?.model
  94. cell.colorStruct = ColorStruct(Red: lightData?.colorR, Green: lightData?.colorG, Blue: lightData?.colorB)
  95. cell.selectionStyle = .none
  96. return cell
  97. }
  98. }
  99. }
  100. extension IHRoomLightView: IHRoomLightBarChartCellDelegate,IHRoomLightColorCellDelegate{
  101. func switchDataType(_ index: Int) {
  102. if let delegate = self.delegate {
  103. delegate.rl_switchDataType(index)
  104. }
  105. }
  106. func controlSwitch(_ status: String) {
  107. if let delegate = self.delegate {
  108. delegate.rl_controlSwitch(status)
  109. }
  110. }
  111. func adjustBrightness(_ light: Int) {
  112. if let delegate = self.delegate {
  113. delegate.rl_adjustBrightness(light)
  114. }
  115. }
  116. func adjustColorTemperature(_ color: Int) {
  117. if let delegate = self.delegate {
  118. delegate.rl_adjustColorTemperature(color)
  119. }
  120. }
  121. func adjustColor(_ red: Int, green: Int, blue: Int) {
  122. log.debug("adjustColor --\(red) - \(green) -- \(blue)")
  123. if let delegate = self.delegate {
  124. delegate.rl_adjustColor(red, green: green, blue: blue)
  125. }
  126. }
  127. }