2020-12-21 19:28:01 +00:00
|
|
|
//
|
|
|
|
// HomeView.swift
|
|
|
|
// DHBW-Service
|
|
|
|
//
|
|
|
|
// Created by Patrick Müller on 21.12.20.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
2020-12-21 21:26:20 +00:00
|
|
|
import CoreData
|
2020-12-21 19:28:01 +00:00
|
|
|
|
|
|
|
struct HomeView: View {
|
2020-12-21 19:54:59 +00:00
|
|
|
@EnvironmentObject var settings: LocalSettings
|
2020-12-22 10:30:41 +00:00
|
|
|
@State private var name: String = ""
|
2020-12-22 12:43:56 +00:00
|
|
|
@State private var course: String = ""
|
|
|
|
@State private var director: String = ""
|
2021-02-01 22:17:15 +00:00
|
|
|
@State private var todaysEvents: [NSManagedObject] = []
|
|
|
|
@State private var tomorrowsEvents: [NSManagedObject] = []
|
|
|
|
@State private var upcomingExams: [NSManagedObject] = []
|
2020-12-21 19:54:59 +00:00
|
|
|
|
2020-12-21 19:28:01 +00:00
|
|
|
var body: some View {
|
2021-02-01 22:17:15 +00:00
|
|
|
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()
|
|
|
|
}
|
2020-12-22 12:43:56 +00:00
|
|
|
}
|
2021-02-01 22:17:15 +00:00
|
|
|
.navigationBarTitle(Text("Home"))
|
2020-12-21 21:26:20 +00:00
|
|
|
}.onAppear{
|
|
|
|
self.readFromCoreData()
|
2021-02-01 22:17:15 +00:00
|
|
|
self.upcomingExams = getUpcomingExams()
|
2020-12-21 21:26:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension HomeView{
|
2020-12-22 10:30:41 +00:00
|
|
|
func readFromCoreData() {
|
2021-02-01 22:17:15 +00:00
|
|
|
let fetchedData = UtilityFunctions.getCoreDataObject(entity: "User")
|
2020-12-21 21:26:20 +00:00
|
|
|
|
2020-12-22 10:30:41 +00:00
|
|
|
if(!fetchedData.isEmpty) {
|
|
|
|
let user = fetchedData[0]
|
|
|
|
self.name = user.value(forKey: "name") as! String
|
2020-12-22 12:43:56 +00:00
|
|
|
self.course = user.value(forKey: "course") as! String
|
|
|
|
self.director = user.value(forKey: "director") as! String
|
2020-12-21 19:54:59 +00:00
|
|
|
}
|
2020-12-21 19:28:01 +00:00
|
|
|
}
|
2021-02-01 22:17:15 +00:00
|
|
|
|
|
|
|
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 []
|
|
|
|
}
|
|
|
|
}
|
2020-12-21 19:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct HomeView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
HomeView()
|
2020-12-21 19:54:59 +00:00
|
|
|
.preferredColorScheme(.dark)
|
2020-12-28 19:09:28 +00:00
|
|
|
.environmentObject(getFirstOpening())
|
|
|
|
.environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
|
|
|
|
}
|
|
|
|
|
|
|
|
static func getFirstOpening() -> LocalSettings {
|
|
|
|
let settings = LocalSettings();
|
|
|
|
settings.isFirstOpening = false;
|
|
|
|
return settings
|
2020-12-21 19:28:01 +00:00
|
|
|
}
|
|
|
|
}
|