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.
171 lines
7.5 KiB
171 lines
7.5 KiB
![]()
2 years ago
|
//
|
||
![]()
2 years ago
|
// HealthViewModels.swift
|
||
![]()
1 year ago
|
// FireBoltt
|
||
![]()
2 years ago
|
//
|
||
|
// Created by lemo. on 2020/3/7.
|
||
|
// Copyright © 2020 Sheldon. All rights reserved.
|
||
|
//
|
||
|
|
||
|
import UIKit
|
||
|
|
||
![]()
2 years ago
|
class HealthViewModels: ViewModel {
|
||
![]()
2 years ago
|
|
||
|
// 用户信息
|
||
![]()
1 year ago
|
let userIcon = BehaviorRelay<UIImage?>(value: R.image.user_Icon_header())
|
||
![]()
2 years ago
|
let userNickname = BehaviorRelay<String?>(value: nil)
|
||
|
|
||
|
// 健康数据
|
||
|
let relay = BehaviorRelay<[HealthSection]>(value: [])
|
||
|
/// 用户信息
|
||
|
private var userInfo = BehaviorRelay<UserInfo?>(value: nil)
|
||
![]()
2 years ago
|
private var sleepHeartCellVM: SleepHeartCellViewModels?
|
||
![]()
2 years ago
|
|
||
|
/// 发现DFU设备,自动升级
|
||
![]()
2 years ago
|
let dfuDeviceUpgrate = PublishSubject<FirmwareUpdateViewModels>()
|
||
![]()
2 years ago
|
|
||
|
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
|
||
![]()
1 year ago
|
kNotificationCenter.post(name: NSNotification.Name(rawValue: DateChangeFireBoltt), object: nil)
|
||
![]()
2 years ago
|
}
|
||
|
})
|
||
|
.disposed(by: rx.disposeBag)
|
||
![]()
1 year ago
|
fbloadDefaultData()
|
||
|
fbloadHealthData()
|
||
![]()
2 years ago
|
monitor()
|
||
|
}
|
||
|
|
||
![]()
1 year ago
|
private func fbloadDefaultData() {
|
||
|
userInfo.accept(UserDefaultsManagerFrieBoltt.getUserInfo())
|
||
![]()
2 years ago
|
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)
|
||
|
}
|
||
|
|
||
![]()
1 year ago
|
private func fbloadHealthData() {
|
||
![]()
2 years ago
|
tempDisposeBag = DisposeBag()
|
||
|
var items: [HealthSectionItem] = []
|
||
![]()
2 years ago
|
let stepCellVM = StepCellViewModels()
|
||
![]()
2 years ago
|
let stepItem = HealthSectionItem.stepItem(viewModel: stepCellVM)
|
||
![]()
2 years ago
|
let sleepHeartCellVM = SleepHeartCellViewModels()
|
||
![]()
2 years ago
|
self.sleepHeartCellVM = sleepHeartCellVM
|
||
|
let sleepHeartItem = HealthSectionItem.sleepHeartItem(viewModel: sleepHeartCellVM)
|
||
|
items = [stepItem, sleepHeartItem]
|
||
|
// 功能适配
|
||
![]()
1 year ago
|
if let adapterInfo = UserDefaultsManagerFrieBoltt.getDeviceInfo()?.adapterInfo {
|
||
![]()
2 years ago
|
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
|
||
![]()
1 year ago
|
DataBaseManagerFireBoltt.shared.fbquerySleepModel(dataDate: DateClass.todayString(), queryType: .day) { [weak self] (model) in
|
||
![]()
2 years ago
|
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)
|
||
![]()
1 year ago
|
let value = MultiLanguageKey_FB.sleepTargetFB.localized + ":" + sleepTarget
|
||
![]()
2 years ago
|
return Observable.just(value)
|
||
|
}.bind(to: sleepHeartCellVM.sleepTarget)
|
||
|
.disposed(by: tempDisposeBag)
|
||
|
}
|
||
|
|
||
|
private func monitor() {
|
||
|
// 用户信息更新
|
||
![]()
1 year ago
|
kNotificationCenter.rx.notification(Notification.Name(rawValue: UserInfoUpdateFireBoltt))
|
||
![]()
2 years ago
|
.flatMapLatest({ _ -> Observable<UserInfo?> in
|
||
![]()
1 year ago
|
return Observable.just(UserDefaultsManagerFrieBoltt.getUserInfo())
|
||
![]()
2 years ago
|
})
|
||
|
.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:
|
||
![]()
1 year ago
|
DataBaseManagerFireBoltt.shared.fbquerySleepModel(dataDate: DateClass.getCurrentTimeStr(formatStr: "yyyy-MM-dd"), queryType: .day) { (model) in
|
||
![]()
2 years ago
|
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)
|
||
|
// 日期改变
|
||
![]()
1 year ago
|
kNotificationCenter.rx.notification(Notification.Name(rawValue: DateChangeFireBoltt))
|
||
![]()
2 years ago
|
.subscribe(onNext: { [weak self] notication in
|
||
|
guard let `self` = self else { return }
|
||
![]()
1 year ago
|
self.fbloadHealthData()
|
||
![]()
2 years ago
|
})
|
||
|
.disposed(by: rx.disposeBag)
|
||
|
// 功能适配
|
||
|
BluetoothService.shared.adapterInfoUpdate
|
||
|
.subscribe(onNext: { [weak self] _ in
|
||
|
guard let `self` = self else { return }
|
||
![]()
1 year ago
|
self.fbloadHealthData()
|
||
![]()
2 years ago
|
})
|
||
|
.disposed(by: rx.disposeBag)
|
||
|
// DFU设备
|
||
|
kNotificationCenter.rx.notification(Notification.Name(rawValue: BluetoothNotificationAtDFUConnectSuccess))
|
||
|
.subscribe(onNext: { [weak self] _ in
|
||
|
guard let `self` = self else { return }
|
||
![]()
2 years ago
|
let viewModel = FirmwareUpdateViewModels(isDFU: true)
|
||
![]()
2 years ago
|
self.dfuDeviceUpgrate.onNext(viewModel)
|
||
|
})
|
||
|
.disposed(by: rx.disposeBag)
|
||
|
}
|
||
|
|
||
|
}
|