Import/export fixes, icon, and graphing
This commit is contained in:
@@ -20,9 +20,11 @@ struct AnalyticsView: View {
|
||||
|
||||
ProgressChartSection()
|
||||
|
||||
FavoriteGymSection()
|
||||
HStack(spacing: 16) {
|
||||
FavoriteGymSection()
|
||||
|
||||
RecentActivitySection()
|
||||
RecentActivitySection()
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
@@ -34,9 +36,9 @@ struct AnalyticsView: View {
|
||||
struct HeaderSection: View {
|
||||
var body: some View {
|
||||
HStack {
|
||||
Image(systemName: "mountain.2.fill")
|
||||
.font(.title)
|
||||
.foregroundColor(.blue)
|
||||
Image("MountainsIcon")
|
||||
.resizable()
|
||||
.frame(width: 32, height: 32)
|
||||
|
||||
Text("Analytics")
|
||||
.font(.title)
|
||||
@@ -133,7 +135,13 @@ struct ProgressChartSection: View {
|
||||
}
|
||||
|
||||
private var usedSystems: [DifficultySystem] {
|
||||
Set(progressData.map { $0.difficultySystem }).sorted { $0.rawValue < $1.rawValue }
|
||||
let uniqueSystems = Set(progressData.map { $0.difficultySystem })
|
||||
return uniqueSystems.sorted {
|
||||
let order: [DifficultySystem] = [.vScale, .font, .yds, .custom]
|
||||
let firstIndex = order.firstIndex(of: $0) ?? order.count
|
||||
let secondIndex = order.firstIndex(of: $1) ?? order.count
|
||||
return firstIndex < secondIndex
|
||||
}
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
@@ -148,20 +156,35 @@ struct ProgressChartSection: View {
|
||||
if usedSystems.count > 1 {
|
||||
Menu {
|
||||
ForEach(usedSystems, id: \.self) { system in
|
||||
Button(system.displayName) {
|
||||
Button(action: {
|
||||
selectedSystem = system
|
||||
}) {
|
||||
HStack {
|
||||
Text(system.displayName)
|
||||
if selectedSystem == system {
|
||||
Spacer()
|
||||
Image(systemName: "checkmark")
|
||||
.foregroundColor(.blue)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} label: {
|
||||
Text(selectedSystem.displayName)
|
||||
.font(.caption)
|
||||
.padding(.horizontal, 8)
|
||||
.padding(.vertical, 4)
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: 8)
|
||||
.fill(.blue.opacity(0.1))
|
||||
)
|
||||
.foregroundColor(.blue)
|
||||
HStack(spacing: 4) {
|
||||
Text(selectedSystem.displayName)
|
||||
.font(.subheadline)
|
||||
.fontWeight(.medium)
|
||||
Image(systemName: "chevron.down")
|
||||
.font(.caption)
|
||||
}
|
||||
.padding(.horizontal, 12)
|
||||
.padding(.vertical, 6)
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: 8)
|
||||
.fill(.blue.opacity(0.1))
|
||||
.stroke(.blue.opacity(0.3), lineWidth: 1)
|
||||
)
|
||||
.foregroundColor(.blue)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -169,37 +192,11 @@ struct ProgressChartSection: View {
|
||||
let filteredData = progressData.filter { $0.difficultySystem == selectedSystem }
|
||||
|
||||
if !filteredData.isEmpty {
|
||||
VStack {
|
||||
// Simple text-based chart placeholder
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
ForEach(filteredData.indices.prefix(5), id: \.self) { index in
|
||||
let point = filteredData[index]
|
||||
HStack {
|
||||
Text("Session \(index + 1)")
|
||||
.font(.caption)
|
||||
.frame(width: 80, alignment: .leading)
|
||||
|
||||
Rectangle()
|
||||
.fill(.blue)
|
||||
.frame(width: CGFloat(point.maxGradeNumeric * 5), height: 20)
|
||||
|
||||
Text(point.maxGrade)
|
||||
.font(.caption)
|
||||
.foregroundColor(.blue)
|
||||
}
|
||||
}
|
||||
|
||||
if filteredData.count > 5 {
|
||||
Text("... and \(filteredData.count - 5) more sessions")
|
||||
.font(.caption)
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
}
|
||||
}
|
||||
.frame(height: 200)
|
||||
LineChartView(data: filteredData, selectedSystem: selectedSystem)
|
||||
.frame(height: 200)
|
||||
|
||||
Text(
|
||||
"X: session number, Y: max \(selectedSystem.displayName.lowercased()) grade achieved"
|
||||
"Progress: max \(selectedSystem.displayName.lowercased()) grade achieved per session"
|
||||
)
|
||||
.font(.caption)
|
||||
.foregroundColor(.secondary)
|
||||
@@ -239,22 +236,28 @@ struct ProgressChartSection: View {
|
||||
let attemptedProblemIds = sessionAttempts.map { $0.problemId }
|
||||
let attemptedProblems = problems.filter { attemptedProblemIds.contains($0.id) }
|
||||
|
||||
guard
|
||||
let highestGradeProblem = attemptedProblems.max(by: {
|
||||
$0.difficulty.numericValue < $1.difficulty.numericValue
|
||||
})
|
||||
else {
|
||||
return nil
|
||||
}
|
||||
// Group problems by difficulty system
|
||||
let problemsBySystem = Dictionary(grouping: attemptedProblems) { $0.difficulty.system }
|
||||
|
||||
return ProgressDataPoint(
|
||||
date: session.date,
|
||||
maxGrade: highestGradeProblem.difficulty.grade,
|
||||
maxGradeNumeric: highestGradeProblem.difficulty.numericValue,
|
||||
climbType: highestGradeProblem.climbType,
|
||||
difficultySystem: highestGradeProblem.difficulty.system
|
||||
)
|
||||
}
|
||||
// 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
|
||||
}
|
||||
|
||||
return ProgressDataPoint(
|
||||
date: session.date,
|
||||
maxGrade: highestGradeProblem.difficulty.grade,
|
||||
maxGradeNumeric: highestGradeProblem.difficulty.numericValue,
|
||||
climbType: highestGradeProblem.climbType,
|
||||
difficultySystem: system
|
||||
)
|
||||
}
|
||||
}.flatMap { $0 }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -275,27 +278,53 @@ struct FavoriteGymSection: View {
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 12) {
|
||||
Text("Favorite Gym")
|
||||
.font(.title2)
|
||||
.fontWeight(.bold)
|
||||
VStack(alignment: .leading, spacing: 16) {
|
||||
HStack {
|
||||
Image(systemName: "location.fill")
|
||||
.font(.title2)
|
||||
.foregroundColor(.purple)
|
||||
|
||||
Text("Favorite Gym")
|
||||
.font(.title2)
|
||||
.fontWeight(.bold)
|
||||
|
||||
Spacer()
|
||||
}
|
||||
|
||||
if let info = favoriteGymInfo {
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
VStack(alignment: .leading, spacing: 12) {
|
||||
Text(info.gym.name)
|
||||
.font(.title3)
|
||||
.fontWeight(.semibold)
|
||||
.foregroundColor(.primary)
|
||||
|
||||
Text("\(info.sessionCount) sessions")
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.secondary)
|
||||
HStack {
|
||||
Image(systemName: "calendar")
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.purple)
|
||||
|
||||
Text("\(info.sessionCount) sessions")
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
|
||||
Spacer()
|
||||
}
|
||||
} else {
|
||||
Text("No sessions yet")
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.secondary)
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
Text("No sessions yet")
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.secondary)
|
||||
|
||||
Text("Start climbing to see your favorite gym!")
|
||||
.font(.caption)
|
||||
.foregroundColor(.secondary)
|
||||
|
||||
Spacer()
|
||||
}
|
||||
}
|
||||
}
|
||||
.frame(maxWidth: .infinity, minHeight: 120, alignment: .topLeading)
|
||||
.padding()
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: 16)
|
||||
@@ -311,21 +340,63 @@ struct RecentActivitySection: View {
|
||||
dataManager.sessions.count
|
||||
}
|
||||
|
||||
private var totalAttempts: Int {
|
||||
dataManager.attempts.count
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 12) {
|
||||
Text("Recent Activity")
|
||||
.font(.title2)
|
||||
.fontWeight(.bold)
|
||||
VStack(alignment: .leading, spacing: 16) {
|
||||
HStack {
|
||||
Image(systemName: "clock.fill")
|
||||
.font(.title2)
|
||||
.foregroundColor(.blue)
|
||||
|
||||
Text("Recent Activity")
|
||||
.font(.title2)
|
||||
.fontWeight(.bold)
|
||||
|
||||
Spacer()
|
||||
}
|
||||
|
||||
if recentSessionsCount > 0 {
|
||||
Text("You've had \(recentSessionsCount) sessions")
|
||||
.font(.subheadline)
|
||||
VStack(alignment: .leading, spacing: 12) {
|
||||
HStack {
|
||||
Image(systemName: "play.circle")
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.blue)
|
||||
|
||||
Text("\(recentSessionsCount) sessions")
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
|
||||
HStack {
|
||||
Image(systemName: "hand.raised")
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.green)
|
||||
|
||||
Text("\(totalAttempts) attempts")
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
|
||||
Spacer()
|
||||
}
|
||||
} else {
|
||||
Text("No recent activity")
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.secondary)
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
Text("No recent activity")
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.secondary)
|
||||
|
||||
Text("Start your first session!")
|
||||
.font(.caption)
|
||||
.foregroundColor(.secondary)
|
||||
|
||||
Spacer()
|
||||
}
|
||||
}
|
||||
}
|
||||
.frame(maxWidth: .infinity, minHeight: 120, alignment: .topLeading)
|
||||
.padding()
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: 16)
|
||||
@@ -334,6 +405,131 @@ 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
|
||||
@@ -344,63 +540,6 @@ struct ProgressDataPoint {
|
||||
|
||||
// MARK: - Helper Functions
|
||||
|
||||
func gradeToNumeric(_ system: DifficultySystem, _ grade: String) -> Int {
|
||||
switch system {
|
||||
case .vScale:
|
||||
if grade == "VB" { return 0 }
|
||||
return Int(grade.replacingOccurrences(of: "V", with: "")) ?? 0
|
||||
case .font:
|
||||
let fontMapping: [String: Int] = [
|
||||
"3": 3, "4A": 4, "4B": 5, "4C": 6, "5A": 7, "5B": 8, "5C": 9,
|
||||
"6A": 10, "6A+": 11, "6B": 12, "6B+": 13, "6C": 14, "6C+": 15,
|
||||
"7A": 16, "7A+": 17, "7B": 18, "7B+": 19, "7C": 20, "7C+": 21,
|
||||
"8A": 22, "8A+": 23, "8B": 24, "8B+": 25, "8C": 26, "8C+": 27,
|
||||
]
|
||||
return fontMapping[grade] ?? 0
|
||||
case .yds:
|
||||
let ydsMapping: [String: Int] = [
|
||||
"5.0": 50, "5.1": 51, "5.2": 52, "5.3": 53, "5.4": 54, "5.5": 55,
|
||||
"5.6": 56, "5.7": 57, "5.8": 58, "5.9": 59, "5.10a": 60, "5.10b": 61,
|
||||
"5.10c": 62, "5.10d": 63, "5.11a": 64, "5.11b": 65, "5.11c": 66,
|
||||
"5.11d": 67, "5.12a": 68, "5.12b": 69, "5.12c": 70, "5.12d": 71,
|
||||
"5.13a": 72, "5.13b": 73, "5.13c": 74, "5.13d": 75, "5.14a": 76,
|
||||
"5.14b": 77, "5.14c": 78, "5.14d": 79, "5.15a": 80, "5.15b": 81,
|
||||
"5.15c": 82, "5.15d": 83,
|
||||
]
|
||||
return ydsMapping[grade] ?? 0
|
||||
case .custom:
|
||||
return Int(grade) ?? 0
|
||||
}
|
||||
}
|
||||
|
||||
func numericToGrade(_ system: DifficultySystem, _ numeric: Int) -> String {
|
||||
switch system {
|
||||
case .vScale:
|
||||
return numeric == 0 ? "VB" : "V\(numeric)"
|
||||
case .font:
|
||||
let fontMapping: [Int: String] = [
|
||||
3: "3", 4: "4A", 5: "4B", 6: "4C", 7: "5A", 8: "5B", 9: "5C",
|
||||
10: "6A", 11: "6A+", 12: "6B", 13: "6B+", 14: "6C", 15: "6C+",
|
||||
16: "7A", 17: "7A+", 18: "7B", 19: "7B+", 20: "7C", 21: "7C+",
|
||||
22: "8A", 23: "8A+", 24: "8B", 25: "8B+", 26: "8C", 27: "8C+",
|
||||
]
|
||||
return fontMapping[numeric] ?? "\(numeric)"
|
||||
case .yds:
|
||||
let ydsMapping: [Int: String] = [
|
||||
50: "5.0", 51: "5.1", 52: "5.2", 53: "5.3", 54: "5.4", 55: "5.5",
|
||||
56: "5.6", 57: "5.7", 58: "5.8", 59: "5.9", 60: "5.10a", 61: "5.10b",
|
||||
62: "5.10c", 63: "5.10d", 64: "5.11a", 65: "5.11b", 66: "5.11c",
|
||||
67: "5.11d", 68: "5.12a", 69: "5.12b", 70: "5.12c", 71: "5.12d",
|
||||
72: "5.13a", 73: "5.13b", 74: "5.13c", 75: "5.13d", 76: "5.14a",
|
||||
77: "5.14b", 78: "5.14c", 79: "5.14d", 80: "5.15a", 81: "5.15b",
|
||||
82: "5.15c", 83: "5.15d",
|
||||
]
|
||||
return ydsMapping[numeric] ?? "\(numeric)"
|
||||
case .custom:
|
||||
return "\(numeric)"
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
AnalyticsView()
|
||||
.environmentObject(ClimbingDataManager.preview)
|
||||
|
||||
Reference in New Issue
Block a user