Repository Public
1// SP Notes iOS2// File: WidgetShared/TaskWidgetViews.swift3// Description: The « Capture et tâches » widget. Small: four capture buttons (note, task, dictation, audio) that open the app on4// the capture sheet. Medium and large: the open tasks due today or late, each with a check button that completes5// the task from the Home Screen without opening the app (`CompleteTaskIntent`), and an add button. While the app6// lock is on the task list — text and check buttons — is replaced by the redacted state.7//8// Author: Simon-Pierre Boucher9// Contact: contact@spboucher.ai10import AppIntents11import SPNotesDesign12import SwiftUI13import WidgetKit1415/// A capture mode offered by the widget.16struct WidgetCaptureMode: Identifiable {17 /// `kind` of `spnotes://capture?kind=…`.18 let id: String19 let title: LocalizedStringKey20 let symbol: String2122 @MainActor static let all: [WidgetCaptureMode] = [23 WidgetCaptureMode(id: "note", title: "Note", symbol: "square.and.pencil"),24 WidgetCaptureMode(id: "tasks", title: "Tâche", symbol: "checklist"),25 WidgetCaptureMode(id: "dictation", title: "Dictée", symbol: "waveform.badge.mic"),26 WidgetCaptureMode(id: "audio", title: "Audio", symbol: "mic"),27 ]28}2930extension WidgetFamily {31 /// Number of tasks the widget lists.32 var taskCapacity: Int {33 switch self {34 case .systemMedium: return 335 case .systemLarge, .systemExtraLarge: return 636 default: return 037 }38 }39}4041struct TasksWidgetView: View {42 let content: WidgetTasksContent43 let family: WidgetFamily44 var now = Date()4546 var body: some View {47 WidgetToneReader { tone in48 switch family {49 case .accessoryCircular:50 ZStack {51 AccessoryWidgetBackground()52 Image(systemName: "plus.circle").font(.title2.weight(.semibold)).widgetAccentable()53 }54 .accessibilityLabel(Text("Capture"))55 .widgetURL(SystemLinks.capture())56 case .systemSmall:57 captureGrid(tone)58 default:59 list(tone)60 }61 }62 }6364 // MARK: small — capture6566 private func captureGrid(_ tone: WidgetTone) -> some View {67 let modes = WidgetCaptureMode.all68 return VStack(spacing: 8) {69 ForEach(0..<2, id: \.self) { row in70 HStack(spacing: 8) {71 ForEach(modes[(row * 2)..<(row * 2 + 2)]) { mode in72 Button(intent: OpenCaptureIntent(kind: mode.id)) { WidgetQuickButton(title: mode.title, symbol: mode.symbol, tone: tone, compact: true) }73 .buttonStyle(.plain)74 }75 }76 }77 }78 .widgetURL(SystemLinks.capture())79 }8081 // MARK: medium, large — tasks8283 @ViewBuilder private func list(_ tone: WidgetTone) -> some View {84 VStack(alignment: .leading, spacing: 0) {85 header(tone).padding(.bottom, 6)86 switch content {87 case .locked:88 WidgetLockedView(family: family, tone: tone)89 case .unavailable:90 WidgetMessageView(message: WidgetContent.openAppMessage, symbol: "checklist", tone: tone)91 case .tasks(let tasks, let more, let open):92 if tasks.isEmpty {93 nothingDue(open: open, tone: tone)94 } else {95 let shown = Array(tasks.prefix(family.taskCapacity))96 ForEach(Array(shown.enumerated()), id: \.element.id) { index, task in97 if index > 0 { Rectangle().fill(tone.line).frame(height: 0.5).padding(.leading, 34) }98 TaskWidgetRow(task: task, tone: tone, now: now).frame(maxHeight: .infinity)99 }100 if shown.count < family.taskCapacity {101 Spacer(minLength: 0).frame(maxHeight: .infinity).layoutPriority(Double(family.taskCapacity - shown.count))102 }103 let hidden = more + tasks.count - shown.count104 if hidden > 0, family != .systemMedium {105 Link(destination: SystemLinks.tasks) {106 Text("\(hidden) de plus").font(.caption2.weight(.medium)).foregroundStyle(tone.muted)107 }108 .padding(.leading, 34)109 }110 }111 }112 if family != .systemMedium { captureBar(tone).padding(.top, 8) }113 }114 .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)115 .widgetURL(content == .locked || content == .unavailable ? nil : SystemLinks.tasks)116 }117118 private func header(_ tone: WidgetTone) -> some View {119 HStack(spacing: 6) {120 WidgetHeader(title: "Aujourd’hui", symbol: "checklist", tone: tone)121 if case .tasks(let tasks, let more, _) = content, tasks.count + more > 0 {122 Text("\(tasks.count + more)").font(.caption2.weight(.semibold).monospacedDigit()).foregroundStyle(tone.muted)123 .accessibilityLabel(Text("\(tasks.count + more) tâches"))124 }125 Spacer(minLength: 4)126 if content == .locked {127 Image(systemName: "lock.fill").font(.caption.weight(.semibold)).foregroundStyle(tone.muted).widgetAccentable().accessibilityHidden(true)128 }129 Link(destination: SystemLinks.capture(kind: "tasks")) {130 Image(systemName: "plus").font(.subheadline.weight(.bold)).foregroundStyle(tone.accent).widgetAccentable()131 .frame(width: 28, height: 24).background(tone.soft, in: Capsule())132 }133 .accessibilityLabel(Text("Ajouter une tâche"))134 }135 }136137 private func nothingDue(open: Int, tone: WidgetTone) -> some View {138 VStack(spacing: 6) {139 Image(systemName: "checkmark.circle").font(.title2).foregroundStyle(tone.accent).widgetAccentable().accessibilityHidden(true)140 Text("Rien à faire aujourd’hui").font(WidgetFont.rowTitle).foregroundStyle(tone.fg)141 if open > 0 {142 Text("\(open) tâches ouvertes").font(.caption).foregroundStyle(tone.muted)143 }144 }145 .multilineTextAlignment(.center)146 .frame(maxWidth: .infinity, maxHeight: .infinity)147 }148149 private func captureBar(_ tone: WidgetTone) -> some View {150 HStack(spacing: 8) {151 ForEach(WidgetCaptureMode.all) { mode in152 Link(destination: SystemLinks.capture(kind: mode.id)) { WidgetQuickButton(title: mode.title, symbol: mode.symbol, tone: tone, compact: true) }153 }154 }155 .frame(height: 52)156 }157}158159private struct TaskWidgetRow: View {160 let task: WidgetTask161 let tone: WidgetTone162 let now: Date163164 var body: some View {165 HStack(spacing: 8) {166 Button(intent: CompleteTaskIntent(task)) {167 Image(systemName: "circle")168 .font(.system(size: 21, weight: .regular))169 .foregroundStyle(task.isHighPriority ? tone.danger : tone.accent)170 .widgetAccentable()171 .frame(width: 26, height: 30)172 .contentShape(Rectangle())173 }174 .buttonStyle(.plain)175 .accessibilityLabel(Text("Terminer « \(task.title) »"))176 Link(destination: task.url) {177 VStack(alignment: .leading, spacing: 1) {178 Text(task.title).font(WidgetFont.rowTitle).foregroundStyle(tone.fg).lineLimit(1)179 HStack(spacing: 4) {180 if let due = task.dueAt { TaskDueText(due: due, overdue: task.isOverdue, now: now).foregroundStyle(task.isOverdue ? tone.danger : tone.muted) }181 if let note = task.noteTitle, !note.isEmpty {182 Text(verbatim: "· \(note)").foregroundStyle(tone.muted)183 }184 }185 .font(.caption2)186 .lineLimit(1)187 }188 .frame(maxWidth: .infinity, alignment: .leading)189 .contentShape(Rectangle())190 }191 .privacySensitive()192 }193 .padding(.vertical, 2)194 .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)195 }196}197198/// « En retard · 12 nov. », « Aujourd’hui », or the time of a timed due date today.199struct TaskDueText: View {200 let due: Date201 let overdue: Bool202 var now = Date()203204 var body: some View {205 let day = TaskDueRule.localDay(due)206 let today = Calendar.current.isDate(day, inSameDayAs: now)207 if today, !TaskDueRule.isBareDay(due) {208 Text(due, format: .dateTime.hour().minute())209 } else if today {210 Text("Aujourd’hui")211 } else if overdue {212 Text("En retard · \(day.formatted(.dateTime.day().month(.abbreviated)))")213 } else {214 Text(day, format: .dateTime.day().month(.abbreviated))215 }216 }217}218219// MARK: sample content (widget gallery)220221extension WidgetTask {222 static var samples: [WidgetTask] {223 let noon = TaskDueRule.bareDay(of: Date())224 return [225 WidgetTask(id: "00000000-0000-4000-8000-0000000000a1", title: "Envoyer le rapport de visite", dueAt: noon.addingTimeInterval(-2 * 86_400), priority: "high", noteTitle: "Dossier Laurel", isOverdue: true),226 WidgetTask(id: "00000000-0000-4000-8000-0000000000a2", title: "Rappeler le notaire", dueAt: noon, noteTitle: "Réunion de lundi"),227 WidgetTask(id: "00000000-0000-4000-8000-0000000000a3", title: "Réserver le train pour Lisbonne", dueAt: noon, noteTitle: "Idées pour le voyage"),228 WidgetTask(id: "00000000-0000-4000-8000-0000000000a4", title: "Relire le chapitre 3", dueAt: noon),229 WidgetTask(id: "00000000-0000-4000-8000-0000000000a5", title: "Payer la facture d’électricité", dueAt: noon, noteTitle: "Budget du mois"),230 WidgetTask(id: "00000000-0000-4000-8000-0000000000a6", title: "Préparer l’ordre du jour", dueAt: noon),231 ]232 }233}234