Repository Public
1import MCCCore2import SwiftUI34/// Compact chip of one occurrence in a day cell: status icon (shape differs per status), category symbol, start5/// time and title. Cancelled windows are also struck through; the tooltip and VoiceOver give the status in words.6struct OccurrenceChip: View {7 @Environment(\.mccTheme) private var theme8 let occurrence: API.MaintenanceOccurrence9 let day: Date10 let calendar: Calendar11 var selected = false1213 var body: some View {14 let status = MaintenanceLogic.displayStatus(occurrence)15 let startsToday = calendar.isDate(occurrence.occurrenceStart, inSameDayAs: day)16 HStack(spacing: 3) {17 Image(systemName: status == .scheduled ? occurrence.window.category.symbol : status.tone.symbol)18 .imageScale(.small)19 .foregroundStyle(status.tone.color(theme))20 .accessibilityHidden(true)21 Text(startsToday ? Format.time(occurrence.occurrenceStart, timeZone: calendar.timeZone) : "…")22 .lineLimit(1)23 .fixedSize()24 .monospacedDigit()25 .foregroundStyle(theme.textSecondary)26 Text(occurrence.window.title)27 .strikethrough(status == .cancelled)28 .foregroundStyle(theme.textPrimary)29 .lineLimit(1)30 Spacer(minLength: 0)31 if occurrence.noteCount > 0 {32 Image(systemName: "note.text").imageScale(.small).foregroundStyle(theme.textTertiary).accessibilityHidden(true)33 }34 }35 .font(.caption)36 .padding(.horizontal, 4)37 .padding(.vertical, 2)38 .frame(maxWidth: .infinity, alignment: .leading)39 .background(selected ? theme.accentMuted : status.tone.muted(theme), in: RoundedRectangle(cornerRadius: Tokens.Radius.sm, style: .continuous))40 .overlay(RoundedRectangle(cornerRadius: Tokens.Radius.sm, style: .continuous)41 .strokeBorder(selected ? theme.accent : status.tone.color(theme).opacity(theme.highContrast ? 0.9 : 0.35), lineWidth: selected ? 2 : 1))42 .help(MaintenanceLogic.summary(of: occurrence, timeZone: calendar.timeZone))43 }44}4546/// Month grid: six weeks, weekday header, today marked, occurrence chips with "+n" overflow, keyboard navigation.47struct MaintenanceMonthView: View {48 @Environment(\.mccTheme) private var theme49 let anchor: Date50 let calendar: Calendar51 let occurrences: [API.MaintenanceOccurrence]52 @Binding var selectedDay: Date53 let selection: OccurrenceRef?54 let onSelect: (API.MaintenanceOccurrence) -> Void55 let onCreate: (Date) -> Void56 let onMoveAnchor: (Date) -> Void57 @FocusState private var focused: Bool5859 private static let maxChips = 36061 var body: some View {62 let days = CalendarMath.monthGrid(containing: anchor, calendar: calendar)63 VStack(spacing: 0) {64 HStack(spacing: 0) {65 ForEach(Array(CalendarMath.weekdaySymbols(calendar: calendar).enumerated()), id: \.offset) { _, s in66 Text(s.capitalizedFirst).font(.caption.weight(.semibold)).foregroundStyle(theme.textSecondary)67 .frame(maxWidth: .infinity).padding(.vertical, Tokens.Space.s2)68 }69 }70 .accessibilityHidden(true)71 Grid(horizontalSpacing: 0, verticalSpacing: 0) {72 ForEach(0..<6, id: \.self) { week in73 GridRow {74 ForEach(0..<7, id: \.self) { col in75 if days.indices.contains(week * 7 + col) { cell(days[week * 7 + col]) }76 }77 }78 }79 }80 .overlay(Rectangle().strokeBorder(theme.borderSubtle, lineWidth: 1))81 }82 .focusable()83 .focused($focused)84 .focusEffectDisabled()85 .onKeyPress(.leftArrow) { move(-1); return .handled }86 .onKeyPress(.rightArrow) { move(1); return .handled }87 .onKeyPress(.upArrow) { move(-7); return .handled }88 .onKeyPress(.downArrow) { move(7); return .handled }89 .onKeyPress(.return) { open(); return .handled }90 .onAppear { focused = true }91 .accessibilityElement(children: .contain)92 .accessibilityLabel(tr("maintenance.calendar.month.a11y", MaintenanceLogic.periodTitle(mode: .month, anchor: anchor, calendar: calendar)))93 .accessibilityAddTraits(.isHeader)94 .accessibilityIdentifier("maintenance-month")95 }9697 private func move(_ days: Int) {98 guard let d = calendar.date(byAdding: .day, value: days, to: selectedDay) else { return }99 selectedDay = d100 if !calendar.isDate(d, equalTo: anchor, toGranularity: .month) { onMoveAnchor(d) }101 }102103 /// Return: the first occurrence of the selected day, or a new window on it.104 private func open() {105 if let first = CalendarMath.occurrences(on: selectedDay, in: occurrences, calendar: calendar).first { onSelect(first) } else { onCreate(selectedDay) }106 }107108 private func fullDate(_ d: Date) -> String {109 var style = Date.FormatStyle(date: .complete, time: .omitted)110 style.locale = Localization.shared.locale111 style.timeZone = calendar.timeZone112 return d.formatted(style)113 }114115 private func cell(_ day: CalendarDay) -> some View {116 let items = CalendarMath.occurrences(on: day.date, in: occurrences, calendar: calendar)117 let isSelected = calendar.isDate(day.date, inSameDayAs: selectedDay)118 let summary = items.isEmpty ? tr("maintenance.day.none")119 : tr("maintenance.day.count", items.count) + ". " + items.map { MaintenanceLogic.summary(of: $0, timeZone: calendar.timeZone) }.joined(separator: ". ")120 return VStack(alignment: .leading, spacing: 2) {121 HStack {122 Text("\(calendar.component(.day, from: day.date))")123 .font(.callout.weight(day.isToday ? .bold : .regular).monospacedDigit())124 .foregroundStyle(day.isToday ? theme.accentContrast : (day.inDisplayedMonth ? theme.textPrimary : theme.textDisabled))125 .padding(.horizontal, 6).padding(.vertical, 1)126 .background(day.isToday ? theme.accent : Color.clear, in: Capsule())127 if day.isToday { Text(tr("maintenance.today")).font(.caption2).foregroundStyle(theme.textSecondary) }128 Spacer(minLength: 0)129 }130 ForEach(items.prefix(Self.maxChips), id: \.occurrenceID) { o in131 OccurrenceChip(occurrence: o, day: day.date, calendar: calendar, selected: selection?.id == OccurrenceRef(o).id)132 .contentShape(Rectangle())133 .onTapGesture { selectedDay = day.date; onSelect(o) }134 }135 if items.count > Self.maxChips {136 Text(tr("maintenance.more", items.count - Self.maxChips)).font(.caption).foregroundStyle(theme.textSecondary).padding(.leading, 4)137 }138 Spacer(minLength: 0)139 }140 .padding(4)141 .frame(maxWidth: .infinity, minHeight: 92, maxHeight: .infinity, alignment: .topLeading)142 .background(isSelected ? theme.hover : (day.inDisplayedMonth ? theme.surface : theme.inset))143 .overlay(Rectangle().strokeBorder(isSelected ? theme.focusRing : theme.borderSubtle, lineWidth: isSelected ? 2 : 0.5))144 .contentShape(Rectangle())145 .onTapGesture(count: 2) { selectedDay = day.date; if items.isEmpty { onCreate(day.date) } else if let f = items.first { onSelect(f) } }146 .onTapGesture { selectedDay = day.date; focused = true }147 .contextMenu { Button(tr("maintenance.new_window_on", Format.date(day.date, timeZone: calendar.timeZone))) { onCreate(day.date) } }148 .accessibilityElement(children: .ignore)149 .accessibilityLabel(fullDate(day.date) + (day.isToday ? ", " + tr("maintenance.today") : ""))150 .accessibilityValue(summary)151 .accessibilityAddTraits(isSelected ? [.isButton, .isSelected] : .isButton)152 .accessibilityAction { selectedDay = day.date; if let f = items.first { onSelect(f) } else { onCreate(day.date) } }153 .accessibilityIdentifier("maintenance-day-\(calendar.component(.day, from: day.date))-\(day.inDisplayedMonth ? "in" : "out")")154 }155}156157/// Week view: seven columns on a 24-hour axis; occurrences are placed by their share of the day and clipped per day.158struct MaintenanceWeekView: View {159 @Environment(\.mccTheme) private var theme160 let anchor: Date161 let calendar: Calendar162 let occurrences: [API.MaintenanceOccurrence]163 @Binding var selectedDay: Date164 let selection: OccurrenceRef?165 let onSelect: (API.MaintenanceOccurrence) -> Void166 let onCreate: (Date) -> Void167168 private static let hourHeight: CGFloat = 44169 private static let axisWidth: CGFloat = 52170171 var body: some View {172 let days = CalendarMath.week(containing: anchor, calendar: calendar)173 let symbols = CalendarMath.weekdaySymbols(calendar: calendar)174 VStack(spacing: 0) {175 HStack(spacing: 0) {176 Color.clear.frame(width: Self.axisWidth, height: 1)177 ForEach(Array(days.enumerated()), id: \.element.id) { i, day in178 VStack(spacing: 1) {179 Text(symbols[i].capitalizedFirst).font(.caption.weight(.semibold)).foregroundStyle(theme.textSecondary)180 Text("\(calendar.component(.day, from: day.date))")181 .font(.callout.weight(day.isToday ? .bold : .regular).monospacedDigit())182 .foregroundStyle(day.isToday ? theme.accentContrast : theme.textPrimary)183 .padding(.horizontal, 7).padding(.vertical, 1)184 .background(day.isToday ? theme.accent : Color.clear, in: Capsule())185 }186 .frame(maxWidth: .infinity)187 .padding(.vertical, Tokens.Space.s2)188 .accessibilityHidden(true)189 }190 }191 Divider()192 ScrollViewReader { proxy in193 ScrollView {194 HStack(alignment: .top, spacing: 0) {195 VStack(spacing: 0) {196 ForEach(0..<24, id: \.self) { h in197 Text(hourLabel(h)).font(.caption2.monospacedDigit()).foregroundStyle(theme.textTertiary)198 .frame(width: Self.axisWidth - 6, height: Self.hourHeight, alignment: .topTrailing)199 .id(h)200 }201 }202 .frame(width: Self.axisWidth)203 .accessibilityHidden(true)204 ForEach(days) { day in column(day) }205 }206 }207 .onAppear { proxy.scrollTo(7, anchor: .top) }208 }209 }210 .overlay(Rectangle().strokeBorder(theme.borderSubtle, lineWidth: 1))211 .accessibilityElement(children: .contain)212 .accessibilityLabel(tr("maintenance.calendar.week.a11y", MaintenanceLogic.periodTitle(mode: .week, anchor: anchor, calendar: calendar)))213 .accessibilityIdentifier("maintenance-week")214 }215216 private func hourLabel(_ h: Int) -> String {217 guard let d = calendar.date(bySettingHour: h, minute: 0, second: 0, of: calendar.startOfDay(for: anchor)) else { return "\(h)" }218 return Format.time(d, timeZone: calendar.timeZone)219 }220221 private func column(_ day: CalendarDay) -> some View {222 let items = CalendarMath.occurrences(on: day.date, in: occurrences, calendar: calendar)223 let cols = MaintenanceLogic.columns(for: items.map { (start: $0.occurrenceStart, end: $0.occurrenceEnd) })224 let total = Self.hourHeight * 24225 let isSelected = calendar.isDate(day.date, inSameDayAs: selectedDay)226 return GeometryReader { geo in227 ZStack(alignment: .topLeading) {228 VStack(spacing: 0) {229 ForEach(0..<24, id: \.self) { _ in230 Rectangle().fill(Color.clear).frame(height: Self.hourHeight)231 .overlay(alignment: .top) { Rectangle().fill(theme.borderSubtle).frame(height: 0.5) }232 }233 }234 ForEach(Array(items.enumerated()), id: \.element.occurrenceID) { i, o in235 if let extent = MaintenanceLogic.dayExtent(start: o.occurrenceStart, end: o.occurrenceEnd, day: day.date, calendar: calendar) {236 let width = (geo.size.width - 4) / CGFloat(cols[i].count)237 let height = max(20, CGFloat(extent.bottom - extent.top) * total)238 weekBlock(o, day: day.date)239 .frame(width: max(12, width - 2), height: height, alignment: .topLeading)240 .offset(x: 2 + CGFloat(cols[i].column) * width, y: CGFloat(extent.top) * total)241 }242 }243 }244 }245 .frame(maxWidth: .infinity)246 .frame(height: total)247 .background(isSelected ? theme.hover : (day.isToday ? theme.raised : theme.surface))248 .overlay(alignment: .leading) { Rectangle().fill(theme.borderSubtle).frame(width: 0.5) }249 .contentShape(Rectangle())250 .onTapGesture(count: 2) { selectedDay = day.date; onCreate(day.date) }251 .onTapGesture { selectedDay = day.date }252 .accessibilityElement(children: .contain)253 .accessibilityLabel(Format.date(day.date, timeZone: calendar.timeZone))254 .accessibilityValue(items.isEmpty ? tr("maintenance.day.none") : tr("maintenance.day.count", items.count))255 }256257 private func weekBlock(_ o: API.MaintenanceOccurrence, day: Date) -> some View {258 let status = MaintenanceLogic.displayStatus(o)259 let selected = selection?.id == OccurrenceRef(o).id260 return Button { selectedDay = day; onSelect(o) } label: {261 VStack(alignment: .leading, spacing: 1) {262 HStack(spacing: 3) {263 Image(systemName: status == .scheduled ? o.window.category.symbol : status.tone.symbol).imageScale(.small)264 .foregroundStyle(status.tone.color(theme)).accessibilityHidden(true)265 Text(o.window.title).font(.caption.weight(.medium)).strikethrough(status == .cancelled).foregroundStyle(theme.textPrimary).lineLimit(2)266 }267 Text(MaintenanceLogic.timeRange(start: o.occurrenceStart, end: o.occurrenceEnd, timeZone: calendar.timeZone))268 .font(.caption2.monospacedDigit()).foregroundStyle(theme.textSecondary).lineLimit(1)269 Text(status.label).font(.caption2).foregroundStyle(theme.textSecondary).lineLimit(1)270 }271 .padding(3)272 .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)273 .background(selected ? theme.accentMuted : status.tone.muted(theme), in: RoundedRectangle(cornerRadius: Tokens.Radius.sm, style: .continuous))274 .overlay(RoundedRectangle(cornerRadius: Tokens.Radius.sm, style: .continuous)275 .strokeBorder(selected ? theme.accent : status.tone.color(theme).opacity(theme.highContrast ? 1 : 0.5), lineWidth: selected ? 2 : 1))276 .clipped()277 }278 .buttonStyle(.plain)279 .help(MaintenanceLogic.summary(of: o, timeZone: calendar.timeZone))280 .accessibilityLabel(MaintenanceLogic.summary(of: o, timeZone: calendar.timeZone))281 .accessibilityValue(MaintenanceLogic.timeRange(start: o.occurrenceStart, end: o.occurrenceEnd, timeZone: calendar.timeZone))282 }283}284285/// One line of the agenda (also used by the node panel).286struct AgendaRow: View {287 @Environment(AppModel.self) private var app288 @Environment(\.mccTheme) private var theme289 let occurrence: API.MaintenanceOccurrence290 var compact = false291 var selected = false292293 private var nodesText: String {294 let ids = occurrence.window.scope.nodeIds295 let names = ids.map { app.session.nodes.alias($0) ?? tr("maintenance.node.unknown") }.sorted { $0.localizedStandardCompare($1) == .orderedAscending }296 if names.count <= 4 { return names.joined(separator: ", ") }297 return names.prefix(4).joined(separator: ", ") + " " + tr("maintenance.more", names.count - 4)298 }299300 var body: some View {301 let w = occurrence.window302 let status = MaintenanceLogic.displayStatus(occurrence)303 HStack(alignment: .top, spacing: Tokens.Space.s4) {304 VStack(alignment: .leading, spacing: 2) {305 Text(MaintenanceLogic.timeRange(start: occurrence.occurrenceStart, end: occurrence.occurrenceEnd))306 .font(.callout.monospacedDigit()).foregroundStyle(theme.textPrimary)307 Text(Format.duration(seconds: occurrence.occurrenceEnd.timeIntervalSince(occurrence.occurrenceStart))).font(.caption).foregroundStyle(theme.textTertiary)308 }309 .frame(width: compact ? 150 : 200, alignment: .leading)310 VStack(alignment: .leading, spacing: 3) {311 HStack(spacing: Tokens.Space.s2) {312 Image(systemName: w.category.symbol).foregroundStyle(theme.textSecondary).accessibilityHidden(true)313 Text(w.title).font(.callout.weight(.semibold)).strikethrough(status == .cancelled).foregroundStyle(theme.textPrimary).lineLimit(1)314 }315 Text(tr("maintenance.agenda.nodes", w.scope.nodeIds.count) + " : " + nodesText).font(.caption).foregroundStyle(theme.textSecondary).lineLimit(2)316 if !compact {317 HStack(spacing: Tokens.Space.s3) {318 Label(w.category.label, systemImage: "tag").labelStyle(.titleOnly)319 if w.recurrence != .none { Label(MaintenanceLogic.recurrenceSummary(w.recurrence, until: w.recurrenceUntil), systemImage: "repeat") }320 if w.setMaintenanceMode { Label(tr("maintenance.flag.mode.short"), systemImage: "wrench.and.screwdriver") }321 if w.silenceAlerts { Label(tr("maintenance.flag.silence.short"), systemImage: "bell.slash") }322 if occurrence.noteCount > 0 { Label(tr("maintenance.note_count", occurrence.noteCount), systemImage: "note.text") }323 }324 .font(.caption)325 .foregroundStyle(theme.textTertiary)326 }327 }328 Spacer(minLength: Tokens.Space.s3)329 StatusBadge(status.label, tone: status.tone, compact: true)330 }331 .padding(Tokens.Space.s3)332 .frame(maxWidth: .infinity, alignment: .leading)333 .background(selected ? theme.accentMuted : theme.surface, in: RoundedRectangle(cornerRadius: Tokens.Radius.md, style: .continuous))334 .overlay(RoundedRectangle(cornerRadius: Tokens.Radius.md, style: .continuous).strokeBorder(selected ? theme.accent : theme.borderSubtle, lineWidth: selected ? 2 : 1))335 .contentShape(Rectangle())336 .accessibilityElement(children: .ignore)337 .accessibilityLabel("\(w.title), \(status.label)")338 .accessibilityValue("\(MaintenanceLogic.timeRange(start: occurrence.occurrenceStart, end: occurrence.occurrenceEnd)), \(tr("maintenance.agenda.nodes", w.scope.nodeIds.count)) : \(nodesText)")339 .accessibilityAddTraits(.isButton)340 }341}342343/// Agenda: occurrences grouped by day, upcoming first.344struct MaintenanceAgendaView: View {345 @Environment(\.mccTheme) private var theme346 let calendar: Calendar347 let occurrences: [API.MaintenanceOccurrence]348 let selection: OccurrenceRef?349 let onSelect: (API.MaintenanceOccurrence) -> Void350351 private func dayTitle(_ d: Date) -> String {352 var style = Date.FormatStyle(date: .complete, time: .omitted)353 style.locale = Localization.shared.locale354 style.timeZone = calendar.timeZone355 return d.formatted(style).capitalizedFirst356 }357358 var body: some View {359 let groups = CalendarMath.agenda(occurrences, calendar: calendar)360 if groups.isEmpty {361 EmptyStateView(tr("maintenance.agenda.empty.title"), message: tr("maintenance.agenda.empty.message"), systemImage: "calendar")362 } else {363 ScrollView {364 LazyVStack(alignment: .leading, spacing: Tokens.Space.s4, pinnedViews: []) {365 ForEach(groups, id: \.day) { group in366 VStack(alignment: .leading, spacing: Tokens.Space.s2) {367 HStack(spacing: Tokens.Space.s2) {368 Text(dayTitle(group.day)).font(.headline).foregroundStyle(theme.textPrimary).accessibilityAddTraits(.isHeader)369 if calendar.isDateInToday(group.day) { StatusBadge(tr("maintenance.today"), tone: .info, compact: true) }370 }371 ForEach(group.items, id: \.occurrenceID) { o in372 Button { onSelect(o) } label: { AgendaRow(occurrence: o, selected: selection?.id == OccurrenceRef(o).id) }373 .buttonStyle(.plain)374 }375 }376 }377 }378 .padding(.bottom, Tokens.Space.s7)379 }380 .accessibilityIdentifier("maintenance-agenda")381 }382 }383}384