// // ScheduleEditing.swift // Workouts // // Copyright 2026 Rouslan Zenetl. All Rights Reserved. // import Foundation import SwiftData /// Shared schedule-editing helpers used by both the iOS `ScheduleAddEditView` and the /// Mac `MacScheduleEditorSheet` / `MacSchedulesView` — weekday ordering, the /// minutes-from-midnight wire-format conversions, and the next-order helper for new /// schedules. Kept in `Shared/` (compiled into every target) so the two platforms never /// drift on these byte-identical semantics. enum ScheduleEditing { /// Calendar weekday numbers in Monday-first display order (Mon…Sat = 2…7, Sun = 1). static let weekdayOrder = [2, 3, 4, 5, 6, 7, 1] /// Gregorian short names, 0-indexed from Sunday — weekday `n` is `symbols[n - 1]`. static let weekdaySymbols = Calendar(identifier: .gregorian).shortWeekdaySymbols /// A Date (today, local calendar) at the stored minutes-from-midnight, for the /// hour-and-minute picker. Nil (no reminder yet) seeds a sensible 8:00 AM default. static func time(fromMinutes minutes: Int?) -> Date { let startOfDay = Calendar.current.startOfDay(for: Date()) return Calendar.current.date(byAdding: .minute, value: minutes ?? 8 * 60, to: startOfDay) ?? startOfDay } /// The picked time back to minutes from midnight (local wall-clock). static func minutesFromMidnight(of time: Date) -> Int { let comps = Calendar.current.dateComponents([.hour, .minute], from: time) return (comps.hour ?? 0) * 60 + (comps.minute ?? 0) } /// One past the highest existing schedule order — appends a new schedule to the end. static func nextOrder(in modelContext: ModelContext) -> Int { let existing = (try? modelContext.fetch(FetchDescriptor())) ?? [] return (existing.map(\.order).max() ?? -1) + 1 } }