Zum Inhalt

apple/PhysarumApp/Sources/UI/UserFacingError.swift

import Foundation

/// A user-presentable error: a short title, an explanatory message, and an
/// optional recovery action. Pure value type — no UI, host-testable.
struct UserFacingError: Identifiable, Equatable {
    let id = UUID()
    let title: String
    let message: String
    var recovery: RecoveryAction = .none

    static func == (a: UserFacingError, b: UserFacingError) -> Bool {
        a.title == b.title && a.message == b.message && a.recovery == b.recovery
    }
}

/// What the alert offers the user beyond a plain dismiss.
enum RecoveryAction: Equatable {
    case none
    /// Deep-link to the app's system settings (e.g. to grant a denied permission).
    case openSettings
}

#if !os(tvOS)
extension UserFacingError {
    /// Pure mapping from a Photos-save failure to a user-presentable error.
    /// (`CaptureSaveError` lives behind `#if !os(tvOS)`, so this init does too.)
    init(captureSaveError e: CaptureSaveError) {
        switch e {
        case .permissionDenied:
            self.init(title: "Couldn’t Save to Photos",
                      message: "Physarum needs permission to add to your photo library. Turn it on in Settings.",
                      recovery: .openSettings)
        case .writeFailed:
            self.init(title: "Couldn’t Save to Photos",
                      message: "Something went wrong while saving. Please try again.",
                      recovery: .none)
        }
    }
}
#endif