You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

153 lines
6.0 KiB

//
// TemperatureViewControllers.swift
// Lookfit
//
// Created by lemo. on 2020/9/6.
// Copyright © 2020 Sheldon. All rights reserved.
//
import UIKit
class TemperatureViewControllers: 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? TemperatureViewModels 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? TemperatureViewModels 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: .temperature)
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? TemperatureViewModels 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)
}
}