From f50ba8f19d4dcbb41d957307b0db25af1887a507 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Mu=CC=88ller?= Date: Wed, 10 Feb 2021 19:17:11 +0100 Subject: [PATCH] :saprkles: Adding CoreData Helper Classes - Also adding lecturer entity and relationship between event and lecturers. - Preparation for CoreData refactoring --- DHBW-Service.xcodeproj/project.pbxproj | 32 +++++++ .../DHBW_Service.xcdatamodel/contents | 15 +++- .../Models/Lecturer+CoreDataClass.swift | 25 ++++++ .../Models/Lecturer+CoreDataProperties.swift | 27 ++++++ .../Models/RaPlaEvent+CoreDataClass.swift | 25 ++++++ .../RaPlaEvent+CoreDataProperties.swift | 50 +++++++++++ .../CoreData/Models/User+CoreDataClass.swift | 25 ++++++ .../Models/User+CoreDataProperties.swift | 27 ++++++ DHBW-Service/Utility/RaPlaFetcher.swift | 87 +++++++++++++++---- 9 files changed, 293 insertions(+), 20 deletions(-) create mode 100644 DHBW-Service/CoreData/Models/Lecturer+CoreDataClass.swift create mode 100644 DHBW-Service/CoreData/Models/Lecturer+CoreDataProperties.swift create mode 100644 DHBW-Service/CoreData/Models/RaPlaEvent+CoreDataClass.swift create mode 100644 DHBW-Service/CoreData/Models/RaPlaEvent+CoreDataProperties.swift create mode 100644 DHBW-Service/CoreData/Models/User+CoreDataClass.swift create mode 100644 DHBW-Service/CoreData/Models/User+CoreDataProperties.swift diff --git a/DHBW-Service.xcodeproj/project.pbxproj b/DHBW-Service.xcodeproj/project.pbxproj index 56c6f6a..2d2e9e2 100644 --- a/DHBW-Service.xcodeproj/project.pbxproj +++ b/DHBW-Service.xcodeproj/project.pbxproj @@ -12,6 +12,7 @@ CD2FC0C625A869FE00963178 /* dhbw-standard-icon@3x.png in Resources */ = {isa = PBXBuildFile; fileRef = CD2FC0C125A869FE00963178 /* dhbw-standard-icon@3x.png */; }; CD2FC0C725A869FE00963178 /* alpaca-alt-icon@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = CD2FC0C325A869FE00963178 /* alpaca-alt-icon@2x.png */; }; CD2FC0C825A869FE00963178 /* alpaca-alt-icon@3x.png in Resources */ = {isa = PBXBuildFile; fileRef = CD2FC0C425A869FE00963178 /* alpaca-alt-icon@3x.png */; }; + CD67FB2A25D45A3300069CDB /* RaPlaEvent+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD67FB2925D45A3300069CDB /* RaPlaEvent+CoreDataProperties.swift */; }; CD730A35259A860E00E0BB69 /* SettingsAcknowledgements.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD730A34259A860E00E0BB69 /* SettingsAcknowledgements.swift */; }; CD8555BE25C47AE500C4ACD6 /* RaPlaFetcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD8555BD25C47AE500C4ACD6 /* RaPlaFetcher.swift */; }; CD8555C325C47B5300C4ACD6 /* ApiService.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD8555C225C47B5300C4ACD6 /* ApiService.swift */; }; @@ -23,10 +24,15 @@ CD9FAB8D258EC60600D6D0C5 /* DHBW_Service.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = CD9FAB8B258EC60600D6D0C5 /* DHBW_Service.xcdatamodeld */; }; CD9FAB98258EC60600D6D0C5 /* DHBW_ServiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD9FAB97258EC60600D6D0C5 /* DHBW_ServiceTests.swift */; }; CD9FABA3258EC60600D6D0C5 /* DHBW_ServiceUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD9FABA2258EC60600D6D0C5 /* DHBW_ServiceUITests.swift */; }; + CDA1CBAE25D4591000DB2AE5 /* User+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDA1CBA825D4591000DB2AE5 /* User+CoreDataProperties.swift */; }; + CDA1CBB025D4591000DB2AE5 /* Lecturer+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDA1CBAA25D4591000DB2AE5 /* Lecturer+CoreDataProperties.swift */; }; 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 */; }; CDD39B4B259A64150078D05F /* SettingsMain.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDD39B4A259A64150078D05F /* SettingsMain.swift */; }; + CDD970DD25D453D90061755E /* User+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDD970D725D453D90061755E /* User+CoreDataClass.swift */; }; + CDD970DF25D453D90061755E /* Lecturer+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDD970D925D453D90061755E /* Lecturer+CoreDataClass.swift */; }; + CDD970E125D453D90061755E /* RaPlaEvent+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDD970DB25D453D90061755E /* RaPlaEvent+CoreDataClass.swift */; }; CDDCF47B2591FE550027CDC5 /* UtilityFunctions.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDDCF47A2591FE550027CDC5 /* UtilityFunctions.swift */; }; CDDCF4842592028A0027CDC5 /* Localizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDDCF4832592028A0027CDC5 /* Localizer.swift */; }; CDDCF493259203390027CDC5 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = CDDCF495259203390027CDC5 /* Localizable.strings */; }; @@ -59,6 +65,7 @@ CD2FC0C125A869FE00963178 /* dhbw-standard-icon@3x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "dhbw-standard-icon@3x.png"; sourceTree = ""; }; CD2FC0C325A869FE00963178 /* alpaca-alt-icon@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "alpaca-alt-icon@2x.png"; sourceTree = ""; }; CD2FC0C425A869FE00963178 /* alpaca-alt-icon@3x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "alpaca-alt-icon@3x.png"; sourceTree = ""; }; + CD67FB2925D45A3300069CDB /* RaPlaEvent+CoreDataProperties.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "RaPlaEvent+CoreDataProperties.swift"; sourceTree = ""; }; CD730A34259A860E00E0BB69 /* SettingsAcknowledgements.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsAcknowledgements.swift; sourceTree = ""; }; CD8555BD25C47AE500C4ACD6 /* RaPlaFetcher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RaPlaFetcher.swift; sourceTree = ""; }; CD8555C225C47B5300C4ACD6 /* ApiService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApiService.swift; sourceTree = ""; }; @@ -76,10 +83,15 @@ CD9FAB9E258EC60600D6D0C5 /* DHBW-ServiceUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "DHBW-ServiceUITests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; CD9FABA2258EC60600D6D0C5 /* DHBW_ServiceUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DHBW_ServiceUITests.swift; sourceTree = ""; }; CD9FABA4258EC60600D6D0C5 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + CDA1CBA825D4591000DB2AE5 /* User+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = "User+CoreDataProperties.swift"; path = "DHBW-Service/CoreData/Models/User+CoreDataProperties.swift"; sourceTree = SOURCE_ROOT; }; + CDA1CBAA25D4591000DB2AE5 /* Lecturer+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = "Lecturer+CoreDataProperties.swift"; path = "DHBW-Service/CoreData/Models/Lecturer+CoreDataProperties.swift"; sourceTree = SOURCE_ROOT; }; 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 = ""; }; CDD39B4A259A64150078D05F /* SettingsMain.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsMain.swift; sourceTree = ""; }; + CDD970D725D453D90061755E /* User+CoreDataClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "User+CoreDataClass.swift"; sourceTree = ""; }; + CDD970D925D453D90061755E /* Lecturer+CoreDataClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Lecturer+CoreDataClass.swift"; sourceTree = ""; }; + CDD970DB25D453D90061755E /* RaPlaEvent+CoreDataClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RaPlaEvent+CoreDataClass.swift"; sourceTree = ""; }; CDDCF47A2591FE550027CDC5 /* UtilityFunctions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UtilityFunctions.swift; sourceTree = ""; }; CDDCF4832592028A0027CDC5 /* Localizer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Localizer.swift; sourceTree = ""; }; CDDCF494259203390027CDC5 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = ""; }; @@ -115,6 +127,19 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + CD1ED26325D33B590030B647 /* Models */ = { + isa = PBXGroup; + children = ( + CDD970D925D453D90061755E /* Lecturer+CoreDataClass.swift */, + CDA1CBAA25D4591000DB2AE5 /* Lecturer+CoreDataProperties.swift */, + CDD970DB25D453D90061755E /* RaPlaEvent+CoreDataClass.swift */, + CD67FB2925D45A3300069CDB /* RaPlaEvent+CoreDataProperties.swift */, + CDD970D725D453D90061755E /* User+CoreDataClass.swift */, + CDA1CBA825D4591000DB2AE5 /* User+CoreDataProperties.swift */, + ); + path = Models; + sourceTree = ""; + }; CD2FC0BD25A869F000963178 /* res */ = { isa = PBXGroup; children = ( @@ -239,6 +264,7 @@ CDCD721125912D5400FBF2F5 /* CoreData */ = { isa = PBXGroup; children = ( + CD1ED26325D33B590030B647 /* Models */, CD9FAB8B258EC60600D6D0C5 /* DHBW_Service.xcdatamodeld */, CD9FAB89258EC60600D6D0C5 /* Persistence.swift */, ); @@ -443,19 +469,25 @@ buildActionMask = 2147483647; files = ( CDDCF4842592028A0027CDC5 /* Localizer.swift in Sources */, + CDD970DF25D453D90061755E /* Lecturer+CoreDataClass.swift in Sources */, CDCD7230259135C500FBF2F5 /* FirstOpeningSettings.swift in Sources */, + CDA1CBB025D4591000DB2AE5 /* Lecturer+CoreDataProperties.swift in Sources */, CD9FAB8A258EC60600D6D0C5 /* Persistence.swift in Sources */, CD730A35259A860E00E0BB69 /* SettingsAcknowledgements.swift in Sources */, CD9FAB83258EC60200D6D0C5 /* ContentView.swift in Sources */, CDCD72242591316500FBF2F5 /* LocalSettings.swift in Sources */, + CDD970E125D453D90061755E /* RaPlaEvent+CoreDataClass.swift in Sources */, + CDD970DD25D453D90061755E /* User+CoreDataClass.swift in Sources */, CD8555BE25C47AE500C4ACD6 /* RaPlaFetcher.swift in Sources */, CDEA70C025C85999001CFE28 /* LecturePlanItem.swift in Sources */, CD8555C325C47B5300C4ACD6 /* ApiService.swift in Sources */, CD9FAB8D258EC60600D6D0C5 /* DHBW_Service.xcdatamodeld in Sources */, CDCD721A25912E1200FBF2F5 /* HomeView.swift in Sources */, CDD39B4B259A64150078D05F /* SettingsMain.swift in Sources */, + CD67FB2A25D45A3300069CDB /* RaPlaEvent+CoreDataProperties.swift in Sources */, CDEA70B225C6054F001CFE28 /* LecturePlanList.swift in Sources */, CDDCF47B2591FE550027CDC5 /* UtilityFunctions.swift in Sources */, + CDA1CBAE25D4591000DB2AE5 /* User+CoreDataProperties.swift in Sources */, CD9FAB81258EC60200D6D0C5 /* DHBW_ServiceApp.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/DHBW-Service/CoreData/DHBW_Service.xcdatamodeld/DHBW_Service.xcdatamodel/contents b/DHBW-Service/CoreData/DHBW_Service.xcdatamodeld/DHBW_Service.xcdatamodel/contents index 14f0f3f..db914b6 100644 --- a/DHBW-Service/CoreData/DHBW_Service.xcdatamodeld/DHBW_Service.xcdatamodel/contents +++ b/DHBW-Service/CoreData/DHBW_Service.xcdatamodeld/DHBW_Service.xcdatamodel/contents @@ -1,6 +1,11 @@ - + + + + + + @@ -9,14 +14,16 @@ + - + - - + + + \ No newline at end of file diff --git a/DHBW-Service/CoreData/Models/Lecturer+CoreDataClass.swift b/DHBW-Service/CoreData/Models/Lecturer+CoreDataClass.swift new file mode 100644 index 0000000..22102df --- /dev/null +++ b/DHBW-Service/CoreData/Models/Lecturer+CoreDataClass.swift @@ -0,0 +1,25 @@ +// +// Lecturer+CoreDataClass.swift +// DHBW-Service +// +// Created by Patrick Müller on 10.02.21. +// +// + +import Foundation +import CoreData + +@objc(Lecturer) +public class Lecturer: NSManagedObject { + @nonobjc public class func getAll() -> [Lecturer] { + let managedContext = + PersistenceController.shared.context + + do { + return try managedContext.fetch(Lecturer.fetchRequest()) + } catch let error as NSError { + print("Could not fetch. \(error), \(error.userInfo)") + return [] + } + } +} diff --git a/DHBW-Service/CoreData/Models/Lecturer+CoreDataProperties.swift b/DHBW-Service/CoreData/Models/Lecturer+CoreDataProperties.swift new file mode 100644 index 0000000..e26c9e0 --- /dev/null +++ b/DHBW-Service/CoreData/Models/Lecturer+CoreDataProperties.swift @@ -0,0 +1,27 @@ +// +// Lecturer+CoreDataProperties.swift +// DHBW-Service +// +// Created by Patrick Müller on 10.02.21. +// +// + +import Foundation +import CoreData + + +extension Lecturer { + + @nonobjc public class func fetchRequest() -> NSFetchRequest { + return NSFetchRequest(entityName: "Lecturer") + } + + @NSManaged public var email: String? + @NSManaged public var name: String? + @NSManaged public var event: RaPlaEvent? + +} + +extension Lecturer : Identifiable { + +} diff --git a/DHBW-Service/CoreData/Models/RaPlaEvent+CoreDataClass.swift b/DHBW-Service/CoreData/Models/RaPlaEvent+CoreDataClass.swift new file mode 100644 index 0000000..3a56ebc --- /dev/null +++ b/DHBW-Service/CoreData/Models/RaPlaEvent+CoreDataClass.swift @@ -0,0 +1,25 @@ +// +// RaPlaEvent+CoreDataClass.swift +// DHBW-Service +// +// Created by Patrick Müller on 10.02.21. +// +// + +import Foundation +import CoreData + +@objc(RaPlaEvent) +public class RaPlaEvent: NSManagedObject { + @nonobjc public class func getAll() -> [RaPlaEvent] { + let managedContext = + PersistenceController.shared.context + + do { + return try managedContext.fetch(RaPlaEvent.fetchRequest()) + } catch let error as NSError { + print("Could not fetch. \(error), \(error.userInfo)") + return [] + } + } +} diff --git a/DHBW-Service/CoreData/Models/RaPlaEvent+CoreDataProperties.swift b/DHBW-Service/CoreData/Models/RaPlaEvent+CoreDataProperties.swift new file mode 100644 index 0000000..86b47cd --- /dev/null +++ b/DHBW-Service/CoreData/Models/RaPlaEvent+CoreDataProperties.swift @@ -0,0 +1,50 @@ +// +// RaPlaEvent+CoreDataProperties.swift +// DHBW-Service +// +// Created by Patrick Müller on 10.02.21. +// +// + +import Foundation +import CoreData + + +extension RaPlaEvent { + + @nonobjc public class func fetchRequest() -> NSFetchRequest { + return NSFetchRequest(entityName: "RaPlaEvent") + } + + @NSManaged public var category: String? + @NSManaged public var descr: String? + @NSManaged public var endDate: Date? + @NSManaged public var isHidden: Bool + @NSManaged public var location: String? + @NSManaged public var startDate: Date? + @NSManaged public var summary: String? + @NSManaged public var uid: String? + @NSManaged public var lecturers: NSSet? + +} + +// MARK: Generated accessors for lecturers +extension RaPlaEvent { + + @objc(addLecturersObject:) + @NSManaged public func addToLecturers(_ value: Lecturer) + + @objc(removeLecturersObject:) + @NSManaged public func removeFromLecturers(_ value: Lecturer) + + @objc(addLecturers:) + @NSManaged public func addToLecturers(_ values: NSSet) + + @objc(removeLecturers:) + @NSManaged public func removeFromLecturers(_ values: NSSet) + +} + +extension RaPlaEvent : Identifiable { + +} diff --git a/DHBW-Service/CoreData/Models/User+CoreDataClass.swift b/DHBW-Service/CoreData/Models/User+CoreDataClass.swift new file mode 100644 index 0000000..0018b5a --- /dev/null +++ b/DHBW-Service/CoreData/Models/User+CoreDataClass.swift @@ -0,0 +1,25 @@ +// +// User+CoreDataClass.swift +// DHBW-Service +// +// Created by Patrick Müller on 10.02.21. +// +// + +import Foundation +import CoreData + +@objc(User) +public class User: NSManagedObject { + @nonobjc public class func getAll() -> [User] { + let managedContext = + PersistenceController.shared.context + + do { + return try managedContext.fetch(User.fetchRequest()) + } catch let error as NSError { + print("Could not fetch. \(error), \(error.userInfo)") + return [] + } + } +} diff --git a/DHBW-Service/CoreData/Models/User+CoreDataProperties.swift b/DHBW-Service/CoreData/Models/User+CoreDataProperties.swift new file mode 100644 index 0000000..37130aa --- /dev/null +++ b/DHBW-Service/CoreData/Models/User+CoreDataProperties.swift @@ -0,0 +1,27 @@ +// +// User+CoreDataProperties.swift +// DHBW-Service +// +// Created by Patrick Müller on 10.02.21. +// +// + +import Foundation +import CoreData + + +extension User { + + @nonobjc public class func fetchRequest() -> NSFetchRequest { + return NSFetchRequest(entityName: "User") + } + + @NSManaged public var course: String? + @NSManaged public var director: String? + @NSManaged public var name: String? + +} + +extension User : Identifiable { + +} diff --git a/DHBW-Service/Utility/RaPlaFetcher.swift b/DHBW-Service/Utility/RaPlaFetcher.swift index 287de1c..d3d563b 100644 --- a/DHBW-Service/Utility/RaPlaFetcher.swift +++ b/DHBW-Service/Utility/RaPlaFetcher.swift @@ -17,6 +17,12 @@ class RaPlaFetcher { var location: String = "" //LOCATION var category: String = "" //CATEGORIES var uid: String = "" //UID + var lecturers: [LecturerObj] = [] //ATTENDEE + } + + public class LecturerObj { + var name: String = "" + var email: String = "" } // Get the RaPla file from the given URL and save the events to CoreData @@ -68,9 +74,24 @@ class RaPlaFetcher { var events: [iCalEvent] = [] for eventString in eventStrings { - let lines = eventString.components(separatedBy: .newlines) + var lines = eventString.components(separatedBy: .newlines) + // Remove all blank lines that somehow are generated by the .components function + lines = removeBlankLines(lines: lines) + let evt = iCalEvent() + // Iterate over all lines and merge lines that have been split by rapla first + for lineNr in 0...lines.count-1 { + if(lines[lineNr].hasPrefix(" ")){ + lines[lineNr] = String(lines[lineNr].dropFirst()) + lines[lineNr-1].append(lines[lineNr]) + lines[lineNr] = "" + } + } + + // Remove all blank lines again as we produced some while merging the lines + lines = removeBlankLines(lines: lines) + for line in lines { var lineWithoutPrefix = line if(!line.contains(":")) { @@ -82,7 +103,7 @@ class RaPlaFetcher { //Date format: 20181101T080000 let dateFormatter = DateFormatter() if(lineWithoutPrefix.contains("Z")){ - dateFormatter.dateFormat = "yyyyMMdd'T'HHmmssZ" + dateFormatter.dateFormat = "yyyyMMdd'T'HHmmss'Z'" } else { dateFormatter.dateFormat = "yyyyMMdd'T'HHmmss" } @@ -91,7 +112,7 @@ class RaPlaFetcher { } else if(line.hasPrefix("DTEND")){ let dateFormatter = DateFormatter() if(lineWithoutPrefix.contains("Z")){ - dateFormatter.dateFormat = "yyyyMMdd'T'HHmmssZ" + dateFormatter.dateFormat = "yyyyMMdd'T'HHmmss'Z'" } else { dateFormatter.dateFormat = "yyyyMMdd'T'HHmmss" } @@ -107,6 +128,22 @@ class RaPlaFetcher { evt.category = lineWithoutPrefix } else if(line.hasPrefix("UID")){ evt.uid = lineWithoutPrefix + } else if(line.hasPrefix("ATTENDEE;ROLE=REQ-PARTICIPANT;")) { + var lecturerName = line + + let begin = lecturerName.firstIndex(of: "\"")! + lecturerName.removeSubrange(lecturerName.startIndex...begin) + + let end = lecturerName.lastIndex(of: "\"")! + lecturerName = String(lecturerName[.. Bool{ - let existingEvents = UtilityFunctions.getCoreDataObject(entity: "RaPlaEvent", sortDescriptors: []) - var existingEventsDict: [String:NSManagedObject] = [:] + let existingEvents: [RaPlaEvent] = [] //RaPlaEvent.getAll() + var existingEventsDict: [String:RaPlaEvent] = [:] for event in existingEvents { existingEventsDict[event.value(forKey: "uid") as! String] = event } @@ -128,23 +165,28 @@ class RaPlaFetcher { for event in eventObjects { // If the event already exists locally, update it. Otherwise, create a new record - let evt: NSManagedObject + let evt: RaPlaEvent if existingEventsDict.keys.contains(event.uid) { evt = existingEventsDict[event.uid]! } else { - let entity = NSEntityDescription.entity(forEntityName: "RaPlaEvent", in: PersistenceController.shared.context)! - evt = NSManagedObject(entity: entity, insertInto: PersistenceController.shared.context) + evt = RaPlaEvent(context: PersistenceController.shared.context) // Set default values for new object - evt.setValue(false, forKey: "isHidden") + evt.isHidden = false + } + evt.startDate = event.startDate + evt.endDate = event.endDate + evt.summary = event.summary + evt.descr = event.description + evt.location = event.location + evt.category = event.category + evt.uid = event.uid + for lecturer in event.lecturers { + let lect = Lecturer(context: PersistenceController.shared.context) + lect.name = lecturer.name + lect.email = lecturer.email + lect.event = evt } - evt.setValue(event.startDate, forKey: "startDate") - evt.setValue(event.endDate, forKey: "endDate") - evt.setValue(event.summary, forKey: "summary") - evt.setValue(event.description, forKey: "descr") - evt.setValue(event.location, forKey: "location") - evt.setValue(event.category, forKey: "category") - evt.setValue(event.uid, forKey: "uid") } // Now we also have to delete locally stored events that have been deleted from RaPla @@ -160,4 +202,17 @@ class RaPlaFetcher { return true } + + private class func removeBlankLines(lines: [String]) -> [String] { + var newLines = lines + + // Remove all blank lines that somehow are generated by the .components function + for line in newLines { + if(line.isEmpty){ + newLines.remove(at: newLines.firstIndex(of: line)!) + } + } + + return newLines + } }