Skip to content

Commit 9068893

Browse files
authored
[Woo POS] don't set loading state for pull to refresh (#15079)
2 parents 68abe4b + 2c19458 commit 9068893

File tree

7 files changed

+58
-18
lines changed

7 files changed

+58
-18
lines changed

WooCommerce/Classes/POS/Controllers/PointOfSaleItemsController.swift

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ protocol PointOfSaleItemsControllerProtocol {
1111
var itemsViewState: ItemsViewState { get }
1212
/// Loads the first page of items for a given base item.
1313
func loadItems(base: ItemListBaseItem) async
14+
/// Refreshes the items for a given base item – will result in showing only the first page.
15+
func refreshItems(base: ItemListBaseItem) async
1416
/// Loads the next page of items for a given base item.
1517
func loadNextItems(base: ItemListBaseItem) async
1618
}
@@ -31,6 +33,17 @@ protocol PointOfSaleItemsControllerProtocol {
3133

3234
@MainActor
3335
func loadItems(base: ItemListBaseItem) async {
36+
setLoadingState(base: base)
37+
await loadFirstPage(base: base)
38+
}
39+
40+
@MainActor
41+
func refreshItems(base: ItemListBaseItem) async {
42+
await loadFirstPage(base: base)
43+
}
44+
45+
@MainActor
46+
private func loadFirstPage(base: ItemListBaseItem) async {
3447
switch base {
3548
case .root:
3649
await loadRootItems()
@@ -41,13 +54,6 @@ protocol PointOfSaleItemsControllerProtocol {
4154

4255
@MainActor
4356
private func loadRootItems() async {
44-
let items = itemsViewState.itemsStack.root.items
45-
if items.isEmpty {
46-
itemsViewState.containerState = .loading
47-
} else {
48-
itemsViewState.itemsStack.root = .loading(items)
49-
}
50-
5157
do {
5258
try await paginationTracker.resync { [weak self] pageNumber in
5359
guard let self else { return true }
@@ -95,9 +101,6 @@ protocol PointOfSaleItemsControllerProtocol {
95101

96102
@MainActor
97103
private func loadChildItems(for parent: POSItem) async {
98-
let items = itemsViewState.itemsStack.itemStates[parent]?.items ?? []
99-
updateState(for: parent, to: .loading(items))
100-
101104
let paginationTracker = paginationTracker(for: parent)
102105
do {
103106
try await paginationTracker.resync { [weak self] pageNumber in
@@ -155,6 +158,32 @@ protocol PointOfSaleItemsControllerProtocol {
155158
}
156159
}
157160

161+
@available(iOS 17.0, *)
162+
private extension PointOfSaleItemsController {
163+
func setLoadingState(base: ItemListBaseItem) {
164+
switch base {
165+
case .root:
166+
setRootLoadingState()
167+
case .parent(let parent):
168+
setChildLoadingState(for: parent)
169+
}
170+
}
171+
172+
func setRootLoadingState() {
173+
let items = itemsViewState.itemsStack.root.items
174+
if items.isEmpty {
175+
itemsViewState.containerState = .loading
176+
} else {
177+
itemsViewState.itemsStack.root = .loading(items)
178+
}
179+
}
180+
181+
func setChildLoadingState(for parent: POSItem) {
182+
let items = itemsViewState.itemsStack.itemStates[parent]?.items ?? []
183+
updateState(for: parent, to: .loading(items))
184+
}
185+
}
186+
158187
@available(iOS 17.0, *)
159188
private extension PointOfSaleItemsController {
160189
/// Fetches items given a page number and appends new unique items to the `allItems` array.

WooCommerce/Classes/POS/Models/PointOfSaleAggregateModel.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ extension PointOfSaleAggregateModel {
9393
await itemsController.loadItems(base: base)
9494
}
9595

96+
@MainActor
97+
func refreshItems(base: ItemListBaseItem) async {
98+
await itemsController.refreshItems(base: base)
99+
}
100+
96101
@MainActor
97102
func loadNextItems(base: ItemListBaseItem) async {
98103
await itemsController.loadNextItems(base: base)

WooCommerce/Classes/POS/Presentation/Item Selector/ChildItemList.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ private extension ChildItemList {
6767
ItemList(state: state,
6868
node: .parent(parentItem))
6969
.transition(.opacity)
70+
.refreshable {
71+
ServiceLocator.analytics.track(.pointOfSaleVariationsPullToRefresh)
72+
await posModel.refreshItems(base: .parent(parentItem))
73+
}
7074
}
7175
}
7276

WooCommerce/Classes/POS/Presentation/ItemListView.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,17 @@ private extension ItemListView {
138138
bannerCardView
139139
}
140140
}
141+
.refreshable {
142+
ServiceLocator.analytics.track(.pointOfSaleProductsPullToRefresh)
143+
await posModel.refreshItems(base: .root)
144+
}
141145
}
142146

143147
@ViewBuilder
144148
func childListView(parentItem: POSItem) -> some View {
145149
switch parentItem {
146150
case let .variableParentProduct(parentProduct):
147151
ChildItemList(parentItem: parentItem, title: parentProduct.name)
148-
.refreshable {
149-
ServiceLocator.analytics.track(.pointOfSaleVariationsPullToRefresh)
150-
await posModel.loadItems(base: .parent(parentItem))
151-
}
152152
default:
153153
EmptyView()
154154
}

WooCommerce/Classes/POS/Presentation/PointOfSaleDashboardView.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,6 @@ struct PointOfSaleDashboardView: View {
8888
HStack {
8989
if posModel.orderStage == .building {
9090
ItemListView()
91-
.refreshable {
92-
ServiceLocator.analytics.track(.pointOfSaleProductsPullToRefresh)
93-
await posModel.loadItems(base: .root)
94-
}
9591
.accessibilitySortPriority(2)
9692
.transition(.move(edge: .leading))
9793
}

WooCommerce/Classes/POS/Utils/PreviewHelpers.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ final class PointOfSalePreviewItemsController: PointOfSaleItemsControllerProtoco
7575
}
7676
}
7777

78+
func refreshItems(base: ItemListBaseItem) async {
79+
await loadItems(base: base)
80+
}
81+
7882
func loadNextItems(base: ItemListBaseItem) async {
7983
itemsViewState = ItemsViewState(containerState: .content, itemsStack: ItemsStackState(root: .loading(mockItems),
8084
itemStates: [:]))

WooCommerce/WooCommerceTests/POS/Mocks/MockPointOfSaleItemsService.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ final class MockPointOfSaleItemsController: PointOfSaleItemsControllerProtocol {
1111

1212
func loadItems(base: ItemListBaseItem) async { }
1313

14+
func refreshItems(base: WooCommerce.ItemListBaseItem) async { }
15+
1416
func loadNextItems(base: ItemListBaseItem) async { }
1517
}

0 commit comments

Comments
 (0)