From be56f475910deb504c60d9081cfe2eab8e970b97 Mon Sep 17 00:00:00 2001 From: oliverhnat Date: Sat, 23 Nov 2024 14:16:38 +0100 Subject: [PATCH] fix(core data): remove core data from the project --- StepMap/ContentView.swift | 71 +++---------------- StepMap/Persistence.swift | 57 --------------- .../StepMap.xcdatamodeld/.xccurrentversion | 8 --- .../StepMap.xcdatamodel/contents | 9 --- StepMap/StepMapApp.swift | 2 - 5 files changed, 11 insertions(+), 136 deletions(-) delete mode 100644 StepMap/Persistence.swift delete mode 100644 StepMap/StepMap.xcdatamodeld/.xccurrentversion delete mode 100644 StepMap/StepMap.xcdatamodeld/StepMap.xcdatamodel/contents diff --git a/StepMap/ContentView.swift b/StepMap/ContentView.swift index 401a3e7..7e6b4dd 100644 --- a/StepMap/ContentView.swift +++ b/StepMap/ContentView.swift @@ -9,78 +9,29 @@ import SwiftUI import CoreData struct ContentView: View { - @Environment(\.managedObjectContext) private var viewContext - - @FetchRequest( - sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)], - animation: .default) - private var items: FetchedResults + + // TODO: create a map + // Add navigation to the map + // Display the calculated distance and how long will it take by walking + // Get walkingStepLength from HealthKit + // Show how many steps does the route take + // Get walkingSpeed + // show how long does the route take with said walking speed var body: some View { NavigationView { List { - ForEach(items) { item in - NavigationLink { - Text("Item at \(item.timestamp!, formatter: itemFormatter)") - } label: { - Text(item.timestamp!, formatter: itemFormatter) - } - } - .onDelete(perform: deleteItems) + Text("HER") } .toolbar { ToolbarItem(placement: .navigationBarTrailing) { - EditButton() - } - ToolbarItem { - Button(action: addItem) { - Label("Add Item", systemImage: "plus") - } + Text("Edit") } } Text("Select an item") } } - - private func addItem() { - withAnimation { - let newItem = Item(context: viewContext) - newItem.timestamp = Date() - - do { - try viewContext.save() - } catch { - // Replace this implementation with code to handle the error appropriately. - // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. - let nsError = error as NSError - fatalError("Unresolved error \(nsError), \(nsError.userInfo)") - } - } - } - - private func deleteItems(offsets: IndexSet) { - withAnimation { - offsets.map { items[$0] }.forEach(viewContext.delete) - - do { - try viewContext.save() - } catch { - // Replace this implementation with code to handle the error appropriately. - // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. - let nsError = error as NSError - fatalError("Unresolved error \(nsError), \(nsError.userInfo)") - } - } - } } - -private let itemFormatter: DateFormatter = { - let formatter = DateFormatter() - formatter.dateStyle = .short - formatter.timeStyle = .medium - return formatter -}() - #Preview { - ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) + ContentView() } diff --git a/StepMap/Persistence.swift b/StepMap/Persistence.swift deleted file mode 100644 index e6efb32..0000000 --- a/StepMap/Persistence.swift +++ /dev/null @@ -1,57 +0,0 @@ -// -// Persistence.swift -// StepMap -// -// Created by Oliver Hnát on 23.11.2024. -// - -import CoreData - -struct PersistenceController { - static let shared = PersistenceController() - - @MainActor - static let preview: PersistenceController = { - let result = PersistenceController(inMemory: true) - let viewContext = result.container.viewContext - for _ in 0..<10 { - let newItem = Item(context: viewContext) - newItem.timestamp = Date() - } - do { - try viewContext.save() - } catch { - // Replace this implementation with code to handle the error appropriately. - // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. - let nsError = error as NSError - fatalError("Unresolved error \(nsError), \(nsError.userInfo)") - } - return result - }() - - let container: NSPersistentContainer - - init(inMemory: Bool = false) { - container = NSPersistentContainer(name: "StepMap") - if inMemory { - container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null") - } - container.loadPersistentStores(completionHandler: { (storeDescription, error) in - if let error = error as NSError? { - // Replace this implementation with code to handle the error appropriately. - // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. - - /* - Typical reasons for an error here include: - * The parent directory does not exist, cannot be created, or disallows writing. - * The persistent store is not accessible, due to permissions or data protection when the device is locked. - * The device is out of space. - * The store could not be migrated to the current model version. - Check the error message to determine what the actual problem was. - */ - fatalError("Unresolved error \(error), \(error.userInfo)") - } - }) - container.viewContext.automaticallyMergesChangesFromParent = true - } -} diff --git a/StepMap/StepMap.xcdatamodeld/.xccurrentversion b/StepMap/StepMap.xcdatamodeld/.xccurrentversion deleted file mode 100644 index 6347d4f..0000000 --- a/StepMap/StepMap.xcdatamodeld/.xccurrentversion +++ /dev/null @@ -1,8 +0,0 @@ - - - - - _XCCurrentVersionName - StepMap.xcdatamodel - - diff --git a/StepMap/StepMap.xcdatamodeld/StepMap.xcdatamodel/contents b/StepMap/StepMap.xcdatamodeld/StepMap.xcdatamodel/contents deleted file mode 100644 index 9ed2921..0000000 --- a/StepMap/StepMap.xcdatamodeld/StepMap.xcdatamodel/contents +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/StepMap/StepMapApp.swift b/StepMap/StepMapApp.swift index 9cfc6cf..96db13c 100644 --- a/StepMap/StepMapApp.swift +++ b/StepMap/StepMapApp.swift @@ -9,12 +9,10 @@ import SwiftUI @main struct StepMapApp: App { - let persistenceController = PersistenceController.shared var body: some Scene { WindowGroup { ContentView() - .environment(\.managedObjectContext, persistenceController.container.viewContext) } } }