Adding preview content, improvements to RaPla stuff

- Now updating locally stored events instead of deleting them and creating completely new events
- Added text color in the list view to distinguish exams from lectures
This commit is contained in:
Patrick Müller 2021-01-31 19:49:52 +01:00 committed by Patrick Müller
parent 95f06cc87d
commit fdbd88ce01
4 changed files with 103 additions and 26 deletions

View File

@ -10,6 +10,7 @@
<attribute name="location" optional="YES" attributeType="String"/>
<attribute name="startDate" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="summary" optional="YES" attributeType="String"/>
<attribute name="uid" optional="YES" attributeType="String"/>
</entity>
<entity name="User" representedClassName="User" syncable="YES" codeGenerationType="class">
<attribute name="course" optional="YES" attributeType="String"/>
@ -18,7 +19,7 @@
</entity>
<elements>
<element name="Item" positionX="-63" positionY="-18" width="128" height="44"/>
<element name="RaPlaEvent" positionX="-63" positionY="9" width="128" height="134"/>
<element name="User" positionX="-63" positionY="-9" width="128" height="74"/>
<element name="RaPlaEvent" positionX="-63" positionY="9" width="128" height="119"/>
</elements>
</model>

View File

@ -65,12 +65,41 @@ struct PersistenceController {
let viewContext = result.container.viewContext
// set mock user
let entity = NSEntityDescription.entity(forEntityName: "User", in: PersistenceController.shared.context)!
let user = NSManagedObject(entity: entity, insertInto: PersistenceController.shared.context)
let userEntity = NSEntityDescription.entity(forEntityName: "User", in: PersistenceController.shared.context)!
let user = NSManagedObject(entity: userEntity, insertInto: PersistenceController.shared.context)
user.setValue("Max Mustermann", forKey: "name")
user.setValue("TINF19B4", forKey: "course")
user.setValue("Dr. Mustermann", forKey: "director")
// Generate mock events
let eventEntity = NSEntityDescription.entity(forEntityName: "RaPlaEvent", in: PersistenceController.shared.context)!
let normalEvent1 = NSManagedObject(entity: eventEntity, insertInto: PersistenceController.shared.context)
let normalEvent2 = NSManagedObject(entity: eventEntity, insertInto: PersistenceController.shared.context)
let examEvent = NSManagedObject(entity: eventEntity, insertInto: PersistenceController.shared.context)
normalEvent1.setValue("Mock Event 1", forKey: "summary")
normalEvent2.setValue("Mock Event 2", forKey: "summary")
examEvent.setValue("Exam Event", forKey: "summary")
normalEvent1.setValue("Mock Event 1 Description", forKey: "descr")
normalEvent2.setValue("Mock Event 2 Description", forKey: "descr")
examEvent.setValue("Exam Event Description", forKey: "descr")
normalEvent1.setValue("E207 INF Hörsaal", forKey: "location")
normalEvent2.setValue("A306 WI Hörsaal", forKey: "location")
examEvent.setValue("Audimax A", forKey: "location")
normalEvent1.setValue("Lehrveranstaltung", forKey: "category")
normalEvent2.setValue("Lehrveranstaltung", forKey: "category")
examEvent.setValue("Prüfung", forKey: "category")
var currentDate = Date()
currentDate.addTimeInterval(1*60*60);normalEvent1.setValue(currentDate, forKey: "startDate")
currentDate.addTimeInterval(1*60*60);normalEvent2.setValue(currentDate, forKey: "startDate")
currentDate.addTimeInterval(1*60*60);examEvent.setValue(currentDate, forKey: "startDate")
currentDate.addTimeInterval(1*60*60);normalEvent1.setValue(currentDate, forKey: "endDate")
currentDate.addTimeInterval(1*60*60);normalEvent2.setValue(currentDate, forKey: "endDate")
currentDate.addTimeInterval(1*60*60);examEvent.setValue(currentDate, forKey: "endDate")
normalEvent1.setValue("totalUniqueId1", forKey: "uid")
normalEvent2.setValue("totalUniqueId2", forKey: "uid")
examEvent.setValue("totalUniqueId3", forKey: "uid")
do {
try viewContext.save()
} catch {

View File

@ -16,6 +16,7 @@ class RaPlaFetcher {
var description: String = "" //DESCRIPTION
var location: String = "" //LOCATION
var category: String = "" //CATEGORIES
var uid: String = "" //UID
}
// Get the RaPla file from the given URL and save the events to CoreData
@ -104,6 +105,8 @@ class RaPlaFetcher {
evt.location = lineWithoutPrefix
} else if(line.hasPrefix("CATEGORIES")){
evt.category = lineWithoutPrefix
} else if(line.hasPrefix("UID")){
evt.uid = lineWithoutPrefix
}
}
@ -114,25 +117,34 @@ class RaPlaFetcher {
}
// Save the given iCalEvent objects to CoreData
// Updates the events if they already exist
private class func saveToCoreData(eventObjects: [iCalEvent]) -> Bool{
// Delete old data
if(UtilityFunctions.deleteAllCoreDataEntitiesOfType(type: "RaPlaEvent")){
let existingEvents = UtilityFunctions.getCoreDataObject(entity: "RaPlaEvent", sortDescriptors: [])
var existingEventsDict: [String:NSManagedObject] = [:]
for event in existingEvents {
existingEventsDict[event.value(forKey: "uid") as! String] = event
}
for event in eventObjects {
// If the event already exists locally, update it. Otherwise, create a new record
let evt: NSManagedObject
if existingEventsDict.keys.contains(event.uid) {
evt = existingEventsDict[event.uid]!
} else {
let entity = NSEntityDescription.entity(forEntityName: "RaPlaEvent", in: PersistenceController.shared.context)!
let evt = NSManagedObject(entity: entity, insertInto: PersistenceController.shared.context)
evt = NSManagedObject(entity: entity, insertInto: PersistenceController.shared.context)
}
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")
}
PersistenceController.shared.save()
return true
} else {
return false
}
}
}

