Skip to content
Open
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
72 changes: 53 additions & 19 deletions Sources/BackgroundTime/ErrorsTabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,30 +69,64 @@ public struct ErrorsTabView: View {

public struct ErrorSummarySection: View {
let errorsByType: [String: Int]

public init(errorsByType: [String: Int]) {
self.errorsByType = errorsByType
}


private var sortedErrorTypes: [String] {
let keys = Array(errorsByType.keys)
return keys.sorted()
}

private var chartHeight: CGFloat {
let minHeight: CGFloat = 200.0
let itemHeight: CGFloat = 30.0
let count = errorsByType.count
let calculatedHeight = CGFloat(count) * itemHeight
return max(minHeight, calculatedHeight)
}

private func errorCount(for errorType: String) -> Int {
return errorsByType[errorType] ?? 0
}

private func truncatedErrorType(_ errorType: String) -> String {
let prefix = errorType.prefix(30)
return String(prefix)
}

public var body: some View {
VStack(alignment: .leading, spacing: 12) {
Text("Error Types")
.font(.headline)
.padding(.horizontal)

Chart {
ForEach(Array(errorsByType.keys).sorted(), id: \.self) { errorType in
BarMark(
x: .value("Count", errorsByType[errorType] ?? 0),
y: .value("Error Type", String(errorType.prefix(30)))
)
.foregroundStyle(.red)
}
}
.frame(height: max(200.0, CGFloat(errorsByType.count) * 30.0))
let content = VStack(alignment: .leading, spacing: 12) {
titleView
errorChart
}

return content
.background(Color(.systemBackground))
.cornerRadius(12)
}

private var titleView: some View {
Text("Error Types")
.font(.headline)
.padding(.horizontal)
}

private var errorChart: some View {
Chart {
ForEach(sortedErrorTypes, id: \.self) { errorType in
let count = errorCount(for: errorType)
let label = truncatedErrorType(errorType)

BarMark(
x: .value("Count", count),
y: .value("Error Type", label)
)
.foregroundStyle(.red)
}
}
.background(Color(.systemBackground))
.cornerRadius(12)
.frame(height: chartHeight)
.padding(.horizontal)
}
}
Loading