// // DrinkRemindViewControllers.swift // Lookfit // // 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 singlePickerCell = ReusableCell(identifier: "SinglePickerCell", nib: nil) static let doubleTimePickerCell = ReusableCell(identifier: "DoubleTimePickerCell", nib: nil) } fileprivate struct Metric { static let descTextColor = kHexColor(0x96A3B3) } class DrinkRemindViewControllers: TableViewController { var drinkingModel = DrinkingModel() override func viewDidLoad() { super.viewDidLoad() monitor() } override func makeUI() { super.makeUI() let rightItem = UIBarButtonItem.baseBarButtonItem(normalImg: "", highImg: nil, title: MultiLanguageKey.save.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.singlePickerCell) tableView.register(Resuable.tableViewCell) tableView.snp.makeConstraints { (make) in make.edges.equalToSuperview() } } override func bindViewModel() { guard let viewModel = viewModel as? DrinkRemindViewModels else { return } drinkingModel = viewModel.drinkingModel let dataSource = RxTableViewSectionedReloadDataSource.init(configureCell: { [weak self] (ds, tv, indexPath, _) -> UITableViewCell in switch ds[indexPath] { case .SinglePicker(let title, let selectedItem): let cell: SinglePickerCell = tv.dequeue(Resuable.singlePickerCell, for: indexPath) cell.selectionStyle = .none cell.titleLabel.text = title cell.singlePickerClosure = { item in self?.drinkingModel.Interval = item } cell.functionType = .drinkRemind cell.selectedItem = selectedItem return cell 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?.drinkingModel.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.selectionStyle = .none cell.begintimeLabel.text = leftTitle cell.endtimeLabel.text = rightTitle cell.pickerColsure = {(items) in self?.drinkingModel.StartTime = items.first ?? "00:00" self?.drinkingModel.EndTime = items.last ?? "00:00" } cell.selectedItems = selectedItems return cell } }) viewModel.dataVariable .bind(to: tableView.rx.items(dataSource: dataSource)) .disposed(by: rx.disposeBag) tableView.rx.itemSelected .filter { $0.section == 3 } .subscribe(onNext: { [weak self] _ in guard let `self` = self else { return } let repeatVM = RepeatViewModels(cycle: self.drinkingModel.Cycle) repeatVM.updateCycle .subscribe(onNext: { (cycleStr) in guard let DrinkRemindViewModels = self.viewModel as? DrinkRemindViewModels else { return } self.drinkingModel.Cycle = cycleStr DrinkRemindViewModels.loadData(model: self.drinkingModel) }) .disposed(by: repeatVM.rx.disposeBag) self.navigator.show(segue: .repeatSet(viewModel: repeatVM), sender: self) }) .disposed(by: rx.disposeBag) } @objc func clickSave() { let startTime = (drinkingModel.StartTime as NSString).integerValue let endTime = (drinkingModel.EndTime as NSString).integerValue if startTime >= endTime { SVProgressHUD.showError(withStatus: MultiLanguageKey.timeTip.localized) return } SVProgressHUD.show() if BluetoothService.shared.checkBleCmdEnable(isShow: true) { BluetoothService.shared.setDrinkRemindWithBleCmdType(model: drinkingModel) } } 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 else { return } let isSuccess = notification.name.rawValue == CmdSuccess switch cmd { case .setDrinkRemind: if isSuccess { GlobalDeviceProfileModel.shareInstance.drinkModel = self.drinkingModel SVProgressHUD.showSuccess(withStatus: MultiLanguageKey.setSuccess.localized) self.navigationController?.popViewController(animated: true) return } SVProgressHUD.showSuccess(withStatus: MultiLanguageKey.setFail.localized) default: break } }) .disposed(by: rx.disposeBag) } } extension DrinkRemindViewControllers: UITableViewDelegate { func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { switch indexPath.section { case 0: return kScaleHeight(50) case 1: return kScaleHeight(178) case 2: return kScaleHeight(184) default: return kScaleHeight(50) } } }