Adding option to hide RaPla events

- Also made changes to home view, lecture plan list view, and added lecture plan item view
This commit is contained in:
2021-02-01 23:17:15 +01:00
committed by Patrick Müller
parent ea0b759007
commit b8c6d44000
7 changed files with 214 additions and 32 deletions
+106 -13
View File
@@ -13,30 +13,97 @@ 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] = []
var body: some View {
VStack {
HStack {
Text("name".localized(tableName: "General", plural: false) + ": ")
Text(self.name)
}
HStack {
Text("course".localized(tableName: "General", plural: false) + ": ")
Text(self.course)
}
HStack {
Text("director".localized(tableName: "General", plural: false) + ": ")
Text(self.director)
NavigationView {
VStack {
HStack {
Text("name".localized(tableName: "General", plural: false) + ": ")
Text(self.name)
}
HStack {
Text("course".localized(tableName: "General", plural: false) + ": ")
Text(self.course)
}
HStack {
Text("director".localized(tableName: "General", plural: false) + ": ")
Text(self.director)
}
// Upcoming events section
HStack {
Spacer()
VStack {
Text("Today's events")
.font(/*@START_MENU_TOKEN@*/.title/*@END_MENU_TOKEN@*/)
.frame(maxWidth: .infinity)
VStack {
Text("Evt 1")
Text("Evt 2")
}
}
.padding()
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color.blue, lineWidth: 4)
)
VStack {
Text("Tomorrow's events")
.font(/*@START_MENU_TOKEN@*/.title/*@END_MENU_TOKEN@*/)
.frame(maxWidth: .infinity)
VStack {
Text("Evt 1")
Text("Evt 2")
}
}
.padding()
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color.blue, lineWidth: 4)
)
Spacer()
}
// Exams section
HStack {
Spacer()
VStack {
Text("Upcoming exams")
.font(/*@START_MENU_TOKEN@*/.title/*@END_MENU_TOKEN@*/)
.frame(maxWidth: .infinity)
VStack {
ForEach(upcomingExams, id: \.self) { exam in
Text(exam.value(forKey: "summary") as! String)
}
}
}
.padding()
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color.red, lineWidth: 4)
)
Spacer()
}
}
.navigationBarTitle(Text("Home"))
}.onAppear{
self.readFromCoreData()
self.upcomingExams = getUpcomingExams()
}
}
}
extension HomeView{
func readFromCoreData() {
let fetchedData = UtilityFunctions.getCoreDataObject(entity: "User", sortDescriptors: [])
let fetchedData = UtilityFunctions.getCoreDataObject(entity: "User")
if(!fetchedData.isEmpty) {
let user = fetchedData[0]
@@ -45,6 +112,32 @@ extension HomeView{
self.director = user.value(forKey: "director") as! String
}
}
func getTodaysEvents() -> [NSManagedObject] {
// let searchPredicate = NSPredicate(format: "(category == 'Lehrveranstaltung') AND (startDate = %@)", Date())
// return Array(UtilityFunctions.getCoreDataObject(entity: "RaPlaEvent", searchPredicate: searchPredicate)[0...1])
return []
}
func getTomorrowsEvents() -> [NSManagedObject] {
// let searchPredicate = NSPredicate(format: "(category == 'Lehrveranstaltung') AND (startDate = %@)", Date().)
// return Array(UtilityFunctions.getCoreDataObject(entity: "RaPlaEvent", searchPredicate: searchPredicate)[0...1])
return []
}
func getUpcomingExams() -> [NSManagedObject] {
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)
if(events.count > 0) {
return Array(events[0...min(1, events.count)])
} else {
return []
}
}
}
struct HomeView_Previews: PreviewProvider {
@@ -0,0 +1,57 @@
//
// LecturePlanItem.swift
// DHBW-Service
//
// Created by Patrick Müller on 01.02.21.
//
import SwiftUI
import CoreData
struct LecturePlanItem: View {
@State var event: NSManagedObject
@State var isHidden = false
var body: some View {
VStack {
Text(event.value(forKey: "summary") as! String)
Button(action: {
event.setValue(!isHidden, forKey: "isHidden")
self.isHidden = !isHidden
PersistenceController.shared.save()
}){
if(self.isHidden){
Text("Show")
} else {
Text("Hide")
}
}
.padding()
.foregroundColor(.white)
.background(Color.blue)
.cornerRadius(15)
}
.onAppear{
self.isHidden = event.value(forKey: "isHidden") as! Bool
}
}
}
struct LecturePlanItem_Previews: PreviewProvider {
static var previews: some View {
LecturePlanItem(event: getPreviewEvent())
.preferredColorScheme(.dark)
.environmentObject(getFirstOpening())
.environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
}
static func getFirstOpening() -> LocalSettings {
let settings = LocalSettings();
settings.isFirstOpening = false;
return settings
}
static func getPreviewEvent() -> NSManagedObject {
return UtilityFunctions.getCoreDataObject(entity: "RaPlaEvent", sortDescriptors: [])[0]
}
}
+38 -16
View File
@@ -13,30 +13,52 @@ struct LecturePlanList: View {
@State private var sortingAscending = true
var body: some View {
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")
}
NavigationView() {
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))
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")
}
}
}
// 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)
})
}
}
.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")
// }
// })
}.onAppear{
let sectionSortDescriptor = NSSortDescriptor(key: "startDate", ascending: true)
let sortDescriptors = [sectionSortDescriptor]
self.events = []
self.events = UtilityFunctions.getCoreDataObject(entity: "RaPlaEvent", sortDescriptors: sortDescriptors)
}
}