1.0.1 (6)
This commit is contained in:
@@ -104,13 +104,14 @@ struct StatCard: View {
|
||||
struct ProgressChartSection: View {
|
||||
@EnvironmentObject var dataManager: ClimbingDataManager
|
||||
@State private var selectedSystem: DifficultySystem = .vScale
|
||||
@State private var showAllTime: Bool = true
|
||||
|
||||
private var progressData: [ProgressDataPoint] {
|
||||
calculateProgressOverTime()
|
||||
private var gradeCountData: [GradeCount] {
|
||||
calculateGradeCounts()
|
||||
}
|
||||
|
||||
private var usedSystems: [DifficultySystem] {
|
||||
let uniqueSystems = Set(progressData.map { $0.difficultySystem })
|
||||
let uniqueSystems = Set(gradeCountData.map { $0.difficultySystem })
|
||||
return uniqueSystems.sorted {
|
||||
let order: [DifficultySystem] = [.vScale, .font, .yds, .custom]
|
||||
let firstIndex = order.firstIndex(of: $0) ?? order.count
|
||||
@@ -121,13 +122,50 @@ struct ProgressChartSection: View {
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 16) {
|
||||
Text("Grade Distribution")
|
||||
.font(.title2)
|
||||
.fontWeight(.bold)
|
||||
|
||||
// Toggles section
|
||||
HStack {
|
||||
Text("Progress Over Time")
|
||||
.font(.title2)
|
||||
.fontWeight(.bold)
|
||||
// Time period toggle
|
||||
HStack(spacing: 8) {
|
||||
Button(action: {
|
||||
showAllTime = true
|
||||
}) {
|
||||
Text("All Time")
|
||||
.font(.caption)
|
||||
.fontWeight(.medium)
|
||||
.padding(.horizontal, 8)
|
||||
.padding(.vertical, 4)
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: 6)
|
||||
.fill(showAllTime ? .blue : .clear)
|
||||
.stroke(.blue.opacity(0.3), lineWidth: 1)
|
||||
)
|
||||
.foregroundColor(showAllTime ? .white : .blue)
|
||||
}
|
||||
|
||||
Button(action: {
|
||||
showAllTime = false
|
||||
}) {
|
||||
Text("7 Days")
|
||||
.font(.caption)
|
||||
.fontWeight(.medium)
|
||||
.padding(.horizontal, 8)
|
||||
.padding(.vertical, 4)
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: 6)
|
||||
.fill(!showAllTime ? .blue : .clear)
|
||||
.stroke(.blue.opacity(0.3), lineWidth: 1)
|
||||
)
|
||||
.foregroundColor(!showAllTime ? .white : .blue)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer()
|
||||
|
||||
// Scale selector (only show if multiple systems)
|
||||
if usedSystems.count > 1 {
|
||||
Menu {
|
||||
ForEach(usedSystems, id: \.self) { system in
|
||||
@@ -164,24 +202,22 @@ struct ProgressChartSection: View {
|
||||
}
|
||||
}
|
||||
|
||||
let filteredData = progressData.filter { $0.difficultySystem == selectedSystem }
|
||||
let filteredData = gradeCountData.filter { $0.difficultySystem == selectedSystem }
|
||||
|
||||
if !filteredData.isEmpty {
|
||||
LineChartView(data: filteredData, selectedSystem: selectedSystem)
|
||||
BarChartView(data: filteredData)
|
||||
.frame(height: 200)
|
||||
|
||||
Text(
|
||||
"Progress: max \(selectedSystem.displayName.lowercased()) grade achieved per session"
|
||||
)
|
||||
.font(.caption)
|
||||
.foregroundColor(.secondary)
|
||||
Text("Successful climbs by grade")
|
||||
.font(.caption)
|
||||
.foregroundColor(.secondary)
|
||||
} else {
|
||||
VStack(spacing: 8) {
|
||||
Image(systemName: "chart.line.uptrend.xyaxis")
|
||||
Image(systemName: "chart.bar")
|
||||
.font(.title)
|
||||
.foregroundColor(.secondary)
|
||||
|
||||
Text("No progress data available for \(selectedSystem.displayName) system")
|
||||
Text("No data available for \(selectedSystem.displayName) system")
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
@@ -201,38 +237,125 @@ struct ProgressChartSection: View {
|
||||
}
|
||||
}
|
||||
|
||||
private func calculateProgressOverTime() -> [ProgressDataPoint] {
|
||||
let sessions = dataManager.completedSessions().sorted { $0.date < $1.date }
|
||||
private func calculateGradeCounts() -> [GradeCount] {
|
||||
let problems = dataManager.problems
|
||||
let attempts = dataManager.attempts
|
||||
|
||||
return sessions.compactMap { session in
|
||||
let sessionAttempts = attempts.filter { $0.sessionId == session.id }
|
||||
let attemptedProblemIds = sessionAttempts.map { $0.problemId }
|
||||
let attemptedProblems = problems.filter { attemptedProblemIds.contains($0.id) }
|
||||
// Filter attempts by time period
|
||||
let filteredAttempts: [Attempt]
|
||||
if showAllTime {
|
||||
filteredAttempts = attempts.filter { $0.result.isSuccessful }
|
||||
} else {
|
||||
let sevenDaysAgo =
|
||||
Calendar.current.date(byAdding: .day, value: -7, to: Date()) ?? Date()
|
||||
filteredAttempts = attempts.filter {
|
||||
$0.result.isSuccessful && $0.timestamp >= sevenDaysAgo
|
||||
}
|
||||
}
|
||||
|
||||
// Group problems by difficulty system
|
||||
let problemsBySystem = Dictionary(grouping: attemptedProblems) { $0.difficulty.system }
|
||||
// Get attempted problems
|
||||
let attemptedProblemIds = filteredAttempts.map { $0.problemId }
|
||||
let attemptedProblems = problems.filter { attemptedProblemIds.contains($0.id) }
|
||||
|
||||
// Create data points for each system used in this session
|
||||
return problemsBySystem.compactMap { (system, systemProblems) -> ProgressDataPoint? in
|
||||
guard
|
||||
let highestGradeProblem = systemProblems.max(by: {
|
||||
$0.difficulty.numericValue < $1.difficulty.numericValue
|
||||
})
|
||||
else {
|
||||
return nil
|
||||
}
|
||||
// Group by difficulty system and grade
|
||||
var gradeCounts: [String: GradeCount] = [:]
|
||||
|
||||
return ProgressDataPoint(
|
||||
date: session.date,
|
||||
maxGrade: highestGradeProblem.difficulty.grade,
|
||||
maxGradeNumeric: highestGradeProblem.difficulty.numericValue,
|
||||
climbType: highestGradeProblem.climbType,
|
||||
difficultySystem: system
|
||||
for problem in attemptedProblems {
|
||||
let successfulAttemptsForProblem = filteredAttempts.filter {
|
||||
$0.problemId == problem.id
|
||||
}
|
||||
let count = successfulAttemptsForProblem.count
|
||||
|
||||
let key = "\(problem.difficulty.system.rawValue)-\(problem.difficulty.grade)"
|
||||
|
||||
if let existing = gradeCounts[key] {
|
||||
gradeCounts[key] = GradeCount(
|
||||
grade: existing.grade,
|
||||
count: existing.count + count,
|
||||
gradeNumeric: existing.gradeNumeric,
|
||||
difficultySystem: existing.difficultySystem
|
||||
)
|
||||
} else {
|
||||
gradeCounts[key] = GradeCount(
|
||||
grade: problem.difficulty.grade,
|
||||
count: count,
|
||||
gradeNumeric: problem.difficulty.numericValue,
|
||||
difficultySystem: problem.difficulty.system
|
||||
)
|
||||
}
|
||||
}.flatMap { $0 }
|
||||
}
|
||||
|
||||
return Array(gradeCounts.values)
|
||||
}
|
||||
}
|
||||
|
||||
struct GradeCount {
|
||||
let grade: String
|
||||
let count: Int
|
||||
let gradeNumeric: Int
|
||||
let difficultySystem: DifficultySystem
|
||||
}
|
||||
|
||||
struct BarChartView: View {
|
||||
let data: [GradeCount]
|
||||
|
||||
private var sortedData: [GradeCount] {
|
||||
data.sorted { $0.gradeNumeric < $1.gradeNumeric }
|
||||
}
|
||||
|
||||
private var maxCount: Int {
|
||||
data.map { $0.count }.max() ?? 1
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
GeometryReader { geometry in
|
||||
let chartWidth = geometry.size.width - 40
|
||||
let chartHeight = geometry.size.height - 40
|
||||
let barWidth = chartWidth / CGFloat(max(sortedData.count, 1)) * 0.8
|
||||
let spacing = chartWidth / CGFloat(max(sortedData.count, 1)) * 0.2
|
||||
|
||||
if sortedData.isEmpty {
|
||||
Rectangle()
|
||||
.fill(.clear)
|
||||
.overlay(
|
||||
Text("No data")
|
||||
.foregroundColor(.secondary)
|
||||
)
|
||||
} else {
|
||||
VStack(alignment: .leading) {
|
||||
// Chart area
|
||||
HStack(alignment: .bottom, spacing: spacing / CGFloat(sortedData.count)) {
|
||||
ForEach(Array(sortedData.enumerated()), id: \.offset) { index, gradeCount in
|
||||
VStack(spacing: 4) {
|
||||
// Bar
|
||||
RoundedRectangle(cornerRadius: 4)
|
||||
.fill(.blue)
|
||||
.frame(
|
||||
width: barWidth,
|
||||
height: CGFloat(gradeCount.count) / CGFloat(maxCount)
|
||||
* chartHeight * 0.8
|
||||
)
|
||||
.overlay(
|
||||
Text("\(gradeCount.count)")
|
||||
.font(.caption2)
|
||||
.fontWeight(.medium)
|
||||
.foregroundColor(.white)
|
||||
.opacity(gradeCount.count > 0 ? 1 : 0)
|
||||
)
|
||||
|
||||
// Grade label
|
||||
Text(gradeCount.grade)
|
||||
.font(.caption2)
|
||||
.foregroundColor(.secondary)
|
||||
.lineLimit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
.frame(height: chartHeight)
|
||||
}
|
||||
.frame(maxWidth: .infinity, alignment: .center)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -253,7 +376,7 @@ struct FavoriteGymSection: View {
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 16) {
|
||||
VStack(alignment: .leading, spacing: 16) {
|
||||
HStack {
|
||||
Image(systemName: "location.fill")
|
||||
.font(.title2)
|
||||
@@ -380,139 +503,6 @@ struct RecentActivitySection: View {
|
||||
}
|
||||
}
|
||||
|
||||
struct LineChartView: View {
|
||||
let data: [ProgressDataPoint]
|
||||
let selectedSystem: DifficultySystem
|
||||
|
||||
private var uniqueGrades: [String] {
|
||||
if selectedSystem == .custom {
|
||||
return Array(Set(data.map { $0.maxGrade })).sorted { grade1, grade2 in
|
||||
return (Int(grade1) ?? 0) > (Int(grade2) ?? 0)
|
||||
}
|
||||
} else {
|
||||
return Array(Set(data.map { $0.maxGrade })).sorted { grade1, grade2 in
|
||||
let grade1Data = data.first(where: { $0.maxGrade == grade1 })
|
||||
let grade2Data = data.first(where: { $0.maxGrade == grade2 })
|
||||
return (grade1Data?.maxGradeNumeric ?? 0)
|
||||
> (grade2Data?.maxGradeNumeric ?? 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var minGrade: Int {
|
||||
data.map { $0.maxGradeNumeric }.min() ?? 0
|
||||
}
|
||||
|
||||
private var maxGrade: Int {
|
||||
data.map { $0.maxGradeNumeric }.max() ?? 1
|
||||
}
|
||||
|
||||
private var gradeRange: Int {
|
||||
max(maxGrade - minGrade, 1)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
GeometryReader { geometry in
|
||||
let chartWidth = geometry.size.width - 40
|
||||
let chartHeight = geometry.size.height - 40
|
||||
|
||||
if data.isEmpty {
|
||||
Rectangle()
|
||||
.fill(.clear)
|
||||
.overlay(
|
||||
Text("No data")
|
||||
.foregroundColor(.secondary)
|
||||
)
|
||||
} else {
|
||||
|
||||
HStack {
|
||||
// Y-axis labels
|
||||
VStack {
|
||||
ForEach(0..<min(5, uniqueGrades.count), id: \.self) { i in
|
||||
let gradeLabel = i < uniqueGrades.count ? uniqueGrades[i] : ""
|
||||
|
||||
Text(gradeLabel)
|
||||
.font(.caption)
|
||||
.foregroundColor(.secondary)
|
||||
.frame(width: 30, alignment: .trailing)
|
||||
|
||||
if i < min(4, uniqueGrades.count - 1) {
|
||||
Spacer()
|
||||
}
|
||||
}
|
||||
}
|
||||
.frame(height: chartHeight)
|
||||
|
||||
// Chart area
|
||||
ZStack {
|
||||
// Grid lines
|
||||
ForEach(0..<5) { i in
|
||||
let y = CGFloat(i) * chartHeight / 4
|
||||
Rectangle()
|
||||
.fill(.gray.opacity(0.2))
|
||||
.frame(height: 0.5)
|
||||
.offset(y: y - chartHeight / 2)
|
||||
}
|
||||
|
||||
// Line chart
|
||||
if data.count > 1 {
|
||||
Path { path in
|
||||
for (index, point) in data.enumerated() {
|
||||
let x = CGFloat(index) * chartWidth / CGFloat(data.count - 1)
|
||||
let normalizedY =
|
||||
CGFloat(point.maxGradeNumeric - minGrade)
|
||||
/ CGFloat(gradeRange)
|
||||
let y = chartHeight - (normalizedY * chartHeight)
|
||||
|
||||
if index == 0 {
|
||||
path.move(to: CGPoint(x: x, y: y))
|
||||
} else {
|
||||
path.addLine(to: CGPoint(x: x, y: y))
|
||||
}
|
||||
}
|
||||
}
|
||||
.stroke(.blue, lineWidth: 2)
|
||||
}
|
||||
|
||||
// Data points
|
||||
ForEach(data.indices, id: \.self) { index in
|
||||
let point = data[index]
|
||||
let x =
|
||||
data.count == 1
|
||||
? chartWidth / 2
|
||||
: CGFloat(index) * chartWidth / CGFloat(data.count - 1)
|
||||
let normalizedY =
|
||||
CGFloat(point.maxGradeNumeric - minGrade) / CGFloat(gradeRange)
|
||||
let y = chartHeight - (normalizedY * chartHeight)
|
||||
|
||||
Circle()
|
||||
.fill(.blue)
|
||||
.frame(width: 8, height: 8)
|
||||
.position(x: x, y: y)
|
||||
.overlay(
|
||||
Circle()
|
||||
.stroke(.white, lineWidth: 2)
|
||||
.frame(width: 8, height: 8)
|
||||
.position(x: x, y: y)
|
||||
)
|
||||
}
|
||||
}
|
||||
.frame(width: chartWidth, height: chartHeight)
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
}
|
||||
|
||||
struct ProgressDataPoint {
|
||||
let date: Date
|
||||
let maxGrade: String
|
||||
let maxGradeNumeric: Int
|
||||
let climbType: ClimbType
|
||||
let difficultySystem: DifficultySystem
|
||||
}
|
||||
|
||||
#Preview {
|
||||
AnalyticsView()
|
||||
.environmentObject(ClimbingDataManager.preview)
|
||||
|
||||
Reference in New Issue
Block a user