-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathItemListView.swift
294 lines (259 loc) · 10 KB
/
ItemListView.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import SwiftUI
import enum Yosemite.POSItem
import protocol Yosemite.POSOrderableItem
@available(iOS 17.0, *)
struct ItemListView: View {
@Environment(\.dynamicTypeSize) private var dynamicTypeSize
@Environment(PointOfSaleAggregateModel.self) private var posModel
@State private var showSimpleProductsModal: Bool = false
private var itemListState: ItemListState {
posModel.itemsViewState.itemsStack.root
}
@AppStorage(BannerState.isSimpleProductsOnlyBannerDismissedKey)
private var isHeaderBannerDismissed: Bool = false
private var shouldShowCoupons: Bool {
ServiceLocator.featureFlagService.isFeatureFlagEnabled(.enableCouponsInPointOfSale)
}
@State private var selectedItemType: ItemType = .products
@State private var showCouponCreationModal: Bool = false
var body: some View {
if #available(iOS 18.0, *) {
NavigationStack {
content
}
} else {
// On iOS 17, NavigationStack causes memory leaks when the POS is closed, NavigationView is a fallback.
NavigationView {
content
}
.navigationViewStyle(.stack)
}
}
var content: some View {
VStack(spacing: 0) {
headerView
HStack {
Button(action: {
displayItemType(.products)
}, label: {
Text("Products")
})
Button(action: {
displayItemType(.coupons)
}, label: {
Text("Coupons")
})
Spacer()
Button(action: {
showCouponCreationModal = true
}, label: {
Text(Image(systemName: "plus.circle.fill"))
})
.font(.posButtonSymbolLarge)
.foregroundStyle(Color.posOnSurface)
.renderedIf(selectedItemType == .coupons)
}
.padding(POSPadding.medium)
.renderedIf(shouldShowCoupons)
switch itemListState {
case .loading(let items),
.loaded(let items, _),
.inlineError(let items, _):
listView(items)
case .error:
// Currently unused, but this will show errors that are displayed inline with previously
// loaded items, e.g. when loading a new page or refreshing.
EmptyView()
}
}
// N.B. This navigationDestination causes a runtime warning in iOS 17, and is ignored. On iOS 17,
// the navigation is handled in a NavigationLink in ItemList.swift. Avoiding the warning is impractical.
.navigationDestination(for: POSItem.self, destination: { item in
childListView(parentItem: item)
})
.background(Color.posSurface)
.accessibilityElement(children: .contain)
.posModal(isPresented: $showSimpleProductsModal) {
SimpleProductsOnlyInformation(isPresented: $showSimpleProductsModal)
}
.posCouponCreationSheet(isPresented: $showCouponCreationModal, onSuccess: { couponItem in
Task { @MainActor in
posModel.addToCart(couponItem)
await posModel.refreshItems(base: .root)
}
})
}
}
/// View Helpers
///
@available(iOS 17.0, *)
private extension ItemListView {
@ViewBuilder
var headerView: some View {
VStack {
POSPageHeaderView(title: Localization.title, trailingContent: {
Button(action: {
ServiceLocator.analytics.track(.pointOfSaleSimpleProductsExplanationDialogShown)
showSimpleProductsModal = true
}, label: {
Text(Image(systemName: "info.circle"))
.font(.posButtonSymbolLarge)
.foregroundStyle(Color.posOnSurface)
.padding(Constants.infoIconInset)
})
.renderedIf(!shouldShowHeaderBanner)
})
if !dynamicTypeSize.isAccessibilitySize, shouldShowHeaderBanner {
bannerCardView
.padding(.horizontal, Constants.bannerCardPadding)
.dynamicTypeSize(...DynamicTypeSize.accessibility1)
}
}
}
var bannerCardView: some View {
POSNoticeView(
title: headerBannerTitle,
icon: Image(systemName: "info.circle"),
onDismiss: {
isHeaderBannerDismissed = true
},
onTap: {
showSimpleProductsModal = true
}
) {
VStack(alignment: .leading, spacing: Constants.bannerTextSpacing) {
Text(headerBannerSubtitle)
bannerHintAndLearnMoreText
}
}
.padding(.bottom, Constants.bannerCardPadding)
}
private var bannerHintAndLearnMoreText: some View {
Text("\(headerBannerHint) \(Localization.headerBannerLearnMoreHint)")
.font(.posBodySmallBold)
.foregroundColor(Color(.posPrimary))
}
@ViewBuilder
func listView(_ items: [POSItem]) -> some View {
ItemList(state: itemListState) {
if dynamicTypeSize.isAccessibilitySize, shouldShowHeaderBanner {
bannerCardView
}
}
.refreshable {
ServiceLocator.analytics.track(.pointOfSaleProductsPullToRefresh)
await posModel.refreshItems(base: .root)
}
}
@ViewBuilder
func childListView(parentItem: POSItem) -> some View {
// Note that navigation is handled by the ItemList in iOS 17, so any changes to this should be reflected in ItemListRow.
switch parentItem {
case let .variableParentProduct(parentProduct):
ChildItemList(parentItem: parentItem, title: parentProduct.name)
default:
EmptyView()
}
}
}
@available(iOS 17.0, *)
private extension ItemListView {
var shouldShowHeaderBanner: Bool {
itemListState.eligibleToShowSimpleProductsBanner && !isHeaderBannerDismissed
}
func displayItemType(_ itemType: ItemType) {
selectedItemType = itemType
Task { @MainActor in
await posModel.switchToItemType(itemType)
}
}
}
private extension ItemListState {
var eligibleToShowSimpleProductsBanner: Bool {
switch self {
case .loading,
.loaded,
.inlineError:
return true
case .error:
return false
}
}
}
/// Constants
///
@available(iOS 17.0, *)
private extension ItemListView {
enum Constants {
static let infoIconInset: EdgeInsets = .init(top: 0, leading: 6, bottom: 0, trailing: 6)
static let bannerCardPadding: CGFloat = POSPadding.medium
static let bannerTextSpacing: CGFloat = POSSpacing.xSmall
}
enum BannerState {
static let isSimpleProductsOnlyBannerDismissedKey = "isSimpleProductsOnlyBannerDismissed"
}
var headerBannerTitle: String {
Localization.headerBannerTitleSimpleAndVariable
}
var headerBannerSubtitle: String {
Localization.headerBannerSubtitleSimpleAndVariable
}
var headerBannerHint: String {
Localization.headerBannerHintSimpleAndVariable
}
enum Localization {
static let title = NSLocalizedString(
"pos.itemlistview.title",
value: "Products",
comment: "Title at the top of the Point of Sale product selector screen."
)
static let headerBannerTitleSimpleAndVariable = NSLocalizedString(
"pos.itemlistview.headerBanner.title.simpleAndVariable",
value: "Showing simple and variable products only",
comment: "Title of the product selector header banner, which explains current POS limitations"
)
static let headerBannerSubtitleSimpleAndVariable = NSLocalizedString(
"pos.itemlistview.headerBanner.subtitle.simpleAndVariable",
value: "Only simple and variable non-downloadable products can be used with POS right now.",
comment: "Subtitle of the product selector header banner, which explains current POS limitations"
)
static let headerBannerHintSimpleAndVariable = NSLocalizedString(
"pos.itemlistview.headerBanner.hint.simpleAndVariable",
value: "Other product types will become available in future updates.",
comment: "Additional text within the product selector header banner, which explains current POS limitations"
)
static let headerBannerLearnMoreHint = NSLocalizedString(
"pos.itemlistview.headerBanner.learnMoreHint",
value: "Learn More",
comment: "Link to more information within the product selector header banner, which explains current POS limitations"
)
}
}
#if DEBUG
@available(iOS 17.0, *)
#Preview("Loaded with all product types") {
let itemsController = PointOfSalePreviewItemsController()
Task { @MainActor in
await itemsController.loadItems(base: .root)
}
let posModel = PointOfSaleAggregateModel(
itemsController: itemsController,
couponsController: PointOfSalePreviewItemsController(),
cardPresentPaymentService: CardPresentPaymentPreviewService(),
orderController: PointOfSalePreviewOrderController(),
collectOrderPaymentAnalyticsTracker: POSCollectOrderPaymentAnalytics())
return ItemListView()
.environment(posModel)
}
@available(iOS 17.0, *)
#Preview("Loading") {
let posModel = PointOfSaleAggregateModel(
itemsController: PointOfSalePreviewItemsController(),
couponsController: PointOfSalePreviewItemsController(),
cardPresentPaymentService: CardPresentPaymentPreviewService(),
orderController: PointOfSalePreviewOrderController(),
collectOrderPaymentAnalyticsTracker: POSCollectOrderPaymentAnalytics())
return ItemListView()
.environment(posModel)
}
#endif