// // NotDisturbViewControllers.swift // FireBoltt // // Created by lemo. on 2020/3/29. // Copyright © 2020 Sheldon. All rights reserved. // import UIKit // MARK:- 复用 private enum Resuable { static let tableViewCell = ReusableCell(nibName: "CommonTableViewCell") static let doubleTimePickerCell = ReusableCell(identifier: "DoubleTimePickerCell", nib: nil) } fileprivate struct Metric { static let descTextColor = kHexColor(0x96A3B3) } class NotDisturbViewControllers: TableViewController { var noDisturbModel = NoDisturbModel() override func viewDidLoad() { super.viewDidLoad() monitor() } override func makeUI() { super.makeUI() let rightItem = UIBarButtonItem.baseBarButtonItem(normalImg: "", highImg: nil, title: MultiLanguageKey_FB.saveFB.localized, target: self, action: #selector(clickSave)) navigationItem.rightBarButtonItem = rightItem tableViewStyle = .grouped tableView.rx.setDelegate(self).disposed(by: rx.disposeBag) tableView.register(Resuable.doubleTimePickerCell) tableView.register(Resuable.tableViewCell) tableView.snp.makeConstraints { (make) in make.edges.equalToSuperview() } } override func bindViewModel() { guard let viewModel = viewModel as? NotDisturbViewModels else { return } noDisturbModel = viewModel.noDisturbModel let dataSource = RxTableViewSectionedReloadDataSource.init(configureCell: { [weak self] (ds, tv, indexPath, _) -> UITableViewCell in switch ds[indexPath] { case .SinglePicker( _, _): return UITableViewCell() case .TableViewCellModel(let cellModel): let cell: CommonTableViewCell = tv.dequeue(Resuable.tableViewCell, for: indexPath) cell.bindViewModel(model: cellModel) cell.rightSwitch.rx.isOn .skip(1) .subscribe(onNext: { (isOn) in self?.noDisturbModel.IsOpen = isOn }) .disposed(by: cell.cellDisposeBag) return cell case .DoubleTimePicker(let leftTitle, let rightTitle, let selectedItems): let cell: DoubleTimePickerCell = tv.dequeue(Resuable.doubleTimePickerCell, for: indexPath) cell.types = 999; cell.selectionStyle = .none cell.fbbegintimeLabel.text = leftTitle cell.fbendtimeLabel.text = rightTitle cell.pickerColsure = { (items) in self?.noDisturbModel.StartTime = items.first ?? "00:00" self?.noDisturbModel.EndTime = items.last ?? "00:00" } cell.selectedItems = selectedItems return cell } }) viewModel.dataVariable .bind(to: tableView.rx.items(dataSource: dataSource)) .disposed(by: rx.disposeBag) } @objc func clickSave() { let startTime = (noDisturbModel.StartTime as NSString).doubleValue let endTime = (noDisturbModel.EndTime as NSString).doubleValue if startTime >= endTime { SVProgressHUD.showError(withStatus: MultiLanguageKey_FB.timeTipFB.localized) return } SVProgressHUD.show() if BluetoothService.shared.checkBleCmdEnable(isShow: true) { BluetoothService.shared.notDisturbWithBleCmdType(model: noDisturbModel) } } func monitor() { /// 指令成功失败监听 Observable.of(kNotificationCenter.rx.notification(Notification.Name(rawValue: CmdSuccess)), kNotificationCenter.rx.notification(Notification.Name(rawValue: CmdTimeout))) .merge() .subscribe(onNext: { [weak self] notification in guard let `self` = self, let cmd = notification.object as? BleCMD_FireBoltt else { return } let isSuccess = notification.name.rawValue == CmdSuccess switch cmd { case .notDisturb: if isSuccess { GlobalDeviceProfileModel.shareInstance.noDisturbModel = self.noDisturbModel SVProgressHUD.showSuccess(withStatus: MultiLanguageKey_FB.setSuccessFB.localized) self.navigationController?.popViewController(animated: true) return } SVProgressHUD.showSuccess(withStatus: MultiLanguageKey_FB.setFailFB.localized) default: break } }) .disposed(by: rx.disposeBag) } } extension NotDisturbViewControllers: UITableViewDelegate { func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { switch indexPath.section { case 0: return kScaleHeight(50) case 1: return kScaleHeight(178) default: return kScaleHeight(184) } } }