diff --git a/DBDebugToolkit/Classes/Swift/Views/RequestDetails.swift b/DBDebugToolkit/Classes/Swift/Views/RequestDetails.swift index 0539475..7aff265 100644 --- a/DBDebugToolkit/Classes/Swift/Views/RequestDetails.swift +++ b/DBDebugToolkit/Classes/Swift/Views/RequestDetails.swift @@ -49,12 +49,23 @@ private extension RequestDetails { rowView(title: key, subtitle: value) } } + CopyButton { + headerFields.keys.sorted().reduce("") { partialResult, key in + if let value = headerFields[key] { + return partialResult + "\(key): \(value)\n" + } + return partialResult + } + } } } Section(header: Text("Body")) { rowView(title: "Body length", subtitle: "\(model.requestBodyLength)") NavigationLink("Body preview", destination: RequestBodyPreview(model: model, mode: .request)) + CopyButton { + String(data: await model.readRequestBody() ?? Data(), encoding: .utf8) + } } } @@ -74,12 +85,23 @@ private extension RequestDetails { rowView(title: key, subtitle: value) } } + CopyButton { + headerFields.keys.sorted().reduce("") { partialResult, key in + if let value = headerFields[key] { + return partialResult + "\(key): \(value)\n" + } + return partialResult + } + } } } Section(header: Text("Body")) { rowView(title: "Body length", subtitle: "\(model.responseBodyLength)") NavigationLink("Body preview", destination: RequestBodyPreview(model: model, mode: .response)) + CopyButton { + String(data: await model.readResponseBody() ?? Data(), encoding: .utf8) + } } } @@ -92,3 +114,33 @@ private extension RequestDetails { } } } + + +private struct CopyButton: View { + let data: () async -> String? + @State private var showCheckmark = false + + var body: some View { + Button { + Task { + if let text = await data() { + UIPasteboard.general.string = text + withAnimation(.easeInOut(duration: 0.3)) { + showCheckmark = true + } + try? await Task.sleep(nanoseconds: 1_500_000_000) + withAnimation(.easeInOut(duration: 0.3)) { + showCheckmark = false + } + } + } + } label: { + HStack { + Text("Copy") + Image(systemName: showCheckmark ? "checkmark.circle.fill" : "doc.on.doc") + .foregroundColor(showCheckmark ? .green : .accentColor) + .animation(.easeInOut(duration: 0.3), value: showCheckmark) + } + } + } +}