71 lines
1.7 KiB
Swift
71 lines
1.7 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
|
|
|
|
@State var activeWorkouts: [Workout] = []
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
if activeWorkouts.isEmpty {
|
|
NoActiveWorkoutView()
|
|
} else {
|
|
ActiveWorkoutListView(workouts: activeWorkouts)
|
|
}
|
|
}
|
|
.onAppear {
|
|
loadActiveWorkouts()
|
|
}
|
|
}
|
|
|
|
func loadActiveWorkouts () {
|
|
let completedStatus = WorkoutStatus.completed.rawValue
|
|
do {
|
|
self.activeWorkouts = try modelContext.fetch(FetchDescriptor<Workout>(
|
|
predicate: #Predicate<Workout> { workout in
|
|
workout.status != completedStatus
|
|
},
|
|
sortBy: [
|
|
SortDescriptor(\Workout.start, order: .reverse)
|
|
]
|
|
))
|
|
} catch {
|
|
print("ERROR: failed to load active workouts \(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)
|
|
}
|