|
|
|
//
|
|
|
|
// SportViewModels.swift
|
|
|
|
// HPlusFit
|
|
|
|
//
|
|
|
|
// Created by lemo. on 2019/9/15.
|
|
|
|
// Copyright © 2019 lemo. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
|
|
|
class SportViewModels: ViewModel {
|
|
|
|
|
|
|
|
var relay = BehaviorRelay<[HealthSection]>(value: [])
|
|
|
|
|
|
|
|
/// 运动分类数据源(选中的类型,运动类型)
|
|
|
|
var sportCategoryVariable = BehaviorRelay<[SectionModel<SportType, SportType>]>(value: [])
|
|
|
|
/// 动态分类控件高度
|
|
|
|
var cateforyHieghtVariable = BehaviorRelay<CGFloat>(value: 0)
|
|
|
|
// 切换当前选择的运动类型(默认展示所有)
|
|
|
|
var selectType: SportType = .placeholder
|
|
|
|
|
|
|
|
/// 查询到的运动数据
|
|
|
|
var sportModelArr: [SportModel] = []
|
|
|
|
/// 当前时间(默认当前)
|
|
|
|
private var currenTimeStr = DateClass.getCurrentTimeStr(formatStr: "yyyy-MM-dd")
|
|
|
|
private var queryType: DataQueryType = .day
|
|
|
|
|
|
|
|
override init() {
|
|
|
|
super.init()
|
|
|
|
// 默认加载当天
|
|
|
|
loadSportData(timeStr: currenTimeStr, queryType: queryType)
|
|
|
|
monitor()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 加载运动数据
|
|
|
|
func loadSportData(timeStr: String?, queryType: DataQueryType?) {
|
|
|
|
// 无查询类型默认为上一次查询类型
|
|
|
|
let tempQueryType = queryType == nil ? self.queryType : queryType!
|
|
|
|
self.queryType = tempQueryType
|
|
|
|
// 查询时间戳
|
|
|
|
let tempTimeStr = timeStr == nil ? currenTimeStr : timeStr!
|
|
|
|
currenTimeStr = tempTimeStr
|
|
|
|
DataBaseManager.shared.querySportData(dataDate: tempTimeStr, queryType: tempQueryType) { [weak self] (results) in
|
|
|
|
guard let `self` = self else { return }
|
|
|
|
guard var sportModelArr = results as? [SportModel], sportModelArr.count > 0 else {
|
|
|
|
self.relay.accept([])
|
|
|
|
self.sportModelArr = []
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// 排序
|
|
|
|
sportModelArr.sort { $0.timestamp > $1.timestamp }
|
|
|
|
self.sportModelArr = sportModelArr
|
|
|
|
var section = sportModelArr.first?.getSportYearMonth() ?? ""
|
|
|
|
var dataSource: [HealthSectionItem] = []
|
|
|
|
var dataVariable: [HealthSection] = []
|
|
|
|
sportModelArr.enumerated().forEach({ (index, sportModel) in
|
|
|
|
// 按照类型区分,若不是全选,则过滤掉非选中的类型运动
|
|
|
|
if self.selectType != .placeholder, sportModel.getSportType != self.selectType {
|
|
|
|
/// 最后一个数据处理
|
|
|
|
if index == sportModelArr.count - 1, dataSource.count > 0 {
|
|
|
|
if dataSource.count > 0 {
|
|
|
|
dataVariable.append(HealthSection.health(title: section, items: dataSource))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// 按月分组拼接
|
|
|
|
let sportYearMonth = sportModel.getSportYearMonth()
|
|
|
|
if sportYearMonth == section {
|
|
|
|
dataSource.append(HealthSectionItem.sportItem(viewModel: SportCellViewModels(sportModel: sportModel)))
|
|
|
|
}else {
|
|
|
|
if dataSource.count > 0 {
|
|
|
|
dataVariable.append(HealthSection.health(title: section, items: dataSource))
|
|
|
|
dataSource.removeAll()
|
|
|
|
}
|
|
|
|
dataSource.append(HealthSectionItem.sportItem(viewModel: SportCellViewModels(sportModel: sportModel)))
|
|
|
|
section = sportYearMonth
|
|
|
|
}
|
|
|
|
/// 最后一个数据
|
|
|
|
if index == sportModelArr.count - 1 {
|
|
|
|
dataVariable.append(HealthSection.health(title: section, items: dataSource))
|
|
|
|
dataSource.append(HealthSectionItem.sportItem(viewModel: SportCellViewModels(sportModel: sportModel)))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
self.relay.accept(dataVariable)
|
|
|
|
// 更新
|
|
|
|
self.sportDataHanlde()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 根据运动类型选择展示的运动数据(不需要重新查询)
|
|
|
|
func switchSportTypeData(sportType: SportType) {
|
|
|
|
if sportType == selectType {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
selectType = sportType
|
|
|
|
sportDataHanlde()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 删除运动数据
|
|
|
|
/// - Parameter sportModel: 运动模型
|
|
|
|
func deleteSport(sportModel: SportModel) {
|
|
|
|
// 删除数据库运动数据
|
|
|
|
// DataBaseManager.shared.deleteSportData(sportTimestamp: sportModel.dataDate)
|
|
|
|
// // 更新数据
|
|
|
|
// loadSportData(timeStr: currenTimeStr, queryType: queryType)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 数据监听
|
|
|
|
private func monitor() {
|
|
|
|
// 日期改变
|
|
|
|
kNotificationCenter.rx.notification(Notification.Name(rawValue: DateChangeFireBoltt))
|
|
|
|
.subscribe(onNext: { [weak self] notication in
|
|
|
|
guard let `self` = self else { return }
|
|
|
|
self.currenTimeStr = DateClass.getCurrentTimeStr(formatStr: "yyyy-MM-dd")
|
|
|
|
self.loadSportData(timeStr: self.currenTimeStr, queryType: self.queryType)
|
|
|
|
})
|
|
|
|
.disposed(by: rx.disposeBag)
|
|
|
|
// 监听单位切换
|
|
|
|
kNotificationCenter.rx.notification(Notification.Name(rawValue: UnitChangeFireBoltt))
|
|
|
|
.subscribe(onNext: { [weak self] _ in
|
|
|
|
guard let `self` = self else { return }
|
|
|
|
self.loadSportData(timeStr: self.currenTimeStr, queryType: self.queryType)
|
|
|
|
})
|
|
|
|
.disposed(by: rx.disposeBag)
|
|
|
|
// 运动同步完成
|
|
|
|
BluetoothService.shared.sportUpdate
|
|
|
|
.subscribe(onNext: { [weak self] in
|
|
|
|
guard let `self` = self else { return }
|
|
|
|
self.loadSportData(timeStr: self.currenTimeStr, queryType: self.queryType)
|
|
|
|
})
|
|
|
|
.disposed(by: rx.disposeBag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension SportViewModels {
|
|
|
|
/// 运动数据分组,分类展示
|
|
|
|
private func sportDataHanlde() {
|
|
|
|
if sportModelArr.count > 0 {
|
|
|
|
var section = sportModelArr.first?.getSportYearMonth() ?? ""
|
|
|
|
var dataSource: [HealthSectionItem] = []
|
|
|
|
var dataVariable: [HealthSection] = []
|
|
|
|
sportModelArr.enumerated().forEach({ (index, sportModel) in
|
|
|
|
// 按照类型区分,若不是全选,则过滤掉非选中的类型运动
|
|
|
|
if self.selectType != .placeholder, sportModel.getSportType != self.selectType {
|
|
|
|
/// 最后一个数据处理
|
|
|
|
if index == sportModelArr.count - 1, dataSource.count > 0 {
|
|
|
|
if dataSource.count > 0 {
|
|
|
|
dataVariable.append(HealthSection.health(title: section, items: dataSource))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// 按月分组拼接
|
|
|
|
let sportYearMonth = sportModel.getSportYearMonth()
|
|
|
|
if sportYearMonth == section {
|
|
|
|
dataSource.append(HealthSectionItem.sportItem(viewModel: SportCellViewModels(sportModel: sportModel)))
|
|
|
|
}else {
|
|
|
|
if dataSource.count > 0 {
|
|
|
|
dataVariable.append(HealthSection.health(title: section, items: dataSource))
|
|
|
|
dataSource.removeAll()
|
|
|
|
}
|
|
|
|
dataSource.append(HealthSectionItem.sportItem(viewModel: SportCellViewModels(sportModel: sportModel)))
|
|
|
|
section = sportYearMonth
|
|
|
|
}
|
|
|
|
/// 最后一个数据
|
|
|
|
if index == sportModelArr.count - 1 {
|
|
|
|
dataVariable.append(HealthSection.health(title: section, items: dataSource))
|
|
|
|
dataSource.append(HealthSectionItem.sportItem(viewModel: SportCellViewModels(sportModel: sportModel)))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
self.relay.accept(dataVariable)
|
|
|
|
// 更新分类属性
|
|
|
|
loadSportCategoryData(sports: sportModelArr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 运动分类数据源拼接(根据是否存在分类展示)
|
|
|
|
private func loadSportCategoryData(sports: [SportModel]) {
|
|
|
|
// 运动类型数据源
|
|
|
|
var sportTypeArr: [SportType] = [.placeholder]
|
|
|
|
// 根据运动类型排序
|
|
|
|
let typeArr = sports.map{ $0.getSportType }
|
|
|
|
.sorted { (sportType1, sportType2) in return sportType1.rawValue < sportType2.rawValue }
|
|
|
|
typeArr.enumerated().forEach({ (_, sportType) in
|
|
|
|
// 仅插入不重复的类型
|
|
|
|
if sportType != sportTypeArr.last {
|
|
|
|
sportTypeArr.append(sportType)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// 高度更新(rowCount * rowHeight + 留白)
|
|
|
|
let rowCount = sportTypeArr.count > 6 ? 6.0 : CGFloat(sportTypeArr.count)
|
|
|
|
cateforyHieghtVariable.accept(rowCount * kScaleHeight(45) + 10)
|
|
|
|
// 分类更新(默认选中所有)
|
|
|
|
sportCategoryVariable.accept([SectionModel(model: self.selectType, items: sportTypeArr)])
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|