Files
purchases-ios/RevenueCatUI/Views/ErrorDisplay.swift
T

59 lines
1.5 KiB
Swift

//
// Copyright RevenueCat Inc. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// ErrorDisplay.swift
//
// Created by Nacho Soto on 7/21/23.
import SwiftUI
/// A modifier that allows easily displaying an `NSError` as an `alert`.
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
struct ErrorDisplay: ViewModifier {
@Binding
var error: NSError?
var onDismiss: (() -> Void)?
func body(content: Content) -> some View {
content
.alert(isPresented: self.isShowingError, error: self.error.map(LocalizedAlertError.init)) { _ in
Button {
self.error = nil
self.onDismiss?()
} label: {
Text("OK", bundle: .module)
}
} message: { error in
Text(error.failureReason ?? "")
}
}
private var isShowingError: Binding<Bool> {
return .init {
self.error != nil
} set: { newValue in
if !newValue {
self.error = nil
}
}
}
}
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
extension View {
func displayError(_ error: Binding<NSError?>, onDismiss: (() -> Void)? = nil) -> some View {
self.modifier(ErrorDisplay(error: error, onDismiss: onDismiss))
}
}