add ActiveWorkoutSession logic, refactor Home, add additional sample data, add isDebug

This commit is contained in:
Felix Förtsch
2024-09-14 17:01:20 +02:00
parent f45d6288dd
commit 0a400ff349
27 changed files with 710 additions and 189 deletions

View File

@@ -1,60 +1,158 @@
//
// ActiveWorkout.swift
// WorkoutsPlus
//
// Created by Felix Förtsch on 07.09.24.
//
import SwiftUI
import SwiftData
struct ActiveWorkoutSession: View {
@Environment(\.dismiss) private var dismiss
@Environment(\.modelContext) private var modelContext
@State var workout: Workout
@Default(\.isWorkingOut) var isWorkingOut
@State var workoutSession: WorkoutSession?
@State var currentExercise: Int = 0
@Query(sort: \WorkoutSession.name) private var workoutSessions: [WorkoutSession]
@State private var activeWorkoutSession: WorkoutSession?
@Default(\.activeWorkoutSessionId) var activeWorkoutSessionId
let startDate = Date() - 100
@Query(sort: \Workout.name) private var workouts: [Workout]
@State private var activeWorkout: Workout?
@Default(\.activeWorkoutId) var activeWorkoutId
@State private var currentTime = Date()
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
var body: some View {
Button("Close") { dismiss() }
Text("\(workout.name)")
Text("Elapsed Time: \(elapsedTimeText(startDate: startDate))")
.onReceive(timer) { time in
currentTime = time // Updates the current time every second
}
var body: some View {
VStack {
List {
// TODO: Unwrap Sets
ForEach(workout.workoutItems) { workoutItem in
HStack {
Text("\(workoutItem.reps)")
Text("\(workoutItem.name)")
Section(footer: Text(activeWorkoutSession?.creationDate.ISO8601Format() ?? "Unknown Date")) {
NavigationLink(destination: {
ItemPicker<Workout>(items: workouts, selectedItem: $activeWorkout)
}) {
Text(activeWorkout?.name ?? "Select your next Workout")
}
.onChange(of: activeWorkout) { _, newWorkout in
if let workout = newWorkout {
activeWorkoutId = workout.id.uuidString
activeWorkoutSession?.workout = workout
}
}
}
if let activeWorkout = activeWorkout {
Section(header: Text("Exercises")) {
ForEach(activeWorkout.workoutItems.sorted(by: { $0.position < $1.position })) { workoutItem in
HStack {
Text(String(workoutItem.reps))
Text(workoutItem.name)
Spacer()
Button(action: {
// TODO: Implement a sheet view; don't use ExerciseDetail since its purpose is editing
}) {
Image(systemName: "info.circle")
.foregroundColor(.blue)
}
}
}
}
}
}
.listStyle(.plain)
Image(systemName: "play.circle.fill")
.resizable()
.frame(width: 100, height: 100)
.font(.title)
.symbolRenderingMode(.palette)
.foregroundStyle(.white, .green)
}
if true { // This condition should be more meaningful.
VStack {
HStack {
Text(String(workoutSessions.count))
Button(action: {
// save(workoutSession: activeWorkoutSession)
}) {
Text("Save Session")
}
.buttonStyle(.borderedProminent)
}
}
}
// MARK: -- Workout Controls
Group {
if activeWorkoutSession?.workout != nil {
if isWorkingOut {
// MARK: -- Stop Workout
VStack {
ProgressView("", value: 10, total: 100)
TimerView(isActive: $isWorkingOut)
.font(.title)
.bold()
Button(action: {
isWorkingOut = false
activeWorkoutSession?.startWorkoutSession()
}) {
HStack {
Image(systemName: "stop.fill")
Text("Stop Workout")
}
}
.buttonStyle(.borderedProminent)
.bold()
.tint(.red)
}
} else {
// MARK: -- Start Workout
Button(action: {
isWorkingOut = true
activeWorkoutSession?.stopWorkoutSession()
}) {
HStack {
Image(systemName: "play.fill")
Text("Start Workout")
}
}
.buttonStyle(.borderedProminent)
.bold()
.tint(.green)
}
}
}
}
.navigationTitle("Workout Session")
.onAppear {
// Load the active workout session and workout onAppear
if let activeWorkoutSession = getItem(from: workoutSessions, by: activeWorkoutSessionId) {
self.activeWorkoutSession = activeWorkoutSession
if let workout = getItem(from: workouts, by: activeWorkoutId) {
self.activeWorkout = workout
}
} else {
createNewWorkoutSession()
}
}
}
private func elapsedTimeText(startDate: Date) -> String {
let elapsedTime = currentTime.timeIntervalSince(startDate)
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.hour, .minute, .second]
formatter.unitsStyle = .positional // For HH:mm:ss format
return formatter.string(from: elapsedTime) ?? "00:00:00"
private func createNewWorkoutSession() {
let newWorkoutSession = WorkoutSession()
activeWorkoutSessionId = newWorkoutSession.id.uuidString
if let selectedWorkout = getItem(from: workouts, by: activeWorkoutId) {
newWorkoutSession.workout = selectedWorkout
}
self.activeWorkoutSession = newWorkoutSession
}
private func getItem<Item: Nameable>(from array: [Item], by id: String) -> Item? {
let filteredItems = array.filter { $0.id == UUID(uuidString: id) }
return filteredItems.count == 1 ? filteredItems.first : nil
}
}
#Preview {
ActiveWorkoutSession(workout: Workout.sampleData)
NavigationStack {
ActiveWorkoutSession()
}
.modelContainer(SampleData.shared.modelContainer)
}
#Preview("No Workout Selected") {
NavigationStack {
ActiveWorkoutSession()
}
.onAppear {
Defaults.shared.isWorkingOut = false
}
.modelContainer(SampleData.shared.modelContainer)
}
#Preview("No Workout Data available") {
NavigationStack {
ActiveWorkoutSession()
}
}