forked from pointfreeco/swift-navigation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventory.swift
More file actions
153 lines (139 loc) · 3.88 KB
/
Copy pathInventory.swift
File metadata and controls
153 lines (139 loc) · 3.88 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import IdentifiedCollections
import SwiftUI
import SwiftUINavigation
class InventoryModel: ObservableObject {
@Published var inventory: IdentifiedArrayOf<ItemRowModel> {
didSet { self.bind() }
}
@Published var destination: Destination?
enum Destination: Equatable {
case add(Item)
case edit(Item)
}
init(
inventory: IdentifiedArrayOf<ItemRowModel> = [],
destination: Destination? = nil
) {
self.inventory = inventory
self.destination = destination
self.bind()
}
func delete(item: Item) {
_ = self.inventory.remove(id: item.id)
}
func add(item: Item) {
withAnimation {
self.inventory.append(ItemRowModel(item: item))
self.destination = nil
}
}
func addButtonTapped() {
self.destination = .add(Item(color: nil, name: "", status: .inStock(quantity: 1)))
}
func cancelButtonTapped() {
self.destination = nil
}
func cancelEditButtonTapped() {
self.destination = nil
}
func commitEdit(item: Item) {
self.inventory[id: item.id]?.item = item
self.destination = nil
}
private func bind() {
for itemRowModel in self.inventory {
itemRowModel.onDelete = { [weak self, weak itemRowModel] in
guard let self, let itemRowModel else { return }
self.delete(item: itemRowModel.item)
}
itemRowModel.onDuplicate = { [weak self] item in
guard let self else { return }
self.add(item: item)
}
itemRowModel.onTap = { [weak self, weak itemRowModel] in
guard let self, let itemRowModel else { return }
self.destination = .edit(itemRowModel.item)
}
}
}
}
struct InventoryView: View {
@ObservedObject var model: InventoryModel
var body: some View {
List {
ForEach(self.model.inventory) {
ItemRowView(model: $0)
}
}
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button("Add") { self.model.addButtonTapped() }
}
}
.navigationTitle("Inventory")
.navigationDestination(
unwrapping: self.$model.destination,
case: /InventoryModel.Destination.edit
) { $item in
ItemView(item: $item)
.navigationBarTitle("Edit")
.navigationBarBackButtonHidden(true)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") {
self.model.cancelEditButtonTapped()
}
}
ToolbarItem(placement: .primaryAction) {
Button("Save") {
self.model.commitEdit(item: item)
}
}
}
}
.sheet(
unwrapping: self.$model.destination,
case: /InventoryModel.Destination.add
) { $itemToAdd in
NavigationStack {
ItemView(item: $itemToAdd)
.navigationTitle("Add")
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") { self.model.cancelButtonTapped() }
}
ToolbarItem(placement: .primaryAction) {
Button("Save") { self.model.add(item: itemToAdd) }
}
}
}
}
}
}
struct InventoryView_Previews: PreviewProvider {
static var previews: some View {
let keyboard = Item(color: .blue, name: "Keyboard", status: .inStock(quantity: 100))
NavigationStack {
InventoryView(
model: InventoryModel(
inventory: [
ItemRowModel(
item: keyboard
),
ItemRowModel(
item: Item(color: .yellow, name: "Charger", status: .inStock(quantity: 20))
),
ItemRowModel(
item: Item(color: .green, name: "Phone", status: .outOfStock(isOnBackOrder: true))
),
ItemRowModel(
item: Item(
color: .green, name: "Headphones", status: .outOfStock(isOnBackOrder: false)
)
),
]
)
)
}
}
}