Repository Public
1// SP Wallet iOS2// File: Core/System/AlertPresentation.swift3// Description: How an alert of the server's catalogue is shown by the app: the language of the wording, severity as icon + word4// + tone, group names, and the in-app destination of the catalogue's web path (navigation only — opening an alert5// never starts or approves anything).6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import SwiftUI10import SPWalletCore1112enum AlertPresentation {13 /** The catalogue has French and English wording; the app's language picks one (French is the source language). */14 static var locale: AlertLocale { Bundle.main.preferredLocalizations.first?.hasPrefix("en") == true ? .en : .fr }1516 /** « il y a 42 min » in the list (the narrow style prints « -42 min », which reads like a negative duration). */17 static func age(_ d: Date, now: Date = Date()) -> String {18 now.timeIntervalSince(d) < 60 ? String(localized: "à l’instant") : d.formatted(.relative(presentation: .numeric, unitsStyle: .abbreviated))19 }2021 /** Where the catalogue's `alertPath` lands in the native app. */22 enum Destination: Equatable {23 case section(AppSection)24 /** The read-only « État du système » screen, inside the alert centre. */25 case systemState26 /** The alert centre itself: nothing more specific to open. */27 case none2829 var label: String? {30 switch self {31 case .section(let s): s.title32 case .systemState: String(localized: "État du système")33 case .none: nil34 }35 }36 }3738 static func destination(path: String) -> Destination {39 let p = path.split(separator: "?").first.map(String.init) ?? path40 if p == "/agents" || p.hasPrefix("/agents/") { return .section(.agents) }41 if p.hasPrefix("/plan/") { return .section(.swap) }42 if p.hasPrefix("/futures") { return .section(.futures) }43 if p == "/activity" { return .section(.activity) }44 if p == "/security" { return .section(.security) }45 if p == "/monitor" { return .systemState }46 return .none47 }48 static func destination(_ a: WalletAlert) -> Destination { destination(path: a.path) }49}5051extension AlertSeverity {52 /** The word shown next to the icon. A severity this app does not know is shown verbatim. */53 var word: String {54 switch self {55 case .info: String(localized: "Info")56 case .warning: String(localized: "Avertissement")57 case .critical: String(localized: "Critique")58 case .unknown(let s): s59 }60 }61 var tone: Tone { switch self { case .info: .info; case .warning: .warn; case .critical: .danger; case .unknown: .neutral } }62 var symbol: String {63 switch self { case .info: "bell.fill"; case .warning: "exclamationmark.triangle.fill"; case .critical: "exclamationmark.octagon.fill"; case .unknown: "questionmark.circle.fill" }64 }65}6667extension AlertGroup {68 var label: String {69 switch self {70 case .transfers: String(localized: "Transferts")71 case .agents: String(localized: "Agents")72 case .derivatives: String(localized: "Dérivés")73 case .security: String(localized: "Sécurité")74 case .system: String(localized: "Système")75 case .unknown(let s): s76 }77 }78}7980extension AlertDelivery {81 /** What the server's dispatcher did with the row (browser push / webhook). Says nothing about this app's local notification. */82 var label: String {83 switch self {84 case .pending: String(localized: "pas encore traitée par le serveur")85 case .inApp: String(localized: "dans l’app seulement (aucun appareil web ni webhook)")86 case .stale: String(localized: "trop ancienne pour être notifiée")87 case .quiet: String(localized: "retenue par les heures calmes")88 case .capped: String(localized: "retenue par le plafond horaire")89 case .sent: String(localized: "remise aux canaux du serveur")90 case .unknown(let s): s91 }92 }93}9495/** The Home toolbar bell: the server's unread count, tinted by the highest unread severity (the count is also in the label). */96struct AlertBellButton: View {97 @Environment(AppModel.self) private var model98 var body: some View {99 let unread = model.alertCenter.counters?.unread ?? 0100 let severity = model.alertCenter.highestUnreadSeverity101 Button { model.router.go(.alerts) } label: {102 HStack(spacing: 3) {103 Image(systemName: unread > 0 ? "bell.fill" : "bell").foregroundStyle(unread > 0 ? (severity ?? .info).tone.color : Theme.ink)104 // the server's count, in the label itself (an overlaid badge is clipped by the toolbar's glass button)105 if unread > 0 { Text(unread > 99 ? "99+" : String(unread)).font(.system(size: 14, weight: .bold)).monospacedDigit().foregroundStyle((severity ?? .info).tone.color) }106 }107 }108 .accessibilityLabel(unread > 0 ? AppSection.alerts.title + ", " + String(localized: "\(unread) non lue(s)") + (severity.map { ", " + $0.word } ?? "") : AppSection.alerts.title)109 .accessibilityIdentifier("home.bell")110 }111}112