Generate labels from workout

This commit is contained in:
Christoph Hagen
2025-08-22 00:01:51 +02:00
parent 9ec207014c
commit f972a2c020
8 changed files with 108 additions and 14 deletions

View File

@@ -77,6 +77,45 @@ final class LocalizedPost: ChangeObservingItem {
var hasVideos: Bool {
images.contains { $0.type.isVideo }
}
func updateLabels(from workout: RouteOverview, locale: Locale) {
insertOrReplace(label: .init(icon: .statisticsDistance, value: String(format: "%.1f km", locale: locale, workout.distance / 1000)))
insertOrReplace(label: .init(icon: .statisticsTime, value: workout.duration.duration(locale: locale)))
insertOrReplace(label: .init(icon: .statisticsElevationUp, value: workout.ascendedElevation.length(roundingToNearest: 50)))
insertOrReplace(label: .init(icon: .statisticsEnergy, value: workout.energy.energy(roundingToNearest: 50)))
}
func insertOrReplace(label: ContentLabel) {
if let index = labels.firstIndex(where: { $0.icon == label.icon }) {
labels[index] = label
} else {
labels.append(label)
}
}
}
private extension TimeInterval {
func duration(locale: Locale) -> String {
let totalMinutes = Int((self / 60).rounded(to: 5))
let hours = totalMinutes / 60
let minutes = totalMinutes % 60
let suffix = locale.identifier.hasPrefix("de") ? "Std" : "h"
return String(format: "%d:%02d ", hours, minutes) + suffix
}
func length(roundingToNearest interval: Double) -> String {
let rounded = Int(self.rounded(to: interval))
return "\(rounded) m"
}
func energy(roundingToNearest interval: Double) -> String {
let rounded = Int(self.rounded(to: interval))
return "\(rounded) kcal"
}
}
// MARK: Storage