Skip to content

Commit

Permalink
Update PointOfSaleItemServiceProtocol.providePointOfSaleItems to al…
Browse files Browse the repository at this point in the history
…so return `PagedItems`.
  • Loading branch information
jaclync committed Dec 19, 2024
1 parent a7e4cf3 commit 11b2b1f
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 16 deletions.
5 changes: 5 additions & 0 deletions Networking/Networking/Remote/Remote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,11 @@ private extension Remote {
public struct PagedItems<T> {
public let items: [T]
public let hasMorePages: Bool

public init(items: [T], hasMorePages: Bool) {
self.items = items
self.hasMorePages = hasMorePages
}
}

// MARK: - Constants!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ class PointOfSaleItemsController: PointOfSaleItemsControllerProtocol {
/// - Returns: A boolean that indicates whether there is next page for the paginated items.
@MainActor
private func fetchItems(pageNumber: Int) async throws -> Bool {
let (newItems, hasNextPage) = try await itemProvider.providePointOfSaleItems(pageNumber: pageNumber)
let pagedItems = try await itemProvider.providePointOfSaleItems(pageNumber: pageNumber)
let newItems = pagedItems.items
let uniqueNewItems = newItems.filter { newItem in
!allItems.contains(newItem)
}
allItems.append(contentsOf: uniqueNewItems)
return hasNextPage
return pagedItems.hasMorePages
}

private func updateItemListStateAfterLoadAttempt() {
Expand Down
5 changes: 3 additions & 2 deletions WooCommerce/Classes/POS/Utils/PreviewHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import protocol Yosemite.POSOrderableItem
import protocol Yosemite.OrderSyncProductTypeProtocol
import struct Yosemite.OrderSyncProductInput
import enum Yosemite.ProductType
import struct Yosemite.PagedItems
import struct Yosemite.ProductBundleItem
import struct Yosemite.OrderItem
import Combine
Expand Down Expand Up @@ -37,8 +38,8 @@ struct POSProductPreview: POSOrderableItem, Equatable {
}

final class PointOfSalePreviewItemService: PointOfSaleItemServiceProtocol {
func providePointOfSaleItems(pageNumber: Int) async throws -> (items: [POSItem], hasNextPage: Bool) {
(items: [], hasNextPage: true)
func providePointOfSaleItems(pageNumber: Int) async throws -> PagedItems<POSItem> {
.init(items: [], hasMorePages: true)
}

func providePointOfSaleItems() -> [POSItem] {
Expand Down
1 change: 1 addition & 0 deletions Yosemite/Yosemite/Model/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ public typealias ShippingMethod = Networking.ShippingMethod
public typealias Site = Networking.Site
public typealias SiteVisibility = Networking.SiteVisibility
public typealias SiteAPI = Networking.SiteAPI
public typealias PagedItems = Networking.PagedItems
public typealias Post = Networking.Post
public typealias SitePlugin = Networking.SitePlugin
public typealias SitePluginStatusEnum = Networking.SitePluginStatusEnum
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ public extension Sequence where Element == POSOrderableItem {
}

public protocol PointOfSaleItemServiceProtocol {
func providePointOfSaleItems(pageNumber: Int) async throws -> (items: [POSItem], hasNextPage: Bool)
func providePointOfSaleItems(pageNumber: Int) async throws -> PagedItems<POSItem>
}

// Default implementation for convenience, so we do not need to pass the first page explicitly
// if no pageNumber is given.
extension PointOfSaleItemServiceProtocol {
func providePointOfSaleItems(pageNumber: Int = 1) async throws -> (items: [POSItem], hasNextPage: Bool) {
func providePointOfSaleItems(pageNumber: Int = 1) async throws -> PagedItems<POSItem> {
try await providePointOfSaleItems(pageNumber: pageNumber)
}
}
6 changes: 3 additions & 3 deletions Yosemite/Yosemite/PointOfSale/PointOfSaleProductService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ public final class PointOfSaleProductService: PointOfSaleItemServiceProtocol {
///
/// - pageNumber: Number of the page that should be retrieved. If none given, defaults to 1
///
public func providePointOfSaleItems(pageNumber: Int = 1) async throws -> (items: [POSItem], hasNextPage: Bool) {
public func providePointOfSaleItems(pageNumber: Int = 1) async throws -> PagedItems<POSItem> {
let productTypes: [ProductType] = isVariableProductsFeatureEnabled ?
[.simple, .variable] : [.simple]
let pagedProducts = try await productsRemote.loadProductsForPointOfSale(for: siteID, productTypes: productTypes, pageNumber: pageNumber)
let products = pagedProducts.items

if pageNumber != 1 && products.count == 0 {
return ([], false)
return .init(items: [], hasMorePages: false)
}

let eligibilityCriteria: [(Product) -> Bool] = [
Expand All @@ -57,7 +57,7 @@ public final class PointOfSaleProductService: PointOfSaleItemServiceProtocol {
]
let filteredProducts = filterProducts(products: products, using: eligibilityCriteria)

return (items: mapProductsToPOSItems(products: filteredProducts), hasNextPage: pagedProducts.hasMorePages)
return .init(items: mapProductsToPOSItems(products: filteredProducts), hasMorePages: pagedProducts.hasMorePages)
}

// Maps result to POSItem, and populate the output with:
Expand Down
16 changes: 9 additions & 7 deletions Yosemite/YosemiteTests/PointOfSale/POSProductProviderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,20 @@ final class PointOfSaleProductServiceTests: XCTestCase {
network.simulateResponse(requestUrlSuffix: "products", filename: "empty-data-array")

// When
let (items, hasNextPage) = try await itemProvider.providePointOfSaleItems(pageNumber: 2)
let pagedItems = try await itemProvider.providePointOfSaleItems(pageNumber: 2)

// Then
XCTAssertTrue(items.isEmpty)
XCTAssertFalse(hasNextPage)
XCTAssertTrue(pagedItems.items.isEmpty)
XCTAssertFalse(pagedItems.hasMorePages)
}

func test_PointOfSaleItemServiceProtocol_provides_no_items_when_store_has_no_products() async throws {
// Given/When
network.simulateResponse(requestUrlSuffix: "products", filename: "empty-data-array")
let (expectedItems, _) = try await itemProvider.providePointOfSaleItems()
let pagedItems = try await itemProvider.providePointOfSaleItems()

// Then
XCTAssertTrue(expectedItems.isEmpty)
XCTAssertTrue(pagedItems.items.isEmpty)
}

func test_PointOfSaleItemServiceProtocol_provides_items_when_store_has_eligible_products() async throws {
Expand All @@ -71,9 +71,10 @@ final class PointOfSaleProductServiceTests: XCTestCase {

// When
network.simulateResponse(requestUrlSuffix: "products", filename: "products-load-all-type-simple")
let (expectedItems, _) = try await itemProvider.providePointOfSaleItems()
let pagedItems = try await itemProvider.providePointOfSaleItems()

// Then
let expectedItems = pagedItems.items
guard let item = expectedItems.first,
case .simpleProduct(let simpleProduct) = item else {
return XCTFail("No eligible products")
Expand All @@ -92,9 +93,10 @@ final class PointOfSaleProductServiceTests: XCTestCase {

// When
network.simulateResponse(requestUrlSuffix: "products", filename: "products-load-all-for-eligibility-criteria")
let (expectedItems, _) = try await itemProvider.providePointOfSaleItems()
let pagedItems = try await itemProvider.providePointOfSaleItems()

// Then
let expectedItems = pagedItems.items
XCTAssertEqual(expectedItems.count, expectedNumberOfItems)

guard case .simpleProduct(let firstEligibleSimpleProduct) = expectedItems.first,
Expand Down

0 comments on commit 11b2b1f

Please sign in to comment.