-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathApp.swift
78 lines (69 loc) · 1.64 KB
/
App.swift
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
import SwiftUI
@main
struct InventoryApp: App {
let model = AppModel(
inventoryModel: InventoryModel(
inventory: [
ItemRowModel(
item: Item(color: .red, name: "Keyboard", status: .inStock(quantity: 100))
),
ItemRowModel(
item: Item(color: .blue, name: "Mouse", status: .inStock(quantity: 200))
),
ItemRowModel(
item: Item(color: .green, name: "Monitor", status: .inStock(quantity: 20))
),
ItemRowModel(
item: Item(color: .yellow, name: "Chair", status: .outOfStock(isOnBackOrder: true))
),
]
)
)
var body: some Scene {
WindowGroup {
AppView(model: self.model)
}
}
}
@Observable
class AppModel {
var inventoryModel: InventoryModel
var selectedTab: Tab
init(
inventoryModel: InventoryModel,
selectedTab: Tab = .first
) {
self.inventoryModel = inventoryModel
self.selectedTab = selectedTab
}
enum Tab {
case first
case inventory
}
}
struct AppView: View {
@State var model: AppModel
var body: some View {
TabView(selection: self.$model.selectedTab) {
Button {
self.model.selectedTab = .inventory
} label: {
Text("Go to inventory tab")
}
.tag(AppModel.Tab.first)
.tabItem {
Label("First", systemImage: "arrow.forward")
}
NavigationStack {
InventoryView(model: self.model.inventoryModel)
}
.tag(AppModel.Tab.inventory)
.tabItem {
Label("Inventory", systemImage: "list.clipboard.fill")
}
}
}
}
#Preview {
AppView(model: AppModel(inventoryModel: InventoryModel()))
}