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

//
// 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)
}
}