@@ -12,49 +12,79 @@ import WebKit
1212struct DiningVenueDetailMenuView : View {
1313 var menus : [ DiningMenu ]
1414 var id : Int
15- @State var menuDate = Date ( )
16- @State private var showMenu = false
15+ var venue : DiningVenue ?
16+ @State var menuDate : Date
17+ @State private var menuIndex : Int
18+ @State private var showMenu : Bool
1719 @EnvironmentObject var diningVM : DiningViewModelSwiftUI
20+ init ( menus: [ DiningMenu ] , id: Int , venue: DiningVenue ? = nil , menuDate: Date = Date ( ) , showMenu: Bool = false ) {
21+ self . menus = menus
22+ self . id = id
23+ self . venue = venue
24+ _showMenu = State ( initialValue: showMenu)
25+ _menuDate = State ( initialValue: menuDate)
26+ _menuIndex = State ( initialValue: 0 )
27+ _menuIndex = State ( initialValue: self . getIndex ( ) )
28+ }
29+ func getIndex( ) -> Int {
30+ var inx = 0
31+ if self . venue != nil && Calendar . current. isDate ( self . menuDate, inSameDayAs: Date ( ) ) {
32+ if let meal = self . venue!. currentOrNearestMeal {
33+ inx = self . menus. firstIndex { $0. service == meal. label } ?? inx
34+ }
35+ }
36+ return inx
37+ }
1838
1939 var body : some View {
2040 DatePicker ( selection: $menuDate, in: Date ( ) ... Date ( ) . addingTimeInterval ( 86400 * 6 ) , displayedComponents: . date) {
2141 Text ( " Menu date " )
2242 } . onChange ( of: menuDate) { newMenuDate in
23- diningVM. refreshMenu ( for: id, at: newMenuDate)
24- }
25- VStack ( alignment: . leading, spacing: 10 ) {
26- Text ( " Penn has recently changed the process in which Penn Labs can access menu data. We are working as fast as possible to fix this. We apologize for the inconvenience. " )
27- Text ( " In the meantime, we have directly linked the menu below. " )
43+ menuIndex = 0
44+ Task . init ( ) {
45+ await diningVM. refreshMenus ( cache: false , at: newMenuDate)
46+ }
47+ if Calendar . current. isDate ( newMenuDate, inSameDayAs: Date ( ) ) {
48+ menuIndex = getIndex ( )
49+ }
2850 }
29- // ForEach(menus, id: \.self) { menu in
30- // DiningMenuRow(for: menu)
31- // .transition(.opacity )
32- // }
33- Button {
34- showMenu . toggle ( )
35- } label : {
36- CardView {
37- HStack {
38- Text ( " Menu " )
39- . font ( . system ( size : 20 , design : . rounded ) )
40- . bold ( )
41- Spacer ( )
42- Image ( systemName : " chevron.right " )
51+ VStack {
52+ Button {
53+ showMenu . toggle ( )
54+ } label : {
55+ CardView {
56+ HStack {
57+ Text ( " Menu " )
58+ . font ( . system ( size : 20 , design : . rounded ) )
59+ . bold ( )
60+ Spacer ( )
61+ Image ( systemName : " chevron.right " )
62+ }
63+ . padding ( )
64+ . foregroundColor ( . blue ) . font ( Font . system ( size : 24 ) . weight ( . bold ) )
4365 }
44- . padding ( )
45- . foregroundColor ( . blue) . font ( Font . system ( size: 24 ) . weight ( . bold) )
66+ . frame ( height: 24 )
67+ . padding ( [ . top, . bottom] )
68+ }
69+ . sheet ( isPresented: $showMenu) {
70+ MenuWebView ( url: URL ( string: DiningVenue . menuUrlDict [ id] ?? " https://university-of-pennsylvania.cafebonappetit.com/ " ) !)
71+ }
72+ if menus. count > 0 {
73+ Picker ( " Menu " , selection: self . $menuIndex) {
74+ ForEach ( 0 ..< menus. count, id: \. self) {
75+ Text ( menus [ $0] . service)
76+ }
77+ }
78+ . pickerStyle ( SegmentedPickerStyle ( ) )
79+ DiningMenuRow ( diningMenu: menus [ menuIndex] )
80+ . transition ( . opacity)
4681 }
47- . frame ( height: 24 )
48- . padding ( [ . top] )
49- }
50- . sheet ( isPresented: $showMenu) {
51- MenuWebView ( url: URL ( string: DiningVenue . menuUrlDict [ id] ?? " https://university-of-pennsylvania.cafebonappetit.com/ " ) !)
5282 }
5383 }
5484}
5585
5686struct DiningVenueDetailMenuView_Previews : PreviewProvider {
57- let diningVenues : DiningMenuAPIResponse = Bundle . main. decode ( " mock_menu.json " )
87+ let diningVenues : MenuList = Bundle . main. decode ( " mock_menu.json " )
5888
5989 static var previews : some View {
6090 return NavigationView {
0 commit comments