-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuView.swift
More file actions
84 lines (73 loc) · 1.95 KB
/
MenuView.swift
File metadata and controls
84 lines (73 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
// MenuView.swift
// NeppatsrevCoffee
//
// Created by Ryan Mackintosh on 3/5/2024.
//
import SwiftUI
// Displays the main menu of the app, listing available drinks and snacks.
struct MenuView: View {
@EnvironmentObject var productManager: ProductManager // Access to the product data.
@EnvironmentObject var cartManager: CartManager // Access to the cart management.
var body: some View {
// Wrapping the content inside a navigation view for hierarchical navigation.
NavigationView {
// List to display sections and items.
List {
// Section for drinks.
Section(header: Text("Drinks")) {
ForEach(productManager.drinks) { drink in
MenuItemView(item: drink)
}
}
// Section for snacks.
Section(header: Text("Snacks")) {
ForEach(productManager.snacks) { snack in
MenuItemView(item: snack)
}
}
}
.navigationTitle("Mobile Order")
}
}
}
// Represents a single menu item within the list.
struct MenuItemView: View {
var item: CartItem
@EnvironmentObject var cartManager: CartManager
@EnvironmentObject var productManager: ProductManager
var body: some View {
// Link to navigate to the detailed view of the item.
NavigationLink(destination: destinationView) {
HStack {
Image(item.imageName)
.resizable()
.frame(width: 50, height: 50)
.cornerRadius(5)
VStack(alignment: .leading) {
Text(item.name).bold()
Text(item.description)
.font(.caption)
.foregroundColor(.gray)
}
Spacer()
}
}
}
// Determines the destination view based on the type of item (drink or snack).
@ViewBuilder
private var destinationView: some View {
if item.type == .drink {
DrinkView(drink: productManager.convertToDrink(item))
} else {
SnackView(item: item)
}
}
}
struct MenuView_Previews: PreviewProvider {
static var previews: some View {
MenuView()
.environmentObject(ProductManager())
.environmentObject(CartManager())
}
}