1.0.0 for iOS is ready to ship
This commit is contained in:
@@ -1,10 +1,5 @@
|
||||
//
|
||||
// SessionDetailView.swift
|
||||
// OpenClimb
|
||||
//
|
||||
// Created by OpenClimb on 2025-01-17.
|
||||
//
|
||||
|
||||
import Combine
|
||||
import SwiftUI
|
||||
|
||||
struct SessionDetailView: View {
|
||||
@@ -14,6 +9,8 @@ struct SessionDetailView: View {
|
||||
@State private var showingDeleteAlert = false
|
||||
@State private var showingAddAttempt = false
|
||||
@State private var editingAttempt: Attempt?
|
||||
@State private var attemptToDelete: Attempt?
|
||||
@State private var currentTime = Date()
|
||||
|
||||
private var session: ClimbSession? {
|
||||
dataManager.session(withId: sessionId)
|
||||
@@ -39,15 +36,20 @@ struct SessionDetailView: View {
|
||||
calculateSessionStats()
|
||||
}
|
||||
|
||||
private let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
|
||||
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
LazyVStack(spacing: 20) {
|
||||
if let session = session, let gym = gym {
|
||||
SessionHeaderCard(session: session, gym: gym, stats: sessionStats)
|
||||
SessionHeaderCard(
|
||||
session: session, gym: gym, stats: sessionStats, currentTime: currentTime)
|
||||
|
||||
SessionStatsCard(stats: sessionStats)
|
||||
|
||||
AttemptsSection(attemptsWithProblems: attemptsWithProblems)
|
||||
AttemptsSection(
|
||||
attemptsWithProblems: attemptsWithProblems,
|
||||
attemptToDelete: $attemptToDelete)
|
||||
} else {
|
||||
Text("Session not found")
|
||||
.foregroundColor(.secondary)
|
||||
@@ -55,6 +57,9 @@ struct SessionDetailView: View {
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
.onReceive(timer) { _ in
|
||||
currentTime = Date()
|
||||
}
|
||||
.navigationTitle("Session Details")
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.toolbar {
|
||||
@@ -80,6 +85,33 @@ struct SessionDetailView: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
.alert(
|
||||
"Delete Attempt",
|
||||
isPresented: Binding<Bool>(
|
||||
get: { attemptToDelete != nil },
|
||||
set: { if !$0 { attemptToDelete = nil } }
|
||||
)
|
||||
) {
|
||||
Button("Cancel", role: .cancel) {
|
||||
attemptToDelete = nil
|
||||
}
|
||||
Button("Delete", role: .destructive) {
|
||||
if let attempt = attemptToDelete {
|
||||
dataManager.deleteAttempt(attempt)
|
||||
attemptToDelete = nil
|
||||
}
|
||||
}
|
||||
} message: {
|
||||
if let attempt = attemptToDelete,
|
||||
let problem = dataManager.problem(withId: attempt.problemId)
|
||||
{
|
||||
Text(
|
||||
"Are you sure you want to delete this attempt on \"\(problem.name ?? "Unknown Problem")\"? This action cannot be undone."
|
||||
)
|
||||
} else {
|
||||
Text("Are you sure you want to delete this attempt? This action cannot be undone.")
|
||||
}
|
||||
}
|
||||
.overlay(alignment: .bottomTrailing) {
|
||||
if session?.status == .active {
|
||||
Button(action: { showingAddAttempt = true }) {
|
||||
@@ -140,12 +172,26 @@ struct SessionDetailView: View {
|
||||
|
||||
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 ?? "")"
|
||||
let difficulties = problems.map { $0.difficulty }
|
||||
|
||||
// Group by difficulty system first
|
||||
let groupedBySystem = Dictionary(grouping: difficulties) { $0.system }
|
||||
|
||||
// For each system, find the range
|
||||
let ranges = groupedBySystem.compactMap { (system, difficulties) -> String? in
|
||||
let sortedDifficulties = difficulties.sorted()
|
||||
guard let min = sortedDifficulties.first, let max = sortedDifficulties.last else {
|
||||
return nil
|
||||
}
|
||||
|
||||
if min == max {
|
||||
return min.grade
|
||||
} else {
|
||||
return "\(min.grade) - \(max.grade)"
|
||||
}
|
||||
}
|
||||
|
||||
return ranges.joined(separator: ", ")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,6 +199,7 @@ struct SessionHeaderCard: View {
|
||||
let session: ClimbSession
|
||||
let gym: Gym
|
||||
let stats: SessionStats
|
||||
let currentTime: Date
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 16) {
|
||||
@@ -165,7 +212,13 @@ struct SessionHeaderCard: View {
|
||||
.font(.title2)
|
||||
.foregroundColor(.blue)
|
||||
|
||||
if let duration = session.duration {
|
||||
if session.status == .active {
|
||||
if let startTime = session.startTime {
|
||||
Text("Duration: \(formatDuration(from: startTime, to: currentTime))")
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
} else if let duration = session.duration {
|
||||
Text("Duration: \(duration) minutes")
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.secondary)
|
||||
@@ -209,6 +262,21 @@ struct SessionHeaderCard: View {
|
||||
formatter.dateStyle = .full
|
||||
return formatter.string(from: date)
|
||||
}
|
||||
|
||||
private func formatDuration(from start: Date, to end: Date) -> String {
|
||||
let interval = end.timeIntervalSince(start)
|
||||
let hours = Int(interval) / 3600
|
||||
let minutes = Int(interval) % 3600 / 60
|
||||
let seconds = Int(interval) % 60
|
||||
|
||||
if hours > 0 {
|
||||
return String(format: "%dh %dm %ds", hours, minutes, seconds)
|
||||
} else if minutes > 0 {
|
||||
return String(format: "%dm %ds", minutes, seconds)
|
||||
} else {
|
||||
return String(format: "%ds", seconds)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct SessionStatsCard: View {
|
||||
@@ -276,6 +344,7 @@ struct StatItem: View {
|
||||
|
||||
struct AttemptsSection: View {
|
||||
let attemptsWithProblems: [(Attempt, Problem)]
|
||||
@Binding var attemptToDelete: Attempt?
|
||||
@EnvironmentObject var dataManager: ClimbingDataManager
|
||||
@State private var editingAttempt: Attempt?
|
||||
|
||||
@@ -311,6 +380,30 @@ struct AttemptsSection: View {
|
||||
ForEach(attemptsWithProblems.indices, id: \.self) { index in
|
||||
let (attempt, problem) = attemptsWithProblems[index]
|
||||
AttemptCard(attempt: attempt, problem: problem)
|
||||
.background(.regularMaterial)
|
||||
.cornerRadius(12)
|
||||
.shadow(radius: 2)
|
||||
.swipeActions(edge: .trailing, allowsFullSwipe: true) {
|
||||
Button(role: .destructive) {
|
||||
// Add haptic feedback for delete action
|
||||
let impactFeedback = UIImpactFeedbackGenerator(style: .medium)
|
||||
impactFeedback.impactOccurred()
|
||||
attemptToDelete = attempt
|
||||
} label: {
|
||||
Label("Delete", systemImage: "trash")
|
||||
}
|
||||
.accessibilityLabel("Delete attempt")
|
||||
.accessibilityHint("Removes this attempt from the session")
|
||||
|
||||
Button {
|
||||
editingAttempt = attempt
|
||||
} label: {
|
||||
Label("Edit", systemImage: "pencil")
|
||||
}
|
||||
.tint(.blue)
|
||||
.accessibilityLabel("Edit attempt")
|
||||
.accessibilityHint("Modify the details of this attempt")
|
||||
}
|
||||
.onTapGesture {
|
||||
editingAttempt = attempt
|
||||
}
|
||||
@@ -327,8 +420,6 @@ struct AttemptsSection: View {
|
||||
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) {
|
||||
@@ -353,15 +444,6 @@ struct AttemptCard: View {
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -378,19 +460,6 @@ struct AttemptCard: View {
|
||||
}
|
||||
}
|
||||
.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?")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user