From a8727c15186ab0568457d40dfbedd941b2d15829 Mon Sep 17 00:00:00 2001 From: Brian Drelling Date: Tue, 14 May 2024 15:24:00 -0500 Subject: [PATCH] Spruced up AlertHandler with source information --- .../Core/ViewModifiers/NavigationHidden.swift | 24 +++---- .../ViewModifiers/View+AlertHandler.swift | 62 +++++++++++++++---- 2 files changed, 58 insertions(+), 28 deletions(-) diff --git a/Sources/KippleUI/SwiftUI/Core/ViewModifiers/NavigationHidden.swift b/Sources/KippleUI/SwiftUI/Core/ViewModifiers/NavigationHidden.swift index f5d69a7..3bcc75e 100644 --- a/Sources/KippleUI/SwiftUI/Core/ViewModifiers/NavigationHidden.swift +++ b/Sources/KippleUI/SwiftUI/Core/ViewModifiers/NavigationHidden.swift @@ -1,23 +1,17 @@ // Copyright © 2024 Brian Drelling. All rights reserved. -#if !os(macOS) - import Foundation import SwiftUI -public struct NavigationHidden: ViewModifier { - public func body(content: Content) -> some View { - content - .navigationBarTitle("") - .navigationBarHidden(true) - .navigationBarBackButtonHidden(true) - } -} - public extension View { - func navigationHidden() -> some View { - modifier(NavigationHidden()) + @ViewBuilder + func navigationHidden(_ isHidden: Bool = true) -> some View { + #if os(macOS) + self + #else + self + .navigationBarHidden(isHidden) + .navigationBarBackButtonHidden(isHidden) + #endif } } - -#endif diff --git a/Sources/KippleUI/SwiftUI/Core/ViewModifiers/View+AlertHandler.swift b/Sources/KippleUI/SwiftUI/Core/ViewModifiers/View+AlertHandler.swift index b768fdd..60e6f6e 100644 --- a/Sources/KippleUI/SwiftUI/Core/ViewModifiers/View+AlertHandler.swift +++ b/Sources/KippleUI/SwiftUI/Core/ViewModifiers/View+AlertHandler.swift @@ -1,10 +1,13 @@ // Copyright © 2024 Brian Drelling. All rights reserved. +import KippleFoundation import SwiftUI private struct AlertHandlerModifier: ViewModifier { @Bindable var alertHandler: AlertHandler + private let isDebugging: Bool + func body(content: Content) -> some View { content .environment(\.alertHandler, self.alertHandler) @@ -16,13 +19,19 @@ private struct AlertHandlerModifier: ViewModifier { Button("Ok") {} }, message: { error in - Text(error.localizedDescription) + if self.isDebugging { + Text("\(error.message) [\(error.source)]") + Text("FFF") + } else { + Text(error.message) + } } ) } - init(alertHandler: AlertHandler) { + init(alertHandler: AlertHandler, isDebugging: Bool) { self.alertHandler = alertHandler + self.isDebugging = isDebugging } } @@ -30,36 +39,42 @@ private struct AlertHandlerModifier: ViewModifier { @Observable public final class AlertHandler { - public private(set) var error: Error? + public private(set) var error: AlertHandlerError? public var isErrorPresented: Bool = false - public func `catch`(_ error: Error) { - self.error = error + public func `catch`(_ error: Error, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) { + self.error = .init( + message: error.localizedDescription, + file: "\(file)", + function: "\(function)", + line: line + ) + self.isErrorPresented = true } - public func `catch`(_ body: @escaping () throws -> Void) { + public func `catch`(_ body: @escaping () throws -> Void, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) { do { try body() } catch { - self.catch(error) + self.catch(error, file: file, function: function, line: line) } } - public func `catch`(_ body: @escaping () async throws -> Void) { + public func `catch`(_ body: @escaping () async throws -> Void, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) { Task { @MainActor [weak self] in guard let self else { return } - await self.catch(body) + await self.catch(body, file: file, function: function, line: line) } } @MainActor - public func `catch`(_ body: @escaping () async throws -> Void) async { + public func `catch`(_ body: @escaping () async throws -> Void, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) async { do { try await body() } catch { - self.catch(error) + self.catch(error, file: file, function: function, line: line) } } } @@ -75,10 +90,31 @@ public extension EnvironmentValues { } } +public struct AlertHandlerError: Sendable, Equatable, Hashable { + public let message: String + public let file: String + public let function: String + public let line: UInt + + public var filename: String { + self.file.split(separator: "/").last?.replacingOccurrences(of: ".swift", with: "") ?? self.file + } + + public var source: String { + "\(self.filename).\(self.function):\(self.line)" + } +} + // MARK: - Extensions public extension View { - func handleAlerts(with alertHandler: AlertHandler? = nil) -> some View { - self.modifier(AlertHandlerModifier(alertHandler: alertHandler ?? .init())) + func handleAlerts(with alertHandler: AlertHandler? = nil, isDebugging: Bool = .isDebugging) -> some View { + self.modifier(AlertHandlerModifier(alertHandler: alertHandler ?? .init(), isDebugging: isDebugging)) + } +} + +extension AlertHandlerError: LocalizedError { + public var errorDescription: String? { + self.message } }