mirror of
https://github.com/Mueller-Patrick/DHBW-Service-App.git
synced 2026-04-28 18:30:09 +00:00
♻️ Refactored CoreData access
This commit is contained in:
@@ -13,9 +13,9 @@ struct HomeView: View {
|
||||
@State private var name: String = ""
|
||||
@State private var course: String = ""
|
||||
@State private var director: String = ""
|
||||
@State private var todaysEvents: [NSManagedObject] = []
|
||||
@State private var tomorrowsEvents: [NSManagedObject] = []
|
||||
@State private var upcomingExams: [NSManagedObject] = []
|
||||
@State private var todaysEvents: [RaPlaEvent] = []
|
||||
@State private var tomorrowsEvents: [RaPlaEvent] = []
|
||||
@State private var upcomingExams: [RaPlaEvent] = []
|
||||
|
||||
var body: some View {
|
||||
NavigationView {
|
||||
@@ -115,25 +115,25 @@ struct HomeView: View {
|
||||
extension HomeView{
|
||||
// Read required data from CoreData and save it to the appropriate variables
|
||||
func readFromCoreData() {
|
||||
let fetchedData = UtilityFunctions.getCoreDataObject(entity: "User")
|
||||
let fetchedData = User.getAll()
|
||||
|
||||
if(!fetchedData.isEmpty) {
|
||||
let user = fetchedData[0]
|
||||
self.name = user.value(forKey: "name") as! String
|
||||
self.course = user.value(forKey: "course") as! String
|
||||
self.director = user.value(forKey: "director") as! String
|
||||
self.name = user.name!
|
||||
self.course = user.course!
|
||||
self.director = user.director!
|
||||
}
|
||||
}
|
||||
|
||||
// Get 0...2 of todays lectures from RaPla
|
||||
// Returns a list of RaPlaEvent NSManagedObjects
|
||||
func getTodaysEvents() -> [NSManagedObject] {
|
||||
func getTodaysEvents() -> [RaPlaEvent] {
|
||||
let searchPredicate = NSPredicate(format: "(category == 'Lehrveranstaltung')")
|
||||
let hiddenPredicate = NSPredicate(format: "isHidden == NO")
|
||||
var predicates = [searchPredicate, hiddenPredicate]
|
||||
predicates.append(contentsOf: getDayPredicates(today: true))
|
||||
let compoundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: predicates)
|
||||
let events = UtilityFunctions.getCoreDataObject(entity: "RaPlaEvent", searchPredicate: compoundPredicate)
|
||||
let events = RaPlaEvent.getSpecified(searchPredicate: compoundPredicate)
|
||||
if(!events.isEmpty) {
|
||||
return Array(events[...min(1, events.count-1)])
|
||||
} else {
|
||||
@@ -143,13 +143,13 @@ extension HomeView{
|
||||
|
||||
// Get 0...2 of tomorrows lectures from RaPla
|
||||
// Returns a list of RaPlaEvent NSManagedObjects
|
||||
func getTomorrowsEvents() -> [NSManagedObject] {
|
||||
func getTomorrowsEvents() -> [RaPlaEvent] {
|
||||
let searchPredicate = NSPredicate(format: "(category == 'Lehrveranstaltung')")
|
||||
let hiddenPredicate = NSPredicate(format: "isHidden == NO")
|
||||
var predicates = [searchPredicate, hiddenPredicate]
|
||||
predicates.append(contentsOf: getDayPredicates(tomorrow: true))
|
||||
let compoundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: predicates)
|
||||
let events = UtilityFunctions.getCoreDataObject(entity: "RaPlaEvent", searchPredicate: compoundPredicate)
|
||||
let events = RaPlaEvent.getSpecified(searchPredicate: compoundPredicate)
|
||||
if(!events.isEmpty) {
|
||||
return Array(events[...min(1, events.count-1)])
|
||||
} else {
|
||||
@@ -159,13 +159,13 @@ extension HomeView{
|
||||
|
||||
// Get 0...2 of upcoming exams from RaPla
|
||||
// Returns a list of RaPlaEvent NSManagedObjects
|
||||
func getUpcomingExams() -> [NSManagedObject] {
|
||||
func getUpcomingExams() -> [RaPlaEvent] {
|
||||
let searchPredicate = NSPredicate(format: "category == %@", "Prüfung")
|
||||
let hiddenPredicate = NSPredicate(format: "isHidden == NO")
|
||||
let compoundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [searchPredicate, hiddenPredicate])
|
||||
let sectionSortDescriptor = NSSortDescriptor(key: "startDate", ascending: true)
|
||||
let sortDescriptors = [sectionSortDescriptor]
|
||||
let events = UtilityFunctions.getCoreDataObject(entity: "RaPlaEvent", sortDescriptors: sortDescriptors, searchPredicate: compoundPredicate)
|
||||
let events = RaPlaEvent.getSpecified(sortDescriptors: sortDescriptors, searchPredicate: compoundPredicate)
|
||||
if(!events.isEmpty) {
|
||||
return Array(events[...min(1, events.count-1)])
|
||||
} else {
|
||||
@@ -222,7 +222,7 @@ extension HomeView{
|
||||
}
|
||||
|
||||
struct UpcomingLecturesBlock: View {
|
||||
let eventsList: [NSManagedObject]
|
||||
let eventsList: [RaPlaEvent]
|
||||
let titleKey: String
|
||||
|
||||
var body: some View {
|
||||
@@ -233,7 +233,7 @@ struct UpcomingLecturesBlock: View {
|
||||
VStack {
|
||||
if(!eventsList.isEmpty){
|
||||
ForEach(eventsList, id: \.self) { exam in
|
||||
Text(exam.value(forKey: "summary") as! String)
|
||||
Text(exam.summary!)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
}
|
||||
} else {
|
||||
@@ -251,7 +251,7 @@ struct UpcomingLecturesBlock: View {
|
||||
}
|
||||
|
||||
struct UpcomingExamsBlock: View {
|
||||
let examsList: [NSManagedObject]
|
||||
let examsList: [RaPlaEvent]
|
||||
let titleKey: String
|
||||
|
||||
var body: some View {
|
||||
@@ -262,7 +262,7 @@ struct UpcomingExamsBlock: View {
|
||||
VStack {
|
||||
if(!examsList.isEmpty){
|
||||
ForEach(examsList, id: \.self) { exam in
|
||||
Text(exam.value(forKey: "summary") as! String)
|
||||
Text(exam.summary!)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -9,7 +9,7 @@ import SwiftUI
|
||||
import CoreData
|
||||
|
||||
struct LecturePlanItem: View {
|
||||
@State var event: NSManagedObject
|
||||
@State var event: RaPlaEvent
|
||||
@State var isHidden = false
|
||||
|
||||
var body: some View {
|
||||
@@ -17,10 +17,10 @@ struct LecturePlanItem: View {
|
||||
HStack {
|
||||
Spacer()
|
||||
VStack {
|
||||
Text(event.value(forKey: "summary") as! String)
|
||||
Text(event.summary!)
|
||||
.font(.title3)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
Text(event.value(forKey: "descr") as! String)
|
||||
Text(event.descr!)
|
||||
.bold()
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
Divider()
|
||||
@@ -31,20 +31,20 @@ struct LecturePlanItem: View {
|
||||
Text("Who")
|
||||
}
|
||||
VStack(alignment: .leading) {
|
||||
Text(getDateAndTimeAsString(date: event.value(forKey: "startDate") as! Date)
|
||||
Text(getDateAndTimeAsString(date: event.startDate!)
|
||||
+ " to "
|
||||
+ getTimeAsString(date: event.value(forKey: "endDate") as! Date))
|
||||
+ getTimeAsString(date: event.endDate!))
|
||||
.bold()
|
||||
Text(event.value(forKey: "location") as! String)
|
||||
Text(event.location!)
|
||||
.bold()
|
||||
Text(event.value(forKey: "location") as! String)
|
||||
Text("WIP")
|
||||
.bold()
|
||||
}.frame(maxWidth: .infinity, alignment: .leading)
|
||||
}
|
||||
Divider()
|
||||
HStack {
|
||||
Button(action: {
|
||||
event.setValue(!isHidden, forKey: "isHidden")
|
||||
event.isHidden = !isHidden
|
||||
self.isHidden = !isHidden
|
||||
PersistenceController.shared.save()
|
||||
}){
|
||||
@@ -72,7 +72,7 @@ struct LecturePlanItem: View {
|
||||
Spacer()
|
||||
}
|
||||
.onAppear{
|
||||
self.isHidden = event.value(forKey: "isHidden") as! Bool
|
||||
self.isHidden = event.isHidden
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -107,7 +107,7 @@ struct LecturePlanItem_Previews: PreviewProvider {
|
||||
return settings
|
||||
}
|
||||
|
||||
static func getPreviewEvent() -> NSManagedObject {
|
||||
return UtilityFunctions.getCoreDataObject(entity: "RaPlaEvent", sortDescriptors: [])[0]
|
||||
static func getPreviewEvent() -> RaPlaEvent {
|
||||
return RaPlaEvent.getSpecified(sortDescriptors: [])[0]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import SwiftUI
|
||||
import CoreData
|
||||
|
||||
struct LecturePlanList: View {
|
||||
@State private var events: [NSManagedObject] = []
|
||||
@State private var events: [RaPlaEvent] = []
|
||||
@State private var sortingAscending = true
|
||||
|
||||
var body: some View {
|
||||
@@ -18,14 +18,14 @@ struct LecturePlanList: View {
|
||||
ForEach(events, id: \.self) { event in
|
||||
NavigationLink(destination: LecturePlanItem(event: event)){
|
||||
HStack {
|
||||
Text(formatDate(date: event.value(forKeyPath: "startDate") as! Date))
|
||||
Text(formatDate(date: event.startDate!))
|
||||
.foregroundColor(getEventForegroundColor(for: event))
|
||||
Text(event.value(forKeyPath: "summary") as! String)
|
||||
Text(event.summary!)
|
||||
.foregroundColor(getEventForegroundColor(for: event))
|
||||
|
||||
Spacer()
|
||||
|
||||
if(event.value(forKey: "isHidden") as! Bool) {
|
||||
if(event.isHidden) {
|
||||
Image(systemName: "eye.slash")
|
||||
.foregroundColor(.red)
|
||||
} else {
|
||||
@@ -38,7 +38,7 @@ struct LecturePlanList: View {
|
||||
let sectionSortDescriptor = NSSortDescriptor(key: "startDate", ascending: true)
|
||||
let sortDescriptors = [sectionSortDescriptor]
|
||||
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 sortDescriptors = [sectionSortDescriptor]
|
||||
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)
|
||||
}
|
||||
|
||||
private func getEventForegroundColor(for event: NSManagedObject) -> Color {
|
||||
private func getEventForegroundColor(for event: RaPlaEvent) -> Color {
|
||||
var textColor: Color = .primary
|
||||
if(event.value(forKeyPath: "category") as! String == "Prüfung") {
|
||||
if(event.category! == "Prüfung") {
|
||||
textColor = Color.red
|
||||
} else {
|
||||
textColor = Color.primary
|
||||
|
||||
Reference in New Issue
Block a user