// // IHRoomLightView.swift // Inhealth // // Created by weclouds on 2019/12/16. // Copyright © 2019 weclouds. All rights reserved. // import UIKit protocol IHRoomLightViewDelegate : NSObjectProtocol { func rl_switchDataType(_ index: Int) func rl_controlSwitch(_ status: String) func rl_adjustBrightness(_ light: Int) func rl_adjustColorTemperature(_ color: Int) func rl_adjustColor(_ red: Int, green: Int, blue: Int) } class IHRoomLightView: UIView { weak var delegate :IHRoomLightViewDelegate? var reportData: ReportData?{ didSet{ if self.reportData != nil { // let indexP = IndexPath(row: 0, section: 0) // self.tableView.reloadRows(at: [indexP], with: .fade) self.tableView.reloadData() } } } var lightData : LightInfodata?{ didSet{ self.tableView.reloadData() } } lazy var tableView: UITableView = { let tableView = UITableView(frame: CGRect.zero, style: .plain) tableView.dataSource = self tableView.delegate = self tableView.separatorStyle = .none tableView.register(UINib(nibName: "IHRoomLightBarChartCell", bundle: nil), forCellReuseIdentifier: "barCell") tableView.register(UINib(nibName: "IHRoomLightColorCell", bundle: nil), forCellReuseIdentifier: "colorCell") return tableView }() override init(frame: CGRect) { super.init(frame: frame) addSubview(tableView) backgroundColor = .white } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } private func reloadData() { let indexP = IndexPath(row: 1, section: 0) self.tableView.reloadRows(at: [indexP], with: .fade) } override func layoutSubviews() { super.layoutSubviews() tableView.frame = self.bounds } } extension IHRoomLightView: UITableViewDelegate,UITableViewDataSource{ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.lightData == nil ? 0 : 2 } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { if indexPath.row == 0 { if self.lightData?.alarmStatus == "1" { return 350 }else{ return 350 - 60 } }else{ if self.lightData?.alarmStatus == "1" { return KSCREENHEIGHT - 350 }else{ return KSCREENHEIGHT - 350 + 60 } // return 375 } } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if indexPath.row == 0 { let cell = tableView.dequeueReusableCell(withIdentifier: "barCell") as! IHRoomLightBarChartCell cell.lightData = lightData cell.reportData = self.reportData cell.delegate = self cell.selectionStyle = .none return cell }else{ let cell = tableView.dequeueReusableCell(withIdentifier: "colorCell") as! IHRoomLightColorCell cell.lightId = self.lightData?.deviceId cell.brightness = self.lightData?.light cell.delegate = self cell.color = self.lightData!.color cell.switchStatus = self.lightData?.status cell.model = self.lightData?.model cell.colorStruct = ColorStruct(Red: lightData?.colorR, Green: lightData?.colorG, Blue: lightData?.colorB) cell.selectionStyle = .none return cell } } } extension IHRoomLightView: IHRoomLightBarChartCellDelegate,IHRoomLightColorCellDelegate{ func switchDataType(_ index: Int) { if let delegate = self.delegate { delegate.rl_switchDataType(index) } } func controlSwitch(_ status: String) { if let delegate = self.delegate { delegate.rl_controlSwitch(status) } } func adjustBrightness(_ light: Int) { if let delegate = self.delegate { delegate.rl_adjustBrightness(light) } } func adjustColorTemperature(_ color: Int) { if let delegate = self.delegate { delegate.rl_adjustColorTemperature(color) } } func adjustColor(_ red: Int, green: Int, blue: Int) { log.debug("adjustColor --\(red) - \(green) -- \(blue)") if let delegate = self.delegate { delegate.rl_adjustColor(red, green: green, blue: blue) } } }