Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Sources/Shared/UnifiedViews.swift3// Description: Unified inbox — one list across every mailbox (inbox, unread, flagged, drafts, sent) with the receiving address as a coloured chip,4// and the scheduled sends that can still be cancelled. Rows open in the regular reader through the engine of their own mailbox.5import SPMailCore6import SwiftUI78struct UnifiedListView: View {9 @Bindable var scene: MailScene10 let view: UnifiedView11 let composeSpace: Namespace.ID12 let compactWindow: Bool13 var onOpen: () -> Void1415 private var unified: UnifiedState { scene.unified }1617 static func title(_ v: UnifiedView) -> String {18 switch v {19 case .inbox: L("Toutes les boîtes"); case .unread: L("Non lus — toutes les boîtes"); case .flagged: L("Avec drapeau — toutes les boîtes")20 case .drafts: L("Brouillons — toutes les boîtes"); case .sent: L("Envoyés — toutes les boîtes")21 }22 }2324 /// The unified rows behind rows of the shared list component (multi-selection and bulk actions included).25 private func messages(_ rows: [RemoteRow]) -> [UnifiedMessage] {26 let ids = Set(rows.map(\.id))27 return unified.rows.filter { ids.contains($0.id) }28 }2930 var body: some View {31 RemoteListView(scene: scene, rows: unified.rows.map(RemoteRow.init), showRecipients: view == .sent || view == .drafts, isLoading: unified.isLoading, isLoadingMore: scene.list.isLoadingMore,32 openId: unified.selection, emptyIcon: "tray.2", compactWindow: compactWindow, composeSpace: composeSpace,33 open: { row in if let m = messages([row]).first { Task { await scene.openUnified(m) }; onOpen() } },34 perform: { action, rows in await scene.performUnified(action, on: messages(rows)) },35 loadMore: { row in if let m = messages([row]).first { await scene.loadMoreUnified(after: m) } }) {36 if unified.isOffline { OfflineNote(text: L("Hors ligne : messages déjà sur cet appareil, toutes boîtes confondues")) }37 }38 .navigationTitle(Self.title(view))39 .refreshable { await scene.refresh() }40 .toolbar { NextListToolbar(scene: scene, compactWindow: compactWindow) }41 }42}4344// MARK: - scheduled sends4546enum OutboundLabels {47 /// What the server observed for a send or one of its recipients — « remis » only when the receiving server accepted the message.48 static func label(_ status: String) -> (text: String, icon: String, color: SPToken) {49 switch status {50 case "scheduled": (L("Programmé"), "clock", SPColor.info)51 case "queued": (L("En file d’attente sur le serveur"), "clock.arrow.circlepath", SPColor.text3)52 case "pending_approval": (L("En attente d’approbation"), "person.badge.clock", SPColor.warn)53 case "sent": (L("Accepté pour livraison"), "checkmark.circle", SPColor.ok)54 case "delivered": (L("Remis au serveur du destinataire"), "checkmark.circle.fill", SPColor.ok)55 case "deferred": (L("Différé — le serveur réessaiera"), "hourglass", SPColor.warn)56 case "bounced": (L("Refusé par le serveur du destinataire"), "exclamationmark.triangle.fill", SPColor.danger)57 case "failed": (L("Échec — le message n’est pas parti"), "xmark.octagon.fill", SPColor.danger)58 case "cancelled": (L("Annulé"), "xmark.circle", SPColor.text3)59 default: (status, "questionmark.circle", SPColor.text3)60 }61 }62}6364struct ScheduledView: View {65 @Bindable var scene: MailScene66 @State private var cancelling: OutboundStatus?6768 var body: some View {69 List {70 if let e = scene.scheduledError { Label(e, systemImage: "wifi.slash").font(.footnote).foregroundStyle(SPColor.warn).listRowBackground(Color.clear) }71 ForEach(scene.scheduled) { item in72 VStack(alignment: .leading, spacing: 4) {73 HStack {74 Text(item.recipients.map(\.address).joined(separator: ", ")).font(.headline).lineLimit(1)75 Spacer()76 Button("Modifier…") { Task { await scene.app.compose.undo(item.id); await scene.reloadScheduled() } }.buttonStyle(.borderless)77 .help("Reprendre ce message dans le rédacteur pour le modifier ou changer sa date d’envoi")78 Button("Annuler l’envoi", role: .destructive) { cancelling = item }.buttonStyle(.borderless)79 }80 if let at = item.sendAt { Label(at.formatted(date: .complete, time: .shortened), systemImage: "clock").font(.subheadline).foregroundStyle(SPColor.info) }81 HStack(spacing: 4) { Text("De").foregroundStyle(SPColor.text3); AddressChip(address: item.from) }.font(.subheadline)82 }83 .padding(.vertical, 5)84 .listRowBackground(Color.clear)85 .swipeActions { Button(role: .destructive) { cancelling = item } label: { Label("Annuler l’envoi", systemImage: "xmark.circle") } }86 .contextMenu {87 Button("Modifier ou reprogrammer…") { Task { await scene.app.compose.undo(item.id); await scene.reloadScheduled() } }88 Button("Annuler l’envoi", role: .destructive) { cancelling = item }89 }90 .accessibilityElement(children: .combine)91 }92 }93 .listStyle(.plain)94 .scrollContentBackground(.hidden)95 .background(SPColor.bg)96 .overlay {97 if scene.scheduled.isEmpty, scene.scheduledError == nil {98 SPEmptyState(icon: "clock.badge", title: L("Aucun envoi programmé"), message: L("Les modèles envoyés avec une date d’envoi attendent ici jusqu’à leur départ."))99 }100 }101 .navigationTitle("Programmés")102 .refreshable { await scene.reloadScheduled() }103 .task { await scene.reloadScheduled() }104 .toolbar { ToolbarItem { Button { Task { await scene.reloadScheduled() } } label: { Label("Actualiser", systemImage: "arrow.clockwise") } } }105 .confirmationDialog("Annuler cet envoi programmé ?", isPresented: Binding(get: { cancelling != nil }, set: { if !$0 { cancelling = nil } }), titleVisibility: .visible, presenting: cancelling) { item in106 Button("Annuler l’envoi", role: .destructive) { Task { await scene.cancelScheduled(item) } }107 Button("Garder", role: .cancel) {}108 } message: { _ in Text("Rien ne sera envoyé ; le brouillon conservé par le serveur est détruit.") }109 }110}111