Files
Pulse/Sources/PulseUI/Features/Inspector/Cells/NetworkRequestInfoCell.swift
2023-02-03 22:12:13 -05:00

64 lines
1.7 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// The MIT License (MIT)
//
// Copyright (c) 2020–2023 Alexander Grebenyuk (github.com/kean).
import SwiftUI
import Pulse
struct NetworkRequestInfoCell: View {
let viewModel: NetworkRequestInfoCellViewModel
var body: some View {
NavigationLink(destination: destinationRequestDetails) {
contents
}
}
private var contents: some View {
(Text(viewModel.httpMethod).fontWeight(.semibold).font(.callout.smallCaps()) + Text(" ") + Text(viewModel.url))
.lineLimit(4)
.font(.callout)
}
private var destinationRequestDetails: some View {
NetworkDetailsView(title: "Request") { viewModel.render() }
}
}
final class NetworkRequestInfoCellViewModel {
let httpMethod: String
let url: String
let render: () -> NSAttributedString
init(task: NetworkTaskEntity) {
self.httpMethod = task.httpMethod ?? "GET"
self.url = task.url ?? "–"
self.render = {
TextRenderer(options: .sharing).make { $0.render(task, content: .all) }
}
}
init(transaction: NetworkTransactionMetricsEntity) {
self.httpMethod = transaction.request.httpMethod ?? "GET"
self.url = transaction.request.url ?? "–"
self.render = { TextRenderer(options: .sharing).make { $0.render(transaction) } }
}
}
#if DEBUG
struct NetworkRequestInfoCell_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
List {
ForEach(MockTask.allEntities, id: \.objectID) { task in
NetworkRequestInfoCell(viewModel: .init(task: task))
}
}
#if os(macOS)
.frame(width: 260)
#endif
}
}
}
#endif