Skip to content

Commit

Permalink
Merge pull request #561 from pennlabs/jon0/fix-dining-menu-and-hours
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanmelitski authored Oct 11, 2024
2 parents a05f71e + 84a9c52 commit aa5c3d9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<CGPoint>) {
init(menus: [DiningMenu], id: Int, venue: DiningVenue, menuDate: Date = Date.currentLocalDate, parentScrollProxy: ScrollViewProxy, parentScrollOffset: Binding<CGPoint>) {
self.id = id
self.venue = venue
self.parentScrollProxy = parentScrollProxy
Expand Down Expand Up @@ -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)

Expand All @@ -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
}

}
}
21 changes: 14 additions & 7 deletions PennMobileShared/Dining/Models/DiningVenue+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit aa5c3d9

Please sign in to comment.