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.
104 lines
4.9 KiB
104 lines
4.9 KiB
// |
|
// SportDetailViewModels.swift |
|
// Lookfit |
|
// |
|
// Created by lemo. on 2020/4/13. |
|
// Copyright © 2020 Sheldon. All rights reserved. |
|
// |
|
|
|
import UIKit |
|
import RxSwift |
|
import CoreLocation |
|
|
|
class SportDetailViewModels: ViewModel, ViewModelType { |
|
|
|
struct Input { |
|
} |
|
|
|
struct Output { |
|
let items: BehaviorRelay<[SectionModel<String, SportDetailCellViewModels>]> |
|
let sportName: Driver<String> |
|
let time: Driver<String> |
|
let distance: Driver<String> |
|
let distanceUnit: Driver<String> |
|
let trajectory: Driver<(Bool, [CLLocationCoordinate2D])> |
|
} |
|
|
|
func transform(input: SportDetailViewModels.Input) -> SportDetailViewModels.Output { |
|
let items = BehaviorRelay<[SectionModel<String, SportDetailCellViewModels>]>(value: []) |
|
var dataSource: [SportDetailCellViewModels] = [] |
|
let cellViewModel1 = SportDetailCellViewModels(title: MultiLanguageKey.sportDuration.localized, img: R.image.icon_sporttime(), value: sportModel.durationStr, unit: nil) |
|
let cellViewModel2 = SportDetailCellViewModels(title: MultiLanguageKey.consume.localized, img: R.image.icon_calories(), value: String(sportModel.calorie), unit: "kCal", rightLineHidden: true) |
|
let cellViewModel3 = SportDetailCellViewModels(title: MultiLanguageKey.paceAvg.localized, img: R.image.icon_pace(), value: sportModel.avgPaceStr, unit: nil) |
|
let cellViewModel4 = SportDetailCellViewModels(title: MultiLanguageKey.speedAvg.localized, img: R.image.icon_speed(), value: sportModel.avgSpeedStr, unit: "km/h", rightLineHidden: true) |
|
let cellViewModel5 = SportDetailCellViewModels(title: MultiLanguageKey.strideAvg.localized, img: R.image.icon_stride(), value: String(sportModel.stepFrequency), unit: MultiLanguageKey.strideTip.localized) |
|
let cellViewModel6 = SportDetailCellViewModels(title: MultiLanguageKey.cadenceAvg.localized, img: R.image.icon_cadence(), value: String(sportModel.stride), unit: MultiLanguageKey.cm.localized, rightLineHidden: true) |
|
let cellViewModel7 = SportDetailCellViewModels(title: MultiLanguageKey.stepNumber.localized, img: R.image.icon_stepNumber(), value: String(sportModel.stepNumber), unit: MultiLanguageKey.stepTip.localized) |
|
let cellViewModel8 = SportDetailCellViewModels(title: MultiLanguageKey.avgHr.localized, img: R.image.icon_heartRate(), value: String(sportModel.heartAvg), unit: "bpm", rightLineHidden: true) |
|
dataSource.append(cellViewModel1) |
|
dataSource.append(cellViewModel2) |
|
dataSource.append(cellViewModel3) |
|
dataSource.append(cellViewModel4) |
|
dataSource.append(cellViewModel5) |
|
dataSource.append(cellViewModel6) |
|
dataSource.append(cellViewModel7) |
|
dataSource.append(cellViewModel8) |
|
items.accept([SectionModel(model: "", items: dataSource)]) |
|
|
|
let sportName = Driver.just(sportModel.getSportType.sportTypeName) |
|
let time = Driver.just(sportModel.dataDate) |
|
let distance = Driver.just(String(format: "%.2lf", Float(sportModel.distance) / 1000.0)) |
|
let metric = UserDefaultsManager.getUserInfo()?.metricUnit ?? 0 |
|
let distanceUnit = Driver.just(metric == 0 ? "km" : "miles") |
|
|
|
let coordinates = sportModel.coordinates |
|
let trajectory = Driver.just((coordinates.count == 0, coordinates)) |
|
|
|
return Output(items: items, |
|
sportName: sportName, |
|
time: time, |
|
distance: distance, |
|
distanceUnit: distanceUnit, |
|
trajectory: trajectory) |
|
} |
|
|
|
private var sportModel: SportModel |
|
|
|
init(sportModel: SportModel) { |
|
self.sportModel = sportModel |
|
super.init() |
|
} |
|
|
|
} |
|
|
|
extension SportModel { |
|
|
|
/// 轨迹数组 |
|
var coordinates: [CLLocationCoordinate2D] { |
|
// let bool = arc4random() % 2 + 1 |
|
// if bool == 1 { |
|
// gpsDeatail = "5|22.536874|113.961758|1281, 5|22.556874|113.961758|1281, 5|22.576874|113.961758|1281" |
|
// }else { |
|
// gpsDeatail = "" |
|
// } |
|
let tempCoordinateArr = gpsDeatail.components(separatedBy: ",") |
|
if tempCoordinateArr.count < 2 { |
|
return [] |
|
} |
|
var coordinateArr: [CLLocationCoordinate2D] = [] |
|
tempCoordinateArr.forEach { (coordinateStr) in |
|
let coords = coordinateStr.components(separatedBy: "|") |
|
let formatter = NumberFormatter() |
|
formatter.locale = .current |
|
formatter.allowsFloats = true |
|
if coords.count >= 4, let latitude = formatter.number(from: coords[1])?.doubleValue, let longitude = formatter.number(from: coords[2])?.doubleValue { |
|
// 转为GCJ-02(火星坐标) |
|
let coordinate = TQLocationConverter.transformFromWGS(toGCJ: CLLocationCoordinate2DMake(latitude, longitude)) |
|
coordinateArr.append(coordinate) |
|
} |
|
} |
|
return coordinateArr |
|
} |
|
|
|
} |
|
|
|
|