SPB Git

Repository Public

Notifier.swift 1.9 KB 43 lines swift

1// SP Drive iOS2// File: App/Notifier.swift3// Description: Local notifications, few and useful: conflict, persistent failure, storage full, deposit received, transfers finished4//              in the background. Each family can be turned off in the app; iOS settings always win.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import UserNotifications1011enum Notifier {12    enum Family: String, CaseIterable {13        case conflict, failure, quota, deposit, finished14        var label: String {15            switch self {16            case .conflict: "Conflits de versions"; case .failure: "Transferts interrompus"; case .quota: "Espace insuffisant"17            case .deposit: "Nouveaux dépôts reçus"; case .finished: "Transferts terminés en arrière-plan"18            }19        }20        var defaultsKey: String { "notify.\(rawValue)" }21        var isEnabled: Bool { UserDefaults.standard.object(forKey: defaultsKey) as? Bool ?? (self != .finished) }22    }2324    static func requestAuthorizationIfNeeded() {25        Task {26            let center = UNUserNotificationCenter.current()27            if await center.notificationSettings().authorizationStatus == .notDetermined { _ = try? await center.requestAuthorization(options: [.alert, .badge, .sound]) }28        }29    }3031    /// One notification per family and body at a time: repeating the same message replaces it instead of piling up.32    static func post(_ family: Family, title: String, body: String) {33        guard family.isEnabled else { return }34        let content = UNMutableNotificationContent()35        content.title = title36        content.body = body37        content.sound = family == .finished ? nil : .default38        content.threadIdentifier = family.rawValue39        let id = "\(family.rawValue)-\(abs(body.hashValue))"40        UNUserNotificationCenter.current().add(UNNotificationRequest(identifier: id, content: content, trigger: nil))41    }42}43
Files main
Menu

Theme