Replacing home view lectures mock with real data

This commit is contained in:
Patrick Müller 2021-02-02 20:23:46 +01:00 committed by Patrick Müller
parent b8c6d44000
commit b8b1439bf1

View File

@ -38,12 +38,17 @@ struct HomeView: View {
Spacer() Spacer()
VStack { VStack {
Text("Today's events") Text("Today")
.font(/*@START_MENU_TOKEN@*/.title/*@END_MENU_TOKEN@*/) .font(/*@START_MENU_TOKEN@*/.title/*@END_MENU_TOKEN@*/)
.frame(maxWidth: .infinity) .frame(maxWidth: .infinity)
VStack { VStack {
Text("Evt 1") if(todaysEvents.count > 0){
Text("Evt 2") ForEach(todaysEvents, id: \.self) { exam in
Text(exam.value(forKey: "summary") as! String)
}
} else {
Text("No lectures")
}
} }
} }
.padding() .padding()
@ -53,12 +58,17 @@ struct HomeView: View {
) )
VStack { VStack {
Text("Tomorrow's events") Text("Tomorrow")
.font(/*@START_MENU_TOKEN@*/.title/*@END_MENU_TOKEN@*/) .font(/*@START_MENU_TOKEN@*/.title/*@END_MENU_TOKEN@*/)
.frame(maxWidth: .infinity) .frame(maxWidth: .infinity)
VStack { VStack {
Text("Evt 1") if(tomorrowsEvents.count > 0){
Text("Evt 2") ForEach(tomorrowsEvents, id: \.self) { exam in
Text(exam.value(forKey: "summary") as! String)
}
} else {
Text("No lectures")
}
} }
} }
.padding() .padding()
@ -79,8 +89,12 @@ struct HomeView: View {
.font(/*@START_MENU_TOKEN@*/.title/*@END_MENU_TOKEN@*/) .font(/*@START_MENU_TOKEN@*/.title/*@END_MENU_TOKEN@*/)
.frame(maxWidth: .infinity) .frame(maxWidth: .infinity)
VStack { VStack {
ForEach(upcomingExams, id: \.self) { exam in if(upcomingExams.count > 0){
Text(exam.value(forKey: "summary") as! String) ForEach(upcomingExams, id: \.self) { exam in
Text(exam.value(forKey: "summary") as! String)
}
} else {
Text("No exams")
} }
} }
} }
@ -96,6 +110,8 @@ struct HomeView: View {
.navigationBarTitle(Text("Home")) .navigationBarTitle(Text("Home"))
}.onAppear{ }.onAppear{
self.readFromCoreData() self.readFromCoreData()
self.todaysEvents = getTodaysEvents()
self.tomorrowsEvents = getTomorrowsEvents()
self.upcomingExams = getUpcomingExams() self.upcomingExams = getUpcomingExams()
} }
} }
@ -114,15 +130,31 @@ extension HomeView{
} }
func getTodaysEvents() -> [NSManagedObject] { func getTodaysEvents() -> [NSManagedObject] {
// let searchPredicate = NSPredicate(format: "(category == 'Lehrveranstaltung') AND (startDate = %@)", Date()) let searchPredicate = NSPredicate(format: "(category == 'Lehrveranstaltung')")
// return Array(UtilityFunctions.getCoreDataObject(entity: "RaPlaEvent", searchPredicate: searchPredicate)[0...1]) let hiddenPredicate = NSPredicate(format: "isHidden == NO")
return [] var predicates = [searchPredicate, hiddenPredicate]
predicates.append(contentsOf: getDayPredicates(today: true))
let compoundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: predicates)
let events = UtilityFunctions.getCoreDataObject(entity: "RaPlaEvent", searchPredicate: compoundPredicate)
if(events.count > 0) {
return Array(events[...min(1, events.count-1)])
} else {
return []
}
} }
func getTomorrowsEvents() -> [NSManagedObject] { func getTomorrowsEvents() -> [NSManagedObject] {
// let searchPredicate = NSPredicate(format: "(category == 'Lehrveranstaltung') AND (startDate = %@)", Date().) let searchPredicate = NSPredicate(format: "(category == 'Lehrveranstaltung')")
// return Array(UtilityFunctions.getCoreDataObject(entity: "RaPlaEvent", searchPredicate: searchPredicate)[0...1]) let hiddenPredicate = NSPredicate(format: "isHidden == NO")
return [] var predicates = [searchPredicate, hiddenPredicate]
predicates.append(contentsOf: getDayPredicates(tomorrow: true))
let compoundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: predicates)
let events = UtilityFunctions.getCoreDataObject(entity: "RaPlaEvent", searchPredicate: compoundPredicate)
if(events.count > 0) {
return Array(events[...min(1, events.count-1)])
} else {
return []
}
} }
func getUpcomingExams() -> [NSManagedObject] { func getUpcomingExams() -> [NSManagedObject] {
@ -133,11 +165,33 @@ extension HomeView{
let sortDescriptors = [sectionSortDescriptor] let sortDescriptors = [sectionSortDescriptor]
let events = UtilityFunctions.getCoreDataObject(entity: "RaPlaEvent", sortDescriptors: sortDescriptors, searchPredicate: compoundPredicate) let events = UtilityFunctions.getCoreDataObject(entity: "RaPlaEvent", sortDescriptors: sortDescriptors, searchPredicate: compoundPredicate)
if(events.count > 0) { if(events.count > 0) {
return Array(events[0...min(1, events.count)]) return Array(events[...min(1, events.count-1)])
} else { } else {
return [] return []
} }
} }
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]
}
} }
struct HomeView_Previews: PreviewProvider { struct HomeView_Previews: PreviewProvider {