wip
This commit is contained in:
@ -9,10 +9,17 @@
|
||||
|
||||
import SwiftUI
|
||||
import SwiftData
|
||||
import CloudKit
|
||||
|
||||
enum AppStorageKeys {
|
||||
static let iCloudSyncEnabled = "iCloudSyncEnabled"
|
||||
}
|
||||
|
||||
struct SettingsView: View {
|
||||
@Environment(\.modelContext) private var modelContext
|
||||
|
||||
|
||||
@State private var showingPopulateData = false
|
||||
@State private var showingClearAllDataConfirmation = false
|
||||
|
||||
var splitsCount: Int? { try? modelContext.fetchCount(FetchDescriptor<Split>()) }
|
||||
var musclesCount: Int? { try? modelContext.fetchCount(FetchDescriptor<Muscle>()) }
|
||||
@ -70,10 +77,81 @@ struct SettingsView: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Section(header: Text("Developer")) {
|
||||
|
||||
Button(action: {
|
||||
showingPopulateData = true
|
||||
}) {
|
||||
HStack {
|
||||
Label("Populate Data", systemImage: "plus")
|
||||
Spacer()
|
||||
}
|
||||
}
|
||||
.confirmationDialog(
|
||||
"Populate Data?",
|
||||
isPresented: $showingPopulateData,
|
||||
titleVisibility: .hidden
|
||||
) {
|
||||
Button("Populate Data") {
|
||||
DataLoader.create(modelContext: modelContext)
|
||||
}
|
||||
Button("Cancel", role: .cancel) {}
|
||||
// } message: {
|
||||
// Text("This action cannot be undone. All data will be permanently deleted.")
|
||||
}
|
||||
|
||||
Button(action: {
|
||||
showingClearAllDataConfirmation = true
|
||||
}) {
|
||||
HStack {
|
||||
Label("Clear All Data", systemImage: "trash")
|
||||
Spacer()
|
||||
}
|
||||
}
|
||||
.foregroundColor(.red)
|
||||
.confirmationDialog(
|
||||
"Clear All Data?",
|
||||
isPresented: $showingClearAllDataConfirmation,
|
||||
titleVisibility: .visible
|
||||
) {
|
||||
Button("Clear All Data", role: .destructive) {
|
||||
clearAllData()
|
||||
}
|
||||
Button("Cancel", role: .cancel) {}
|
||||
} message: {
|
||||
Text("This action cannot be undone. All data will be permanently deleted.")
|
||||
}
|
||||
}
|
||||
}
|
||||
.navigationTitle("Settings")
|
||||
}
|
||||
}
|
||||
|
||||
func deleteAllObjects<T: PersistentModel>(ofType type: T.Type, from context: ModelContext) throws {
|
||||
let descriptor = FetchDescriptor<T>()
|
||||
let allObjects = try context.fetch(descriptor)
|
||||
for object in allObjects {
|
||||
context.delete(object)
|
||||
}
|
||||
try context.save()
|
||||
}
|
||||
|
||||
private func clearAllData () {
|
||||
do {
|
||||
try deleteAllObjects(ofType: ExerciseType.self, from: modelContext)
|
||||
try deleteAllObjects(ofType: Exercise.self, from: modelContext)
|
||||
try deleteAllObjects(ofType: Muscle.self, from: modelContext)
|
||||
try deleteAllObjects(ofType: MuscleGroup.self, from: modelContext)
|
||||
try deleteAllObjects(ofType: Split.self, from: modelContext)
|
||||
try deleteAllObjects(ofType: SplitExerciseAssignment.self, from: modelContext)
|
||||
try deleteAllObjects(ofType: Workout.self, from: modelContext)
|
||||
try deleteAllObjects(ofType: WorkoutLog.self, from: modelContext)
|
||||
try modelContext.save()
|
||||
} catch {
|
||||
print("Failed to clear all data: \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ExercisesListView: View {
|
||||
|
Reference in New Issue
Block a user