diff --git a/DHBW-Service.xcodeproj/project.pbxproj b/DHBW-Service.xcodeproj/project.pbxproj index f239b18..dce52e3 100644 --- a/DHBW-Service.xcodeproj/project.pbxproj +++ b/DHBW-Service.xcodeproj/project.pbxproj @@ -18,6 +18,7 @@ CDCD721A25912E1200FBF2F5 /* HomeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDCD721925912E1200FBF2F5 /* HomeView.swift */; }; CDCD72242591316500FBF2F5 /* LocalSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDCD72232591316500FBF2F5 /* LocalSettings.swift */; }; CDCD7230259135C500FBF2F5 /* FirstOpeningSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDCD722F259135C500FBF2F5 /* FirstOpeningSettings.swift */; }; + CDDCF47B2591FE550027CDC5 /* UtilityFunctions.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDDCF47A2591FE550027CDC5 /* UtilityFunctions.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -55,6 +56,7 @@ CDCD721925912E1200FBF2F5 /* HomeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeView.swift; sourceTree = ""; }; CDCD72232591316500FBF2F5 /* LocalSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalSettings.swift; sourceTree = ""; }; CDCD722F259135C500FBF2F5 /* FirstOpeningSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FirstOpeningSettings.swift; sourceTree = ""; }; + CDDCF47A2591FE550027CDC5 /* UtilityFunctions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UtilityFunctions.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -107,6 +109,7 @@ children = ( CDCD720F25912D3C00FBF2F5 /* App */, CDCD721025912D4900FBF2F5 /* Views */, + CDDCF4792591FE410027CDC5 /* Utility */, CDCD72222591314000FBF2F5 /* Observables */, CDCD721125912D5400FBF2F5 /* CoreData */, CDCD721225912D6300FBF2F5 /* Supporting Files */, @@ -201,6 +204,14 @@ path = Other; sourceTree = ""; }; + CDDCF4792591FE410027CDC5 /* Utility */ = { + isa = PBXGroup; + children = ( + CDDCF47A2591FE550027CDC5 /* UtilityFunctions.swift */, + ); + path = Utility; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -336,6 +347,7 @@ CDCD72242591316500FBF2F5 /* LocalSettings.swift in Sources */, CD9FAB8D258EC60600D6D0C5 /* DHBW_Service.xcdatamodeld in Sources */, CDCD721A25912E1200FBF2F5 /* HomeView.swift in Sources */, + CDDCF47B2591FE550027CDC5 /* UtilityFunctions.swift in Sources */, CD9FAB81258EC60200D6D0C5 /* DHBW_ServiceApp.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -495,7 +507,7 @@ ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_STYLE = Automatic; DEVELOPMENT_ASSET_PATHS = "\"DHBW-Service/Preview Content\""; - DEVELOPMENT_TEAM = G3TRS8UHWN; + DEVELOPMENT_TEAM = HS7KNT4MZ2; ENABLE_PREVIEWS = YES; INFOPLIST_FILE = "DHBW-Service/Supporting Files/Info.plist"; IPHONEOS_DEPLOYMENT_TARGET = 14.0; @@ -517,7 +529,7 @@ ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_STYLE = Automatic; DEVELOPMENT_ASSET_PATHS = "\"DHBW-Service/Preview Content\""; - DEVELOPMENT_TEAM = G3TRS8UHWN; + DEVELOPMENT_TEAM = HS7KNT4MZ2; ENABLE_PREVIEWS = YES; INFOPLIST_FILE = "DHBW-Service/Supporting Files/Info.plist"; IPHONEOS_DEPLOYMENT_TARGET = 14.0; diff --git a/DHBW-Service/CoreData/Persistence.swift b/DHBW-Service/CoreData/Persistence.swift index 936bb2e..6bff296 100644 --- a/DHBW-Service/CoreData/Persistence.swift +++ b/DHBW-Service/CoreData/Persistence.swift @@ -51,7 +51,7 @@ struct PersistenceController { if self.context.hasChanges { do { try self.context.save() - print("In CoreData.stack.save()") + print("In PersistenceController.shared.save()") } catch { let nserror = error as NSError fatalError("Unresolved error \(nserror), \(nserror.userInfo)") diff --git a/DHBW-Service/Utility/UtilityFunctions.swift b/DHBW-Service/Utility/UtilityFunctions.swift new file mode 100644 index 0000000..f9de9cb --- /dev/null +++ b/DHBW-Service/Utility/UtilityFunctions.swift @@ -0,0 +1,42 @@ +// +// UtilityFunctions.swift +// DHBW-Service +// +// Created by Patrick Müller on 22.12.20. +// + +import Foundation +import CoreData + +class UtilityFunctions { + public class func getCoreDataObject(entity: String) -> [NSManagedObject]{ + let managedContext = + PersistenceController.shared.context + + let fetchRequest = + NSFetchRequest(entityName: entity) + + do { + return try managedContext.fetch(fetchRequest) + } catch let error as NSError { + print("Could not fetch. \(error), \(error.userInfo)") + return [] + } + } + + public class func deleteAllCoreDataEntitiesOfType(type: String) -> Bool{ + let fetchRequest: NSFetchRequest = NSFetchRequest(entityName: type) + let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest) + + do { + try PersistenceController.shared.context.execute(deleteRequest) + + PersistenceController.shared.save() + + return true + } catch let error as NSError { + print(error) + return false + } + } +} diff --git a/DHBW-Service/Views/Other/FirstOpeningSettings.swift b/DHBW-Service/Views/Other/FirstOpeningSettings.swift index 7869feb..2e1d4d6 100644 --- a/DHBW-Service/Views/Other/FirstOpeningSettings.swift +++ b/DHBW-Service/Views/Other/FirstOpeningSettings.swift @@ -34,6 +34,7 @@ struct FirstOpeningSettings: View { .padding(.horizontal) Button(action: { self.settings.isFirstOpening = !self.settings.isFirstOpening + self.saveToCoreData() }){ Text("Confirm") .padding() @@ -47,6 +48,11 @@ struct FirstOpeningSettings: View { extension FirstOpeningSettings{ func saveToCoreData(){ + // Delete old user data + let status = UtilityFunctions.deleteAllCoreDataEntitiesOfType(type: "User") + print("Deleting old user data status: \(status)") + + // Insert new user data let entity = NSEntityDescription.entity(forEntityName: "User", in: PersistenceController.shared.context)! let user = NSManagedObject(entity: entity, insertInto: PersistenceController.shared.context) user.setValue(name, forKey: "name") diff --git a/DHBW-Service/Views/Tabs/HomeView.swift b/DHBW-Service/Views/Tabs/HomeView.swift index 5a4b051..a9a9ecb 100644 --- a/DHBW-Service/Views/Tabs/HomeView.swift +++ b/DHBW-Service/Views/Tabs/HomeView.swift @@ -10,10 +10,7 @@ import CoreData struct HomeView: View { @EnvironmentObject var settings: LocalSettings - @State var user: NSManagedObject = NSManagedObject() - - - + @State private var name: String = "" var body: some View { VStack { @@ -24,7 +21,7 @@ struct HomeView: View { } Text("Test") -// Text(user.value(forKey: "name") as! String) + Text(self.name) }.onAppear{ self.readFromCoreData() @@ -33,15 +30,12 @@ struct HomeView: View { } extension HomeView{ - func readFromCoreData(){ - let managedContext = PersistenceController.shared.context - let fetchRequest = NSFetchRequest(entityName: "User") + func readFromCoreData() { + let fetchedData = UtilityFunctions.getCoreDataObject(entity: "User") - do { - try print(managedContext.fetch(fetchRequest)) - self.user = try managedContext.fetch(fetchRequest)[0] - } catch let error as NSError { - print(error) + if(!fetchedData.isEmpty) { + let user = fetchedData[0] + self.name = user.value(forKey: "name") as! String } } }