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.

165 lines
8.9 KiB

//
// TemperatureViewModels.swift
// Lookfit
//
// Created by lemo. on 2020/9/6.
// Copyright © 2020 Sheldon. All rights reserved.
//
import UIKit
class TemperatureViewModels: ViewModel {
//
lazy var relay: BehaviorRelay<ChartViewModel> = {
return BehaviorRelay<ChartViewModel>(value: defaultVM)
}()
//
let formRealy = BehaviorRelay<HealthFormModel?>(value: nil)
//
private lazy var defaultVM: ChartViewModel = {
let chartViewModel: ChartViewModel = ChartViewModel(leftIcon: nil, title: nil, legends: nil, source: [], chartType: .gradentFill, maxAxis: 0, xAxis: [], lineColor: kHexColor(0x27ABFF), isContinuous: false, isShowMeasureLine: false)
return chartViewModel
}()
/// ()
private var currenTimeStr = DateClass.getCurrentTimeStr(formatStr: "yyyy-MM-dd")
private var queryType: DataQueryType = .day
override init() {
super.init()
//
loadHrData(timeStr: currenTimeStr, queryType: queryType)
}
//
func loadHrData(timeStr: String?, queryType: DataQueryType?) {
//
let tempQueryType = queryType == nil ? self.queryType : queryType!
self.queryType = tempQueryType
//
let tempTimeStr = timeStr == nil ? currenTimeStr : timeStr!
currenTimeStr = tempTimeStr
DataBaseManager.shared.queryTemperatureData(dataDate: tempTimeStr, queryType: tempQueryType) { [weak self] (results) in
guard let `self` = self else { return }
guard let tempModelArr = results as? [TemperatureModel], tempModelArr.count > 0 else {
self.relay.accept(self.defaultVM)
self.formRealy.accept(nil)
return
}
//
var chartViewModel: ChartViewModel!
var formModel: HealthFormModel?
switch tempQueryType {
case .day:
let tempModel = tempModelArr.first!
if tempModel.temperatureAvg == 0 {
self.relay.accept(self.defaultVM)
self.formRealy.accept(nil)
return
}
let temps: [CGFloat] = tempModel.temperatureDetails.components(separatedBy: ",").map { CGFloat($0.components(separatedBy: "|").first?.floatValue ?? 0)}
var sources: [ChartViewData] = []
var maxAxis: CGFloat = 0
temps.enumerated().forEach({ (offset, temp) in
if temp > maxAxis {
maxAxis = temp
}
sources.append(ChartViewData(date: nil, floatValue: temp))
})
chartViewModel = ChartViewModel(source: sources, chartType: .temp, floatMaxAxis: maxAxis * 1.2, xAxis: ["00:00", "06:00", "12:00", "18:00", "24:00"], lineColor: kHexColor(0x27ABFF), isContinuous: false, isShowMeasureLine: false, center: String(format: "%.1lf", tempModel.temperatureAvg), centerColor: kHexColor(0x27ABFF), centerUnit: MultiLanguageKey.centigrade.localized)
chartViewModel.points = sources.count
//
formModel = HealthFormModel(oneValue: String(format: "%.1lf", tempModel.temperatureAvg), twoValue: String(format: "%.1lf", tempModel.temperatureMax), threeValue: String(format: "%.1lf", tempModel.temperatureMin), fourValue: "")
case .week:
//
let weeks = (0...6).map { DateClass.dateStringOffset(from: tempTimeStr, offset: $0) }
var sources: [ChartViewData] = []
var maxAxis: Double = 0
var totalAvgTemp: Double = 0
var minTemp: Double = 0
var xAxis: [String] = []
//
for dataDate in weeks {
let showDate = DateClass.getTimeStrToDate(formatStr: "yyyy-MM-dd", timeStr: dataDate).tranFormDateStr(format: "MM/dd")
xAxis.append(showDate)
var isMatch = false
for tempModel in tempModelArr {
if dataDate == tempModel.dataDate {
isMatch = true
if tempModel.temperatureAvg > maxAxis {
maxAxis = tempModel.temperatureAvg
}
if tempModel.temperatureAvg < minTemp || minTemp == 0 {
minTemp = tempModel.temperatureAvg
}
totalAvgTemp += tempModel.temperatureAvg
sources.append(ChartViewData(date: nil, floatValue: CGFloat(tempModel.temperatureAvg)))
break
}
}
//
if !isMatch {
sources.append(ChartViewData(date: nil, floatValue: 0))
}
}
totalAvgTemp = totalAvgTemp / Double(tempModelArr.count)
chartViewModel = ChartViewModel(source: sources, chartType: .temp, floatMaxAxis: CGFloat(maxAxis) * 1.2, xAxis: xAxis, lineColor: kHexColor(0x27ABFF), isContinuous: false, isShowMeasureLine: false, center: String(format: "%.1lf", totalAvgTemp), centerColor: kHexColor(0x27ABFF), centerUnit: MultiLanguageKey.centigrade.localized)
chartViewModel.points = sources.count
//
formModel = HealthFormModel(oneValue: String(format: "%.1lf", totalAvgTemp), twoValue: String(format: "%.1lf", maxAxis), threeValue: String(format: "%.1lf", minTemp), fourValue: "")
case .month:
//
let selectDate = DateClass.getTimeStrToDate(formatStr: "yyyy-MM-dd", timeStr: tempTimeStr)
let monthSatrtDate = selectDate.startOfCurrentMonth().tranFormDateStr(format: "yyyy-MM-dd")
let monthRange = selectDate.getMonthHowManyDay()
let months = monthRange.map { DateClass.dateStringOffset(from: monthSatrtDate, offset: $0 - 1) }
var sources: [ChartViewData] = []
var maxAxis: Double = 0
var totalAvgTemp: Double = 0
var minTemp: Double = 0
var xAxis: [String] = []
//
for (offset, dataDate) in months.enumerated() {
//
if offset == 0 || offset == months.count - 1 || offset == (months.count / 2) {
let showDate = DateClass.getTimeStrToDate(formatStr: "yyyy-MM-dd", timeStr: dataDate).tranFormDateStr(format: "dd")
xAxis.append(showDate)
}
var isMatch = false
for tempModel in tempModelArr {
if dataDate == tempModel.dataDate {
isMatch = true
if tempModel.temperatureAvg > maxAxis {
maxAxis = tempModel.temperatureAvg
}
if tempModel.temperatureAvg < minTemp || minTemp == 0 {
minTemp = tempModel.temperatureAvg
}
totalAvgTemp += tempModel.temperatureAvg
sources.append(ChartViewData(date: nil, floatValue: CGFloat(tempModel.temperatureAvg)))
break
}
}
//
if !isMatch {
sources.append(ChartViewData(date: nil, floatValue: 0))
}
}
totalAvgTemp = totalAvgTemp / Double(tempModelArr.count)
chartViewModel = ChartViewModel(source: sources, chartType: .temp, floatMaxAxis: CGFloat(maxAxis) * 1.2, xAxis: xAxis, lineColor: kHexColor(0x27ABFF), isContinuous: false, isShowMeasureLine: false, center: String(format: "%.1lf", totalAvgTemp), centerColor: kHexColor(0x27ABFF), centerUnit: MultiLanguageKey.centigrade.localized)
chartViewModel.points = sources.count
//
formModel = HealthFormModel(oneValue: String(format: "%.1lf", totalAvgTemp), twoValue: String(format: "%.1lf", maxAxis), threeValue: String(format: "%.1lf", minTemp), fourValue: "")
default:
self.relay.accept(self.defaultVM)
self.formRealy.accept(nil)
return
}
//
self.relay.accept(chartViewModel)
self.formRealy.accept(formModel)
}
}
}