Skip to content

Commit

Permalink
[Woo POS] don't set loading state for pull to refresh (#15079)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshheald authored Feb 7, 2025
2 parents 68abe4b + 2c19458 commit 9068893
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ protocol PointOfSaleItemsControllerProtocol {
var itemsViewState: ItemsViewState { get }
/// Loads the first page of items for a given base item.
func loadItems(base: ItemListBaseItem) async
/// Refreshes the items for a given base item – will result in showing only the first page.
func refreshItems(base: ItemListBaseItem) async
/// Loads the next page of items for a given base item.
func loadNextItems(base: ItemListBaseItem) async
}
Expand All @@ -31,6 +33,17 @@ protocol PointOfSaleItemsControllerProtocol {

@MainActor
func loadItems(base: ItemListBaseItem) async {
setLoadingState(base: base)
await loadFirstPage(base: base)
}

@MainActor
func refreshItems(base: ItemListBaseItem) async {
await loadFirstPage(base: base)
}

@MainActor
private func loadFirstPage(base: ItemListBaseItem) async {
switch base {
case .root:
await loadRootItems()
Expand All @@ -41,13 +54,6 @@ protocol PointOfSaleItemsControllerProtocol {

@MainActor
private func loadRootItems() async {
let items = itemsViewState.itemsStack.root.items
if items.isEmpty {
itemsViewState.containerState = .loading
} else {
itemsViewState.itemsStack.root = .loading(items)
}

do {
try await paginationTracker.resync { [weak self] pageNumber in
guard let self else { return true }
Expand Down Expand Up @@ -95,9 +101,6 @@ protocol PointOfSaleItemsControllerProtocol {

@MainActor
private func loadChildItems(for parent: POSItem) async {
let items = itemsViewState.itemsStack.itemStates[parent]?.items ?? []
updateState(for: parent, to: .loading(items))

let paginationTracker = paginationTracker(for: parent)
do {
try await paginationTracker.resync { [weak self] pageNumber in
Expand Down Expand Up @@ -155,6 +158,32 @@ protocol PointOfSaleItemsControllerProtocol {
}
}

@available(iOS 17.0, *)
private extension PointOfSaleItemsController {
func setLoadingState(base: ItemListBaseItem) {
switch base {
case .root:
setRootLoadingState()
case .parent(let parent):
setChildLoadingState(for: parent)
}
}

func setRootLoadingState() {
let items = itemsViewState.itemsStack.root.items
if items.isEmpty {
itemsViewState.containerState = .loading
} else {
itemsViewState.itemsStack.root = .loading(items)
}
}

func setChildLoadingState(for parent: POSItem) {
let items = itemsViewState.itemsStack.itemStates[parent]?.items ?? []
updateState(for: parent, to: .loading(items))
}
}

@available(iOS 17.0, *)
private extension PointOfSaleItemsController {
/// Fetches items given a page number and appends new unique items to the `allItems` array.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ extension PointOfSaleAggregateModel {
await itemsController.loadItems(base: base)
}

@MainActor
func refreshItems(base: ItemListBaseItem) async {
await itemsController.refreshItems(base: base)
}

@MainActor
func loadNextItems(base: ItemListBaseItem) async {
await itemsController.loadNextItems(base: base)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ private extension ChildItemList {
ItemList(state: state,
node: .parent(parentItem))
.transition(.opacity)
.refreshable {
ServiceLocator.analytics.track(.pointOfSaleVariationsPullToRefresh)
await posModel.refreshItems(base: .parent(parentItem))
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions WooCommerce/Classes/POS/Presentation/ItemListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,17 @@ private extension ItemListView {
bannerCardView
}
}
.refreshable {
ServiceLocator.analytics.track(.pointOfSaleProductsPullToRefresh)
await posModel.refreshItems(base: .root)
}
}

@ViewBuilder
func childListView(parentItem: POSItem) -> some View {
switch parentItem {
case let .variableParentProduct(parentProduct):
ChildItemList(parentItem: parentItem, title: parentProduct.name)
.refreshable {
ServiceLocator.analytics.track(.pointOfSaleVariationsPullToRefresh)
await posModel.loadItems(base: .parent(parentItem))
}
default:
EmptyView()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ struct PointOfSaleDashboardView: View {
HStack {
if posModel.orderStage == .building {
ItemListView()
.refreshable {
ServiceLocator.analytics.track(.pointOfSaleProductsPullToRefresh)
await posModel.loadItems(base: .root)
}
.accessibilitySortPriority(2)
.transition(.move(edge: .leading))
}
Expand Down
4 changes: 4 additions & 0 deletions WooCommerce/Classes/POS/Utils/PreviewHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ final class PointOfSalePreviewItemsController: PointOfSaleItemsControllerProtoco
}
}

func refreshItems(base: ItemListBaseItem) async {
await loadItems(base: base)
}

func loadNextItems(base: ItemListBaseItem) async {
itemsViewState = ItemsViewState(containerState: .content, itemsStack: ItemsStackState(root: .loading(mockItems),
itemStates: [:]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ final class MockPointOfSaleItemsController: PointOfSaleItemsControllerProtocol {

func loadItems(base: ItemListBaseItem) async { }

func refreshItems(base: WooCommerce.ItemListBaseItem) async { }

func loadNextItems(base: ItemListBaseItem) async { }
}

0 comments on commit 9068893

Please sign in to comment.