add Workout, Exercise and their Library, Add, and Details views

This commit is contained in:
Felix Förtsch
2024-08-19 15:33:32 +02:00
parent 97d4038fb6
commit 419e2bc699
19 changed files with 522 additions and 549 deletions
+48
View File
@@ -0,0 +1,48 @@
//
// SampleData.swift
// WorkoutsPlus
//
// Created by Felix Förtsch on 17.08.24.
//
import Foundation
import SwiftData
@MainActor // With your annotation, youre declaring that all code in this class must run on the main actor, including access to the mainContext property. Since all the SwiftUI code in an app runs on the main actor by default, youve satisfied the condition.
class SampleData {
static let shared = SampleData()
let modelContainer: ModelContainer
var context: ModelContext {
modelContainer.mainContext
}
private init() {
let schema = Schema([Exercise.self, Workout.self])
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: true)
do {
modelContainer = try ModelContainer(for: schema, configurations: [modelConfiguration])
insertSampleData()
} catch {
fatalError("Could not create ModelContainer: \(error)")
}
}
func insertSampleData() {
for exercise in Exercise.sampleData {
context.insert(exercise)
}
for workout in Workout.sampleData {
context.insert(workout)
}
do {
try context.save()
} catch {
print("Sample data context failed to save.")
}
}
}