-
Notifications
You must be signed in to change notification settings - Fork 120
[Local Catalog] Show items from GRDB in POS #16235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
joshheald
merged 11 commits into
trunk
from
issue/woomob-1088-observable-itemcontroller-for-grdb
Oct 20, 2025
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6b373a5
Add .initial state to ItemListState
joshheald d999e5c
Add PointOfSaleObservableItemsController
joshheald 06cb40f
Add tests for PointOfSaleObservableItemsController
joshheald 6257ba5
Use PointOfSaleObservableItemsController when GRDB is available
joshheald d199ffa
Periphery ignore
joshheald ee974cc
Fix periphery ignore
joshheald 46b634a
Separate product and variation errors to prevent context leakage
joshheald 044b137
Clear variation error when switching to new parent
joshheald 2bdf4ab
Set `hasLoaded` after we actually load
joshheald 0a68b51
Fix tests
joshheald 9ef581f
Fix test
joshheald File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
160 changes: 160 additions & 0 deletions
160
Modules/Sources/PointOfSale/Controllers/PointOfSaleObservableItemsController.swift
This file contains hidden or 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,160 @@ | ||
| import Foundation | ||
| import Observation | ||
| import class WooFoundation.CurrencySettings | ||
| import enum Yosemite.POSItem | ||
| import protocol Yosemite.POSObservableDataSourceProtocol | ||
| import struct Yosemite.POSVariableParentProduct | ||
| import class Yosemite.GRDBObservableDataSource | ||
| import protocol Storage.GRDBManagerProtocol | ||
|
|
||
| /// Controller that wraps an observable data source for POS items | ||
| /// Uses computed state based on data source observations for automatic UI updates | ||
| @Observable | ||
| final class PointOfSaleObservableItemsController: PointOfSaleItemsControllerProtocol { | ||
| private let dataSource: POSObservableDataSourceProtocol | ||
|
|
||
| // Track which items have been loaded at least once | ||
| private var hasLoadedProducts = false | ||
| private var hasLoadedVariationsForCurrentParent = false | ||
|
|
||
| // Track current parent for variation state mapping | ||
| private var currentParentItem: POSItem? | ||
|
|
||
| var itemsViewState: ItemsViewState { | ||
| ItemsViewState( | ||
| containerState: containerState, | ||
| itemsStack: ItemsStackState( | ||
| root: rootState, | ||
| itemStates: variationStates | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| init(siteID: Int64, | ||
| grdbManager: GRDBManagerProtocol, | ||
| currencySettings: CurrencySettings) { | ||
| self.dataSource = GRDBObservableDataSource( | ||
| siteID: siteID, | ||
| grdbManager: grdbManager, | ||
| currencySettings: currencySettings | ||
| ) | ||
| } | ||
|
|
||
| // periphery:ignore - used by tests | ||
| init(dataSource: POSObservableDataSourceProtocol) { | ||
| self.dataSource = dataSource | ||
| } | ||
|
|
||
| func loadItems(base: ItemListBaseItem) async { | ||
| switch base { | ||
| case .root: | ||
| dataSource.loadProducts() | ||
| hasLoadedProducts = true | ||
| case .parent(let parent): | ||
| guard case .variableParentProduct(let parentProduct) = parent else { | ||
| assertionFailure("Unsupported parent type for loading items: \(parent)") | ||
| return | ||
| } | ||
|
|
||
| // If switching to a different parent, reset the loaded flag | ||
| if currentParentItem != parent { | ||
| currentParentItem = parent | ||
| hasLoadedVariationsForCurrentParent = false | ||
| } | ||
|
|
||
| dataSource.loadVariations(for: parentProduct) | ||
| hasLoadedVariationsForCurrentParent = true | ||
| } | ||
| } | ||
|
|
||
| func refreshItems(base: ItemListBaseItem) async { | ||
| switch base { | ||
| case .root: | ||
| dataSource.refresh() | ||
| case .parent(let parent): | ||
| guard case .variableParentProduct(let parentProduct) = parent else { | ||
| assertionFailure("Unsupported parent type for refreshing items: \(parent)") | ||
| return | ||
| } | ||
| dataSource.loadVariations(for: parentProduct) | ||
| } | ||
| } | ||
|
|
||
| func loadNextItems(base: ItemListBaseItem) async { | ||
| switch base { | ||
| case .root: | ||
| dataSource.loadMoreProducts() | ||
| case .parent: | ||
| dataSource.loadMoreVariations() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // MARK: - State Computation | ||
| private extension PointOfSaleObservableItemsController { | ||
| var containerState: ItemsContainerState { | ||
| // Use .loading during initial load, .content otherwise | ||
| if !hasLoadedProducts && dataSource.isLoadingProducts { | ||
| return .loading | ||
| } | ||
| return .content | ||
| } | ||
|
|
||
| var rootState: ItemListState { | ||
| let items = dataSource.productItems | ||
|
|
||
| // Initial state - not yet loaded | ||
| if !hasLoadedProducts { | ||
| return .initial | ||
| } | ||
|
|
||
| // Loading state - preserve existing items | ||
| if dataSource.isLoadingProducts { | ||
| return .loading(items) | ||
| } | ||
|
|
||
| // Error state | ||
| if let error = dataSource.productError, items.isEmpty { | ||
| return .error(.errorOnLoadingProducts(error: error)) | ||
| } | ||
|
|
||
| // Empty state | ||
| if items.isEmpty { | ||
| return .empty | ||
| } | ||
|
|
||
| // Loaded state | ||
| return .loaded(items, hasMoreItems: dataSource.hasMoreProducts) | ||
| } | ||
|
|
||
| var variationStates: [POSItem: ItemListState] { | ||
| guard let parentItem = currentParentItem else { | ||
| return [:] | ||
| } | ||
|
|
||
| let items = dataSource.variationItems | ||
|
|
||
| // Initial state - not yet loaded | ||
| if !hasLoadedVariationsForCurrentParent { | ||
| return [parentItem: .initial] | ||
| } | ||
|
|
||
| // Loading state - preserve existing items | ||
| if dataSource.isLoadingVariations { | ||
| return [parentItem: .loading(items)] | ||
| } | ||
|
|
||
| // Error state | ||
| if let error = dataSource.variationError, items.isEmpty { | ||
| return [parentItem: .error(.errorOnLoadingVariations(error: error))] | ||
| } | ||
|
|
||
| // Empty state | ||
| if items.isEmpty { | ||
| return [parentItem: .empty] | ||
| } | ||
|
|
||
| // Loaded state | ||
| return [parentItem: .loaded(items, hasMoreItems: dataSource.hasMoreVariations)] | ||
| } | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import enum Yosemite.POSItem | ||
|
|
||
| enum ItemListState { | ||
| case initial | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added this state for more accurate modelling of what's happening. My intention is to delay showing the loading indicators when we open variations with GRDB – it causes a slight flicker at the moment. This is some groundwork for that. |
||
| case loading(_ currentItems: [POSItem]) | ||
| case loaded(_ items: [POSItem], hasMoreItems: Bool) | ||
| case inlineError(_ items: [POSItem], error: PointOfSaleErrorState, context: InlineErrorContext) | ||
|
|
@@ -38,7 +39,7 @@ extension ItemListState { | |
| .loaded(let items, _), | ||
| .inlineError(let items, _, _): | ||
| return items | ||
| case .error, .empty: | ||
| case .initial, .error, .empty: | ||
| return [] | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, this is not implemented. Is the idea to trigger an incremental sync when refresh is called?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's right