DHBW-Service-App/DHBW-Service/Views/Tabs/HomeView.swift

157 lines
5.7 KiB
Swift
Raw Normal View History

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
@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] = []
2020-12-21 19:54:59 +00:00
2020-12-21 19:28:01 +00:00
var body: some View {
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"))
2020-12-21 21:26:20 +00:00
}.onAppear{
self.readFromCoreData()
self.upcomingExams = getUpcomingExams()
2020-12-21 21:26:20 +00:00
}
}
}
extension HomeView{
func readFromCoreData() {
let fetchedData = UtilityFunctions.getCoreDataObject(entity: "User")
2020-12-21 21:26:20 +00:00
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
2020-12-21 19:54:59 +00:00
}
2020-12-21 19:28:01 +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)
.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
}
}