// // HRViewControllers.swift // HPlusFit // // Created by lemo. on 2019/10/4. // Copyright © 2019 lemo. All rights reserved. // import UIKit class HRViewControllers: ViewController { /// 日期选择 private lazy var dateHeaderView: DateSwitchHeaderView = { let view = DateSwitchHeaderView(frame: CGRect(x: 0, y: 0, width: kScreenW, height: kScaleHeight(103))) view.segControlValueChangeClosure = { [weak self] (type) in guard let `self` = self else { return } guard let viewModel = self.viewModel as? HRViewModels else { return } var dateStr: String = DateClass.todayString() var queryType: DataQueryType = .day if type == .week { queryType = .week let weekRegionComps = view.dateSelectedView.dateStr!.components(separatedBy: "-") dateStr = weekRegionComps[0].pregReplace(pattern: "/", with: "-") }else if type == .month { queryType = .month } viewModel.loadHrData(timeStr: dateStr, queryType: queryType) } view.dateSelectedClosure = { [weak self] (selectedDateStr) in guard let `self` = self else { return } guard let viewModel = self.viewModel as? HRViewModels else { return } viewModel.loadHrData(timeStr: selectedDateStr, queryType: nil) } return view }() // 图表 private lazy var chartView: MatchStickView = { let chart = MatchStickView(frame: CGRect(x: kScreenW, y: 0, width: kScreenW, height: kScaleHeight(278))) chart.backgroundColor = .white return chart }() // 底部数据视图 private lazy var healthFormView: HealthFormView = { let view = HealthFormView.loadFromNib() view.configUI(type: .heartRate) return view }() // 图表滚动视图 private lazy var chartScrollView: UIScrollView = { let scrollView = UIScrollView() scrollView.alwaysBounceHorizontal = true scrollView.contentSize = CGSize(width: kScreenW * 3, height: kScaleHeight(278)) scrollView.isPagingEnabled = true scrollView.bounces = false scrollView.setContentOffset(CGPoint(x: kScreenW, y: 0), animated: false) scrollView.showsHorizontalScrollIndicator = false // 添加三个图表 for i in 0...2 { if i == 1 { scrollView.addSubview(chartView) continue } let chart = MatchStickView(frame: CGRect(x: CGFloat(i) * kScreenW , y: 0, width: kScreenW, height: kScaleHeight(278))) scrollView.addSubview(chart) } view.addSubview(scrollView) return scrollView }() override func viewDidLoad() { super.viewDidLoad() } override func makeUI() { super.makeUI() view.backgroundColor = kHexColor(0xFAFAFA) view.addSubview(dateHeaderView) view.addSubview(healthFormView) layout() } func layout() { // 图表滚动图示 chartScrollView.snp.makeConstraints { (make) in make.left.right.equalToSuperview() make.top.equalTo(dateHeaderView.snp.bottom).offset(10) make.height.equalTo(kScaleHeight(278)) } // 底部数据 healthFormView.snp.makeConstraints { (make) in make.top.equalTo(chartScrollView.snp.bottom).offset(10) make.left.right.equalToSuperview() make.height.equalTo(kScaleHeight(180)) } } override func bindViewModel() { super.bindViewModel() guard let viewModel = viewModel as? HRViewModels else { return } viewModel.relay .bind(to: chartView.rx.chartDatsource()) .disposed(by: rx.disposeBag) viewModel.formRealy .bind(to: healthFormView.rx.formDataSource()) .disposed(by: rx.disposeBag) } override func handleEvent() { super.handleEvent() // 滚动监听 chartScrollView.rx.didScroll .observeOn(MainScheduler.asyncInstance) .subscribe(onNext: { [weak self] _ in guard let `self` = self else { return } // 前后日期未达到的 禁止滚动 let offsetX = self.chartScrollView.contentOffset.x if Int(offsetX) % Int(kScreenW) == 0 { return } if offsetX < kScreenW { // 日期早于2019年不允许向后滚动 if !self.dateHeaderView.isforwardEnable(isforward: false) { self.chartScrollView.setContentOffset(CGPoint(x: kScreenW, y: 0), animated: false) } return } if offsetX > kScreenW { // 当前当周当月不允许向前滚动 if !self.dateHeaderView.isforwardEnable(isforward: true) { self.chartScrollView.setContentOffset(CGPoint(x: kScreenW, y: 0), animated: false) } } }) .disposed(by: rx.disposeBag) chartScrollView.rx.contentOffset .observeOn(MainScheduler.asyncInstance) .subscribe(onNext: { [weak self] (offset) in guard let `self` = self else { return } if offset.x == 0 || offset.x == kScreenW * 2 { // 日期切换 self.dateHeaderView.switchDate(isforward: offset.x == kScreenW * 2) // 数据更新后 位置交换 GCDTimer.shared.scheduledDispatchTimer(WithTimerName: "setContentOffset", timeInterval: 0.1, queue: .main, repeats: false, immediately: false, action: { self.chartScrollView.setContentOffset(CGPoint(x: kScreenW, y: 0), animated: false) }) } }) .disposed(by: rx.disposeBag) } }