Skip to content

Commit

Permalink
Spruced up AlertHandler with source information
Browse files Browse the repository at this point in the history
  • Loading branch information
bdrelling committed May 14, 2024
1 parent fa7d208 commit a8727c1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 28 deletions.
24 changes: 9 additions & 15 deletions Sources/KippleUI/SwiftUI/Core/ViewModifiers/NavigationHidden.swift
Original file line number Diff line number Diff line change
@@ -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
62 changes: 49 additions & 13 deletions Sources/KippleUI/SwiftUI/Core/ViewModifiers/View+AlertHandler.swift
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -16,50 +19,62 @@ 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
}
}

// MARK: - Supporting Types

@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)
}
}
}
Expand All @@ -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
}
}

0 comments on commit a8727c1

Please sign in to comment.