2021-01-30 22:05:44 +00:00
|
|
|
//
|
|
|
|
// LecturePlanList.swift
|
|
|
|
// DHBW-Service
|
|
|
|
//
|
|
|
|
// Created by Patrick Müller on 30.01.21.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
import CoreData
|
|
|
|
|
|
|
|
struct LecturePlanList: View {
|
|
|
|
@State private var events: [NSManagedObject] = []
|
2021-01-31 18:49:52 +00:00
|
|
|
@State private var sortingAscending = true
|
2021-01-30 22:05:44 +00:00
|
|
|
|
|
|
|
var body: some View {
|
2021-02-01 22:17:15 +00:00
|
|
|
NavigationView() {
|
2021-01-31 18:49:52 +00:00
|
|
|
List {
|
|
|
|
ForEach(events, id: \.self) { event in
|
2021-02-01 22:17:15 +00:00
|
|
|
NavigationLink(destination: LecturePlanItem(event: event)){
|
|
|
|
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))
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
if(event.value(forKey: "isHidden") as! Bool) {
|
|
|
|
Image(systemName: "eye.slash")
|
|
|
|
.foregroundColor(.red)
|
|
|
|
} else {
|
|
|
|
Image(systemName: "eye")
|
|
|
|
}
|
|
|
|
}
|
2021-01-31 18:49:52 +00:00
|
|
|
}
|
2021-02-01 22:17:15 +00:00
|
|
|
// When an event gets updated from child view, reload it here as this will not trigger the onAppear() function
|
|
|
|
.onReceive(event.objectWillChange, perform: { _ in
|
|
|
|
let sectionSortDescriptor = NSSortDescriptor(key: "startDate", ascending: true)
|
|
|
|
let sortDescriptors = [sectionSortDescriptor]
|
|
|
|
self.events = []
|
|
|
|
self.events = UtilityFunctions.getCoreDataObject(entity: "RaPlaEvent", sortDescriptors: sortDescriptors)
|
|
|
|
})
|
2021-01-30 22:05:44 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-01 22:17:15 +00:00
|
|
|
.navigationBarTitle(Text("Lectures"))
|
|
|
|
// .navigationBarItems(trailing: {
|
|
|
|
// 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")
|
|
|
|
// }
|
|
|
|
// })
|
2021-02-07 14:17:29 +00:00
|
|
|
}
|
|
|
|
.navigationViewStyle(StackNavigationViewStyle())
|
|
|
|
.onAppear{
|
2021-01-30 22:05:44 +00:00
|
|
|
let sectionSortDescriptor = NSSortDescriptor(key: "startDate", ascending: true)
|
|
|
|
let sortDescriptors = [sectionSortDescriptor]
|
2021-02-01 22:17:15 +00:00
|
|
|
self.events = []
|
2021-01-30 22:05:44 +00:00
|
|
|
self.events = UtilityFunctions.getCoreDataObject(entity: "RaPlaEvent", sortDescriptors: sortDescriptors)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct LecturePlanList_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
LecturePlanList()
|
2021-01-31 18:49:52 +00:00
|
|
|
.preferredColorScheme(.dark)
|
|
|
|
.environmentObject(getFirstOpening())
|
|
|
|
.environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
|
|
|
|
}
|
|
|
|
|
|
|
|
static func getFirstOpening() -> LocalSettings {
|
|
|
|
let settings = LocalSettings();
|
|
|
|
settings.isFirstOpening = false;
|
|
|
|
return settings
|
2021-01-30 22:05:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension LecturePlanList {
|
|
|
|
private func formatDate(date: Date) -> String {
|
|
|
|
let formatter = DateFormatter()
|
|
|
|
formatter.dateStyle = .short
|
|
|
|
return formatter.string(from: date)
|
|
|
|
}
|
2021-01-31 18:49:52 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2021-01-30 22:05:44 +00:00
|
|
|
}
|