From fdbd88ce01129021b277fcac4f19b5290f0a05e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Mu=CC=88ller?= Date: Sun, 31 Jan 2021 19:49:52 +0100 Subject: [PATCH] :zap: 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 --- .../DHBW_Service.xcdatamodel/contents | 3 +- DHBW-Service/CoreData/Persistence.swift | 33 ++++++++++++- DHBW-Service/Utility/RaPlaFetcher.swift | 48 ++++++++++++------- DHBW-Service/Views/Tabs/LecturePlanList.swift | 45 +++++++++++++++-- 4 files changed, 103 insertions(+), 26 deletions(-) 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 64c2386..7edf666 100644 --- a/DHBW-Service/CoreData/DHBW_Service.xcdatamodeld/DHBW_Service.xcdatamodel/contents +++ b/DHBW-Service/CoreData/DHBW_Service.xcdatamodeld/DHBW_Service.xcdatamodel/contents @@ -10,6 +10,7 @@ + @@ -18,7 +19,7 @@ + - \ No newline at end of file diff --git a/DHBW-Service/CoreData/Persistence.swift b/DHBW-Service/CoreData/Persistence.swift index 584fb73..8bb986c 100644 --- a/DHBW-Service/CoreData/Persistence.swift +++ b/DHBW-Service/CoreData/Persistence.swift @@ -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 { diff --git a/DHBW-Service/Utility/RaPlaFetcher.swift b/DHBW-Service/Utility/RaPlaFetcher.swift index f85bbbb..8536f7f 100644 --- a/DHBW-Service/Utility/RaPlaFetcher.swift +++ b/DHBW-Service/Utility/RaPlaFetcher.swift @@ -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")){ - for event in eventObjects { - let entity = NSEntityDescription.entity(forEntityName: "RaPlaEvent", in: PersistenceController.shared.context)! - let 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") - } - - PersistenceController.shared.save() - - return true - } else { - return false + 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)! + 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 } } diff --git a/DHBW-Service/Views/Tabs/LecturePlanList.swift b/DHBW-Service/Views/Tabs/LecturePlanList.swift index 27e100d..04e74e9 100644 --- a/DHBW-Service/Views/Tabs/LecturePlanList.swift +++ b/DHBW-Service/Views/Tabs/LecturePlanList.swift @@ -10,13 +10,28 @@ import CoreData struct LecturePlanList: View { @State private var events: [NSManagedObject] = [] + @State private var sortingAscending = true var body: some View { - List { - ForEach(events, id: \.self) { event in - HStack { - Text(formatDate(date: event.value(forKeyPath: "startDate") as! Date)) - Text(event.value(forKeyPath: "summary") as! String) + 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 + } }