91 lines
2.4 KiB
Swift
91 lines
2.4 KiB
Swift
//
|
|
// ContentView.swift
|
|
// Workouts
|
|
//
|
|
// Created by rzen on 7/15/25 at 7:09 PM.
|
|
//
|
|
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
|
|
//
|
|
|
|
import SwiftUI
|
|
import SwiftData
|
|
|
|
struct ContentView: View {
|
|
@Environment(\.modelContext) private var modelContext
|
|
|
|
// Use string literal for completed status to avoid enum reference in predicate
|
|
// @Query(filter: #Predicate<Workout> { workout in
|
|
// workout.status?.rawValue != 3
|
|
// }, sort: \Workout.start, order: .reverse) var activeWorkouts: [Workout]
|
|
|
|
// @Query(sort: [SortDescriptor(\Workout.start)]) var allWorkouts: [Workout]
|
|
|
|
@State var activeWorkouts: [Workout] = []
|
|
@State var splits: [Split] = []
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
if activeWorkouts.isEmpty {
|
|
NoActiveWorkoutView()
|
|
} else {
|
|
ActiveWorkoutListView(workouts: activeWorkouts)
|
|
}
|
|
}
|
|
.onAppear {
|
|
loadSplits()
|
|
loadActiveWorkouts()
|
|
}
|
|
}
|
|
|
|
func loadActiveWorkouts () {
|
|
do {
|
|
print("loading active workouts")
|
|
self.activeWorkouts = try modelContext.fetch(FetchDescriptor<Workout>(
|
|
sortBy: [
|
|
SortDescriptor(\Workout.start)
|
|
]
|
|
))
|
|
print("loaded active workouts \(activeWorkouts.count)")
|
|
} catch {
|
|
print("ERROR: failed to load active workouts \(error)")
|
|
}
|
|
}
|
|
|
|
func loadSplits () {
|
|
do {
|
|
self.splits = try modelContext.fetch(FetchDescriptor<Split>(
|
|
sortBy: [
|
|
SortDescriptor(\Split.order),
|
|
SortDescriptor(\Split.name)
|
|
]
|
|
))
|
|
} catch {
|
|
print("ERROR: failed to load splits \(error)")
|
|
}
|
|
}
|
|
}
|
|
|
|
struct NoActiveWorkoutView: View {
|
|
var body: some View {
|
|
VStack(spacing: 16) {
|
|
Image(systemName: "dumbbell.fill")
|
|
.font(.system(size: 40))
|
|
.foregroundStyle(.gray)
|
|
|
|
Text("No Active Workout")
|
|
.font(.headline)
|
|
|
|
Text("Start a workout in the main app")
|
|
.font(.caption)
|
|
.foregroundStyle(.gray)
|
|
.multilineTextAlignment(.center)
|
|
}
|
|
.padding()
|
|
}
|
|
}
|
|
|
|
//#Preview {
|
|
// ContentView()
|
|
// .modelContainer(AppContainer.preview)
|
|
//}
|