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:
@@ -1,6 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="17709" systemVersion="20D62" minimumToolsVersion="Automatic" sourceLanguage="Swift" usedWithCloudKit="YES" userDefinedModelVersionIdentifier="">
|
||||
<entity name="RaPlaEvent" representedClassName="RaPlaEvent" syncable="YES" codeGenerationType="class">
|
||||
<entity name="Lecturer" representedClassName="Lecturer" syncable="YES">
|
||||
<attribute name="email" optional="YES" attributeType="String"/>
|
||||
<attribute name="name" optional="YES" attributeType="String"/>
|
||||
<relationship name="event" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="RaPlaEvent" inverseName="lecturers" inverseEntity="RaPlaEvent"/>
|
||||
</entity>
|
||||
<entity name="RaPlaEvent" representedClassName="RaPlaEvent" syncable="YES">
|
||||
<attribute name="category" optional="YES" attributeType="String"/>
|
||||
<attribute name="descr" optional="YES" attributeType="String"/>
|
||||
<attribute name="endDate" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
|
||||
@@ -9,14 +14,16 @@
|
||||
<attribute name="startDate" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
|
||||
<attribute name="summary" optional="YES" attributeType="String"/>
|
||||
<attribute name="uid" optional="YES" attributeType="String"/>
|
||||
<relationship name="lecturers" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="Lecturer" inverseName="event" inverseEntity="Lecturer"/>
|
||||
</entity>
|
||||
<entity name="User" representedClassName="User" syncable="YES" codeGenerationType="class">
|
||||
<entity name="User" representedClassName="User" syncable="YES">
|
||||
<attribute name="course" optional="YES" attributeType="String"/>
|
||||
<attribute name="director" optional="YES" attributeType="String"/>
|
||||
<attribute name="name" optional="YES" attributeType="String"/>
|
||||
</entity>
|
||||
<elements>
|
||||
<element name="RaPlaEvent" positionX="-63" positionY="9" width="128" height="149"/>
|
||||
<element name="User" positionX="-63" positionY="-9" width="128" height="74"/>
|
||||
<element name="Lecturer" positionX="-351" positionY="99" width="128" height="74"/>
|
||||
<element name="RaPlaEvent" positionX="-271.3642578125" positionY="83.64776611328125" width="128" height="164"/>
|
||||
<element name="User" positionX="-428.6358032226562" positionY="2.067169189453125" width="128" height="74"/>
|
||||
</elements>
|
||||
</model>
|
||||
@@ -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 []
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<Lecturer> {
|
||||
return NSFetchRequest<Lecturer>(entityName: "Lecturer")
|
||||
}
|
||||
|
||||
@NSManaged public var email: String?
|
||||
@NSManaged public var name: String?
|
||||
@NSManaged public var event: RaPlaEvent?
|
||||
|
||||
}
|
||||
|
||||
extension Lecturer : Identifiable {
|
||||
|
||||
}
|
||||
@@ -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 []
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<RaPlaEvent> {
|
||||
return NSFetchRequest<RaPlaEvent>(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 {
|
||||
|
||||
}
|
||||
@@ -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 []
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<User> {
|
||||
return NSFetchRequest<User>(entityName: "User")
|
||||
}
|
||||
|
||||
@NSManaged public var course: String?
|
||||
@NSManaged public var director: String?
|
||||
@NSManaged public var name: String?
|
||||
|
||||
}
|
||||
|
||||
extension User : Identifiable {
|
||||
|
||||
}
|
||||
@@ -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