2023-07-13 10:16:16 -07:00
|
|
|
//
|
2023-08-24 14:18:04 -07:00
|
|
|
// 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
|
|
|
|
|
//
|
2023-07-13 10:16:16 -07:00
|
|
|
// DebugErrorView.swift
|
|
|
|
|
//
|
|
|
|
|
// Created by Nacho Soto on 7/13/23.
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
|
|
/// A view that displays an error in debug builds
|
2023-11-08 07:35:19 -08:00
|
|
|
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
|
2023-09-05 11:50:16 -07:00
|
|
|
struct DebugErrorView<Content: View>: View {
|
2023-07-13 10:16:16 -07:00
|
|
|
|
|
|
|
|
private let description: String
|
2023-07-13 14:03:00 -07:00
|
|
|
private let releaseBehavior: ReleaseBehavior
|
|
|
|
|
|
|
|
|
|
enum ReleaseBehavior {
|
|
|
|
|
|
|
|
|
|
case emptyView
|
|
|
|
|
case fatalError
|
2023-09-05 11:50:16 -07:00
|
|
|
case replacement(Content)
|
2023-07-13 10:16:16 -07:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-05 11:50:16 -07:00
|
|
|
init(_ error: Error, replacement content: Content) {
|
2023-07-13 14:03:00 -07:00
|
|
|
self.init(
|
|
|
|
|
(error as NSError).localizedDescription,
|
2023-09-05 11:50:16 -07:00
|
|
|
replacement: content
|
2023-07-13 14:03:00 -07:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-05 11:50:16 -07:00
|
|
|
init(_ description: String, replacement content: Content) {
|
2023-07-13 10:16:16 -07:00
|
|
|
self.description = description
|
2023-09-05 11:50:16 -07:00
|
|
|
self.releaseBehavior = .replacement(content)
|
2023-07-13 10:16:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var body: some View {
|
2023-07-24 15:49:26 +02:00
|
|
|
Group {
|
|
|
|
|
switch self.releaseBehavior {
|
|
|
|
|
case .emptyView:
|
|
|
|
|
#if DEBUG
|
|
|
|
|
self.errorView
|
|
|
|
|
#else
|
|
|
|
|
// Not using `EmptyView` so
|
|
|
|
|
// this view can be laid out consistently.
|
|
|
|
|
Rectangle()
|
|
|
|
|
.hidden()
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
case let .replacement(view):
|
2023-09-05 11:50:16 -07:00
|
|
|
#if DEBUG
|
2023-09-13 18:20:03 -07:00
|
|
|
view.overlay(alignment: .top) { self.errorView }
|
2023-09-05 11:50:16 -07:00
|
|
|
#else
|
|
|
|
|
view
|
|
|
|
|
#endif
|
2023-07-24 15:49:26 +02:00
|
|
|
|
|
|
|
|
case .fatalError:
|
|
|
|
|
#if DEBUG
|
|
|
|
|
self.errorView
|
|
|
|
|
#else
|
|
|
|
|
fatalError(self.description)
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.onAppear {
|
|
|
|
|
Logger.warning("Error: \(self.description)")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private var errorView: some View {
|
2023-07-13 10:16:16 -07:00
|
|
|
Text(self.description)
|
2023-07-24 15:49:26 +02:00
|
|
|
.unredacted()
|
2023-07-24 19:22:38 -03:00
|
|
|
.padding()
|
2023-07-13 10:16:16 -07:00
|
|
|
.background(
|
|
|
|
|
Color.red
|
|
|
|
|
.edgesIgnoringSafeArea(.all)
|
|
|
|
|
)
|
2023-07-24 19:22:38 -03:00
|
|
|
.foregroundColor(.white)
|
2023-09-13 18:20:03 -07:00
|
|
|
.font(.caption.bold())
|
2023-07-28 00:27:41 +02:00
|
|
|
.minimumScaleFactor(0.5)
|
2023-07-24 19:22:38 -03:00
|
|
|
.cornerRadius(8)
|
|
|
|
|
.shadow(radius: 8)
|
2023-07-13 10:16:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2023-09-05 11:50:16 -07:00
|
|
|
|
2023-11-08 07:35:19 -08:00
|
|
|
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
|
2023-09-05 11:50:16 -07:00
|
|
|
extension DebugErrorView where Content == AnyView {
|
|
|
|
|
|
|
|
|
|
init(_ error: Error, releaseBehavior: ReleaseBehavior) {
|
|
|
|
|
self.init(
|
|
|
|
|
(error as NSError).localizedDescription,
|
|
|
|
|
releaseBehavior: releaseBehavior
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init(_ description: String, releaseBehavior: ReleaseBehavior) {
|
|
|
|
|
self.description = description
|
|
|
|
|
self.releaseBehavior = releaseBehavior
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|