Some enhancements to the lecture plan list

This commit is contained in:
Patrick Müller 2021-04-30 22:33:46 +02:00 committed by Patrick Müller
parent f6e1d979fd
commit 543bd190eb
2 changed files with 63 additions and 16 deletions

View File

@ -46,7 +46,6 @@ struct LecturePlanItem: View {
Button(action: { Button(action: {
event.isHidden = !isHidden event.isHidden = !isHidden
self.isHidden = !isHidden self.isHidden = !isHidden
PersistenceController.shared.save()
}){ }){
if(self.isHidden){ if(self.isHidden){
Text("Show") Text("Show")
@ -74,6 +73,9 @@ struct LecturePlanItem: View {
.onAppear{ .onAppear{
self.isHidden = event.isHidden self.isHidden = event.isHidden
} }
.onDisappear{
PersistenceController.shared.save()
}
} }
} }

View File

@ -7,12 +7,14 @@
import SwiftUI import SwiftUI
import CoreData import CoreData
import Combine
struct LecturePlanList: View { struct LecturePlanList: View {
@State private var events: [RaPlaEvent] = [] @State private var events: [RaPlaEvent] = []
@State private var daysWithEvents: [Date:[RaPlaEvent]] = [:] @State private var daysWithEvents: [Date:[RaPlaEvent]] = [:]
@State private var sortingAscending = true @State private var sortingAscending = true
var body: some View { var body: some View {
NavigationView() { NavigationView() {
ScrollView(.vertical) { ScrollView(.vertical) {
@ -25,11 +27,12 @@ struct LecturePlanList: View {
// .padding() // .padding()
ForEach(daysWithEvents.sorted(by: {$0.key < $1.key}), id: \.key) { key, value in ForEach(daysWithEvents.sorted(by: {$0.key < $1.key}), id: \.key) { key, value in
let dayBlock = DayWithEventsBlock(date: key, eventsList: value, parent: self) HStack {
dayBlock Spacer()
DayWithEventsBlock(date: key, eventsList: value, parent: self)
Spacer()
}
} }
} }
} }
.navigationBarTitle(Text("Lectures")) .navigationBarTitle(Text("Lectures"))
@ -77,6 +80,15 @@ extension LecturePlanList {
return formatter.string(from: date) return formatter.string(from: date)
} }
public func formatTime(date: Date) -> String {
let formatter = DateFormatter()
formatter.timeStyle = .short
return formatter.string(from: date)
}
/**
Get the correct foreground color for the given event, e.g. primary for normal events and red for exams.
*/
public func getEventForegroundColor(for event: RaPlaEvent) -> Color { public func getEventForegroundColor(for event: RaPlaEvent) -> Color {
var textColor: Color = .primary var textColor: Color = .primary
if(event.category! == "Prüfung") { if(event.category! == "Prüfung") {
@ -88,6 +100,9 @@ extension LecturePlanList {
return textColor return textColor
} }
/**
DEPRECATED, used to find the element in the list of days that represents the next day
*/
public func findNextDay() { public func findNextDay() {
// As this list is already sorted ascending, we can just return the first event with a start date that is in the future // As this list is already sorted ascending, we can just return the first event with a start date that is in the future
let sortedEvents = self.events.sorted(by: { $0.startDate! < $1.startDate! }) let sortedEvents = self.events.sorted(by: { $0.startDate! < $1.startDate! })
@ -99,6 +114,9 @@ extension LecturePlanList {
} }
} }
/**
Loads required data from CoreData
*/
public func getRaPlaEvents() { public func getRaPlaEvents() {
let sectionSortDescriptor = NSSortDescriptor(key: "startDate", ascending: true) let sectionSortDescriptor = NSSortDescriptor(key: "startDate", ascending: true)
let sortDescriptors = [sectionSortDescriptor] let sortDescriptors = [sectionSortDescriptor]
@ -130,8 +148,15 @@ extension LecturePlanList {
self.daysWithEvents[dayOnly] = eventsList self.daysWithEvents[dayOnly] = eventsList
} }
} }
public func updateDay(day: Date, events: [RaPlaEvent]) {
self.daysWithEvents[day] = events
}
} }
/**
Each of these represents one day block in the view
*/
struct DayWithEventsBlock: View { struct DayWithEventsBlock: View {
@State var date: Date @State var date: Date
@State var eventsList: [RaPlaEvent] @State var eventsList: [RaPlaEvent]
@ -146,15 +171,10 @@ struct DayWithEventsBlock: View {
VStack { VStack {
if(!eventsList.isEmpty){ if(!eventsList.isEmpty){
ForEach(eventsList, id: \.self) { event in ForEach(eventsList, id: \.self) { event in
NavigationLink(destination: LecturePlanItem(event: event)) { var visibleIconGroup = Group {
HStack { Button(action: {
Text(parent.formatDate(date: event.startDate!)) event.isHidden.toggle()
.foregroundColor(parent.getEventForegroundColor(for: event)) }){
Text(event.summary!)
.foregroundColor(parent.getEventForegroundColor(for: event))
Spacer()
if(event.isHidden) { if(event.isHidden) {
Image(systemName: "eye.slash") Image(systemName: "eye.slash")
.foregroundColor(.red) .foregroundColor(.red)
@ -162,6 +182,19 @@ struct DayWithEventsBlock: View {
Image(systemName: "eye") Image(systemName: "eye")
} }
} }
}
NavigationLink(destination: LecturePlanItem(event: event)) {
HStack {
Text(parent.formatTime(date: event.startDate!))
.foregroundColor(parent.getEventForegroundColor(for: event))
Text(event.summary!)
.foregroundColor(parent.getEventForegroundColor(for: event))
Spacer()
visibleIconGroup
}
.padding() .padding()
.background( .background(
RoundedRectangle(cornerRadius: 10) RoundedRectangle(cornerRadius: 10)
@ -170,8 +203,20 @@ struct DayWithEventsBlock: View {
} }
// When an event gets updated from child view, reload it here as this will not trigger the onAppear() function // When an event gets updated from child view, reload it here as this will not trigger the onAppear() function
.onReceive(event.objectWillChange, perform: { _ in .onReceive(event.objectWillChange, perform: { _ in
print("receiving event") print("receiving event: \(event.isHidden)")
parent.getRaPlaEvents() visibleIconGroup = Group {
Button(action: {
event.isHidden.toggle()
}){
if(event.isHidden) {
Image(systemName: "eye.slash")
.foregroundColor(.red)
} else {
Image(systemName: "eye")
}
}
}
// parent.updateDay(day: self.date, events: self.eventsList)
}) })
} }
} else { } else {