Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions Sources/LoggingUI/AppRunView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ struct AppRunView: View {

func contextMenu(for entry: LogEntry) -> some View {
VStack {
#if os(iOS)
Button {
UIPasteboard.general.string = entry.composedMessage
} label: {
Image(systemName: "document.on.document")
Text("Copy")
}
#endif
ExportView(logEntry: entry)
#if os(iOS)
Button {
UIPasteboard.general.string = entry.composedMessage
} label: {
Image(systemName: "document.on.document")
Text("Copy")
}
#endif
ExportView(groupedEntries: [entry.appRun: [entry]])
Menu {
similarItem(entry: entry, scope: .message)
similarItem(entry: entry, scope: .level)
Expand All @@ -70,13 +70,13 @@ struct AppRunView: View {
}
}

func appRunHeader(appRun: AppRun) -> some View {
func appRunHeader(appRun: AppRun, entries: [LogEntry]) -> some View {
HStack(alignment: .center) {
Text(appRun.launchDate.formatted())
.font(.headline)
Spacer()
Menu {
ExportView(logEntries: groupedEntries[appRun]!)
ExportView(groupedEntries: [appRun: entries])
Section {
Text("App version: \(appRun.appVersion)")
Text("Operating System Version: \(appRun.operatingSystemVersion)")
Expand Down Expand Up @@ -107,7 +107,10 @@ struct AppRunView: View {
.contextMenu { contextMenu(for: entry) }
}
} header: {
appRunHeader(appRun: appRun)
appRunHeader(
appRun: appRun,
entries: groupedEntries[appRun]!
)
}
}
}
Expand Down Expand Up @@ -154,7 +157,7 @@ struct AppRunView: View {
var menu: some View {
Menu {
Section {
ExportView(logEntries: filteredEntries)
ExportView(groupedEntries: groupedEntries)
}
ForEach(Metadata.allCases, id: \.self) { metadata in
Toggle(isOn: .init(get: { isShowingMetadata.contains(metadata) }, set: {
Expand Down
72 changes: 49 additions & 23 deletions Sources/LoggingUI/ExportView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,33 @@ import Support
import SwiftUI

struct ExportView: View {
var file: URL

var encoder = mutating(JSONEncoder()) {
$0.outputFormatting = [.prettyPrinted, .sortedKeys]
$0.dateEncodingStrategy = .iso8601
}

init(logEntry: LogEntry) {
let jsonData = try? encoder.encode(logEntry.snapshot)
file = FileManager.default.temporaryDirectory.appendingPathComponent("logEntry.json")
if let data = jsonData {
try? data.write(to: file, options: .atomic)
}
}

init(logEntries: [LogEntry]) {
let jsonData = try? encoder.encode(logEntries.map(\.snapshot))
file = FileManager.default.temporaryDirectory.appendingPathComponent("logEntries.json")
if let data = jsonData {
try? data.write(to: file, options: .atomic)
private let shareData: Data

init(groupedEntries: [AppRun: [LogEntry]]) {
let appRunSnapshots = groupedEntries.map { key, value in
AppRunExportSnapshot(
info: key.snapshot.info,
logEntries: value.map(\.snapshot)
)
}

let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
encoder.dateEncodingStrategy = .custom({ date, encoder in
let dateString = date.ISO8601Format(Date.ISO8601FormatStyle(includingFractionalSeconds: true))
var container = encoder.singleValueContainer()
try container.encode(dateString)
})

let jsonData = try? encoder.encode(appRunSnapshots)
shareData = jsonData ?? .init()
}

var body: some View {
ShareLink(item: file, preview: SharePreview("Log Entries", image: Image(systemName: "text.document"))) {
Image(systemName: "square.and.arrow.up")
ShareLink(
item: JSONFile(data: shareData),
preview: SharePreview("App Runs", image: Image(systemName: "doc.text"))
) {
HStack {
Image(systemName: "square.and.arrow.up")
Text("Export")
Expand All @@ -38,4 +39,29 @@ struct ExportView: View {
}
}

private struct JSONFile: Transferable {
static let dateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd_HH-mm-ss"
return dateFormatter
}()

let data: Data

static var transferRepresentation: some TransferRepresentation {
DataRepresentation(exportedContentType: .json) { file in
file.data
}
.suggestedFileName { file in
let timestamp = Self.dateFormatter.string(from: Date())
return "app_runs_\(timestamp).json"
}
}
}

private struct AppRunExportSnapshot: Codable {
let info: AppRun.Snapshot.Info
let logEntries: [LogEntry.Snapshot]
}

#endif
2 changes: 1 addition & 1 deletion Sources/LoggingUI/LogEntry+helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ extension LogEntry {
case .fault:
Color.red.opacity(0.2)
default:
Color.white
Color.clear
}
}

Expand Down