-
Notifications
You must be signed in to change notification settings - Fork 131
/
Inventory.swift
151 lines (137 loc) · 3.54 KB
/
Inventory.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
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
import IdentifiedCollections
import SwiftUI
import SwiftUINavigation
@Observable
class InventoryModel {
var inventory: IdentifiedArrayOf<ItemRowModel> {
didSet { bind() }
}
var destination: Destination?
@CasePathable
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) {
_ = inventory.remove(id: item.id)
}
func add(item: Item) {
withAnimation {
inventory.append(ItemRowModel(item: item))
destination = nil
}
}
func addButtonTapped() {
destination = .add(Item(color: nil, name: "", status: .inStock(quantity: 1)))
}
func cancelButtonTapped() {
destination = nil
}
func cancelEditButtonTapped() {
destination = nil
}
func commitEdit(item: Item) {
inventory[id: item.id]?.item = item
destination = nil
}
private func bind() {
for itemRowModel in inventory {
itemRowModel.onDelete = { [weak self, weak itemRowModel] in
guard let self, let itemRowModel else { return }
delete(item: itemRowModel.item)
}
itemRowModel.onDuplicate = { [weak self] item in
guard let self else { return }
add(item: item)
}
itemRowModel.onTap = { [weak self, weak itemRowModel] in
guard let self, let itemRowModel else { return }
destination = .edit(itemRowModel.item)
}
}
}
}
struct InventoryView: View {
@State var model: InventoryModel
var body: some View {
List {
ForEach(model.inventory) {
ItemRowView(model: $0)
}
}
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button("Add") { model.addButtonTapped() }
}
}
.navigationTitle("Inventory")
.navigationDestination(item: $model.destination.edit) { $item in
ItemView(item: $item)
.navigationBarTitle("Edit")
.navigationBarBackButtonHidden(true)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") {
model.cancelEditButtonTapped()
}
}
ToolbarItem(placement: .primaryAction) {
Button("Save") {
model.commitEdit(item: item)
}
}
}
}
.sheet(item: $model.destination.add) { $itemToAdd in
NavigationStack {
ItemView(item: $itemToAdd)
.navigationTitle("Add")
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") { model.cancelButtonTapped() }
}
ToolbarItem(placement: .primaryAction) {
Button("Save") { model.add(item: itemToAdd) }
}
}
}
}
}
}
#Preview {
let keyboard = Item(
color: .blue,
name: "Keyboard",
status: .inStock(quantity: 100)
)
return 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)
)
),
]
)
)
}
}