-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Woo POS] Extract items code from aggregate model (#14498)
- Loading branch information
Showing
9 changed files
with
475 additions
and
368 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
WooCommerce/Classes/POS/Services/PointOfSaleItemsService.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import Foundation | ||
import Combine | ||
import protocol Yosemite.POSItem | ||
import protocol Yosemite.POSItemProvider | ||
import enum Yosemite.POSProductProviderError | ||
|
||
protocol PointOfSaleItemsServiceProtocol { | ||
var itemListStatePublisher: any Publisher<ItemListState, Never> { get } | ||
@available(*, deprecated, message: "allItems will be removed in a future release. Use itemListState's associated value instead") | ||
var allItems: [POSItem] { get } | ||
func loadInitialItems() async | ||
func loadNextItems() async | ||
func reload() async | ||
} | ||
|
||
class PointOfSaleItemsService: PointOfSaleItemsServiceProtocol { | ||
private(set) var itemListStatePublisher: any Publisher<ItemListState, Never> | ||
private var itemListStateSubject: PassthroughSubject<ItemListState, Never> = .init() | ||
private(set) var allItems: [POSItem] = [] | ||
private var currentPage: Int = Constants.initialPage | ||
private var mightHaveMorePages: Bool = true | ||
private let itemProvider: POSItemProvider | ||
|
||
init(itemProvider: POSItemProvider) { | ||
self.itemProvider = itemProvider | ||
itemListStatePublisher = itemListStateSubject.eraseToAnyPublisher() | ||
} | ||
|
||
@MainActor | ||
func loadInitialItems() async { | ||
mightHaveMorePages = true | ||
itemListStateSubject.send(.initialLoading) | ||
try? await load(pageNumber: Constants.initialPage) | ||
} | ||
|
||
@MainActor | ||
func loadNextItems() async { | ||
do { | ||
guard mightHaveMorePages else { | ||
return | ||
} | ||
itemListStateSubject.send(.loading(allItems)) | ||
|
||
let nextPage = currentPage + 1 | ||
try await load(pageNumber: nextPage) | ||
currentPage = nextPage | ||
} catch { | ||
// Handle errors without incrementing currentPage. | ||
} | ||
} | ||
|
||
@MainActor | ||
func reload() async { | ||
allItems.removeAll() | ||
currentPage = Constants.initialPage | ||
mightHaveMorePages = true | ||
itemListStateSubject.send(.loading(allItems)) | ||
try? await load(pageNumber: currentPage) | ||
} | ||
|
||
@MainActor | ||
private func load(pageNumber: Int) async throws { | ||
do { | ||
try await fetchItems(pageNumber: pageNumber) | ||
mightHaveMorePages = true | ||
updateItemListStateAfterLoadAttempt() | ||
} catch POSProductProviderError.pageOutOfRange { | ||
mightHaveMorePages = false | ||
updateItemListStateAfterLoadAttempt() | ||
throw POSProductProviderError.pageOutOfRange | ||
} catch { | ||
itemListStateSubject.send(.error(PointOfSaleErrorState.errorOnLoadingProducts())) | ||
throw error | ||
} | ||
} | ||
|
||
@MainActor | ||
private func fetchItems(pageNumber: Int) async throws { | ||
let newItems = try await itemProvider.providePointOfSaleItems(pageNumber: pageNumber) | ||
let uniqueNewItems = newItems.filter { newItem in | ||
!allItems.contains(where: { $0.productID == newItem.productID }) | ||
} | ||
allItems.append(contentsOf: uniqueNewItems) | ||
} | ||
|
||
private func updateItemListStateAfterLoadAttempt() { | ||
if allItems.isEmpty { | ||
itemListStateSubject.send(.empty) | ||
} else { | ||
itemListStateSubject.send(.loaded(allItems)) | ||
} | ||
} | ||
|
||
private enum Constants { | ||
static let initialPage: Int = 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.