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.
174 lines
7.0 KiB
174 lines
7.0 KiB
// |
|
// HealthHomeViewController.swift |
|
// Lookfit |
|
// |
|
// Created by Sheldon on 2021/9/19. |
|
// Copyright © 2021 Sheldon. All rights reserved. |
|
// |
|
|
|
import UIKit |
|
|
|
fileprivate enum Resuable { |
|
static let stepCell = ReusableCell<HealthStepItemCell>() |
|
static let helathCell = ReusableCell<HelathItemCell>() |
|
static let blankCell = ReusableCell<CollectionViewCell>() |
|
} |
|
|
|
// MARK: - 常量 |
|
private struct Metric { |
|
static let stepItemSize = CGSize(width: kScreenW, height: kScaleWidth(215)) |
|
static let healthItemSize = CGSize(width: kScreenW / 2.0 - 13, height: kScaleWidth(224)) |
|
static let blankItemSize = CGSize(width: 13, height: kScaleWidth(224)) |
|
} |
|
|
|
class HealthHomeViewController: CollectionController { |
|
|
|
lazy var navigationBar: HealthNavigationBar = { |
|
let navbar = HealthNavigationBar() |
|
navbar.gradient(colors: [kHexColor(0xFFF0DE), kHexColor(0xFFFFFF)], locations: nil, autoLaoutFrame: CGRect(x: 0, y: 0, width: kScreenW, height: kScaleWidth(90))) |
|
return navbar |
|
}() |
|
|
|
override func viewWillDisappear(_ animated: Bool) { |
|
super.viewWillDisappear(animated) |
|
} |
|
|
|
override func viewDidLoad() { |
|
super.viewDidLoad() |
|
// 未设置用户信息 |
|
guard let _ = UserDefaultsManager.getUserInfo() else { |
|
// 未设置用户信息 |
|
navigator.show(segue: .guideUserInfo, sender: self, transition: .modal) |
|
return |
|
} |
|
} |
|
|
|
override func makeUI() { |
|
super.makeUI() |
|
// 设置布局 |
|
let flowLayout = UICollectionViewFlowLayout() |
|
flowLayout.itemSize = Metric.stepItemSize |
|
flowLayout.minimumInteritemSpacing = 0 |
|
flowLayout.minimumLineSpacing = 0 |
|
collectionViewFlowLayout = flowLayout |
|
// 设置视图 |
|
collectionView.backgroundColor = .white |
|
collectionView.register(Resuable.stepCell) |
|
collectionView.register(Resuable.helathCell) |
|
collectionView.register(Resuable.blankCell) |
|
collectionView.rx.setDelegate(self).disposed(by: rx.disposeBag) |
|
view.addSubview(navigationBar) |
|
layout() |
|
} |
|
|
|
func layout() { |
|
navigationBar.snp.makeConstraints { (make) in |
|
make.top.equalToSuperview() |
|
make.width.equalTo(kScreenW) |
|
make.height.equalTo(kBatteryH + 70) |
|
} |
|
collectionView.snp.makeConstraints { (make) in |
|
make.top.equalTo(navigationBar.snp.bottom) |
|
make.left.right.bottom.equalToSuperview() |
|
} |
|
} |
|
|
|
override func bindViewModel() { |
|
guard let viewModel = viewModel as? HealthHomeViewModel else { return } |
|
let rxDataSource = RxCollectionViewSectionedReloadDataSource |
|
<HealthSection>( |
|
configureCell: { (_ , cv, indexPath, model) -> UICollectionViewCell in |
|
switch model { |
|
case .stepItem(let viewModel): |
|
let cell = cv.dequeue(Resuable.stepCell, for: indexPath) |
|
cell.bind(model: viewModel) |
|
return cell |
|
case .healthItem(let viewModel): |
|
let cell = cv.dequeue(Resuable.helathCell, for: indexPath) |
|
cell.bind(model: viewModel) |
|
return cell |
|
case .blankItem: |
|
let cell = cv.dequeue(Resuable.blankCell, for: indexPath) |
|
return cell |
|
default: return UICollectionViewCell() |
|
} |
|
} |
|
) |
|
viewModel.userIcon |
|
.bind(to: navigationBar.headImg.rx.image) |
|
.disposed(by: rx.disposeBag) |
|
viewModel.userNickname |
|
.bind(to: navigationBar.nicknameLabel.rx.text) |
|
.disposed(by: rx.disposeBag) |
|
BluetoothService.shared.deviceConnectState |
|
.bind(to: navigationBar.statusBtn.rx.isSelected) |
|
.disposed(by: rx.disposeBag) |
|
BluetoothService.shared.powerState |
|
.bind(to: navigationBar.powerBtn.rx.image()) |
|
.disposed(by: rx.disposeBag) |
|
viewModel.relay |
|
.bind(to: collectionView.rx.items(dataSource: rxDataSource)) |
|
.disposed(by: rx.disposeBag) |
|
// DFU设备升级 |
|
viewModel.dfuDeviceUpgrate |
|
.subscribe(onNext: { [weak self] viewModel in |
|
guard let `self` = self else { return } |
|
if let currenVC = currentViewController(), currenVC.isKind(of: FirmwareUpdateViewController.self) { |
|
return |
|
} |
|
currentTabbarController()?.selectedIndex = 0 |
|
self.navigator.show(segue: .firmwareUpdate(viewModel: viewModel), sender: self) |
|
}) |
|
.disposed(by: rx.disposeBag) |
|
} |
|
|
|
override func handleEvent() { |
|
navigationBar.rx.tap |
|
.subscribe(onNext: { [weak self] _ in |
|
let viewModel = UserInfoViewModel() |
|
self?.navigator.show(segue: .userInfo(viewModel: viewModel), sender: self) |
|
}) |
|
.disposed(by: rx.disposeBag) |
|
collectionView.rx.modelSelected(HealthSectionItem.self) |
|
.subscribe(onNext: { [weak self] (item) in |
|
guard let `self` = self else { return } |
|
switch item { |
|
case .stepItem(_): |
|
let viewModel = StepViewModel() |
|
self.navigator.show(segue: .step(viewModel: viewModel), sender: self) |
|
case .healthItem(let viewModel): |
|
switch viewModel.type { |
|
case .sleep: |
|
self.navigator.show(segue: .sleep(viewModel: SleepViewModel()), sender: self) |
|
case .heartRate: |
|
self.navigator.show(segue: .heartRate(viewModel: HRViewModel()), sender: self) |
|
case .temperature: |
|
self.navigator.show(segue: .temperature(viewModel: TemperatureViewModel()), sender: self) |
|
case .bloodPressure: |
|
self.navigator.show(segue: .bloodPressure(viewModel: BloodPressureViewModel()), sender: self) |
|
case .bloodOxygen: |
|
self.navigator.show(segue: .bloodOxygen(viewModel: BloodOxygenViewModel()), sender: self) |
|
default: break |
|
} |
|
default: break |
|
} |
|
}) |
|
.disposed(by: rx.disposeBag) |
|
} |
|
|
|
} |
|
|
|
extension HealthHomeViewController: UICollectionViewDelegateFlowLayout { |
|
|
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { |
|
guard let viewModel = viewModel as? HealthHomeViewModel else { return Metric.healthItemSize } |
|
let item = viewModel.relay.value[indexPath.section].items[indexPath.row] |
|
switch item { |
|
case .stepItem(_): return Metric.stepItemSize |
|
case .healthItem(_): return Metric.healthItemSize |
|
case .blankItem: return Metric.blankItemSize |
|
default: return Metric.healthItemSize |
|
} |
|
} |
|
|
|
}
|
|
|