SPB Git

Repository Public

Notifier.swift 1.7 KB 32 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: App/Notifier.swift3// Description: macOS notifications for terminal outcomes only. A notification never announces a success before the chain finalized the transaction; 'diffusée' is never notified as done.4import Foundation5import UserNotifications6import SPWalletCore78@MainActor enum Notifier {9    static var enabled: Bool { UserDefaults.standard.object(forKey: "notificationsEnabled") as? Bool ?? true }1011    static func requestPermission() async -> Bool { (try? await UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound])) ?? false }1213    static func post(title: String, body: String, id: String = UUID().uuidString) {14        guard enabled else { return }15        Task {16            let c = UNUserNotificationCenter.current()17            let s = await c.notificationSettings()18            if s.authorizationStatus == .notDetermined { _ = await requestPermission() }19            let content = UNMutableNotificationContent(); content.title = title; content.body = body; content.sound = .default20            try? await c.add(UNNotificationRequest(identifier: id, content: content, trigger: nil))21        }22    }2324    /// Only for a terminal state, with the state's exact wording.25    static func proposal(_ p: Proposal) {26        guard p.state.isTerminal else { return }27        let what = "\(Fmt.amount(p.amountDisplay, maxFraction: 8)) \(p.symbol) · \(p.networkName ?? p.network)"28        if p.state.isSuccess { post(title: "Transaction finalisée", body: "\(what) — confirmée par la chaîne.", id: "p-\(p.id)") }29        else { post(title: "Transaction : \(ErrorText.state(p.state))", body: "\(what)\(p.error.map { " — \($0)" } ?? "")", id: "p-\(p.id)") }30    }31}32
Files main
Menu

Theme