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.
170 lines
7.4 KiB
170 lines
7.4 KiB
// |
|
// HealthViewModels.swift |
|
// Lookfit |
|
// |
|
// Created by lemo. on 2020/3/7. |
|
// Copyright © 2020 Sheldon. All rights reserved. |
|
// |
|
|
|
import UIKit |
|
|
|
class HealthViewModels: ViewModel { |
|
|
|
// 用户信息 |
|
let userIcon = BehaviorRelay<UIImage?>(value: R.image.icon_header()) |
|
let userNickname = BehaviorRelay<String?>(value: nil) |
|
|
|
// 健康数据 |
|
let relay = BehaviorRelay<[HealthSection]>(value: []) |
|
/// 用户信息 |
|
private var userInfo = BehaviorRelay<UserInfo?>(value: nil) |
|
private var sleepHeartCellVM: SleepHeartCellViewModels? |
|
|
|
/// 发现DFU设备,自动升级 |
|
let dfuDeviceUpgrate = PublishSubject<FirmwareUpdateViewModels>() |
|
|
|
private var tempDisposeBag = DisposeBag() |
|
|
|
override init() { |
|
super.init() |
|
// 监听系统时间改变 |
|
var currenDay = DateClass.todayString() |
|
kNotificationCenter.rx.notification(UIApplication.significantTimeChangeNotification) |
|
.subscribe(onNext: { notication in |
|
// 日期改变了 |
|
let newDay = DateClass.todayString() |
|
if currenDay != newDay { |
|
currenDay = newDay |
|
kNotificationCenter.post(name: NSNotification.Name(rawValue: DateChange), object: nil) |
|
} |
|
}) |
|
.disposed(by: rx.disposeBag) |
|
loadDefaultData() |
|
loadHealthData() |
|
monitor() |
|
} |
|
|
|
private func loadDefaultData() { |
|
userInfo.accept(UserDefaultsManager.getUserInfo()) |
|
userInfo.flatMapLatest({ (userInfo) -> Observable<UIImage?> in |
|
return Observable.just(userInfo?.avatarImg) |
|
}) |
|
.filter { $0 != nil } |
|
.bind(to: userIcon) |
|
.disposed(by: rx.disposeBag) |
|
userInfo.flatMapLatest({ (userInfo) -> Observable<String?> in |
|
return Observable.just(userInfo?.nickname) |
|
}) |
|
.bind(to: userNickname) |
|
.disposed(by: rx.disposeBag) |
|
} |
|
|
|
private func loadHealthData() { |
|
tempDisposeBag = DisposeBag() |
|
var items: [HealthSectionItem] = [] |
|
let stepCellVM = StepCellViewModels() |
|
let stepItem = HealthSectionItem.stepItem(viewModel: stepCellVM) |
|
let sleepHeartCellVM = SleepHeartCellViewModels() |
|
self.sleepHeartCellVM = sleepHeartCellVM |
|
let sleepHeartItem = HealthSectionItem.sleepHeartItem(viewModel: sleepHeartCellVM) |
|
items = [stepItem, sleepHeartItem] |
|
// 功能适配 |
|
if let adapterInfo = UserDefaultsManager.getDeviceInfo()?.adapterInfo { |
|
if adapterInfo.bodyTemperature { |
|
let temperatureCellModel = TemperatureCellModel() |
|
let temperatureItem = HealthSectionItem.temperatureIitem(viewModel: temperatureCellModel) |
|
items.append(temperatureItem) |
|
// 实时温度 |
|
BluetoothService.shared.realTemperature |
|
.flatMapLatest { (temperature) -> Observable<String> in |
|
let value = temperature.temperature == 0 ? "--" : String(format: "%.1f", temperature.temperature) |
|
return Observable.just(value) |
|
} |
|
.bind(to: temperatureCellModel.temperature) |
|
.disposed(by: tempDisposeBag) |
|
} |
|
} |
|
let section = HealthSection.health(title: "", items: items) |
|
relay.accept([section]) |
|
// 绑定数据更新 |
|
BluetoothService.shared.realStep |
|
.bind(to: stepCellVM.realStepModel) |
|
.disposed(by: tempDisposeBag) |
|
// 实时心率 |
|
BluetoothService.shared.realHeartRate |
|
.flatMapLatest { (heartRate) -> Observable<String> in |
|
let value = heartRate.heartRate == 0 ? "--" : String(heartRate.heartRate) |
|
return Observable.just(value) |
|
} |
|
.bind(to: sleepHeartCellVM.heartRate) |
|
.disposed(by: tempDisposeBag) |
|
// 睡眠数据 |
|
BluetoothService.shared.sleepUpdate |
|
.subscribe(onNext: { _ in |
|
DataBaseManager.shared.querySleepModel(dataDate: DateClass.todayString(), queryType: .day) { [weak self] (model) in |
|
guard let sleepModel = model.first as? SleepModel else { return } |
|
let sleep = String(format: "%.1f", Float(sleepModel.totalSleepDuration) / 60.0) |
|
let progress = Float(sleepModel.totalSleepDuration) / Float(self?.userInfo.value?.sleepTarget ?? 480) |
|
sleepHeartCellVM.sleep.accept(sleep) |
|
sleepHeartCellVM.sleepProgress.accept(progress) |
|
} |
|
}) |
|
.disposed(by: tempDisposeBag) |
|
userInfo.flatMapLatest { (userInfo) -> Observable<String> in |
|
let sleepTarget = String(format: "%.1f", Float(userInfo?.sleepTarget ?? 480) / 60.0) |
|
let value = MultiLanguageKey.sleepTarget.localized + ":" + sleepTarget |
|
return Observable.just(value) |
|
}.bind(to: sleepHeartCellVM.sleepTarget) |
|
.disposed(by: tempDisposeBag) |
|
} |
|
|
|
private func monitor() { |
|
// 用户信息更新 |
|
kNotificationCenter.rx.notification(Notification.Name(rawValue: UserInfoUpdate)) |
|
.flatMapLatest({ _ -> Observable<UserInfo?> in |
|
return Observable.just(UserDefaultsManager.getUserInfo()) |
|
}) |
|
.bind(to: userInfo) |
|
.disposed(by: rx.disposeBag) |
|
// 数据同步完成刷新睡眠 |
|
BluetoothService.shared.syncState.skip(1) |
|
.subscribe(onNext: { [weak self] (syncState) in |
|
guard let `self` = self else { return } |
|
switch syncState { |
|
case .normal: |
|
DataBaseManager.shared.querySleepModel(dataDate: DateClass.getCurrentTimeStr(formatStr: "yyyy-MM-dd"), queryType: .day) { (model) in |
|
guard let sleepModel = model.first as? SleepModel else { return } |
|
let sleep = String(format: "%.1f", Float(sleepModel.totalSleepDuration) / 60.0) |
|
let progress = Float(sleepModel.totalSleepDuration) / Float(self.userInfo.value?.sleepTarget ?? 480) |
|
self.sleepHeartCellVM?.sleep.accept(sleep) |
|
self.sleepHeartCellVM?.sleepProgress.accept(progress) |
|
} |
|
default: break |
|
} |
|
}) |
|
.disposed(by: rx.disposeBag) |
|
// 日期改变 |
|
kNotificationCenter.rx.notification(Notification.Name(rawValue: DateChange)) |
|
.subscribe(onNext: { [weak self] notication in |
|
guard let `self` = self else { return } |
|
self.loadHealthData() |
|
}) |
|
.disposed(by: rx.disposeBag) |
|
// 功能适配 |
|
BluetoothService.shared.adapterInfoUpdate |
|
.subscribe(onNext: { [weak self] _ in |
|
guard let `self` = self else { return } |
|
self.loadHealthData() |
|
}) |
|
.disposed(by: rx.disposeBag) |
|
// DFU设备 |
|
kNotificationCenter.rx.notification(Notification.Name(rawValue: BluetoothNotificationAtDFUConnectSuccess)) |
|
.subscribe(onNext: { [weak self] _ in |
|
guard let `self` = self else { return } |
|
let viewModel = FirmwareUpdateViewModels(isDFU: true) |
|
self.dfuDeviceUpgrate.onNext(viewModel) |
|
}) |
|
.disposed(by: rx.disposeBag) |
|
} |
|
|
|
}
|
|
|