♻️ Refactored CoreData access

This commit is contained in:
Patrick Müller 2021-02-10 20:26:53 +01:00 committed by Patrick Müller
parent f50ba8f19d
commit b689a7bbf1
10 changed files with 132 additions and 72 deletions

View File

@ -22,4 +22,23 @@ public class Lecturer: NSManagedObject {
return [] return []
} }
} }
@nonobjc public class func getSpecified(sortDescriptors: [NSSortDescriptor] = [], searchPredicate: NSPredicate? = nil) -> [Lecturer]{
let managedContext =
PersistenceController.shared.context
let fetchRequest: NSFetchRequest = Lecturer.fetchRequest()
fetchRequest.sortDescriptors = sortDescriptors
if(searchPredicate != nil) {
fetchRequest.predicate = searchPredicate
}
do {
return try managedContext.fetch(fetchRequest)
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
return []
}
}
} }

View File

@ -22,4 +22,23 @@ public class RaPlaEvent: NSManagedObject {
return [] return []
} }
} }
@nonobjc public class func getSpecified(sortDescriptors: [NSSortDescriptor] = [], searchPredicate: NSPredicate? = nil) -> [RaPlaEvent]{
let managedContext =
PersistenceController.shared.context
let fetchRequest: NSFetchRequest = RaPlaEvent.fetchRequest()
fetchRequest.sortDescriptors = sortDescriptors
if(searchPredicate != nil) {
fetchRequest.predicate = searchPredicate
}
do {
return try managedContext.fetch(fetchRequest)
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
return []
}
}
} }

View File

@ -22,4 +22,23 @@ public class User: NSManagedObject {
return [] return []
} }
} }
@nonobjc public class func getSpecified(sortDescriptors: [NSSortDescriptor] = [], searchPredicate: NSPredicate? = nil) -> [User]{
let managedContext =
PersistenceController.shared.context
let fetchRequest: NSFetchRequest = User.fetchRequest()
fetchRequest.sortDescriptors = sortDescriptors
if(searchPredicate != nil) {
fetchRequest.predicate = searchPredicate
}
do {
return try managedContext.fetch(fetchRequest)
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
return []
}
}
} }

View File

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

View File

@ -9,6 +9,7 @@ import Foundation
import CoreData import CoreData
class UtilityFunctions { class UtilityFunctions {
// DEPRECATED, replaced by the respective getSpecified() method of each CoreData object
public class func getCoreDataObject(entity: String, sortDescriptors: [NSSortDescriptor] = [], searchPredicate: NSPredicate? = nil) -> [NSManagedObject]{ public class func getCoreDataObject(entity: String, sortDescriptors: [NSSortDescriptor] = [], searchPredicate: NSPredicate? = nil) -> [NSManagedObject]{
let managedContext = let managedContext =
PersistenceController.shared.context PersistenceController.shared.context

View File

@ -47,7 +47,7 @@ struct ContentView: View {
} }
.onAppear{ .onAppear{
// Called upon the opening of the app // Called upon the opening of the app
RaPlaFetcher.getRaplaFileAndSaveToCoreData(from: "https://rapla.dhbw-karlsruhe.de/rapla?page=ical&user=eisenbiegler&file=TINF19B4") RaPlaFetcher.getRaplaFileAndSaveToCoreData(from: "https://rapla.dhbw-karlsruhe.de/rapla?page=ical&user=eisenbiegler&file=TINF20B4")
} }
} }
} }

View File

