-
Notifications
You must be signed in to change notification settings - Fork 297
/
ItemRow.swift
212 lines (185 loc) · 5.45 KB
/
ItemRow.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import CasePaths
import ItemFeature
import Models
import SwiftUI
import SwiftUINavigation
import XCTestDynamicOverlay
public final class ItemRowModel: Hashable, Identifiable, ObservableObject {
@Published public var item: Item
@Published public var destination: Destination?
@Published var isSaving = false
public enum Destination: Equatable {
case deleteConfirmationAlert
case duplicate(ItemModel)
case edit(ItemModel)
}
public var commitDeletion: () -> Void = unimplemented("ItemRowModel.commitDeletion")
public var commitDuplication: (Item) -> Void = unimplemented("ItemRowModel.commitDuplication")
public var id: Item.ID { self.item.id }
public init(
destination: Destination? = nil,
item: Item
) {
self.destination = destination
self.item = item
}
public func deleteButtonTapped() {
self.destination = .deleteConfirmationAlert
}
func deleteConfirmationButtonTapped() {
self.commitDeletion()
self.destination = nil
}
public func duplicateButtonTapped() {
self.destination = .duplicate(ItemModel(item: self.item.duplicate()))
}
func commitDuplicate() {
guard case let .some(.duplicate(itemModel)) = self.destination
else { return }
self.commitDuplication(itemModel.item)
self.destination = nil
}
public func setEditNavigation(isActive: Bool) {
self.destination = isActive ? .edit(ItemModel(item: self.item)) : nil
}
@MainActor
func commitEdit() async {
guard case let .some(.edit(itemModel)) = self.destination
else { return } // TODO: precondition?
self.isSaving = true
defer { self.isSaving = false }
do {
// NB: Emulate an API request
try await Task.sleep(nanoseconds: NSEC_PER_SEC)
} catch {}
self.item = itemModel.item
self.destination = nil
}
public func cancelButtonTapped() {
self.destination = nil
}
public func hash(into hasher: inout Hasher) {
hasher.combine(self.item.id)
}
public static func == (lhs: ItemRowModel, rhs: ItemRowModel) -> Bool {
lhs === rhs
}
}
extension Item {
public func duplicate() -> Self {
Self(name: self.name, color: self.color, status: self.status)
}
}
public struct ItemRowView: View {
@ObservedObject var model: ItemRowModel
public init(model: ItemRowModel) {
self.model = model
}
public var body: some View {
NavigationLink(
unwrapping: self.$model.destination,
case: /ItemRowModel.Destination.edit
) { isActive in
self.model.setEditNavigation(isActive: isActive)
} destination: { $itemModel in
ItemView(model: itemModel)
.navigationBarTitle("Edit")
.navigationBarBackButtonHidden(true)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") {
self.model.cancelButtonTapped()
}
}
ToolbarItem(placement: .primaryAction) {
HStack {
if self.model.isSaving {
ProgressView()
}
Button("Save") {
Task { await self.model.commitEdit() }
}
}
.disabled(self.model.isSaving)
}
}
} label: {
HStack {
VStack(alignment: .leading) {
Text(self.model.item.name)
switch self.model.item.status {
case let .inStock(quantity):
Text("In stock: \(quantity)")
case let .outOfStock(isOnBackOrder):
Text("Out of stock" + (isOnBackOrder ? ": on back order" : ""))
}
}
Spacer()
if let color = self.model.item.color {
Rectangle()
.frame(width: 30, height: 30)
.foregroundColor(color.swiftUIColor)
.border(Color.black, width: 1)
}
Button(action: { self.model.duplicateButtonTapped() }) {
Image(systemName: "square.fill.on.square.fill")
}
.padding(.leading)
Button(action: { self.model.deleteButtonTapped() }) {
Image(systemName: "trash.fill")
}
.padding(.leading)
}
.buttonStyle(.plain)
.foregroundColor(self.model.item.status.isInStock ? nil : Color.gray)
}
.alert(
title: { _ in Text(self.model.item.name) },
unwrapping: self.$model.destination,
case: /ItemRowModel.Destination.deleteConfirmationAlert,
actions: { _ in
Button("Delete", role: .destructive) {
self.model.deleteConfirmationButtonTapped()
}
},
message: { _ in
Text("Are you sure you want to delete this item?")
}
)
.popover(
unwrapping: self.$model.destination,
case: /ItemRowModel.Destination.duplicate
) { $itemModel in
NavigationView {
ItemView(model: itemModel)
.navigationBarTitle("Duplicate")
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") {
self.model.cancelButtonTapped()
}
}
ToolbarItem(placement: .primaryAction) {
Button("Add") {
self.model.commitDuplicate()
}
}
}
}
.frame(minWidth: 300, minHeight: 500)
}
}
}
struct ItemRowPreviews: PreviewProvider {
static var previews: some View {
NavigationView {
List {
ItemRowView(
model: ItemRowModel(
item: Item(name: "Keyboard", status: .inStock(quantity: 1))
)
)
}
}
}
}