diff --git a/Sources/BackgroundTime/ErrorsTabView.swift b/Sources/BackgroundTime/ErrorsTabView.swift index b9c1a9a..392f8cf 100644 --- a/Sources/BackgroundTime/ErrorsTabView.swift +++ b/Sources/BackgroundTime/ErrorsTabView.swift @@ -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) } } \ No newline at end of file