From 84a9c52e95b4006d8b6889e4d859a9c784810f95 Mon Sep 17 00:00:00 2001 From: Jonathan Melitski Date: Fri, 11 Oct 2024 18:45:31 -0400 Subject: [PATCH] Fix2 --- .../DiningVenueDetailMenuView.swift | 20 +++++++++--------- .../Models/DiningVenue+Extensions.swift | 21 ++++++++++++------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/PennMobile/Dining/SwiftUI/Views/Venue/Detail View/DiningVenueDetailMenuView.swift b/PennMobile/Dining/SwiftUI/Views/Venue/Detail View/DiningVenueDetailMenuView.swift index fddc863ba..911608502 100644 --- a/PennMobile/Dining/SwiftUI/Views/Venue/Detail View/DiningVenueDetailMenuView.swift +++ b/PennMobile/Dining/SwiftUI/Views/Venue/Detail View/DiningVenueDetailMenuView.swift @@ -28,7 +28,7 @@ struct DiningVenueDetailMenuView: View { @Binding private var parentScrollOffset: CGPoint - init(menus: [DiningMenu], id: Int, venue: DiningVenue, menuDate: Date = Date(), parentScrollProxy: ScrollViewProxy, parentScrollOffset: Binding) { + init(menus: [DiningMenu], id: Int, venue: DiningVenue, menuDate: Date = Date.currentLocalDate, parentScrollProxy: ScrollViewProxy, parentScrollOffset: Binding) { self.id = id self.venue = venue self.parentScrollProxy = parentScrollProxy @@ -87,7 +87,8 @@ struct DiningVenueDetailMenuView: View { } else { Text("Closed For Today") } - DatePicker("", selection: $menuDate, in: Date()...Date().add(minutes: 8640), displayedComponents: .date) + DatePicker("", selection: $menuDate, in: Date.currentLocalDate...Date.currentLocalDate.add(minutes: 8640), displayedComponents: .date) + } .padding(.horizontal) @@ -100,19 +101,18 @@ struct DiningVenueDetailMenuView: View { } } } - - .onChange(of: currentMenu) { _ in - print((currentMenu?.service ?? "no menu") + " on " + menuDate.description) - selectedStation = currentMenu?.stations.first ?? nil - } - .onChange(of: menuDate) { newDate in + .onChange(of: menuDate) { _ in Task.init() { - await diningVM.refreshMenus(cache: true, at: newDate) - menuDate = newDate + await diningVM.refreshMenus(cache: true, at: menuDate) menus = diningVM.diningMenus[venue.id]?.menus ?? [] currentMenu = getMenu() } } + .onChange(of: currentMenu) { _ in + print((currentMenu?.service ?? "no menu") + " on " + menuDate.description) + selectedStation = nil + } + } } diff --git a/PennMobileShared/Dining/Models/DiningVenue+Extensions.swift b/PennMobileShared/Dining/Models/DiningVenue+Extensions.swift index 1de1407fb..d4ca6e153 100755 --- a/PennMobileShared/Dining/Models/DiningVenue+Extensions.swift +++ b/PennMobileShared/Dining/Models/DiningVenue+Extensions.swift @@ -23,21 +23,24 @@ public extension DiningVenue { // MARK: - Venue Status var mealsToday: [Meal] { - - return mealsOnDate(date: Date()) + return mealsOnDate(date: Date.currentLocalDate) } func mealsOnDate(date: Date) -> [Meal] { let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd" + dateFormatter.timeZone = .gmt + let formattedDate = dateFormatter.string(from: date) - return self.days.first(where: { day in - day.date == dateFormatter.string(from: date) + let meals = self.days.first(where: { day in + day.date == formattedDate })?.meals.sorted(by: { el1, el2 in - return el1.starttime > el2.starttime + return el1.starttime < el2.starttime }) ?? [] + + return meals } - + var isOpen: Bool { if mealsToday.isEmpty { return false } @@ -79,7 +82,11 @@ public extension DiningVenue { } var currentOrNearestMeal: Meal? { - return self.mealsToday.first(where: { $0.isCurrentlyServing }) ?? (self.mealsToday.first(where: { $0.starttime > Date() }) ?? nil) + guard let index = currentOrNearestMealIndex else { + return nil + } + + return self.mealsToday[index] } var hasMealsToday: Bool {