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
8.8 KiB
171 lines
8.8 KiB
![]()
2 years ago
|
//
|
||
![]()
2 years ago
|
// StepViewModels.swift
|
||
![]()
2 years ago
|
// HPlusFit
|
||
|
//
|
||
|
// Created by lemo. on 2019/10/4.
|
||
|
// Copyright © 2019 lemo. All rights reserved.
|
||
|
//
|
||
|
|
||
|
import UIKit
|
||
|
|
||
![]()
2 years ago
|
class StepViewModels: ViewModel {
|
||
![]()
2 years ago
|
|
||
|
// 图表数据
|
||
|
lazy var relay: BehaviorRelay<ChartViewModel> = {
|
||
|
return BehaviorRelay<ChartViewModel>(value: defaultVM)
|
||
|
}()
|
||
|
// 底部数据
|
||
|
let formRealy = BehaviorRelay<HealthFormModel?>(value: nil)
|
||
|
// 默认数据
|
||
|
private lazy var defaultVM: ChartViewModel = {
|
||
|
var sources: [ChartViewData] = []
|
||
|
for i in 0...24 {
|
||
|
sources.append(ChartViewData(date: "", value: 0))
|
||
|
}
|
||
|
let chartViewModel: ChartViewModel = ChartViewModel(leftIcon: nil, title: nil, legends: nil, source:sources, chartType: .stepRoundBar, maxAxis: 0, xAxis: [], lineColor: kHexColor(0xEEA82C), center: nil, centerColor: kHexColor(0xEEA82C), centerUnit: nil)
|
||
|
return chartViewModel
|
||
|
}()
|
||
|
/// 当前时间(默认当前)
|
||
|
private var currenTimeStr = DateClass.getCurrentTimeStr(formatStr: "yyyy-MM-dd")
|
||
|
private var queryType: DataQueryType = .day
|
||
|
|
||
|
override init() {
|
||
|
super.init()
|
||
|
// 默认加载当天
|
||
|
loadStepData(timeStr: currenTimeStr, queryType: queryType)
|
||
|
}
|
||
|
|
||
|
// 加载计步数据
|
||
|
func loadStepData(timeStr: String?, queryType: DataQueryType?) {
|
||
|
// 无查询类型默认为上一次查询类型
|
||
|
let tempQueryType = queryType == nil ? self.queryType : queryType!
|
||
|
self.queryType = tempQueryType
|
||
|
// 查询时间戳
|
||
|
let tempTimeStr = timeStr == nil ? currenTimeStr : timeStr!
|
||
|
currenTimeStr = tempTimeStr
|
||
![]()
1 year ago
|
DataBaseManagerFireBoltt.shared.fbqueryStepModel(dataDate: tempTimeStr, queryType: tempQueryType) { [weak self] (results) in
|
||
![]()
2 years ago
|
guard let `self` = self else { return }
|
||
|
guard let stepModelArr = results as? [StepModel], stepModelArr.count > 0 else {
|
||
|
self.relay.accept(self.defaultVM)
|
||
|
self.formRealy.accept(nil)
|
||
|
return
|
||
|
}
|
||
|
// 拼接数据模型
|
||
|
var chartViewModel: ChartViewModel!
|
||
|
var formModel: HealthFormModel?
|
||
|
switch tempQueryType {
|
||
|
case .day:
|
||
|
let stepModel = stepModelArr.first!
|
||
|
if stepModel.stepNumber == 0 {
|
||
|
self.relay.accept(self.defaultVM)
|
||
|
self.formRealy.accept(nil)
|
||
|
return
|
||
|
}
|
||
|
let steps: [Int] = stepModel.stepDetails.components(separatedBy: ",").map { $0.integerValue }
|
||
|
var sources: [ChartViewData] = []
|
||
|
var maxAxis = 0
|
||
|
steps.enumerated().forEach({ (offset, step) in
|
||
|
if step > maxAxis {
|
||
|
maxAxis = step
|
||
|
}
|
||
|
sources.append(ChartViewData(date: nil, value: step))
|
||
|
})
|
||
![]()
1 year ago
|
chartViewModel = ChartViewModel(leftIcon: nil, title: nil, legends: nil, source:sources, chartType: .stepRoundBar, maxAxis: maxAxis, xAxis: ["00:00", "06:00", "12:00", "18:00", "24:00"], lineColor: kHexColor(0x388AFF), center: String(stepModel.stepNumber), centerColor: kHexColor(0x388AFF), centerUnit: MultiLanguageKey_FB.stepTipFB.localized)
|
||
![]()
2 years ago
|
chartViewModel.points = sources.count
|
||
|
// 底部数据总和
|
||
|
formModel = HealthFormModel(oneValue: String(maxAxis), twoValue: String(format: "%.2f", Double(stepModel.stepDistance) / 1000.0), threeValue: String(stepModel.stepDuration), fourValue: String(stepModel.stepCalorie))
|
||
|
case .week:
|
||
|
let weeks = (0...6).map { DateClass.dateStringOffset(from: tempTimeStr, offset: $0) }
|
||
|
var sources: [ChartViewData] = []
|
||
|
var maxAxis = 0
|
||
|
var totalStep = 0
|
||
|
var totalDistance = 0
|
||
|
var totalCalorie: Float = 0
|
||
|
var totalDuration = 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 stepModel in stepModelArr {
|
||
|
if dataDate == stepModel.dataDate && stepModel.stepNumber != 0 {
|
||
|
isMatch = true
|
||
|
if stepModel.stepNumber > maxAxis {
|
||
|
maxAxis = stepModel.stepNumber
|
||
|
}
|
||
|
totalStep += stepModel.stepNumber
|
||
|
totalDistance += stepModel.stepDistance
|
||
|
totalCalorie += stepModel.stepCalorie
|
||
|
totalDuration += stepModel.stepDuration
|
||
|
sources.append(ChartViewData(date: nil, value: stepModel.stepNumber))
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
// 添加空数据
|
||
|
if !isMatch {
|
||
|
sources.append(ChartViewData(date: nil, value: 0))
|
||
|
}
|
||
|
}
|
||
![]()
1 year ago
|
chartViewModel = ChartViewModel(leftIcon: nil, title: nil, legends: nil, source:sources, chartType: .stepRoundBar, maxAxis: maxAxis, xAxis: xAxis, lineColor: kHexColor(0x388AFF), center: String(totalStep), centerColor: kHexColor(0x388AFF), centerUnit: MultiLanguageKey_FB.stepTipFB.localized)
|
||
![]()
2 years ago
|
chartViewModel.points = sources.count
|
||
|
// 底部数据总和
|
||
|
formModel = HealthFormModel(oneValue: String(maxAxis), twoValue: String(format: "%.2f", Double(totalDistance) / 1000.0), threeValue: String(totalDuration), fourValue: String(totalCalorie))
|
||
|
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 = 0
|
||
|
var totalStep = 0
|
||
|
var totalDistance = 0
|
||
|
var totalCalorie: Float = 0
|
||
|
var totalDuration = 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 stepModel in stepModelArr {
|
||
|
if dataDate == stepModel.dataDate && stepModel.stepNumber != 0 {
|
||
|
isMatch = true
|
||
|
if stepModel.stepNumber > maxAxis {
|
||
|
maxAxis = stepModel.stepNumber
|
||
|
}
|
||
|
totalStep += stepModel.stepNumber
|
||
|
totalDistance += stepModel.stepDistance
|
||
|
totalCalorie += stepModel.stepCalorie
|
||
|
totalDuration += stepModel.stepDuration
|
||
|
sources.append(ChartViewData(date: nil, value: stepModel.stepNumber))
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
// 添加空数据
|
||
|
if !isMatch {
|
||
|
sources.append(ChartViewData(date: nil, value: 0))
|
||
|
}
|
||
|
}
|
||
![]()
1 year ago
|
chartViewModel = ChartViewModel(leftIcon: nil, title: nil, legends: nil, source:sources, chartType: .stepRoundBar, maxAxis: maxAxis, xAxis: xAxis, lineColor: kHexColor(0x388AFF), center: String(totalStep), centerColor: kHexColor(0x388AFF), centerUnit: MultiLanguageKey_FB.stepTipFB.localized)
|
||
![]()
2 years ago
|
chartViewModel.points = sources.count
|
||
|
// 底部数据总和
|
||
|
formModel = HealthFormModel(oneValue: String(maxAxis), twoValue: String(format: "%.2f", Double(totalDistance) / 1000.0), threeValue: String(totalDuration), fourValue: String(totalCalorie))
|
||
|
break
|
||
|
default:
|
||
|
self.relay.accept(self.defaultVM)
|
||
|
self.formRealy.accept(nil)
|
||
|
return
|
||
|
}
|
||
|
// 更新数据
|
||
|
self.relay.accept(chartViewModel)
|
||
|
self.formRealy.accept(formModel)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|