-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sjmarf <[email protected]>
- Loading branch information
1 parent
64aa346
commit 04ac212
Showing
10 changed files
with
178 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// ErrorsTracker.swift | ||
// Mlem | ||
// | ||
// Created by Eric Andrews on 2024-12-29. | ||
// | ||
|
||
import Observation | ||
|
||
@Observable | ||
class ErrorsTracker { | ||
private(set) var errors: [ErrorDetails] = .init() | ||
|
||
@MainActor | ||
func addError(_ error: Error) { | ||
errors.prepend(.init(error: error)) | ||
} | ||
|
||
static var main: ErrorsTracker = .init() | ||
|
||
func createErrorLog() -> String { | ||
var ret: String = "" | ||
|
||
errors.forEach { details in | ||
let description = details.error?.localizedDescription ?? "No Description" | ||
ret += "\(details.when.formatted(.iso8601))\t\(details.title ?? "Error")\t\(description)\n" | ||
} | ||
|
||
return ret | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// | ||
// ErrorLogView.swift | ||
// Mlem | ||
// | ||
// Created by Eric Andrews on 2024-12-29. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct ErrorLogView: View { | ||
@Environment(ErrorsTracker.self) var errorsTracker | ||
@Environment(Palette.self) var palette | ||
@Environment(NavigationLayer.self) var navigation | ||
|
||
var body: some View { | ||
FancyScrollView { | ||
LazyVStack(spacing: Constants.main.standardSpacing) { | ||
if errorsTracker.errors.isEmpty { | ||
Text(verbatim: "No errors") | ||
.foregroundStyle(palette.secondary) | ||
} | ||
ForEach(Array(errorsTracker.errors.enumerated()), id: \.offset) { _, errorDetails in | ||
errorView(errorDetails) | ||
} | ||
.padding(.horizontal, Constants.main.standardSpacing) | ||
} | ||
} | ||
.background(palette.groupedBackground) | ||
.navigationTitle(String("Error Log")) | ||
.toolbar { | ||
if !errorsTracker.errors.isEmpty { | ||
ToolbarItem(placement: .topBarTrailing) { | ||
Button { | ||
Task { | ||
if let url = await downloadTextToFileSystem( | ||
fileName: "mlem_error_log.txt", | ||
text: errorsTracker.createErrorLog() | ||
) { | ||
navigation.shareInfo = .init(url: url) | ||
} else { | ||
ToastModel.main.add(.failure(String("Failed to share error log"))) | ||
} | ||
} | ||
} label: { | ||
Image(systemName: Icons.share) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@ViewBuilder | ||
func errorView(_ details: ErrorDetails) -> some View { | ||
VStack(alignment: .leading, spacing: Constants.main.standardSpacing) { | ||
HStack { | ||
Text(details.title ?? "Error") | ||
.fontWeight(.semibold) | ||
|
||
Spacer() | ||
|
||
Button { | ||
if let text = details.error?.localizedDescription { | ||
UIPasteboard.general.string = text | ||
ToastModel.main.add(.success(String("Copied"))) | ||
} | ||
} label: { | ||
Text(Image(systemName: Icons.copy)) | ||
.font(.subheadline) | ||
.foregroundStyle(palette.accent) | ||
} | ||
} | ||
|
||
Text(details.errorText) | ||
|
||
Text(details.when.formatted(date: .abbreviated, time: .standard)) | ||
.font(.caption) | ||
.foregroundStyle(palette.secondary) | ||
} | ||
.padding(Constants.main.standardSpacing) | ||
.background(palette.secondaryGroupedBackground) | ||
.clipShape(.rect(cornerRadius: Constants.main.mediumItemCornerRadius)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters