Files
workouts/Shared/Utils/Date+Extensions.swift
T
rzen 2c1e4759ae Add machine comfort settings and tighten the document schemas
Machine-based exercises now carry ordered name/value comfort settings
(seat height, back-rest position, ...) on both ExerciseDocument and, as a
plan-time snapshot, WorkoutLogDocument — the optional array doubles as the
machine flag. Editable from the exercise editor's new Machine section and
from the workout row's settings sheet, whose mid-workout edits write back
to the originating split (workout saved first, so seed clone-on-edit
repointing can't clobber the log edit).

Schema tightening rides the same rev: splits bump to v2 (weight-reminder
fields and the unused exercise category removed), workouts to v3 (the
derived `completed` flag removed; status is the single source). Starter
seeds regenerated at v2 with unchanged ULIDs; SwiftData cache schema
bumped to rebuild. SCHEMA.md documents the shapes.

Claude-Session: https://claude.ai/code/session_01LEoff8bXGBS83tK1c55Mf7
2026-07-06 16:29:44 -04:00

49 lines
2.0 KiB
Swift

import Foundation
extension Date {
// Cached formatters — DateFormatter init is expensive and these are called
// in list rows on every render pass.
private static let shortDateTime: DateFormatter = {
let f = DateFormatter(); f.dateStyle = .short; f.timeStyle = .short; return f
}()
private static let timeOnly: DateFormatter = {
let f = DateFormatter(); f.dateStyle = .none; f.timeStyle = .short; return f
}()
private static let mediumDate: DateFormatter = {
let f = DateFormatter(); f.dateStyle = .medium; f.timeStyle = .none; return f
}()
private static let monthAbbrev: DateFormatter = {
let f = DateFormatter(); f.dateFormat = "MMM"; return f
}()
private static let weekdayAbbrev: DateFormatter = {
let f = DateFormatter(); f.dateFormat = "EEE"; return f
}()
private static let relativeDateTime: DateFormatter = {
let f = DateFormatter()
f.dateStyle = .medium; f.timeStyle = .short
f.doesRelativeDateFormatting = true
return f
}()
func formattedDate() -> String { Self.shortDateTime.string(from: self) }
/// "Today at 10:44 AM" / "Yesterday at 9:12 AM" / "Jul 3, 2026 at 8:00 AM".
func formattedRelativeDateTime() -> String { Self.relativeDateTime.string(from: self) }
func formattedTime() -> String { Self.timeOnly.string(from: self) }
func formatDate() -> String { Self.mediumDate.string(from: self) }
func isSameDay(as other: Date) -> Bool {
Calendar.current.isDate(self, inSameDayAs: other)
}
var abbreviatedMonth: String { Self.monthAbbrev.string(from: self) }
var abbreviatedWeekday: String { Self.weekdayAbbrev.string(from: self) }
var dayOfMonth: Int { Calendar.current.component(.day, from: self) }
func humanTimeInterval(to other: Date) -> String {
let interval = other.timeIntervalSince(self)
let hours = Int(interval) / 3600
let minutes = (Int(interval) % 3600) / 60
return hours > 0 ? "\(hours)h \(minutes)m" : "\(minutes)m"
}
}