View File

@ -10,13 +10,28 @@ import CoreData
struct LecturePlanList: View {
@State private var events: [NSManagedObject] = []
@State private var sortingAscending = true
var body: some View {
VStack {
Button(action: {
// This is obviously bullshit, but it could be used to sort afer summary or smth like that
self.sortingAscending = !self.sortingAscending
let sectionSortDescriptor = NSSortDescriptor(key: "startDate", ascending: sortingAscending)
let sortDescriptors = [sectionSortDescriptor]
self.events = UtilityFunctions.getCoreDataObject(entity: "RaPlaEvent", sortDescriptors: sortDescriptors)
}){
Text("Switch order")
}
List {
ForEach(events, id: \.self) { event in
HStack {
Text(formatDate(date: event.value(forKeyPath: "startDate") as! Date))
.foregroundColor(getEventForegroundColor(for: event))
Text(event.value(forKeyPath: "summary") as! String)
.foregroundColor(getEventForegroundColor(for: event))
}
}
}
}.onAppear{
@ -30,6 +45,15 @@ struct LecturePlanList: View {
struct LecturePlanList_Previews: PreviewProvider {
static var previews: some View {
LecturePlanList()
.preferredColorScheme(.dark)
.environmentObject(getFirstOpening())
.environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
}
static func getFirstOpening() -> LocalSettings {
let settings = LocalSettings();
settings.isFirstOpening = false;
return settings
}
}
@ -39,4 +63,15 @@ extension LecturePlanList {
formatter.dateStyle = .short
return formatter.string(from: date)
}
private func getEventForegroundColor(for event: NSManagedObject) -> Color {
var textColor: Color = .primary
if(event.value(forKeyPath: "category") as! String == "Prüfung") {
textColor = Color.red
} else {
textColor = Color.primary
}
return textColor
}
}