Share schedule-editing and starter-seed-state helpers across platforms

This commit is contained in:
2026-07-16 19:54:57 -04:00
parent 9968cb2056
commit f6c5bc911f
4 changed files with 79 additions and 57 deletions
+41
View File
@@ -0,0 +1,41 @@
//
// 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 (MonSat = 27, 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<Schedule>())) ?? []
return (existing.map(\.order).max() ?? -1) + 1
}
}