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

301 lines
11 KiB
Swift
Raw Permalink 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 = ""
2021-02-10 19:26:53 +00:00
@State private var todaysEvents: [RaPlaEvent] = []
@State private var tomorrowsEvents: [RaPlaEvent] = []
@State private var upcomingExams: [RaPlaEvent] = []
2020-12-21 19:54:59 +00:00
2020-12-21 19:28:01 +00:00
var body: some View {
NavigationView {
VStack {
HStack {
2021-02-07 14:49:02 +00:00
Spacer()
VStack {
Text("hey".localized(tableName: "HomeView", plural: false))
.font(.title3)
.frame(maxWidth: .infinity, alignment: .leading)
Text(self.name)
.bold()
.frame(maxWidth: .infinity, alignment: .leading)
}
.padding()
.background(
RoundedRectangle(cornerRadius: 10)
.fill(Color.gray)
)
Spacer()
}
HStack {
Spacer()
VStack {
Text("information".localized(tableName: "HomeView", plural: false))
.font(.title3)
.frame(maxWidth: .infinity, alignment: .leading)
HStack {
VStack(alignment: .leading) {
Text("course".localized(tableName: "General", plural: false) + ": ")
Text("director".localized(tableName: "General", plural: false) + ": ")
}
VStack(alignment: .leading) {
Text(self.course)
.bold()
Text(self.director)
.bold()
}.frame(maxWidth: .infinity, alignment: .leading)
}
}
.padding()
.background(
RoundedRectangle(cornerRadius: 10)
.fill(Color.gray)
)
Spacer()
}
2021-02-07 14:49:02 +00:00
Spacer()
HStack {
Text("course".localized(tableName: "General", plural: false) + ": ")
Text(self.course)
}
HStack {
Text("director".localized(tableName: "General", plural: false) + ": ")
Text(self.director)
}
HStack {
Text("Next Theory Phase: ")
Text(getNextTheoryPhaseStartDate())
}
// Upcoming events section
HStack {
Spacer()
// Todays lectures block
UpcomingLecturesBlock(eventsList: todaysEvents, titleKey: "today")
// Tomorrows lectures block
UpcomingLecturesBlock(eventsList: tomorrowsEvents, titleKey: "tomorrow")
Spacer()
}
// Exams section
HStack {
Spacer()
UpcomingExamsBlock(examsList: upcomingExams, titleKey: "upcomingExams")
Spacer()
}
2021-02-07 14:49:02 +00:00
Spacer()
}
.navigationBarTitle(Text("Home"))
2021-02-07 14:17:29 +00:00
}
.navigationViewStyle(StackNavigationViewStyle())
.onAppear{
2020-12-21 21:26:20 +00:00
self.readFromCoreData()
self.todaysEvents = getTodaysEvents()
self.tomorrowsEvents = getTomorrowsEvents()
self.upcomingExams = getUpcomingExams()
2020-12-21 21:26:20 +00:00
}
}
}
extension HomeView{
// Read required data from CoreData and save it to the appropriate variables
func readFromCoreData() {
2021-02-10 19:26:53 +00:00
let fetchedData = User.getAll()
2020-12-21 21:26:20 +00:00
if(!fetchedData.isEmpty) {
let user = fetchedData[0]
2021-02-10 19:26:53 +00:00
self.name = user.name!
self.course = user.course!
self.director = user.director!
2020-12-21 19:54:59 +00:00
}
2020-12-21 19:28:01 +00:00
}
// Get 0...2 of todays lectures from RaPla
// Returns a list of RaPlaEvent NSManagedObjects
2021-02-10 19:26:53 +00:00
func getTodaysEvents() -> [RaPlaEvent] {
let searchPredicate = NSPredicate(format: "(category == 'Lehrveranstaltung')")
let hiddenPredicate = NSPredicate(format: "isHidden == NO")
var predicates = [searchPredicate, hiddenPredicate]
predicates.append(contentsOf: getDayPredicates(today: true))
let compoundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: predicates)
2021-02-10 19:26:53 +00:00
let events = RaPlaEvent.getSpecified(searchPredicate: compoundPredicate)
2021-02-02 21:32:32 +00:00
if(!events.isEmpty) {
return Array(events[...min(1, events.count-1)])
} else {
return []
}
}
// Get 0...2 of tomorrows lectures from RaPla
// Returns a list of RaPlaEvent NSManagedObjects
2021-02-10 19:26:53 +00:00
func getTomorrowsEvents() -> [RaPlaEvent] {
let searchPredicate = NSPredicate(format: "(category == 'Lehrveranstaltung')")
let hiddenPredicate = NSPredicate(format: "isHidden == NO")
var predicates = [searchPredicate, hiddenPredicate]
predicates.append(contentsOf: getDayPredicates(tomorrow: true))
let compoundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: predicates)
2021-02-10 19:26:53 +00:00
let events = RaPlaEvent.getSpecified(searchPredicate: compoundPredicate)
2021-02-02 21:32:32 +00:00
if(!events.isEmpty) {
return Array(events[...min(1, events.count-1)])
} else {
return []
}
}
// Get 0...2 of upcoming exams from RaPla
// Returns a list of RaPlaEvent NSManagedObjects
2021-02-10 19:26:53 +00:00
func getUpcomingExams() -> [RaPlaEvent] {
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]
2021-02-10 19:26:53 +00:00
let events = RaPlaEvent.getSpecified(sortDescriptors: sortDescriptors, searchPredicate: compoundPredicate)
2021-02-02 21:32:32 +00:00
if(!events.isEmpty) {
return Array(events[...min(1, events.count-1)])
} else {
return []
}
}
// Get the required NSPredicates to fetch only RaPla events with a start date that is today
func getDayPredicates(today: Bool = false, tomorrow: Bool = false) -> [NSPredicate] {
var calendar = Calendar.current
calendar.timeZone = NSTimeZone.local
var dateFrom = Date()
var dateTo = Date()
if(today) {
//Get today's beginning & end
dateFrom = calendar.startOfDay(for: Date())
dateTo = calendar.date(byAdding: .day, value: 1, to: dateFrom)!
} else if (tomorrow) {
dateFrom = calendar.startOfDay(for: Date())
dateFrom = calendar.date(byAdding: .day, value: 1, to: dateFrom)!
dateTo = calendar.date(byAdding: .day, value: 2, to: dateFrom)!
}
let fromPredicate = NSPredicate(format: "startDate >= %@", dateFrom as NSDate)
let toPredicate = NSPredicate(format: "startDate < %@", dateTo as NSDate)
return [fromPredicate, toPredicate]
}
// Get the formatted date when the next theory phase starts
func getNextTheoryPhaseStartDate() -> String {
var calendar = Calendar.current
calendar.timeZone = NSTimeZone.local
let categoryPredicate = NSPredicate(format: "category == %@", "Sonstiger Termin")
let datePredicate = NSPredicate(format: "startDate >= %@", calendar.startOfDay(for: Date()) as NSDate)
let titlePredicate = NSPredicate(format: "summary CONTAINS[cd] %@", "beginn")
let compoundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [categoryPredicate, datePredicate, titlePredicate])
let sectionSortDescriptor = NSSortDescriptor(key: "startDate", ascending: true)
let sortDescriptors = [sectionSortDescriptor]
let events = UtilityFunctions.getCoreDataObject(entity: "RaPlaEvent", sortDescriptors: sortDescriptors, searchPredicate: compoundPredicate)
if(!events.isEmpty) {
let date = events[0].value(forKey: "startDate") as! Date
let formatter = DateFormatter()
formatter.dateStyle = .short
return formatter.string(from: date)
} else {
return "N/A"
}
}
2020-12-21 19:28:01 +00:00
}
struct UpcomingLecturesBlock: View {
2021-02-10 19:26:53 +00:00
let eventsList: [RaPlaEvent]
let titleKey: String
var body: some View {
VStack {
Text(titleKey.localized(tableName: "HomeView"))
.font(.title)
.frame(maxWidth: .infinity, alignment: .leading)
VStack {
if(!eventsList.isEmpty){
ForEach(eventsList, id: \.self) { exam in
2021-02-10 19:26:53 +00:00
Text(exam.summary!)
.frame(maxWidth: .infinity, alignment: .leading)
}
} else {
Text("noLectures".localized(tableName: "HomeView"))
.frame(maxWidth: .infinity, alignment: .leading)
}
}
}
.padding()
.background(
RoundedRectangle(cornerRadius: 10)
.fill(Color.gray)
)
}
}
struct UpcomingExamsBlock: View {
2021-02-10 19:26:53 +00:00
let examsList: [RaPlaEvent]
let titleKey: String
var body: some View {
VStack {
Text(titleKey.localized(tableName: "HomeView"))
.font(.title)
.frame(maxWidth: .infinity, alignment: .leading)
VStack {
if(!examsList.isEmpty){
ForEach(examsList, id: \.self) { exam in
2021-02-10 19:26:53 +00:00
Text(exam.summary!)
.frame(maxWidth: .infinity, alignment: .leading)
}
} else {
Text("noExams".localized(tableName: "HomeView"))
.frame(maxWidth: .infinity, alignment: .leading)
}
}
}
.padding()
.background(
RoundedRectangle(cornerRadius: 10)
.fill(Color.gray)
)
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color.red, lineWidth: 4)
)
}
}
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)
.previewDevice("iPhone 12")
.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
}
}