mirror of
https://github.com/Mueller-Patrick/DHBW-Service-App.git
synced 2026-04-28 18:30:09 +00:00
:saprkles: Adding CoreData Helper Classes
- Also adding lecturer entity and relationship between event and lecturers. - Preparation for CoreData refactoring
This commit is contained in:
@@ -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[..<end])
|
||||
|
||||
let lecturerEmail = String(lineWithoutPrefix[String.Index(utf16Offset: 7, in: lineWithoutPrefix)..<lineWithoutPrefix.endIndex])
|
||||
|
||||
let lecturer = LecturerObj()
|
||||
lecturer.name = lecturerName
|
||||
lecturer.email = lecturerEmail
|
||||
|
||||
evt.lecturers.append(lecturer)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,8 +156,8 @@ class RaPlaFetcher {
|
||||
// Save the given iCalEvent objects to CoreData
|
||||
// Updates the events if they already exist and deletes old (/invalid) ones
|
||||
private class func saveToCoreData(eventObjects: [iCalEvent]) -> 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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user