Skip to content

Commit 8ff6a47

Browse files
committed
14869 Don’t set the loading state when refreshing
When we pull to refresh, the built in controller shows a loading indicator. If we set our state to `loading`, we also showed a ghost cell at the bottom of the list. On a pull to refresh, it’s unlikely that the list is going to get longer – more likely, the full first page of results will be shown, so it’ll be the same length or shorter. This commit prevents us from showing the ghost cell on refresh.
1 parent 6bd0f05 commit 8ff6a47

File tree

5 files changed

+49
-12
lines changed

5 files changed

+49
-12
lines changed

WooCommerce/Classes/POS/Controllers/PointOfSaleItemsController.swift

Lines changed: 38 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,16 @@ protocol PointOfSaleItemsControllerProtocol {
3133

3234
@MainActor
3335
func loadItems(base: ItemListBaseItem) async {
36+
setLoadingState(base: base)
37+
await loadFirstPage(base: base)
38+
}
39+
40+
@MainActor func refreshItems(base: ItemListBaseItem) async {
41+
await loadFirstPage(base: base)
42+
}
43+
44+
@MainActor
45+
private func loadFirstPage(base: ItemListBaseItem) async {
3446
switch base {
3547
case .root:
3648
await loadRootItems()
@@ -41,13 +53,6 @@ protocol PointOfSaleItemsControllerProtocol {
4153

4254
@MainActor
4355
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-
5156
do {
5257
try await paginationTracker.resync { [weak self] pageNumber in
5358
guard let self else { return true }
@@ -95,9 +100,6 @@ protocol PointOfSaleItemsControllerProtocol {
95100

96101
@MainActor
97102
private func loadChildItems(for parent: POSItem) async {
98-
let items = itemsViewState.itemsStack.itemStates[parent]?.items ?? []
99-
updateState(for: parent, to: .loading(items))
100-
101103
let paginationTracker = paginationTracker(for: parent)
102104
do {
103105
try await paginationTracker.resync { [weak self] pageNumber in
@@ -155,6 +157,32 @@ protocol PointOfSaleItemsControllerProtocol {
155157
}
156158
}
157159

160+
@available(iOS 17.0, *)
161+
private extension PointOfSaleItemsController {
162+
func setLoadingState(base: ItemListBaseItem) {
163+
switch base {
164+
case .root:
165+
setRootLoadingState()
166+
case .parent(let parent):
167+
setChildLoadingState(for: parent)
168+
}
169+
}
170+
171+
func setRootLoadingState() {
172+
let items = itemsViewState.itemsStack.root.items
173+
if items.isEmpty {
174+
itemsViewState.containerState = .loading
175+
} else {
176+
itemsViewState.itemsStack.root = .loading(items)
177+
}
178+
}
179+
180+
func setChildLoadingState(for parent: POSItem) {
181+
let items = itemsViewState.itemsStack.itemStates[parent]?.items ?? []
182+
updateState(for: parent, to: .loading(items))
183+
}
184+
}
185+
158186
@available(iOS 17.0, *)
159187
private extension PointOfSaleItemsController {
160188
/// 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/ItemListView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ private extension ItemListView {
147147
ChildItemList(parentItem: parentItem, title: parentProduct.name)
148148
.refreshable {
149149
ServiceLocator.analytics.track(.pointOfSaleVariationsPullToRefresh)
150-
await posModel.loadItems(base: .parent(parentItem))
150+
await posModel.refreshItems(base: .parent(parentItem))
151151
}
152152
default:
153153
EmptyView()

WooCommerce/Classes/POS/Presentation/PointOfSaleDashboardView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ struct PointOfSaleDashboardView: View {
9090
ItemListView()
9191
.refreshable {
9292
ServiceLocator.analytics.track(.pointOfSaleProductsPullToRefresh)
93-
await posModel.loadItems(base: .root)
93+
await posModel.refreshItems(base: .root)
9494
}
9595
.accessibilitySortPriority(2)
9696
.transition(.move(edge: .leading))

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: [:]))

0 commit comments

Comments
 (0)