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.
164 lines
8.9 KiB
164 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) |
|
} |
|
} |
|
|
|
}
|
|
|