@ -96,11 +96,10 @@ extension FirstOpeningSettings{
print("Deleting old user data status: \(status)") print("Deleting old user data status: \(status)")
// Insert new user data // Insert new user data
let entity = NSEntityDescription.entity(forEntityName: "User", in: PersistenceController.shared.context)! let user = User(context: PersistenceController.shared.context)
let user = NSManagedObject(entity: entity, insertInto: PersistenceController.shared.context) user.name = name
user.setValue(name, forKey: "name") user.course = course
user.setValue(course, forKey: "course") user.director = director
user.setValue(director, forKey: "director")
self.settings.isFirstOpening = !self.settings.isFirstOpening self.settings.isFirstOpening = !self.settings.isFirstOpening
PersistenceController.shared.save() PersistenceController.shared.save()

View File

@ -13,9 +13,9 @@ struct HomeView: View {
@State private var name: String = "" @State private var name: String = ""
@State private var course: String = "" @State private var course: String = ""
@State private var director: String = "" @State private var director: String = ""
@State private var todaysEvents: [NSManagedObject] = [] @State private var todaysEvents: [RaPlaEvent] = []
@State private var tomorrowsEvents: [NSManagedObject] = [] @State private var tomorrowsEvents: [RaPlaEvent] = []
@State private var upcomingExams: [NSManagedObject] = [] @State private var upcomingExams: [RaPlaEvent] = []
var body: some View { var body: some View {
NavigationView { NavigationView {
@ -115,25 +115,25 @@ struct HomeView: View {
extension HomeView{ extension HomeView{
// Read required data from CoreData and save it to the appropriate variables // Read required data from CoreData and save it to the appropriate variables
func readFromCoreData() { func readFromCoreData() {
let fetchedData = UtilityFunctions.getCoreDataObject(entity: "User") let fetchedData = User.getAll()
if(!fetchedData.isEmpty) { if(!fetchedData.isEmpty) {
let user = fetchedData[0] let user = fetchedData[0]
self.name = user.value(forKey: "name") as! String self.name = user.name!
self.course = user.value(forKey: "course") as! String self.course = user.course!
self.director = user.value(forKey: "director") as! String self.director = user.director!
} }
} }
// Get 0...2 of todays lectures from RaPla // Get 0...2 of todays lectures from RaPla
// Returns a list of RaPlaEvent NSManagedObjects // Returns a list of RaPlaEvent NSManagedObjects
func getTodaysEvents() -> [NSManagedObject] { func getTodaysEvents() -> [RaPlaEvent] {
let searchPredicate = NSPredicate(format: "(category == 'Lehrveranstaltung')") let searchPredicate = NSPredicate(format: "(category == 'Lehrveranstaltung')")
let hiddenPredicate = NSPredicate(format: "isHidden == NO") let hiddenPredicate = NSPredicate(format: "isHidden == NO")
var predicates = [searchPredicate, hiddenPredicate] var predicates = [searchPredicate, hiddenPredicate]
predicates.append(contentsOf: getDayPredicates(today: true)) predicates.append(contentsOf: getDayPredicates(today: true))
let compoundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: predicates) let compoundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: predicates)
let events = UtilityFunctions.getCoreDataObject(entity: "RaPlaEvent", searchPredicate: compoundPredicate) let events = RaPlaEvent.getSpecified(searchPredicate: compoundPredicate)
if(!events.isEmpty) { if(!events.isEmpty) {
return Array(events[...min(1, events.count-1)]) return Array(events[...min(1, events.count-1)])
} else { } else {
@ -143,13 +143,13 @@ extension HomeView{
// Get 0...2 of tomorrows lectures from RaPla // Get 0...2 of tomorrows lectures from RaPla
// Returns a list of RaPlaEvent NSManagedObjects // Returns a list of RaPlaEvent NSManagedObjects
func getTomorrowsEvents() -> [NSManagedObject] { func getTomorrowsEvents() -> [RaPlaEvent] {
let searchPredicate = NSPredicate(format: "(category == 'Lehrveranstaltung')") let searchPredicate = NSPredicate(format: "(category == 'Lehrveranstaltung')")
let hiddenPredicate = NSPredicate(format: "isHidden == NO") let hiddenPredicate = NSPredicate(format: "isHidden == NO")
var predicates = [searchPredicate, hiddenPredicate] var predicates = [searchPredicate, hiddenPredicate]
predicates.append(contentsOf: getDayPredicates(tomorrow: true)) predicates.append(contentsOf: getDayPredicates(tomorrow: true))
let compoundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: predicates) let compoundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: predicates)
let events = UtilityFunctions.getCoreDataObject(entity: "RaPlaEvent", searchPredicate: compoundPredicate) let events = RaPlaEvent.getSpecified(searchPredicate: compoundPredicate)
if(!events.isEmpty) { if(!events.isEmpty) {
return Array(events[...min(1, events.count-1)]) return Array(events[...min(1, events.count-1)])
} else { } else {
@ -159,13 +159,13 @@ extension HomeView{
// Get 0...2 of upcoming exams from RaPla // Get 0...2 of upcoming exams from RaPla
// Returns a list of RaPlaEvent NSManagedObjects // Returns a list of RaPlaEvent NSManagedObjects
func getUpcomingExams() -> [NSManagedObject] { func getUpcomingExams() -> [RaPlaEvent] {
let searchPredicate = NSPredicate(format: "category == %@", "Prüfung") let searchPredicate = NSPredicate(format: "category == %@", "Prüfung")
let hiddenPredicate = NSPredicate(format: "isHidden == NO") let hiddenPredicate = NSPredicate(format: "isHidden == NO")
let compoundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [searchPredicate, hiddenPredicate]) let compoundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [searchPredicate, hiddenPredicate])
let sectionSortDescriptor = NSSortDescriptor(key: "startDate", ascending: true) let sectionSortDescriptor = NSSortDescriptor(key: "startDate", ascending: true)
let sortDescriptors = [sectionSortDescriptor] let sortDescriptors = [sectionSortDescriptor]
let events = UtilityFunctions.getCoreDataObject(entity: "RaPlaEvent", sortDescriptors: sortDescriptors, searchPredicate: compoundPredicate) let events = RaPlaEvent.getSpecified(sortDescriptors: sortDescriptors, searchPredicate: compoundPredicate)
if(!events.isEmpty) { if(!events.isEmpty) {
return Array(events[...min(1, events.count-1)]) return Array(events[...min(1, events.count-1)])
} else { } else {
@ -222,7 +222,7 @@ extension HomeView{
} }
struct UpcomingLecturesBlock: View { struct UpcomingLecturesBlock: View {
let eventsList: [NSManagedObject] let eventsList: [RaPlaEvent]
let titleKey: String let titleKey: String
var body: some View { var body: some View {
@ -233,7 +233,7 @@ struct UpcomingLecturesBlock: View {
VStack { VStack {
if(!eventsList.isEmpty){ if(!eventsList.isEmpty){
ForEach(eventsList, id: \.self) { exam in ForEach(eventsList, id: \.self) { exam in
Text(exam.value(forKey: "summary") as! String) Text(exam.summary!)
.frame(maxWidth: .infinity, alignment: .leading) .frame(maxWidth: .infinity, alignment: .leading)
} }
} else { } else {
@ -251,7 +251,7 @@ struct UpcomingLecturesBlock: View {
} }
struct UpcomingExamsBlock: View { struct UpcomingExamsBlock: View {
let examsList: [NSManagedObject] let examsList: [RaPlaEvent]
let titleKey: String let titleKey: String
var body: some View { var body: some View {
@ -262,7 +262,7 @@ struct UpcomingExamsBlock: View {
VStack { VStack {
if(!examsList.isEmpty){ if(!examsList.isEmpty){
ForEach(examsList, id: \.self) { exam in ForEach(examsList, id: \.self) { exam in
Text(exam.value(forKey: "summary") as! String) Text(exam.summary!)
.frame(maxWidth: .infinity, alignment: .leading) .frame(maxWidth: .infinity, alignment: .leading)
} }
} else { } else {

View File

@ -9,7 +9,7 @@ import SwiftUI
import CoreData import CoreData
struct LecturePlanItem: View { struct LecturePlanItem: View {
@State var event: NSManagedObject @State var event: RaPlaEvent
@State var isHidden = false @State var isHidden = false
var body: some View { var body: some View {
@ -17,10 +17,10 @@ struct LecturePlanItem: View {
HStack { HStack {
Spacer() Spacer()
VStack { VStack {
Text(event.value(forKey: "summary") as! String) Text(event.summary!)
.font(.title3) .font(.title3)
.frame(maxWidth: .infinity, alignment: .leading) .frame(maxWidth: .infinity, alignment: .leading)
Text(event.value(forKey: "descr") as! String) Text(event.descr!)
.bold() .bold()
.frame(maxWidth: .infinity, alignment: .leading) .frame(maxWidth: .infinity, alignment: .leading)
Divider() Divider()
@ -31,20 +31,20 @@ struct LecturePlanItem: View {
Text("Who") Text("Who")
} }
VStack(alignment: .leading) { VStack(alignment: .leading) {
Text(getDateAndTimeAsString(date: event.value(forKey: "startDate") as! Date) Text(getDateAndTimeAsString(date: event.startDate!)
+ " to " + " to "
+ getTimeAsString(date: event.value(forKey: "endDate") as! Date)) + getTimeAsString(date: event.endDate!))
.bold() .bold()
Text(event.value(forKey: "location") as! String) Text(event.location!)
.bold() .bold()
Text(event.value(forKey: "location") as! String) Text("WIP")
.bold() .bold()
}.frame(maxWidth: .infinity, alignment: .leading) }.frame(maxWidth: .infinity, alignment: .leading)
} }
Divider() Divider()
HStack { HStack {
Button(action: { Button(action: {
event.setValue(!isHidden, forKey: "isHidden") event.isHidden = !isHidden
self.isHidden = !isHidden self.isHidden = !isHidden
PersistenceController.shared.save() PersistenceController.shared.save()
}){ }){
@ -72,7 +72,7 @@ struct LecturePlanItem: View {
Spacer() Spacer()
} }
.onAppear{ .onAppear{
self.isHidden = event.value(forKey: "isHidden") as! Bool self.isHidden = event.isHidden
} }
} }
} }
@ -107,7 +107,7 @@ struct LecturePlanItem_Previews: PreviewProvider {
return settings return settings
} }
static func getPreviewEvent() -> NSManagedObject { static func getPreviewEvent() -> RaPlaEvent {
return UtilityFunctions.getCoreDataObject(entity: "RaPlaEvent", sortDescriptors: [])[0] return RaPlaEvent.getSpecified(sortDescriptors: [])[0]
} }
} }

View File

@ -9,7 +9,7 @@ import SwiftUI
import CoreData import CoreData
struct LecturePlanList: View { struct LecturePlanList: View {
@State private var events: [NSManagedObject] = [] @State private var events: [RaPlaEvent] = []
@State private var sortingAscending = true @State private var sortingAscending = true
var body: some View { var body: some View {
@ -18,14 +18,14 @@ struct LecturePlanList: View {
ForEach(events, id: \.self) { event in ForEach(events, id: \.self) { event in
NavigationLink(destination: LecturePlanItem(event: event)){ NavigationLink(destination: LecturePlanItem(event: event)){
HStack { HStack {
Text(formatDate(date: event.value(forKeyPath: "startDate") as! Date)) Text(formatDate(date: event.startDate!))
.foregroundColor(getEventForegroundColor(for: event)) .foregroundColor(getEventForegroundColor(for: event))
Text(event.value(forKeyPath: "summary") as! String) Text(event.summary!)
.foregroundColor(getEventForegroundColor(for: event)) .foregroundColor(getEventForegroundColor(for: event))
Spacer() Spacer()
if(event.value(forKey: "isHidden") as! Bool) { if(event.isHidden) {
Image(systemName: "eye.slash") Image(systemName: "eye.slash")
.foregroundColor(.red) .foregroundColor(.red)
} else { } else {
@ -38,7 +38,7 @@ struct LecturePlanList: View {
let sectionSortDescriptor = NSSortDescriptor(key: "startDate", ascending: true) let sectionSortDescriptor = NSSortDescriptor(key: "startDate", ascending: true)
let sortDescriptors = [sectionSortDescriptor] let sortDescriptors = [sectionSortDescriptor]
self.events = [] self.events = []
self.events = UtilityFunctions.getCoreDataObject(entity: "RaPlaEvent", sortDescriptors: sortDescriptors) self.events = RaPlaEvent.getSpecified(sortDescriptors: sortDescriptors)
}) })
} }
} }
@ -61,7 +61,7 @@ struct LecturePlanList: View {
let sectionSortDescriptor = NSSortDescriptor(key: "startDate", ascending: true) let sectionSortDescriptor = NSSortDescriptor(key: "startDate", ascending: true)
let sortDescriptors = [sectionSortDescriptor] let sortDescriptors = [sectionSortDescriptor]
self.events = [] self.events = []
self.events = UtilityFunctions.getCoreDataObject(entity: "RaPlaEvent", sortDescriptors: sortDescriptors) self.events = RaPlaEvent.getSpecified(sortDescriptors: sortDescriptors)
} }
} }
} }
@ -89,9 +89,9 @@ extension LecturePlanList {
return formatter.string(from: date) return formatter.string(from: date)
} }
private func getEventForegroundColor(for event: NSManagedObject) -> Color { private func getEventForegroundColor(for event: RaPlaEvent) -> Color {
var textColor: Color = .primary var textColor: Color = .primary
if(event.value(forKeyPath: "category") as! String == "Prüfung") { if(event.category! == "Prüfung") {
textColor = Color.red textColor = Color.red
} else { } else {
textColor = Color.primary textColor = Color.primary