1.5.0 Initial run as iOS in a monorepo
This commit is contained in:
443
ios/OpenClimb/Views/Detail/SessionDetailView.swift
Normal file
443
ios/OpenClimb/Views/Detail/SessionDetailView.swift
Normal file
@@ -0,0 +1,443 @@
|
||||
//
|
||||
// SessionDetailView.swift
|
||||
// OpenClimb
|
||||
//
|
||||
// Created by OpenClimb on 2025-01-17.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct SessionDetailView: View {
|
||||
let sessionId: UUID
|
||||
@EnvironmentObject var dataManager: ClimbingDataManager
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
@State private var showingDeleteAlert = false
|
||||
@State private var showingAddAttempt = false
|
||||
@State private var editingAttempt: Attempt?
|
||||
|
||||
private var session: ClimbSession? {
|
||||
dataManager.session(withId: sessionId)
|
||||
}
|
||||
|
||||
private var gym: Gym? {
|
||||
guard let session = session else { return nil }
|
||||
return dataManager.gym(withId: session.gymId)
|
||||
}
|
||||
|
||||
private var attempts: [Attempt] {
|
||||
dataManager.attempts(forSession: sessionId)
|
||||
}
|
||||
|
||||
private var attemptsWithProblems: [(Attempt, Problem)] {
|
||||
attempts.compactMap { attempt in
|
||||
guard let problem = dataManager.problem(withId: attempt.problemId) else { return nil }
|
||||
return (attempt, problem)
|
||||
}.sorted { $0.0.timestamp < $1.0.timestamp }
|
||||
}
|
||||
|
||||
private var sessionStats: SessionStats {
|
||||
calculateSessionStats()
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
LazyVStack(spacing: 20) {
|
||||
if let session = session, let gym = gym {
|
||||
SessionHeaderCard(session: session, gym: gym, stats: sessionStats)
|
||||
|
||||
SessionStatsCard(stats: sessionStats)
|
||||
|
||||
AttemptsSection(attemptsWithProblems: attemptsWithProblems)
|
||||
} else {
|
||||
Text("Session not found")
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
.navigationTitle("Session Details")
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.toolbar {
|
||||
ToolbarItemGroup(placement: .navigationBarTrailing) {
|
||||
if let session = session {
|
||||
if session.status == .active {
|
||||
Button("End Session") {
|
||||
dataManager.endSession(session.id)
|
||||
dismiss()
|
||||
}
|
||||
.foregroundColor(.orange)
|
||||
} else {
|
||||
Menu {
|
||||
Button(role: .destructive) {
|
||||
showingDeleteAlert = true
|
||||
} label: {
|
||||
Label("Delete Session", systemImage: "trash")
|
||||
}
|
||||
} label: {
|
||||
Image(systemName: "ellipsis.circle")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.overlay(alignment: .bottomTrailing) {
|
||||
if session?.status == .active {
|
||||
Button(action: { showingAddAttempt = true }) {
|
||||
Image(systemName: "plus")
|
||||
.font(.title2)
|
||||
.foregroundColor(.white)
|
||||
.frame(width: 56, height: 56)
|
||||
.background(Circle().fill(.blue))
|
||||
.shadow(radius: 4)
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
}
|
||||
.alert("Delete Session", isPresented: $showingDeleteAlert) {
|
||||
Button("Cancel", role: .cancel) {}
|
||||
Button("Delete", role: .destructive) {
|
||||
if let session = session {
|
||||
dataManager.deleteSession(session)
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
} message: {
|
||||
Text(
|
||||
"Are you sure you want to delete this session? This will also delete all attempts associated with this session."
|
||||
)
|
||||
}
|
||||
.sheet(isPresented: $showingAddAttempt) {
|
||||
if let session = session, let gym = gym {
|
||||
AddAttemptView(session: session, gym: gym)
|
||||
}
|
||||
}
|
||||
.sheet(item: $editingAttempt) { attempt in
|
||||
EditAttemptView(attempt: attempt)
|
||||
}
|
||||
}
|
||||
|
||||
private func calculateSessionStats() -> SessionStats {
|
||||
let successfulAttempts = attempts.filter { $0.result.isSuccessful }
|
||||
let uniqueProblems = Set(attempts.map { $0.problemId })
|
||||
let completedProblems = Set(successfulAttempts.map { $0.problemId })
|
||||
|
||||
let attemptedProblems = uniqueProblems.compactMap { dataManager.problem(withId: $0) }
|
||||
let boulderProblems = attemptedProblems.filter { $0.climbType == .boulder }
|
||||
let ropeProblems = attemptedProblems.filter { $0.climbType == .rope }
|
||||
|
||||
let boulderRange = gradeRange(for: boulderProblems)
|
||||
let ropeRange = gradeRange(for: ropeProblems)
|
||||
|
||||
return SessionStats(
|
||||
totalAttempts: attempts.count,
|
||||
successfulAttempts: successfulAttempts.count,
|
||||
uniqueProblemsAttempted: uniqueProblems.count,
|
||||
uniqueProblemsCompleted: completedProblems.count,
|
||||
boulderRange: boulderRange,
|
||||
ropeRange: ropeRange
|
||||
)
|
||||
}
|
||||
|
||||
private func gradeRange(for problems: [Problem]) -> String? {
|
||||
guard !problems.isEmpty else { return nil }
|
||||
let grades = problems.map { $0.difficulty }.sorted()
|
||||
if grades.count == 1 {
|
||||
return grades.first?.grade
|
||||
} else {
|
||||
return "\(grades.first?.grade ?? "") - \(grades.last?.grade ?? "")"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct SessionHeaderCard: View {
|
||||
let session: ClimbSession
|
||||
let gym: Gym
|
||||
let stats: SessionStats
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 16) {
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
Text(gym.name)
|
||||
.font(.title)
|
||||
.fontWeight(.bold)
|
||||
|
||||
Text(formatDate(session.date))
|
||||
.font(.title2)
|
||||
.foregroundColor(.blue)
|
||||
|
||||
if let duration = session.duration {
|
||||
Text("Duration: \(duration) minutes")
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
|
||||
if let notes = session.notes, !notes.isEmpty {
|
||||
Text(notes)
|
||||
.font(.body)
|
||||
.padding(.top, 4)
|
||||
}
|
||||
}
|
||||
|
||||
// Status indicator
|
||||
HStack {
|
||||
Image(systemName: session.status == .active ? "play.fill" : "checkmark.circle.fill")
|
||||
.foregroundColor(session.status == .active ? .green : .blue)
|
||||
|
||||
Text(session.status == .active ? "In Progress" : "Completed")
|
||||
.font(.subheadline)
|
||||
.fontWeight(.medium)
|
||||
.foregroundColor(session.status == .active ? .green : .blue)
|
||||
|
||||
Spacer()
|
||||
}
|
||||
.padding(.horizontal, 12)
|
||||
.padding(.vertical, 6)
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: 8)
|
||||
.fill((session.status == .active ? Color.green : Color.blue).opacity(0.1))
|
||||
)
|
||||
}
|
||||
.padding()
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: 16)
|
||||
.fill(.regularMaterial)
|
||||
)
|
||||
}
|
||||
|
||||
private func formatDate(_ date: Date) -> String {
|
||||
let formatter = DateFormatter()
|
||||
formatter.dateStyle = .full
|
||||
return formatter.string(from: date)
|
||||
}
|
||||
}
|
||||
|
||||
struct SessionStatsCard: View {
|
||||
let stats: SessionStats
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 16) {
|
||||
Text("Session Stats")
|
||||
.font(.title2)
|
||||
.fontWeight(.bold)
|
||||
|
||||
if stats.totalAttempts == 0 {
|
||||
Text("No attempts recorded yet")
|
||||
.foregroundColor(.secondary)
|
||||
.frame(maxWidth: .infinity, alignment: .center)
|
||||
.padding()
|
||||
} else {
|
||||
LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: 2), spacing: 16) {
|
||||
StatItem(label: "Total Attempts", value: "\(stats.totalAttempts)")
|
||||
StatItem(label: "Problems", value: "\(stats.uniqueProblemsAttempted)")
|
||||
StatItem(label: "Successful", value: "\(stats.successfulAttempts)")
|
||||
StatItem(label: "Completed", value: "\(stats.uniqueProblemsCompleted)")
|
||||
}
|
||||
|
||||
// Grade ranges
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
if let boulderRange = stats.boulderRange, let ropeRange = stats.ropeRange {
|
||||
HStack {
|
||||
StatItem(label: "Boulder Range", value: boulderRange)
|
||||
StatItem(label: "Rope Range", value: ropeRange)
|
||||
}
|
||||
} else if let singleRange = stats.boulderRange ?? stats.ropeRange {
|
||||
StatItem(label: "Grade Range", value: singleRange)
|
||||
.frame(maxWidth: .infinity, alignment: .center)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: 16)
|
||||
.fill(.regularMaterial)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
struct StatItem: View {
|
||||
let label: String
|
||||
let value: String
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 4) {
|
||||
Text(value)
|
||||
.font(.title2)
|
||||
.fontWeight(.bold)
|
||||
.foregroundColor(.blue)
|
||||
|
||||
Text(label)
|
||||
.font(.caption)
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
.frame(maxWidth: .infinity)
|
||||
}
|
||||
}
|
||||
|
||||
struct AttemptsSection: View {
|
||||
let attemptsWithProblems: [(Attempt, Problem)]
|
||||
@EnvironmentObject var dataManager: ClimbingDataManager
|
||||
@State private var editingAttempt: Attempt?
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 16) {
|
||||
Text("Attempts (\(attemptsWithProblems.count))")
|
||||
.font(.title2)
|
||||
.fontWeight(.bold)
|
||||
|
||||
if attemptsWithProblems.isEmpty {
|
||||
VStack(spacing: 12) {
|
||||
Image(systemName: "hand.raised.slash")
|
||||
.font(.title)
|
||||
.foregroundColor(.secondary)
|
||||
|
||||
Text("No attempts yet")
|
||||
.font(.headline)
|
||||
.foregroundColor(.secondary)
|
||||
|
||||
Text("Start attempting problems to see your progress!")
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.secondary)
|
||||
.multilineTextAlignment(.center)
|
||||
}
|
||||
.frame(maxWidth: .infinity)
|
||||
.padding()
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: 16)
|
||||
.fill(.regularMaterial)
|
||||
)
|
||||
} else {
|
||||
LazyVStack(spacing: 12) {
|
||||
ForEach(attemptsWithProblems.indices, id: \.self) { index in
|
||||
let (attempt, problem) = attemptsWithProblems[index]
|
||||
AttemptCard(attempt: attempt, problem: problem)
|
||||
.onTapGesture {
|
||||
editingAttempt = attempt
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.sheet(item: $editingAttempt) { attempt in
|
||||
EditAttemptView(attempt: attempt)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct AttemptCard: View {
|
||||
let attempt: Attempt
|
||||
let problem: Problem
|
||||
@EnvironmentObject var dataManager: ClimbingDataManager
|
||||
@State private var showingDeleteAlert = false
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 12) {
|
||||
HStack {
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
Text(problem.name ?? "Unknown Problem")
|
||||
.font(.headline)
|
||||
.fontWeight(.semibold)
|
||||
|
||||
Text("\(problem.difficulty.system.displayName): \(problem.difficulty.grade)")
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.blue)
|
||||
|
||||
if let location = problem.location {
|
||||
Text(location)
|
||||
.font(.caption)
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer()
|
||||
|
||||
VStack(alignment: .trailing, spacing: 8) {
|
||||
AttemptResultBadge(result: attempt.result)
|
||||
|
||||
HStack(spacing: 12) {
|
||||
Button(action: { showingDeleteAlert = true }) {
|
||||
Image(systemName: "trash")
|
||||
.font(.caption)
|
||||
.foregroundColor(.red)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let notes = attempt.notes, !notes.isEmpty {
|
||||
Text(notes)
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
|
||||
if let highestHold = attempt.highestHold, !highestHold.isEmpty {
|
||||
Text("Highest hold: \(highestHold)")
|
||||
.font(.caption)
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: 12)
|
||||
.fill(.ultraThinMaterial)
|
||||
.stroke(.quaternary, lineWidth: 1)
|
||||
)
|
||||
.alert("Delete Attempt", isPresented: $showingDeleteAlert) {
|
||||
Button("Cancel", role: .cancel) {}
|
||||
Button("Delete", role: .destructive) {
|
||||
dataManager.deleteAttempt(attempt)
|
||||
}
|
||||
} message: {
|
||||
Text("Are you sure you want to delete this attempt?")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct AttemptResultBadge: View {
|
||||
let result: AttemptResult
|
||||
|
||||
private var badgeColor: Color {
|
||||
switch result {
|
||||
case .success, .flash:
|
||||
return .green
|
||||
case .fall:
|
||||
return .orange
|
||||
case .noProgress:
|
||||
return .red
|
||||
}
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
Text(result.displayName)
|
||||
.font(.caption)
|
||||
.fontWeight(.medium)
|
||||
.padding(.horizontal, 8)
|
||||
.padding(.vertical, 4)
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: 6)
|
||||
.fill(badgeColor.opacity(0.1))
|
||||
)
|
||||
.foregroundColor(badgeColor)
|
||||
.overlay(
|
||||
RoundedRectangle(cornerRadius: 6)
|
||||
.stroke(badgeColor.opacity(0.3), lineWidth: 1)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
struct SessionStats {
|
||||
let totalAttempts: Int
|
||||
let successfulAttempts: Int
|
||||
let uniqueProblemsAttempted: Int
|
||||
let uniqueProblemsCompleted: Int
|
||||
let boulderRange: String?
|
||||
let ropeRange: String?
|
||||
}
|
||||
|
||||
#Preview {
|
||||
NavigationView {
|
||||
SessionDetailView(sessionId: UUID())
|
||||
.environmentObject(ClimbingDataManager.preview)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user